DeleteAnswerController.class.php 2.79 KB
<?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);
    }
}