SaveController.class.php
4.35 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
<?php
/**
* Created by PhpStorm.
* User: tangxingguo
* Date: 2017/4/27
* Time: 10:59
*/
namespace Apicp\Controller\Award;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\Integral;
use Common\Service\ArticleService;
use Common\Service\AwardService;
use Common\Service\RightService;
class SaveController extends \Apicp\Controller\AbstractController
{
/**
* Save
* @author tangxingguo
* @desc 保存激励
* @param Int award_id 激励ID
* @param String award_action:true 激励行为(最多20字符)
* @param String description 描述(最多140字符)
* @param Int award_type:true 激励类型(1=勋章)
* @param Int medal_id 勋章ID(激励类型为勋章时必填)
* @param Array right:true 发放对象
* @param String right.is_all 是否全公司(1=否;2=是)
* @param Array right.uids 人员ID
* @param Array right.dp_ids 部门ID
* @param Array right.tag_ids 标签ID
* @param Array article_ids:true 课程ID数组
* @param Int condition:true 勋章发送条件(必须学习课程数量)
*/
public function Index_post()
{
// 验证规则
$rules = [
'award_id' => 'integer',
'award_action' => 'require|max:20',
'description' => 'max:140',
'award_type' => 'require|in:1,2',
'medal_id' => 'integer',
'condition' => 'require|integer',
'article_ids' => 'require|array',
'right' => 'require|array',
];
// 验证请求数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$postData = $validate->postData;
// 数据检查
list($awardData, $awardRight) = $this->_checkPostData($postData);
// 激励设置保存
$awardServ = new AwardService();
if (!isset($postData['award_id'])) {
// 新增
$postData['award_id'] = $awardServ->insert($awardData);
} else {
// 编辑
$awardServ->update($postData['award_id'], $awardData);
}
// 权限保存
$rightServ = new RightService();
$rightServ->saveData(['award_id' => $postData['award_id']], $awardRight);
}
/**
* @desc 检查请求数据,返回激励数据与发放对象数据
* @author tangxingguo
* @param array $postData 接收到的请求数据
* @return array
*/
private function _checkPostData($postData)
{
// 激励数据
$awardData = [
'award_action' => $postData['award_action'],
'description' => isset($postData['description']) ? $postData['description'] : '',
];
// 勋章发送条件不能大于所选课程数量
if ($postData['condition'] > count($postData['article_ids'])) {
E('_ERR_AWARD_SEND_COND_FAIL');
}
$awardData['condition'] = $postData['condition'];
// 勋章检查
if (Constant::AWARD_TYPE_IS_MEDAL == $postData['award_type']) {
// 激励类型:勋章
if (!isset($postData['medal_id'])) {
E('_ERR_AWARD_TYPE_NEED_MEDAL');
}
// 勋章有效性
$IntegralServ = new Integral();
$integralList = $IntegralServ->listMedal();
$medalIds = array_column($integralList, 'im_id');
if (!in_array($postData['medal_id'], $medalIds)) {
E('_ERR_AWARD_MEDAL_NOT_FOUND');
}
$awardData['award_type'] = Constant::AWARD_TYPE_IS_MEDAL;
$awardData['medal_id'] = $postData['medal_id'];
}
// 课程有效性
$articleServ = new ArticleService();
$articleList = $articleServ->list_by_conds(['article_status' => Constant::ARTICLE_STATUS_SEND]);
$totalArticleIds = array_column($articleList, 'article_id');
if (!empty(array_diff($postData['article_ids'], $totalArticleIds)) || empty($postData['article_ids'])) {
E('_ERR_ARTICLE_DATA_NOT_FOUND');
}
$awardData['article_ids'] = serialize($postData['article_ids']);
// 权限
$rightServ = new RightService();
$right = $rightServ->formatPostData($postData['right']);
if (empty($right)) {
E('_ERR_CLASS_RIGHT_EMPTY');
}
return [$awardData, $postData['right']];
}
}