CheckController.class.php 8.14 KB
<?php
/**
 * 【业绩比拼】回帖审核接口
 *
 * User: heyuelong
 * Date: 2018年1月29日17:36:12
 */

namespace Api\Controller\Comment;

use Com\PackageValidate;
use Common\Common\Helper;
use Common\Common\User;
use Common\Service\ActivityService;
use Common\Service\CommentService;

class CheckController extends \Api\Controller\AbstractController
{

    /**
     * 接收参数
     * comment_id int 必填 评论ID
     * check_status int 必填 审核状态(1:已审核,2:已驳回)
     * reason string 选填 驳回理由
     *
     * @return bool
     */
    public function Index_post()
    {

        // 验证规则
        $rules = [
            'comment_id' => 'require',
            'check_status' => 'require|between:1,2',
            'reason' => 'max:140'
        ];

        $msg = [
            'comment_id.require' => L('_ERR_COMMENT_ID_EMPTY'),
            'check_status.require' => L('_ERR_APPROVAL_TYPE'),
            'check_status.between' => L('_ERR_APPROVAL_TYPE'),
            'reason.max' => L('_ERR_APPROVAL_REASON_LENGTH'),
        ];

        // 验证数据
        $validate = new PackageValidate($rules, $msg, array_keys($rules));
        $postData = $validate->postData;

        // 根据评论ID 获取 评论详情与活动详情(含验证)
        $cmt_and_ac_info = $this->get_ac_info_by_comment_id($postData['comment_id']);

        // 获取审核人列表
        $check_user_list = unserialize($cmt_and_ac_info['activity_info']['check_user_list']);
        $mem_uids = array_column($check_user_list, 'memID');
        // 无权进行操作
        if (!in_array($this->uid, $mem_uids)) {

            E('_ERR_COMMENT_QUIT');
        }

        // 只许审核【待审核】状态的回帖
        if ($cmt_and_ac_info['comment_info']['check_status'] != CommentService::CHECK_ING) {

            E('_ERR_COMMENT_CHECK_STATUS');
        }

        $comment_s = new CommentService();

        // 更新评论信息
        $comment_update_data = [
            'check_status' => $postData['check_status'],
            'check_uname' => strval($this->_login->user['memUsername']),
            'check_phone' => strval($this->_login->user['memMobile']),
            'reason' => $postData['reason'] ? $postData['reason'] : '', // 只有驳回时会填写理由
            'check_uid' => $this->uid,
            'check_time' => MILLI_TIME, // 审核时间
        ];

        try {
            // 开始事务
            $comment_s->start_trans();
            $comment_s->update($postData['comment_id'], $comment_update_data);

            if (CommentService::CHECK_OK == $postData['check_status']) {
                // 活动id
                $ac_id = rintval($cmt_and_ac_info['activity_info']['ac_id']);

                // 查询该用户在此活动的已通过的参与记录【包含已删除的】
                $count = $comment_s->count_by_conds_contain_del([
                    'check_status' => CommentService::CHECK_OK,
                    'uid' => $cmt_and_ac_info['comment_info']['uid'],
                    'ac_id' => $ac_id
                ]);

                // 初始化主表
                $activity_s = new ActivityService();
                if (Helper::COMMENT_COUNT_ONE == $count) {
                    // 更新活动表参与人数与评论数
                    $activity_s->update($ac_id, ['join_num=join_num+(?)' => 1, 'comments=comments+(?)' => 1]);
                } else {
                    // 更新活动评论数
                    $activity_s->update($ac_id, ['comments=comments+(?)' => 1]);
                }
            }

            // 提交事物
            $comment_s->commit();
        } catch (\Think\Exception $e) {

            \Think\Log::record($e);
            $comment_s->rollback();

            return false;
        } catch (\Exception $e) {

            \Think\Log::record($e);
            $comment_s->rollback();

            return false;
        }

        // 发送审核消息
        $comment_msg_params = [
            'subject' => $cmt_and_ac_info['activity_info']['subject'],
            'comment_id' => $cmt_and_ac_info['comment_info']['comment_id'],
            'ac_id' => $cmt_and_ac_info['activity_info']['ac_id'],
            'created' => $cmt_and_ac_info['comment_info']['created'],
            'username' => strval($this->_login->user['memUsername']),
            'check_time' => MILLI_TIME,
            'uids' => $cmt_and_ac_info['comment_info']['uid']
        ];
        // 回帖审核通过
        $message_type = CommentService::MSG_COMMENT_CHECK_SUCCESS;

        // 审核状态:已驳回
        if (CommentService::COMMENT_CHECK_REJECT == $postData['check_status']) {
            // 回帖审核驳回
            $message_type = CommentService::MSG_COMMENT_CHECK_FAIL;
            // 驳回原因
            $comment_msg_params['reason'] = $postData['reason'] ? $postData['reason'] : '';
        }

        // 推送审核消息
        $comment_s->send_msg($comment_msg_params, $message_type);

        // 如果开启发帖推送消息提醒 且 【审核通过】
        if (Helper::SALE_ALL_LOOK == $cmt_and_ac_info['activity_info']['is_post_remind']
            && CommentService::CHECK_OK == $postData['check_status']) {
            // 获取用户详情
            $user_info = User::instance()->getByUid($cmt_and_ac_info['comment_info']['uid']);
            // 拼接发送消息内容
            $cmt_and_ac_info['activity_info']['content'] = strval($cmt_and_ac_info['comment_info']['content']);
            // 给有权限【查看】活动的人员推送消息
            $comment_s->send_look_msg($cmt_and_ac_info['activity_info'], $user_info['memUsername'], $cmt_and_ac_info['comment_info']['uid']);
        }

        // 活动开启了红包功能 且 【审核通过】
        if (ActivityService::RED_OPENED == $cmt_and_ac_info['activity_info']['is_red_open']
            && CommentService::CHECK_OK == $postData['check_status']) {
            // 组织分配红包请求参数
            $red_params = [
                'ac_id' => $cmt_and_ac_info['comment_info']['ac_id'],
                'p_type' => CommentService::RED_PACKET_RAND,
                'uid' => $cmt_and_ac_info['comment_info']['uid'],
                'comment_id' => $cmt_and_ac_info['comment_info']['comment_id'],
            ];
            // 随机获取剩余红包并发送消息
            $red_data = $comment_s->get_red_record($red_params);

            // 发放红包失败(超过领取限制或其他)
            if (empty($red_data['rid'])) {

                return true;
            }

            $red_msg_params = [
                'comment_id' => $cmt_and_ac_info['comment_info']['comment_id'],
                'rid' => $red_data['rid'],
                'ac_id' => $red_data['ac_id'],
                'subject' => $cmt_and_ac_info['activity_info']['subject'],
                'give_time' => $red_data['give_time'],
                'uids' => $cmt_and_ac_info['comment_info']['uid'],
            ];

            // 给用户发消息【获得随机红包】
            $comment_s->send_msg($red_msg_params, CommentService::MSG_GET_RANDOM_REDPACKET);

        }

        return true;
    }

    /**
     * 根据评论ID获取评论与活动详情(含验证)
     *
     * @param $comment_id
     *
     * @return array
     *          + array comment_info 评论详情
     *          + array activity_info 活动详情
     */
    protected function get_ac_info_by_comment_id($comment_id)
    {

        // 查询评论详情
        $comment_s = new CommentService();
        $comment_info = $comment_s->get($comment_id);
        // 评论不存在或已被删除
        if (empty($comment_info)) {

            E('_ERR_COMMENT_NOT_FOUND');
        }

        // 查询活动详情
        $activity_s = new ActivityService();
        $activity_info = $activity_s->get($comment_info['ac_id']);
        // 只开启审核未开启红包,存在活动被删除的情况
        if (empty($activity_info)) {

            E('_ERR_ACTIVITY_NOT_FOUND');
        }

        // 返回评论详情与活动详情
        return [
            'comment_info' => $comment_info, // 评论详情
            'activity_info' => $activity_info, // 活动详情
        ];
    }
}