AnswerBestController.class.php
4.84 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
<?php
/**
* 设置最佳回答接口
* User: tangxingguo
* Date: 2017/4/11
* Time: 18:36
*/
namespace Api\Controller\Answer;
use Com\PackageValidate;
use Common\Common\AnswerHelper;
use Common\Common\Constant;
use Common\Common\Notice;
use Common\Service\ActionLogService;
use Common\Service\AnswerService;
use Common\Service\QuestionService;
use Common\Common\IntegralStrategy;
use Common\Service\UserActionService;
class AnswerBestController extends \Api\Controller\AbstractController
{
/**
* AnswerBest
*
* @author
* @desc 设置最佳回答接口
*
* @param Int answer_id 回答ID
*
* @return void
*/
public function Index_post()
{
// 验证规则
$rules = [
'answer_id' => 'require',
];
// 验证数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$answerId = $validate->postData['answer_id'];
$answerServ = new AnswerService();
$answerInfo = $answerServ->get($answerId);
if (empty($answerInfo)) {
// 回答数据不存在
E('_ERR_ANSWER_NOT_FOUND');
}
if (Constant::ANSWER_IS_BEST_TRUE == $answerInfo['is_best']) {
// 已经是最佳答案
E('_ERR_ANSWER_ALREADY_BEST');
}
// 获取提问信息
$questionServ = new QuestionService();
$questionInfo = $questionServ->get($answerInfo['question_id']);
// 提问解决状态:已解决
if (Constant::QUESTION_STATUS_IS_SOLVE == $questionInfo['is_solve']) {
// 该提问已经存在最佳答案
E('_ERR_ANSWER_QUESTION_ALREADY_BEST');
}
if ($questionInfo['uid'] != $this->uid) {
// 提问人才能设置最佳答案
E('_ERR_ANSWER_BEST_APPROVED_NEED_QER');
}
try {
// 开始事务
$answerServ->start_trans();
// 保存最佳答案
$answerServ->update($answerId, ['is_best' => Constant::ANSWER_IS_BEST_TRUE]);
// 修改问题解决状态
$questionServ->update($questionInfo['question_id'], ['is_solve' => Constant::QUESTION_STATUS_IS_SOLVE]);
// 提交事务
$answerServ->commit();
} catch (\Think\Exception $e) {
\Think\Log::record($e);
// 事务回滚
$answerServ->rollback();
E('_ERR_DATA_UPDATE');
} catch (\Exception $e) {
\Think\Log::record($e);
// 事务回滚
$answerServ->rollback();
E('_ERR_DATA_UPDATE');
}
// 记录被设置最佳答案的问题(每个问题仅记录一次)
$actionServ = new UserActionService();
$saveRes = $actionServ->save($answerInfo['uid'], $questionInfo['question_id'], Constant::INT_ACT_BEST_ANSWERED);
if ($saveRes) {
// 触发积分策略:设置最佳答案次数
$bestTotal = $actionServ->count_by_conds([
'uid' => $answerInfo['uid'],
'action_key' => Constant::INT_ACT_BEST_ANSWERED,
]);
$triggerTypes[] = [
'triggerKey' => Constant::INT_TRIGGER_BEST_ANSWERED,
'value' => $bestTotal,
];
// 触发积分规则,获得积分数
$strategyServ = &IntegralStrategy::instance();
$strategyServ->trigger($answerInfo['uid'], $questionInfo['question_id'], '', Constant::INT_ACT_BEST_ANSWERED, $triggerTypes);
}
// 初始化记录
$ActionLogServ=new ActionLogService();
$logData=[
'answer_id'=>$answerId,
'answer_uid'=>$answerInfo['uid'],
'type'=>Constant::LOG_BEST_ANSWERED_TYPE,
'question_id'=>$answerInfo['question_id'],
'uid'=>$this->uid,
'class_id'=>$questionInfo['class_id']
];
$ActionLogServ->insert($logData);
// 获取勋章
$ActionLogServ->medal($questionInfo['class_id'],$answerInfo['uid'],$this->uid,$award);
// 推送消息给回答人
$noticeServ = &Notice::instance();
$sendData = $questionInfo;
$sendData['uid'] = $answerInfo['uid'];
$sendData['answer_content'] = $answerInfo['answer_content'];
if ($questionInfo['integral'] > 0) {
// 非管理员,增加积分
if (Constant::ANSWER_PERON_IS_USER == $answerInfo['user_type']) {
$answerHelper = &AnswerHelper::instance();
$answerHelper->updateUserIntegral($answerInfo['uid'], $questionInfo['integral'], Constant::INTEGRAL_CHANGE_DESC_LIST['best_answer']);
}
} else {
// 无积分
$noticeServ->bestUnintegralNotice($sendData);
}
$this->_result = [
'award' => !empty($award)?$award:[]
];
}
}