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

namespace Apicp\Controller\Answer;

use Com\PackageValidate;
use Common\Common\AnswerHelper;
use Common\Common\Constant;
use Common\Service\AnswerService;
use Common\Service\QuestionService;
use Common\Service\ImgService;


class AnswerListController extends \Apicp\Controller\AbstractController
{
    /**
     * AnswerList
     * @author
     * @desc 回答列表
     * @param Int page:1 当前页
     * @param Int limit:20 每页数据总数
     * @param Int question_id:true 提问ID
     * @param Int check_status:true 审核状态(1=未审核;2=审核通过;3=审核未通过)
     * @return array 回答列表
                    array(
                    'total' => 1, // 当前数据总数
                    'wait_total' => 1, // 未审批回答数
                    'pass_total' => 1, // 已通过回答数
                    'fail_total' => 1, // 未通过回答数
                    'list' => array(
                    'answer_recommend'=>1, // 是否推荐答案(1=推荐 2=不推荐)
                    'answer_id' => 2,
                    'user_type' => 2, // 回答人类型(1=用户;2=管理员)
                    'uid' => '0BD8C5557F00000171F5B2321A874407', // 人员ID
                    'username' => '张三', // 人员姓名
                    'face' => 'http://qy.vchangyi.com', // 人员头像
                    'answer_content' => '这个问题简单', // 回答内容
                    'imgs' => array(
                    array(
                    'at_id' => 'abcdefg', // 图片ID
                    'at_url' => 'http://qy.vchagyi.com', // 图片URL
                    ),
                    ),
                    'is_best' => 1, // 是否是最佳答案(1=否;2=是)
                    'like_total' => 10, // 点赞总数
                    'check_status' => 1, // 审核状态(1=未审核;2=审核通过;3=审核未通过)
                    'checker_name' => '李四', // 审核人姓名
                    'checker_dps' => '技术部', // 审核人部门
                    'checker_type' => 1, // 审核人类型(1=用户;2=管理员)
                    'check_time' => 1234567890, // 审核时间
                    )
                    );
     */
    public function Index_post()
    {

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

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

        // 分页默认值
        $postData['page'] = isset($postData['page']) ? $postData['page'] : Constant::PAGING_DEFAULT_PAGE;
        $postData['limit'] = isset($postData['limit']) ? $postData['limit'] : Constant::PAGING_DEFAULT_LIMIT;
        list($start, $perpage) = page_limit($postData['page'], $postData['limit']);

        // 条件
        $conds = [
            'question_id' => $postData['question_id'],
            'check_status' => $postData['check_status'],
        ];

        // 排序(最佳答案 > 点赞数 > 审批时间)
        $order_option = [
            'is_best' => 'DESC',
            'is_recommend'=>'DESC',
            'like_total' => 'DESC',
            'check_time' => 'DESC',
            'created' => 'DESC'
        ];

        // 获取问题详情
        $question_service = new QuestionService();
        $info = $question_service->get($postData['question_id']);

        // 问题列表
        $answerServ = new AnswerService();
        $list = $answerServ->list_by_conds($conds, [$start, $perpage], $order_option,"*,IF (answer_id='".$info['answer_id']."','1','0') AS is_recommend");
        if (!empty($list)) {
            // 提问人的头像
            $uids = array_values(array_column($list, 'uid'));
            $answerHelper = &AnswerHelper::instance();
            list($dpNames, $faceList, $usernameList, $userSex) = $answerHelper->getUserInfo($uids);

            // 审核人部门
            $checkerIds = [];
            foreach ($list as $key => $answer) {
                if ($answer['checker_type'] == Constant::CHECKER_IS_USER) {
                    $checkerIds[] = $answer['checker_uid'];
                }
            }
            if (!empty($checkerIds)) {
                list($checkerDps) = $answerHelper->getUserInfo($checkerIds);
            }

            // 图片
            $answerIds = array_column($list, 'answer_id');
            $imgServ = new ImgService();
            $imgList = $imgServ->list_by_conds(['answer_id' => $answerIds]);

            // 组合部门、头像、图片
            foreach ($list as $k => $v) {
                $list[$k]['answer_recommend'] = $info['answer_id'] == $v['answer_id'] ? Constant::ANSWER_ID_RECOMMEND : Constant::ANSWER_ID_NOT_RECOMMEND;
                $list[$k]['username'] = isset($usernameList[$v['uid']]) ? $usernameList[$v['uid']] : $v['username'];
                $list[$k]['face'] = isset($faceList[$v['uid']]) ? $answerHelper->format_face($faceList[$v['uid']]) : '';
                $list[$k]['user_sex'] = isset($userSex[$v['uid']]) ? $userSex[$v['uid']] : 0;
                $list[$k]['checker_dps'] = isset($checkerDps[$v['checker_uid']]) ? $checkerDps[$v['checker_uid']] : '';
                $list[$k]['imgs'] = [];
                foreach ($imgList as $imgkey => $imgInfo) {
                    if ($imgInfo['answer_id'] == $v['answer_id']) {

                        $list[$k]['imgs'][] = $imgInfo;
                        unset($imgList[$imgkey]);
                    }
                }

                if (Constant::ANONYMOUS_OK == $v['is_anonymous']) {
                    $list[$k]['username'] = '';
                    $list[$k]['face'] = '';
                }
            }
        }

        // 各个状态的回答总数
        $count = [];
        // 待审核回答数
        $conds['check_status'] = Constant::ANSWER_CHECK_STATUS_WAIT;
        $count[Constant::ANSWER_CHECK_STATUS_WAIT] = $answerServ->count_by_conds($conds);

        // 审核通过回答数
        $conds['check_status'] = Constant::ANSWER_CHECK_STATUS_PASS;
        $count[Constant::ANSWER_CHECK_STATUS_PASS] = $answerServ->count_by_conds($conds);

        // 审核未通过回答数
        $conds['check_status'] = Constant::ANSWER_CHECK_STATUS_FAIL;
        $count[Constant::ANSWER_CHECK_STATUS_FAIL] = $answerServ->count_by_conds($conds);

        $this->_result = [
            'total' => $count[$postData['check_status']],
            'wait_total' => $count[Constant::ANSWER_CHECK_STATUS_WAIT],
            'pass_total' => $count[Constant::ANSWER_CHECK_STATUS_PASS],
            'fail_total' => $count[Constant::ANSWER_CHECK_STATUS_FAIL],
            'list' => $list,
        ];
    }
}