SaveAnswerController.class.php
3.12 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
<?php
/**
* 保存回答接口
* User: tangxingguo
* Date: 2017/4/11
* Time: 18:36
*/
namespace Apicp\Controller\Answer;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\Notice;
use Common\Service\AnswerService;
use Common\Service\QuestionService;
class SaveAnswerController extends \Apicp\Controller\AbstractController
{
/**
* SaveAnswer
*
* @author
* @desc 保存回答接口
*
* @param Int question_id 提问ID
* @param Int answer_content 回答内容
*/
public function Index_post()
{
// 验证规则
$rules = [
'question_id' => 'require|integer',
'answer_content' => 'require',
];
// 验证数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$questionId = $validate->postData['question_id'];
$answerContent = $validate->postData['answer_content'];
// 提问检查
$questionServ = new QuestionService();
$questionInfo = $questionServ->get($questionId);
if (empty($questionInfo)) {
// 问题信息不存在
E('_ERR_ANSWER_QUESTION_NOT_FOUND');
}
// 审批状态为非审批通过
if ($questionInfo['check_status'] != Constant::QUESTION_CHECK_STATUS_PASS) {
// 提问通过审批才可以回答
E('_ERR_ANSWER_QUESTION_NOT_PASS');
}
// 添加回答,审核为通过
$data = [
'question_id' => $questionId,
'answer_content' => $answerContent,
'class_id' => $questionInfo['class_id'],
'user_type' => Constant::ANSWER_PERON_IS_ADMIN,
'checker_type' => Constant::CHECKER_IS_ADMIN,
'check_time' => MILLI_TIME,
'check_status' => Constant::ANSWER_CHECK_STATUS_PASS,
'checker_uid' => $this->_login->user['eaId'],
'checker_name' => $this->_login->user['eaRealname'],
'uid' => $this->_login->user['eaId'],
'username' => $this->_login->user['eaRealname'],
];
$answerServ = new AnswerService();
try {
// 开启事务
$answerServ->start_trans();
$answerId = $answerServ->insert($data);
// 推送新回答给提问人
$answerInfo = $answerServ->get($answerId);
$answerInfo['question_title'] = $questionInfo['question_title'];
$answerInfo['q_uid'] = $questionInfo['uid'];
$noticeServ = Notice::instance();
$noticeServ->passAnswerToQer($answerInfo);
// 提问通过的回答数加一
$questionServ->update($questionId, ['answer_pass_total = answer_pass_total + ?' => 1]);
// 提交事务
$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');
}
}
}