UserMedalDeleteController.class.php
3.57 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
<?php
/**
* Created by IntelliJ IDEA.
* 勋章列表
* User: zhoutao
* Reader: zhoutao 2017-05-31 10:07:35
* Date: 2017-05-24 15:43:49
*/
namespace Apicp\Controller\Medal;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\Msg;
use Common\Model\MedalModel;
use Common\Service\MedalLogService;
use Common\Service\MedalService;
use Common\Service\MemberMedalService;
class UserMedalDeleteController extends AbstractController
{
/**
* UserMedalDelete
* @author tangxingguo
* @desc 人员勋章删除接口
* @param String uid:true 人员ID
* @param Array ml_ids 勋章记录ID
* @param String description 描述
* @return Array
*/
public function index()
{
$rules = [
'uid' => 'require',
'ml_ids' => 'require|array',
'description' => 'max:80',
];
// 验证请求数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$postData = $validate->postData;
$uid = $postData['uid'];
$mlIds = $postData['ml_ids'];
$desc = $postData['description'];
$medalServ = new MedalService();
$medalLogServ = new MedalLogService();
$memMedalServ = new MemberMedalService();
// 取勋章记录
$logList = $medalLogServ->list_by_conds(['ml_id' => $mlIds]);
if (!empty($logList)) {
// 取人员勋章信息
$imIds = array_column($logList, 'im_id');
$userMedalList = $memMedalServ->list_by_conds(['im_id' => $imIds, 'mem_uid' => $uid]);
// 勋章信息
$medalList = $medalServ->list_by_conds(['im_id' => $imIds]);
if (!empty($medalList)) {
$medalList = array_combine_by_key($medalList, 'im_id');
}
// 统计每个勋章待删除的个数
$imIdCount = array_count_values($imIds);
// 删除 或 减少人员勋章
foreach ($userMedalList as $v) {
// 获得的勋章数 > 删除的勋章数
if ($v['im_total'] > $imIdCount[$v['im_id']]) {
$data = ['im_total = im_total - ?' => $imIdCount[$v['im_id']]];
$memMedalServ->update_by_conds(['im_id' => $v['im_id'], 'mem_uid' => $uid], $data);
// 获得的勋章数 <= 删除的勋章数
} else {
$memMedalServ->delete_by_conds(['im_id' => $v['im_id'], 'mem_uid' => $uid]);
}
// 勋章存在,发送消息
if (isset($medalList[$v['im_id']])) {
$this->_sendAwardNotice($uid, $medalList[$v['im_id']]['name'], $desc);
}
}
// 删除勋获取记录
$medalLogServ->delete($mlIds);
}
}
/**
* @desc 消息推送
* @author tangxingguo
* @param array $uid 用户信息
* @param array $medalName 勋章名称
* @param string $desc 描述
* @return bool;
*/
private function _sendAwardNotice($uid, $medalName, $desc = '')
{
if (!empty($desc)) {
$desc = "\n内容:{$desc}";
} else {
return;
}
$msgServ = &Msg::instance();
$msg_data = [
[
'title' => "您的【{$medalName}】勋章已被管理员移除",
'description' => "移除时间:" . rgmdate(MILLI_TIME, 'Y-m-d H:i') . $desc,
'url' => oaUrl('Frontend/Index/Medal/Index'),
]
];
$msgServ->sendNews($uid, null, null, $msg_data);
}
}