SaveSignSettingController.class.php
7.82 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
<?php
/**
* Created by PhpStorm.
* User: tangxingguo
* Date: 2017/7/24
* Time: 11:51
*/
namespace Apicp\Controller\Dailytask;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\DailytaskConfig;
use Common\Service\DailytaskService;
use Common\Service\UserSignService;
class SaveSignSettingController extends \Apicp\Controller\AbstractController
{
/**
* SaveSignSetting
* @author tangxingguo
* @desc 保存、编辑签到设置接口
* @param Int is_open:true 是否开启(1=未开启;2=已开启)
* @param Int sign_type:true 签到规则类型(1=固定获取积分;2=递增获取积分)
* @param Int sign_fixed.day_integral 每天签到获取积分数(前提条件:sign_type=1)
* @param Int sign_fixed.reward_rule.cycle 连续签到天数(前提条件:sign_type=1)
* @param Int sign_fixed.reward_rule.integral 一次性获取积分数(前提条件:sign_type=1)
* @param Int sign_cycle.cycle 递增周期天数(前提条件:sign_type=2)
* @param Int sign_cycle.first 首次签到获取积分数(前提条件:sign_type=2)
* @param Int sign_cycle.integral 每连续签到多一天获取的积分数(前提条件:sign_type=2)
*/
public function Index_post()
{
// 验证规则
$rules = [
'is_open' => 'require|integer|in:1,2',
'sign_type' => 'require|integer|in:1,2',
'sign_fixed' => 'array',
'sign_cycle' => 'array',
];
// 验证数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$postData = $validate->postData;
// 检查数据格式
if ($postData['is_open'] == Constant::DAILYTASK_IS_OPEN_FALSE) {
$sqlData = ['is_open' => Constant::DAILYTASK_IS_OPEN_FALSE];
} else {
$sqlData = $this->formatPostData($postData);
}
// 数据入库
$dailytaskServ = new DailytaskService();
$signSetting = $dailytaskServ->get_by_conds(['app' => Constant::APP_SIGN]);
if (empty($signSetting)) {
// 添加
$dailytaskServ->insert($sqlData);
} else {
// 修改
$dailytaskServ->update($signSetting['dailytask_id'], $sqlData);
// 对比数据是否变动,将所有人连续签到天数修改为0
if ($postData['is_open'] == Constant::DAILYTASK_IS_OPEN_TRUE) {
$dbData = unserialize($signSetting['rules']);
$diff = $this->diffData($postData, $dbData);
if (!$diff) {
$signServ = new UserSignService();
$signServ->update_by_conds([], ['sign_total' => 0]);
}
}
}
// 如果关闭签到,则删除首页栏目推荐
if ($postData['is_open'] == Constant::DAILYTASK_IS_OPEN_FALSE) {
// 签到是否推荐到首页icon栏目
$model = new \Common\Model\CommonModel('recommender', 'oa_common_');
$is_icon = $model->count_by_conds([
'type' => Constant::TYPE_ICON,
'system' => Constant::SYSTEM_NO,
'app_dir' => 'Sign',
]);
// 删除推荐
if ($is_icon) {
$model->delete_by_conds([
'type' => Constant::TYPE_ICON,
'system' => Constant::SYSTEM_NO,
'app_dir' => 'Sign',
]);
}
}
// 清除缓存
$taskConfig = &DailytaskConfig::instance();
$taskConfig->clearCacheData(Constant::APP_SIGN);
}
/**
* @desc 格式化请求数据
* @author tangxingguo
* @param array $data 请求数据
* @return array
*/
private function formatPostData($data)
{
$sqlData = [
// 数据标识(数据库内app字段)
'app' => Constant::APP_SIGN,
// 是否开启签到
'is_open' => $data['is_open'],
// 更新时间
'update_time' => MILLI_TIME,
];
// 检查必要数据
switch ($data['sign_type']) {
// 固定签到
case Constant::DAILYTASK_SIGN_TYPE_IS_FIXED:
if (!isset($data['sign_fixed']['day_integral'])) {
E('_ERR_DAILYTASK_SIGN_INTEGRAL_EMPTY');
}
if (isset($data['sign_fixed']['reward_rule'])) {
if (!is_array($data['sign_fixed']['reward_rule'])) {
E('_ERR_DAILYTASK_SIGN_FIXED_FORMAT_ERROR');
}
foreach ($data['sign_fixed']['reward_rule'] as $v) {
if (!isset($v['cycle'], $v['integral'])) {
E('_ERR_DAILYTASK_SIGN_FIXED_FORMAT_ERROR');
}
}
// 规则排序
$cycles = array_column($data['sign_fixed']['reward_rule'], 'cycle');
array_multisort($cycles, SORT_ASC, $data['sign_fixed']['reward_rule']);
}
$sqlData['rules'] = serialize(['sign_type' => $data['sign_type'], 'sign_fixed' => $data['sign_fixed']]);
break;
// 递增签到
case Constant::DAILYTASK_SIGN_TYPE_IS_CYCLE:
if (!isset($data['sign_cycle']['cycle'], $data['sign_cycle']['first'], $data['sign_cycle']['integral'])) {
E('_ERR_DAILYTASK_SIGN_CYCLE_FORMAT_ERROR');
}
$sqlData['rules'] = serialize(['sign_type' => $data['sign_type'], 'sign_cycle' => $data['sign_cycle']]);
break;
}
return $sqlData;
}
/**
* @desc 比较规则是否有变动
* @author tangxingguo
* @param array $postData 提交的数据
* @param array $dbData 数据库数据
* @return bool
*/
private function diffData($postData, $dbData)
{
// 提交的签到规则与数据库规则不同
if ($postData['sign_type'] != $dbData['sign_type']) {
return false;
}
// 对比数据
switch ($postData['sign_type']) {
// 固定签到
case Constant::DAILYTASK_SIGN_TYPE_IS_FIXED:
// 每天签到获取积分数不一样
if ($postData['sign_fixed']['day_integral'] != $dbData['sign_fixed']['day_integral']) {
return false;
}
// 规则存在是否相同
if (isset($postData['sign_fixed']['reward_rule']) != isset($dbData['sign_fixed']['reward_rule'])) {
return false;
}
// 都不存在规则
if (!isset($postData['sign_fixed']['reward_rule'], $dbData['sign_fixed']['reward_rule'])) {
return true;
}
// 固定签到规则数量不一样
if (count($postData['sign_fixed']['reward_rule']) != count($dbData['sign_fixed']['reward_rule'])) {
return false;
}
// 值对比(前提:已经排序)
foreach ($postData['sign_fixed']['reward_rule'] as $k => $v) {
if ($v['cycle'] != $dbData['sign_fixed']['reward_rule'][$k]['cycle'] || $v['integral'] != $dbData['sign_fixed']['reward_rule'][$k]['integral']) {
return false;
}
}
break;
// 递增签到
case Constant::DAILYTASK_SIGN_TYPE_IS_CYCLE:
if ($postData['sign_cycle']['cycle'] != $dbData['sign_cycle']['cycle'] || $postData['sign_cycle']['first'] != $dbData['sign_cycle']['first'] || $postData['sign_cycle']['integral'] != $dbData['sign_cycle']['integral']) {
return false;
}
break;
}
return true;
}
}