CommonRecommenderService.class.php
11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
/**
* CommonRecommenderService.class.php
* 推荐系统 Service
* @author Deepseath
* @version $Id$
*/
namespace Common\Service;
use Common\Model\CommonRecommenderModel;
use Com\Validator;
use Common\Common\Setting;
class CommonRecommenderService extends AbstractService
{
/**
* 有效的推荐类型列表
*/
public $typeList = [];
/**
* 应用配置
*/
protected $_settingAppConfig = null;
/**
* 推荐系统 Service 构造方法
*/
public function __construct()
{
parent::__construct();
$this->_d = new CommonRecommenderModel();
$this->typeList = [
CommonRecommenderModel::TYPE_BANNER,
CommonRecommenderModel::TYPE_ICON,
CommonRecommenderModel::TYPE_ARTICLE
];
}
/**
* 验证推荐类型是否合法
* @param number $type
* @return boolean
*/
public function is_type($type)
{
if (!is_scalar($type) || !in_array($type, [
CommonRecommenderModel::TYPE_BANNER,
CommonRecommenderModel::TYPE_ICON,
CommonRecommenderModel::TYPE_ARTICLE
])) {
return false;
}
return true;
}
/**
* 获取应用配置信息
* @return mixed|object
*/
public function getAppConfig()
{
if ($this->_settingAppConfig === null) {
// $set = &Setting::instance();
// $this->_settingAppConfig = $set->get('Common.appConfig');
// todo tangxingguo 2018-5-9 14:57:31 避免缓存
$this->_settingAppConfig = \Common\Sql\DefaultData::installData();
if (!is_array($this->_settingAppConfig)) {
$this->_settingAppConfig = [];
}
}
return $this->_settingAppConfig;
}
/**
* 验证 app 标识合法性
* @param unknown $app
*/
public function is_app($app)
{
if (empty($app)) {
return false;
}
$config = $this->getAppConfig();
if (!is_array($config) || !isset($config[$app])) {
return false;
}
return true;
}
/**
* 计算指定推荐数据关联的个数,一般用于检测关联的数据是否存在
* @param integer $type 待检查的数据类型,见 CommonRecommenderModel::TYPE_*
* @param string $appDir 应用目录标识名
* @param string $dataId 数据关联的所在应用数据 ID
* @param string $dataCategoryId 数据关联的所在应用分类 ID
* @param number $ignoreRecommenderId 需要排除的数据 Id
* @return number
*/
public function countDuplicate($type, $appDir, $dataId, $dataCategoryId = null, $ignoreRecommenderId = 0)
{
return (int) $this->_d->countByTypeAppdirDataidCategoryid($type, $appDir, $dataId, $dataCategoryId, $ignoreRecommenderId);
}
/**
* 获取指定推荐数据关联的所有数据,一般用于查找重复的关联数据
* @param integer $type 待检查的数据类型,见 CommonRecommenderModel::TYPE_*
* @param string $appDir 应用目录标识名
* @param string $dataId 数据关联的所在应用数据 ID
* @param string $dataCategoryId 数据关联的所在应用分类 ID
* @param number $ignoreRecommenderId 需要排除的数据 Id
* @return number
*/
public function getDuplicate($type, $appDir, $dataId, $dataCategoryId = null, $ignoreRecommenderId = 0)
{
return $this->_d->getByTypeAppdirDataidCategoryid($type, $appDir, $dataId, $dataCategoryId, $ignoreRecommenderId);
}
/**
* 根据数据类型取列表
* @param number $type 类型标记,见 \Common\Model\CommonRecommenderModel::TYPE_* 定义
* @param number $hide 是否取出隐藏的数据,见 \Common\Model\CommonRecommenderModel::HIDE_* 定义,null=取出全部
* @param number $start 开始数据行号
* @param number $limit 取出数据条数
* @return array( 'total' => '总数',
* 'pages' => '总页码',
* 'list' => array()
* )
*/
public function listByType($type, $hide = null, $start = 0, $limit = 6)
{
$conds = [];
$conds['type'] = $type;
if ($hide != null) {
$conds['hide'] = $hide;
}
if ($start < 0) {
$start = 0;
}
if ($limit < 1) {
$limit = 6;
}
$total = (int) $this->_d->count_by_conds($conds, 'recommender_id');
$pages = ceil($total / $limit);
$list = [];
if ($total > 0) {
$list = $this->_d->list_by_conds($conds, [
$start,
$limit
], [
'displayorder' => 'DESC',
'dateline' => 'DESC'
]);
}
return [
'type' => $type,
'total' => $total,
'pages' => $pages,
'limit' => $limit,
'list' => $list
];
}
/**
* 格式化数据输出,主要用于手机端
* @param array $data
* @return array( 'id' => '数据 ID',
* 'title' => '标题',
* 'pic' => '图片 URL',
* 'url' => '链接地址',
* 'time' => '发布时间戳',
* 'key' => '数据唯一标识值',
* 'remark' => '备注信息',
* 'end_time' => '结束时间',
* 'attach_id' => '图片附件ID',
* 'app_dir' => '应用目录名',
* )
*/
public function format($data)
{
return array(
'id' => $data['recommender_id'],
'title' => $data['title'],
'pic' => $data['pic'],
'url' => $data['url'],
'time' => $data['dateline'],
'key' => md5("Recommender" . "\t" . $data['type'] . "\t" . $data['recommender_id']),
'remark' => isset($data['data_type']) ? $data['data_type'] : '',
'end_time' => isset($data['end_time']) ? $data['end_time'] : null,
'attach_id' => $data['attach_id'],
'app_dir' => $data['app_dir'],
'add_type' => $data['add_type'],
);
}
/**
* 检查字段数据 标题 是否合法
* @param string $title 标题
* @param number $maxlength 限制最大长度
* @return boolean
*/
public function verifyFieldTitle($title, $maxlength = 4)
{
return \Com\Validator::is_string_count_in_range($title, 1, $maxlength);
}
/**
* 检查字段数据 URL 是否合法
* @param string $url URL
* @return boolean
*/
public function verifyFieldUrl($url)
{
if (!isset($url{0}) || isset($url{255})) {
return false;
}
return true;
}
/**
* 检查字段数据 描述 是否合法
* @param string $description 描述内容
* @return boolean
*/
public function verifyFieldDescription($description)
{
return empty($description) || \Com\Validator::is_string_count_in_range($description, 0, 140) === true;
}
/**
* 更新、添加 一条推荐数据
* @param array $data 推荐数据
* @param null|number $recommenderId 待更新的推荐数据 ID
* @return \Think\mixed|string
*/
public function recommenderUpdate($data, $recommenderId = null)
{
$values = [];
$fields = [
'type',
'displayorder',
'hide',
'system',
'title',
'attach_id',
'pic',
'url',
'description',
'data_category_id',
'app_dir',
'app_identifier',
'data_id',
'data',
'dateline',
'adminer_id',
'adminer',
'add_type',
'data_type',
'end_time',
];
$values = [];
foreach ($fields as $_field) {
if (isset($data[$_field])) {
$values[$_field] = $data[$_field];
}
}
if (isset($data['data'])) {
$values['data'] = serialize($values['data']);
}
if ($recommenderId === null) {
return $this->_d->insert($values);
} else {
$this->_d->update($recommenderId, $values);
return $recommenderId;
}
}
/**
* 栏目数据格式化输出,主要用于管理后台
* @param array $data
* @return array
*/
public function iconDataFormat($data)
{
$config = $this->getAppConfig();
if (isset($config[$data['app_dir']])) {
$appName = $config[$data['app_dir']]['name'];
} else {
$appName = '';
}
return [
'id' => $data['recommender_id'],
'hide' => $data['hide'],
'system' => $data['system'],
'title' => $data['title'],
'attachId' => $data['attach_id'],
'pic' => $data['pic'],
'url' => $data['url'],
'description' => $data['description'],
'dataCategoryId' => $data['data_category_id'],
'dateline' => $data['dateline'],
'adminer' => $data['adminer'],
'app' => $data['app_dir'],
'appName' => $appName,
'addType' => $data['add_type'],
'data' => unserialize($data['data'])
];
}
/**
* 栏目数据格式化输出,主要用于管理后台
* @param array $data
* @return array
*/
public function bannerDataFormat($data)
{
if ($this->_settingAppConfig === null) {
$set = &Setting::instance();
$this->_settingAppConfig = $set->get('Common.appConfig');
}
return [
'id' => $data['recommender_id'],
'hide' => $data['hide'],
'system' => $data['system'],
'title' => $data['title'],
'attachId' => $data['attach_id'],
'pic' => $data['pic'],
'url' => $data['url'],
// 'description' => $data['description'],
'appName' => isset($this->_settingAppConfig[$data['app_dir']]) ? $this->_settingAppConfig[$data['app_dir']]['name'] : '',
'dataId' => $data['data_id'],
'dataCategoryId' => $data['data_category_id'],
'dateline' => $data['dateline'],
'adminer' => $data['adminer'],
'addType' => $data['add_type'],
'data' => unserialize($data['data'])
];
}
/**
* 返回指定 id 的数据列表(按照 ids 列表的顺序排序)
* @param array $ids
* @return array
*/
public function listByIds(array $ids)
{
$list = [];
if (count($ids) > 0) {
$datas = [];
foreach ($this->_d->list_by_pks($ids) as $_key => $_data) {
$datas[$_data['recommender_id']] = $_data;
}
unset($_data);
foreach ($ids as $_id) {
if (isset($datas[$_id])) {
$list[] = $datas[$_id];
}
}
}
return (array)$list;
}
/**
* 列表指定推荐数据关联的所有数据(无排序)
* @param integer $type 待检查的数据类型,见 CommonRecommenderModel::TYPE_*
* @param string $appDir 应用目录标识名
* @param string $dataId 数据关联的所在应用数据 ID
* @param string $dataCategoryId 数据关联的所在应用分类 ID
* @return array
*/
public function list_by_appData($type, $appDir, $dataId, $dataCategoryId = null)
{
$list = $this->_d->listByTypeAppdirDataidCategoryid($type, $appDir, $dataId, $dataCategoryId);
$result = [];
if (is_array($list)) {
foreach ($list as $_data) {
$result[$_data['recommender_id']] = $_data;
}
}
return $result;
}
}