CommentListController.class.php 11.8 KB
<?php
/**
 * 【销售活动-后台】获取评论列表接口
 *
 * User: WJY
 * Date: 2017-11-02
 */

namespace Apicp\Controller\Comment;

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

class CommentListController extends \Apicp\Controller\AbstractController
{

    // 回复默认页码,默认页大小
    const REPLY_DEFAULT_PAGE = 1;
    const REPLY_DEFAULT_LIMIT = 5;

    /** @var ActivityService */
    protected $activity_s;
    /** @var PacketrecordService */
    protected $packetrecord_s;
    /** @var CommentService */
    protected $comment_s;
    /** @var ReplyService */
    protected $reply_s;
    /** @var User */
    protected $user_s;

    public function before_action($action = '')
    {

        if (!parent::before_action($action)) {

            return false;
        }

        // 实例化活动信息表
        $this->activity_s = new ActivityService();
        // 实例化红包记录表
        $this->packetrecord_s = new PacketrecordService();
        // 实例化评论表
        $this->comment_s = new CommentService();
        // 实例化回复表
        $this->reply_s = new ReplyService();
        // 用户公共类
        $this->user_s = new User();

        return true;
    }

    public function Index_post()
    {

        // 验证规则
        $rules = [
            'ac_id' => 'require',
            'check_status' => 'require|in:0,1,2',
        ];

        $msg = [
            'ac_id.require' => L('_EMPTY_ACTIVITY_ID'),
            'check_status.require' => L('_ERR_COMMENT_STATUS_QUERY'),
            'check_status.in' => L('_ERR_COMMENT_STATUS_QUERY'),
        ];

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

        $postData = $validate->postData;
        $params = I('post.');

        // 分页
        $page = !empty($params['page']) ? intval($params['page']) : ActivityService::DEFAULT_PAGE;
        $limit = !empty($params['limit']) ? intval($params['limit']) : ActivityService::DEFAULT_LIMIT;
        list($start, $limit) = page_limit($page, $limit);

        // 按照发布时间排序
        $order_option = ['created' => 'DESC'];

        // 活动获取
        $activity_info = $this->activity_s->get($postData['ac_id']);
        if (empty($activity_info)) {

            E('_ERR_ACTIVITY_NOT_FOUND');
        }

        // tab头部的数目统计 待审核|已通过|已驳回
        // 查询待审核
        $check_conds = [
            'ac_id' => $postData['ac_id'],
            'check_status' => CommentService::CHECK_ING,
        ];
        // 查询已通过
        $pass_conds = [
            'ac_id' => $postData['ac_id'],
            'check_status' => CommentService::CHECK_OK,
        ];
        // 查询已驳回
        $fail_conds = [
            'ac_id' => $postData['ac_id'],
            'check_status' => CommentService::CHECK_NO,
        ];
        // 查询当前状态数据
        $comment_conds = [
            'ac_id' => $postData['ac_id'],
            'check_status' => $postData['check_status'],
        ];

        // 如果查询人员
        if (!empty($params['username'])) {

            $uids = $this->activity_s->get_username_uid($params['username']);
            // 组装用户查询条件
            $check_conds['uid'] = $uids;
            $pass_conds['uid'] = $uids;
            $fail_conds['uid'] = $uids;
            $comment_conds['uid'] = $uids;
        }

        $checking_total = $this->comment_s->count_by_conds($check_conds);
        $pass_total = $this->comment_s->count_by_conds($pass_conds);
        $fail_total = $this->comment_s->count_by_conds($fail_conds);

        // 定义当前列表的总数
        if (CommentService::CHECK_ING == $postData['check_status']) {

            $total = $checking_total;
        } elseif (CommentService::CHECK_OK == $postData['check_status']) {

            $total = $pass_total;
        } else {

            $total = $fail_total;
        }

        $fields = 'comment_id,uid,created,check_time,content,check_uid,check_uname,check_phone,likes,replys,pic_ids,reason,ext_fields';
        $comment_list = $this->comment_s->list_by_conds($comment_conds, [$start, $limit], $order_option, $fields);

        // 获取评论列表的用户信息
        $comment_uids = array_column($comment_list, 'uid');
        $user_list = $this->user_s->listByUid($comment_uids);

        // 开启红包 && 已通过 组装红包数据
        if (ActivityService::RED_OPENED == $activity_info['is_red_open']
            && CommentService::CHECK_OK == $postData['check_status']) {
            // 当前页评论ID
            $comment_ids = array_column($comment_list, 'comment_id');
            // 获取当前页的所有红包
            $red_conds = [
                'cid' => $comment_ids,
            ];
            $red_list_all = $this->packetrecord_s->list_by_conds($red_conds);
            $red_list_all = array_combine_by_key($red_list_all, 'rid');
        }

        // 查询回复列表
        // check_avatar	String	审核人头像 二期加上
        foreach ($comment_list as $cmt_k => $cmt_v) {
            // 存在子评论则查询获取
            $reply_list = [];
            if (!empty($cmt_v['replys'])) {

                $reply_list = $this->get_reply_list($cmt_v['comment_id']);
            }

            // 如果本页评论存在红包
            if (!empty($red_list_all)) {

                $red_list = [];
                $red_temp = array_column($red_list_all, 'cid', 'rid');
                $rids = array_keys($red_temp, $cmt_v['comment_id']);
                foreach ($rids as $red_v) {

                    $red_list[] = [
                        'red_id' => $red_list_all[$red_v]['rid'],
                        'red_type' => $red_list_all[$red_v]['p_type'],
                        'packet_status' => $red_list_all[$red_v]['packet_status'],
                        'red_money' => sprintf("%.2f", $red_list_all[$red_v]['rand_money'] / 100),
                        'send_uname' => $red_list_all[$red_v]['send_uname'],
                        'created' => $red_list_all[$red_v]['give_time']
                    ];
                }
            }
            // 处理已删除角色的信息
            $user_info = $user_list[$cmt_v['uid']];
            if (empty($user_info)) {
                $user_info = $this->user_s->getByUid($cmt_v['uid']);
            }
            $comment_temp = [
                'reply_list' => !empty($reply_list) ? $reply_list : [],
                'name' => $user_info['memUsername'],
                'avatar' => $user_info['memFace'],
                'likes_num' => $cmt_v['likes'],
                'content' => $this->comment_s->enter($cmt_v['content']),
                'image_list' => $this->list_img_by_atids($cmt_v['pic_ids']),
                'red_list' => !empty($red_list) ? $red_list : [],
                'reply_total' => (int)$cmt_v['replys'],
                'reply_page' => self::REPLY_DEFAULT_PAGE,
                'reply_limit' => self::REPLY_DEFAULT_LIMIT
            ];

            unset($comment_list[$cmt_k]['likes'], $comment_list[$cmt_k]['content']);
            // 重新定义扩展字段的值
            $comment_list[$cmt_k]['ext_fields'] = $this->get_ext_fields_list($comment_list[$cmt_k]['ext_fields']);
            $comment_list[$cmt_k] = array_merge((array)$comment_list[$cmt_k], (array)$comment_temp);
        }

        $result = [
            'page' => (int)$page,
            'limit' => (int)$limit,
            'total' => (int)$total,
            'stop_pay_time' => $activity_info['stop_pay_time'],
            'checking_total' => (int)$checking_total,
            'pass_total' => (int)$pass_total,
            'fail_total' => (int)$fail_total,
            'is_red_open' => (int)$activity_info['is_red_open'],
            'check_type' => (int)$activity_info['check_type'],
            'list' => $comment_list
        ];

        $this->_result = $result;

        return true;
    }

    /**
     * 获取自定义字段列表
     * @param array $data 自定义字段数据
     * @return array
     */
    protected function get_ext_fields_list($data = [])
    {
        if (empty($data)) {
            return [];
        }

        // 初始化列表
        $list = [];
        $data_list = unserialize($data);
        // 遍历数据
        foreach ($data_list as $key => $v) {
            // 如果是文本类型或者时间类型
            if (Helper::EXT_FIELD_TEXT_TYPE == $v['type'] ||
                Helper::EXT_FIELD_TIME_HI_TYPE == $v['type'] ||
                Helper::EXT_FIELD_TIME_YMD_TYPE == $v['type'] ||
                Helper::EXT_FIELD_TIME_YMD_HI_TYPE == $v['type']
            ) {
                $list[] = ['name' => $v['name'], 'value' => $v['text'], 'type' => intval($v['type'])];
            }
            // 数字类型
            if (Helper::EXT_FIELD_NUMBER_TYPE == $v['type']) {
                $list[] = ['name' => $v['name'], 'value' => $v['text'] . $v['unit'], 'type' => intval($v['type'])];
            }
            // 单选类型或者多选类型
            if (Helper::EXT_FIELD_RADIO_TYPE == $v['type'] || Helper::EXT_FIELD_MULTI_SELECT_TYPE == $v['type']) {
                // 默认选中项
                $check = [];
                foreach ($v['options'] as $item) {
                    // 如果被选中
                    if (Helper::EXT_FIELD_CHECK == $item['value']) {
                        $check[] = $item['key'];
                    }
                }

                $list[] = ['name' => $v['name'], 'value' => implode(';', $check), 'type' => intval($v['type'])];
            }
        }

        return $list;
    }

    /**
     * 根据评论ID获取回复列表
     *
     * @param $comment_id
     *
     * @return array|bool
     */
    protected function get_reply_list($comment_id)
    {

        // 回复分页设置
        list($start, $limit) = page_limit(self::REPLY_DEFAULT_PAGE, self::REPLY_DEFAULT_LIMIT);

        $fields = 'comment_id,reply_id,created,content,likes,uid,pic_ids';
        $reply_list = $this->reply_s->list_by_conds(['comment_id' => $comment_id], [$start, $limit],
            ['created' => 'DESC'], $fields);

        // 获取回复列表的用户信息
        $reply_uids = array_column($reply_list, 'uid');
        $user_list = $this->user_s->listByUid($reply_uids);

        foreach ($reply_list as $rep_k => $rep_v) {

            // 处理已删除角色的信息
            $user_info = $user_list[$rep_v['uid']];
            if (empty($user_info)) {
                $user_info = $this->user_s->getByUid($rep_v['uid']);
            }

            $reply_temp = [
                'name' => $user_info['memUsername'],
                'avatar' => $user_info['memFace'],
                'time' => $rep_v['created'],
                'content' => $this->reply_s->enter($rep_v['content']),
                'likes_num' => $rep_v['likes'],
                'image_list' => $this->list_img_by_atids($rep_v['pic_ids'])
            ];

            unset(
                $reply_list[$rep_k]['created'],
                $reply_list[$rep_k]['likes'],
                $reply_list[$rep_k]['content']
            );

            $reply_list[$rep_k] = array_merge((array)$reply_list[$rep_k], (array)$reply_temp);
        }

        return $reply_list;
    }

    /**
     * 根据atid(逗号分隔)获取图片列表数组
     *
     * @param string $atids
     *
     * @return array
     */
    protected function list_img_by_atids($atids = '')
    {

        $img_list = [];
        if (!empty($atids)) {

            $atid_arr = explode(',', $atids);
            foreach ($atid_arr as $at_v) {

                $img_list[] = [
                    'atId' => $at_v,
                    'imgUrl' => imgUrlReal($at_v)
                ];
            }
        }

        return $img_list;
    }
}