DeleteAnswerController.class.php
2.79 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
<?php
/**
* Created by PhpStorm.
* User: tangxingguo
* Date: 2017/4/11
* Time: 18:36
*/
namespace Apicp\Controller\Answer;
use Com\PackageValidate;
use Common\Common\AttachOperation;
use Common\Common\Constant;
use Common\Service\AnswerService;
use Common\Service\ImgService;
use Common\Service\QuestionService;
class DeleteAnswerController extends \Apicp\Controller\AbstractController
{
/**
* DeleteAnswer
*
* @author
* @desc 删除回答接口
*
* @param Int answer_ids 回答ID(一维数组)
*/
public function Index_post()
{
// 验证规则
$rules = [
'answer_ids' => 'require|array',
];
// 验证数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$answerIds = $validate->postData['answer_ids'];
// 最佳答案检查
$answerServ = new AnswerService();
$count = $answerServ->count_by_conds(['answer_id' => $answerIds, 'is_best' => Constant::ANSWER_IS_BEST_TRUE]);
// 最佳答案不允许删除
if ($count > 0) {
E('_ERR_ANSWER_BEST_NOT_ALLOW_DELETE');
}
// 同步提问的回答数
$questionServ = new QuestionService();
$list = $answerServ->list_by_conds(['answer_id' => $answerIds]);
if (!empty($list)) {
$questionIds = array_values(array_column($list, 'question_id'));
$questionList = $questionServ->list_by_conds(['question_id' => $questionIds]);
$questionList = array_combine_by_key($questionList, 'question_id');
}
foreach ($list as $answerInfo) {
if (Constant::ANSWER_CHECK_STATUS_WAIT == $answerInfo['check_status'] &&
isset($questionList[$answerInfo['question_id']]) &&
$questionList[$answerInfo['question_id']]['answer_wait_total'] > 0) {
// 提问内待审核的回答数减一
$questionServ->update($answerInfo['question_id'], ['answer_wait_total = answer_wait_total - ?' => 1]);
} elseif (isset($questionList[$answerInfo['question_id']]) &&
$questionList[$answerInfo['question_id']]['answer_pass_total'] > 0 &&
Constant::ANSWER_CHECK_STATUS_PASS == $answerInfo['check_status']) {
// 提问内通过的回答数减一
$questionServ->update($answerInfo['question_id'], ['answer_pass_total = answer_pass_total - ?' => 1]);
}
}
// 删除回答
$answerServ->delete($answerIds);
// 删除图片
$imgServ = new ImgService();
$imgServ->delete_by_conds(['answer_id' => $answerIds]);
$attach_serv = new AttachOperation();
// 通知uc删除附件
$attach_serv->delete_attach(APP_DIR, 'answer', $answerIds);
}
}