ApproveQuestionController.class.php 4.24 KB
<?php
/**
 * Created by PhpStorm.
 * User: tangxingguo
 * Date: 2017/4/11
 * Time: 18:36
 */

namespace Apicp\Controller\Answer;

use Com\PackageValidate;
use Common\Common\TaskCenter;
use Common\Common\AnswerHelper;
use Common\Common\Constant;
use Common\Common\Notice;
use Common\Service\QuestionService;
use Common\Common\IntegralStrategy;
use Common\Service\UserActionService;

class ApproveQuestionController extends \Apicp\Controller\AbstractController
{

    /**
     * ApproveQuestion
     *
     * @author
     * @desc 审批提问接口
     *
     * @param Int type:true 审批类型(1=驳回;2=通过)
     * @param Int question_id:true 问题ID
     */
    public function Index_post()
    {

        // 验证规则
        $rules = [
            'type' => 'require|integer|in:1,2',
            'question_id' => 'require|integer',
        ];

        // 验证数据
        $validate = new PackageValidate($rules, [], array_keys($rules));
        $type = $validate->postData['type'];
        $questionId = $validate->postData['question_id'];

        // 提问信息
        $questionServ = new QuestionService();
        $questionInfo = $questionServ->get($questionId);
        // 提问数据不存在
        if (empty($questionInfo)) {

            E('_ERR_ANSWER_QUESTION_NOT_FOUND');
        }

        // 审批状态为非待审批
        if ($questionInfo['check_status'] != Constant::QUESTION_CHECK_STATUS_WAIT) {
            // 提问已经被审批
            E('_ERR_ANSWER_QUESTION_IS_APPROVED');
        }

        // 审批
        $checkStatus = $type == Constant::QUESTION_CHECK_IS_FAIL ? Constant::QUESTION_CHECK_STATUS_FAIL : Constant::QUESTION_CHECK_STATUS_PASS;

        $data = [
            'checker_type' => Constant::CHECKER_IS_ADMIN, // 审批人类型:管理员
            'check_time' => MILLI_TIME,
            'check_status' => $checkStatus,
            'checker_uid' => $this->_login->user['eaId'],
            'checker_name' => $this->_login->user['eaRealname'],
        ];
        $questionServ->update($questionId, $data);

        // 记录提问次数(审核通过后)
        if (Constant::QUESTION_CHECK_STATUS_PASS == $type) {
            $actionServ = new UserActionService();
            $saveRes = $actionServ->save($questionInfo['uid'], $questionInfo['question_id'], Constant::INT_ACT_QUESTION);
            if ($saveRes) {
                // 触发积分策略:提问次数
                $questionTotal = $actionServ->count_by_conds([
                    'uid' => $questionInfo['uid'],
                    'action_key' => Constant::INT_ACT_QUESTION,
                ]);

                $triggerTypes[] = [
                    'triggerKey' => Constant::INT_TRIGGER_QUESTION,
                    'value' => $questionTotal,
                ];
                // 触发积分规则,获得积分数
                $strategyServ = &IntegralStrategy::instance();
                $intTotal = $strategyServ->trigger($questionInfo['uid'], $questionInfo['question_id'], '', Constant::INT_ACT_QUESTION, $triggerTypes);
            }
        }

        // 消息推送
        $noticeServ = &Notice::instance();
        $questionInfo = $questionServ->get($questionId);

        $answerHelper = &AnswerHelper::instance();
        // 驳回
        if (Constant::QUESTION_CHECK_IS_FAIL == $type) {

            if ($questionInfo['integral'] > 0) {
                // 有积分,退积分
                $answerHelper->updateUserIntegral($questionInfo['uid'], $questionInfo['integral'], Constant::INTEGRAL_CHANGE_DESC_LIST['reject_question']);
            } else {
                // 没积分,消息通知发起人
                $noticeServ->failNoticeToQer($questionInfo['uid'], $questionInfo);
            }
        } else { // 通过
            // 任务中心-日常任务埋点
            $taskCenter = &TaskCenter::instance();
            $taskCenter->triggerDailytask([
                'uid' => $questionInfo['uid'],
                'app_data_id' => $questionInfo['question_id'],
                'action_key' => Constant::ACTION_KEYS_ANSWER_SEND_QUESTION,
                'description' => '提问审批通过',
            ]);

            // 发消息给推送人
            $noticeServ->passNoticeToQer($questionInfo['uid'], $questionInfo);
        }
    }
}