<?php
/**
 * QuestionService.class.php
 * 调研问题信息表
 *
 * @author: dj
 * @copyright: vchangyi.com
 */

namespace Common\Service;

use Common\Common\User;
use Common\Model\AnswerModel;
use Common\Model\BaseinfoModel;
use Common\Model\QuestionModel;
use Common\Model\RecordModel;

class QuestionService extends AbstractService
{

    // 必填问题
    const REQUIRED_YES = 2;
    // 非必填问题
    const REQUIRED_NO = 1;

    // 含有其他选项
    const OTHER_YES = 1;
    // 不含有其他选项
    const OTHER_NO = 0;

    // 选项类型(1单选,2复选,3下拉选择,14性别选择)
    const OPTION_TYPE = [1, 2, 3, 14];

    // 数字,金额类型
    const NUMBER_TYPE = [7, 8];

    // 段落类型
    const NOTE_TYPE = 12;
    // 多行文本
    const TXT_ROWS_TYPE = 5;

    // 上传图片和上传文件类型
    const FILE_TYPE = [10, 11];

    // 评分类型
    const SCORE_TYPE = 9;

    // 地址类型
    const ADDRESS_TYPE = 22;

    // 存在最长长度和最短长度的类型
    const LENGTH_TYPE = [4, 5];

    // 微信类型
    const WECHAT_TYPE = 17;

    // 性别
    const SEX = 14;

    // 上传图片
    const PICTURE = 10;

    // 选项数
    const OPTION_NUM = 2;

    // 客观题型(1单选,2复选,3下拉选择,9评分)
    const OBJECTIVE = [1, 2, 3, 9];

    // 主观题型(4单行文本,5多行文本,12段落说明)
    const SUBJECTIVE = [4, 5, 12];

    // 问题类型
    const QUESTION_TYPES = [
        1=>'单项选择',
        2=>'多项选择',
        3=>'下拉选择',
        4=>'单行文本',
        5=>'多行文本',
        6=>'日期时间',
        7=>'数字',
        8=>'金额',
        9=>'评分',
        10=>'上传图片',
        11=>'上传文件',
        12=>'段落说明',
        13=>'姓名',
        14=>'性别',
        15=>'手机号',
        16=>'邮箱',
        17=>'微信号',
        18=>'生日',
        19=>'公司',
        20=>'部门',
        21=>'职位',
        22=>'地址'
    ];

    /**
     * @var AnswerModel 调研回答用户记录表
     */
    protected $_d_answer;

    /**
     * @var RecordModel 调研回答详情表
     */
    protected $_d_record;

    /**
     * @var BaseinfoModel 调研基本信息表
     */
    protected $_d_baseinfo;

    // 构造方法
    public function __construct()
    {

        $this->_d = new QuestionModel();
        $this->_d_answer = new AnswerModel();
        $this->_d_record = new RecordModel();
        $this->_d_baseinfo = new BaseinfoModel();

        parent::__construct();
        /*
            问题类型:
            1=单项选择[radio],
            2=多项选择[checkbox],
            3=下拉选择[select],
            4=单行文本[text],
            5=多行文本[textarea],
            6=日期时间[datetime],
            7=数字[number],
            8=金额[money],
            9=评分[score],
            10=上传图片[img],
            11=上传文件[file],
            12=段落说明[note],
            13=姓名[name],
            14=性别[sex],
            15=手机号[mobile],
            16=邮箱[email],
            17=微信号[wechat],
            18=生日[birthday],
            19=公司[company],
            20=部门[department],
            21=职位[job],
            22=地址[address])
         */
    }

    // 数据验证
    public function checkData($data)
    {

        // 问题列表是否为空
        if (empty($data)) {

            E('_EMPTY_QUESTION');
        }

        // 遍历判断问题的各项设置
        foreach ($data as $v) {
            // 问题标题不能为空(段落除外)
            if (empty($v['q_title']) && self::NOTE_TYPE != $v['q_type']) {

                E('_EMPTY_QUESTION_TITLE');
            }

            // 问题类型不能为空
            if (empty($v['q_type'])) {

                E('_EMPTY_QUESTION_TYPE');
            }

            // 问题是否必填不能为空
            if (!isset($v['q_required'])) {

                E('_EMPTY_QUESTION_REQUIRE');
            }

            if (isset($v['q_other']) && !in_array($v['q_other'], [self::OTHER_YES, self::OTHER_NO])) {

                E('_ERR_Q_OTHER');
            }

            // 如果是单选,复选,下拉选择,则最少需要设置两个选项
            if (in_array($v['q_type'], self::OPTION_TYPE)) {

                if (empty($v['q_option']) || count($v['q_option']) < self::OPTION_NUM) {

                    E('_EMPTY_QUESTION_OPTION');
                }

                // 此处需要判断是不是存在重复选项,如果存在则抛错
                $option_data = array_column($v['q_option'], 'option');
                // 选项总数
                $option_count = count($option_data);

                $filter_option_data = [];
                foreach ($option_data as $val) {

                    if (isset($val) && $val != '') {

                        array_push($filter_option_data, $val);
                    }
                }

                // 选项为空
                if (count($filter_option_data) != $option_count) {

                    E('_ERR_EPEAT_OPTION');
                }

                // 选项重复
                if (count(array_unique($filter_option_data)) != $option_count) {

                    E('_ERR_REPEAT_OPTION');
                }
            }

            continue;
        }

        return true;
    }

    /**
     * 组装调研问题数据
     *
     * @param int $qu_id 调研ID
     * @param array $data 调研问题数据
     *
     * @return array  入库问题数据
     */
    public function create_data($qu_id, $data)
    {
        $attach_ids = [];
        $add_data = [];
        $q_order = 0;

        foreach ($data as $v) {
            // 过滤错误数据
            if (empty($v['q_type']) || (empty($v['q_title']) && self::NOTE_TYPE != $v['q_type'])) {

                continue;
            }

            // 如果问题类型是数字或者金额,保留两位小数,其他的保留整数
            if (in_array($v['q_type'], self::NUMBER_TYPE)) {

                $q_min = round($v['q_min'], 2); // 最小值
                $q_max = round($v['q_max'], 2); // 最大值
            } else {

                $q_min = intval($v['q_min']); // 最小值/最少选项数量/最小长度
                $q_max = intval($v['q_max']); // 最大值/最多选项数量/最大长度/评分满分值/上传图片最大数量
            }

            $field = '';
            // 如果是单选,复选,下拉框,
            if (in_array($v['q_type'], self::OPTION_TYPE)) {
                // 此处需要给问题选项新增一个is_check字段(是否选中 0=未选中,1=已选中 )
                foreach ($v['q_option'] as &$_v) {

                    $_v['is_check'] = 0;
                    if (!isset($_v['img_options'])) {

                        $_v['img_options'] = [];
                    } else {
                        $option_img_ids = array_column($_v['img_options'], 'option_img');
                        $attach_ids = array_merge($attach_ids, $option_img_ids);
                    }
                }

                $field = serialize($v['q_option']);
            }

            $add_data[] = [
                'qu_id' => rintval($qu_id), // 调研id
                'q_title' => $v['q_title'], // 问题名称
                'q_placeholder' => $v['q_placeholder'], // 问题描述
                'q_type' => rintval($v['q_type']), // 问题类型
                'q_order' => $q_order, // 问题排序
                'q_other_title' => $v['q_other_title'], // 其他选项标题
                'q_required' => rintval($v['q_required']), // 是否必填(0:非必填,1:必填)
                'q_other' => rintval($v['q_other']), // 是否包含其他选项(0:不包含,1:包含)
                'q_min' => $q_min,
                'q_max' => $q_max,
                'q_data_type' => rintval($v['q_data_type']), // 日期时间/生日格式(1:日期时间,2:日期,3:时间,4:年月日,5:年月,6:月日)
                'q_money_unit' => $v['q_money_unit'], // 金额单位
                'q_field' => $field // 把选项组合成以,分隔的形式
            ];

            $q_order++;
        }

        return [$add_data, $attach_ids];
    }

    /**
     * 遍历检查调研回答
     *
     * @param int $qu_id 调研ID
     * @param int $a_id 用户回答ID
     * @param array $answer_list 回答列表
     *
     * @return array|bool  回答数组
     */
    public function check_answer($qu_id, $a_id, $answer_list)
    {

        $answer_data = [];
        $question_list = $this->_d->list_by_conds(['qu_id' => $qu_id], null, ['q_order' => 'ASC']);

        // 把问题答案转换成以问题ID为key的数组
        $answer_list = array_combine_by_key($answer_list, 'qid');

        $attach_ids = [];

        foreach ($question_list as $v) {
            // 获取此问题的答案信息
            $answer = $answer_list[$v['qid']];

            // 如果问题是单选,复选,下拉选择,性别选择
            if (in_array($v['q_type'], self::OPTION_TYPE)) {

                // 如果该问题为必填选项,并且问题答案为空
                if (self::REQUIRED_YES == $v['q_required']
                    && empty($answer['q_answer_other'])
                    && empty($answer['q_answer_option'])) {

                    E('_EMPTY_ANSWER');
                }

                // 其他选项答案数量
                $answer_option_num = 0;
                if (!empty($answer['q_answer_other'])) {

                    $answer_option_num = 1;
                }

                // 答案选中的总选项数量
                $answer_option_all = count($answer['q_answer_option']) + $answer_option_num;

                // 如果为多选类型,并且设置最少或者最毒选项数量,问题答案不为空的情况下验证当前答案数量是否符合设置
                if ($v['q_type'] == 2 && $answer_option_all > 0) {

                    if (intval($v['q_min']) > 0 && $answer_option_all < intval($v['q_min'])) {

                        E('_ERR_ANSWER_OPTION_MIN');
                    }

                    if (intval($v['q_max']) > 0 && $answer_option_all > intval($v['q_max'])) {

                        E('_ERR_ANSWER_OPTION_MAX');
                    }
                }

                // 组装入库数据
                $answer_data[] = [
                    'qu_id' => $qu_id,
                    'a_id' => $a_id,
                    'q_id' => $v['qid'],
                    'answer' => !empty($answer['q_answer_option']) ? serialize($answer['q_answer_option']) : '',
                    'other' => $answer['q_answer_other']
                ];
            } elseif (in_array($v['q_type'], self::FILE_TYPE)) {
                // 如果问题是图片、附件类型

                // 如果该问题为必填选项,并且问题答案为空
                if (self::REQUIRED_YES == $v['q_required'] && empty($answer['q_answer_option'])) {

                    E('_EMPTY_ANSWER');
                }

                // 组装入库数据
                $answer_data[] = [
                    'qu_id' => $qu_id,
                    'a_id' => $a_id,
                    'q_id' => $v['qid'],
                    'answer' => !empty($answer['q_answer_option']) ? serialize($answer['q_answer_option']) : '',
                    'other' => $answer['q_answer_other']
                ];

                // 收集附件ID
                $img_ids = array_column($answer['q_answer_option'], 'option_img');
                $attach_ids = array_merge($attach_ids, $img_ids);

            } else {
                // 如果该问题为必填选项,类型不是段落类型,并且问题答案为空,抛错
                if (self::REQUIRED_YES == $v['q_required'] && $v['q_type'] != self::NOTE_TYPE
                    && empty($answer['q_answer'])) {

                    E('_EMPTY_ANSWER');
                }

                // 此处检查地址详情的必填信息
                if (self::ADDRESS_TYPE == $v['q_type'] && self::REQUIRED_YES == $v['q_required']
                    && self::OTHER_YES == $v['q_other'] && empty($answer['q_answer_other'])) {

                    E('_EMPTY_ADDRESS_DETAIL');
                }

                // 如果答案不为空,此处检查字符长度合法性
                if (!empty($answer['q_answer']) && in_array($v['q_type'], self::LENGTH_TYPE)) {
                    // 答案实际长度
                    $length = mb_strlen($answer['q_answer'], 'UTF8');

                    // 如果设置了最小长度,检查合法性
                    if ($length < intval($v['q_min']) && intval($v['q_min']) > 0) {

                        E('_ERR_ANSWER_MIN_LENGTH');
                    }

                    // 如果设置了最大长度,检查合法性
                    if ($length > intval($v['q_max']) && intval($v['q_max']) > 0) {

                        E('_ERR_ANSWER_MAX_LENGTH');
                    }
                }

                // 如果答案不为空并且设置了最小值,此处检查合法性
                if (!empty($answer['q_answer']) && in_array($v['q_type'], self::NUMBER_TYPE)) {
                    // 答案的实际数字
                    $number = round($answer['q_answer'], 2);

                    // 如果设置了最小值 ,检查合法性
                    if ($number < round($v['q_min'], 2) && $v['q_min'] > 0) {

                        E('_ERR_ANSWER_MIN_VALUE');
                    }

                    // 如果设置了最大值 ,检查合法性
                    if ($number > round($v['q_max'], 2) && $v['q_max'] > 0) {

                        E('_ERR_ANSWER_MAX_VALUE');
                    }
                }

                // 验证微信
                if (self::WECHAT_TYPE == $v['q_type'] && self::REQUIRED_YES == $v['q_required']) {

                    $wx_reg = "/^[a-zA-Z]{1}[-_a-zA-Z0-9]{5,19}$/";
                    if (!preg_match($wx_reg, $answer['q_answer'])) {

                        E('_ERR_ANSWER_WECHAT');
                    }
                }

                // 组装入库数据
                $answer_data[] = [
                    'qu_id' => $qu_id,
                    'a_id' => $a_id,
                    'q_id' => $v['qid'],
                    'answer' => $answer['q_answer'],
                    'other' => $answer['q_answer_other']
                ];
            }
        }

        return [array_values($answer_data), $attach_ids];
    }

    /**
     * 格式化返回数据
     *
     * @param $data array 需要格式化的数据
     *
     * @return mixed
     */
    public function format_data(&$data)
    {

        $data['qid'] = rintval($data['qid']);
        $data['qu_id'] = rintval($data['qu_id']);
        $data['q_type'] = rintval($data['q_type']);
        $data['q_order'] = rintval($data['q_order']);
        $data['q_required'] = rintval($data['q_required']);
        $data['q_other'] = rintval($data['q_other']);
        $data['q_data_type'] = rintval($data['q_data_type']);
        $data['created'] = rintval($data['created']);

        // 如果问题类型是数字或者金额,保留两位小数,其他的保留整数
        if (in_array($data['q_type'], self::NUMBER_TYPE)) {

            $data['q_min'] = round($data['q_min'], 2);
            $data['q_max'] = round($data['q_max'], 2);
        } else {

            $data['q_min'] = rintval($data['q_min']);
            $data['q_max'] = rintval($data['q_max']);
        }

        $data['q_answer'] = '';
        $data['q_answer_other'] = '';
        $data['q_answer_option'] = [];

        unset($data['deleted'], $data['updated'], $data['status'], $data['domain']);

        return $data;
    }

    /**
     * 格式化问题选项中的图片
     *
     * @param array $option 选项列表
     *
     * @return mixed
     */
    public function _format_img_url(&$option)
    {

        // 如果选项不为空
        if (is_array($option)) {

            foreach ($option as $k => $v) {
                // 如果选项存在图片
                if (count($v['img_options'])) {

                    foreach ($v['img_options'] as $_k => $_v) {

                        $option[$k]['img_options'][$_k]['option_img_url'] = imgUrl($_v['option_img']);
                    }
                }
            }
        }

        return $option;
    }

    /**
     * 获取调研问题回答统计数据
     *
     * @param array $param 请求参数
     *
     * @return array  返回结果
     */
    public function get_result_data($param = [])
    {

        // 获取问题基本信息
        $question = $this->get($param['q_id']);

        // 获取调研基本信息
        $base_info = $this->_d_baseinfo->get($question['qu_id']);

        // 组装基本信息
        $res_data = [
            'q_id' => rintval($question['qid']),
            'q_title' => $question['q_title'],
            'q_placeholder' => $question['q_placeholder'],
            'q_type' => rintval($question['q_type']),
            'q_required' => rintval($question['q_required']),
            'q_other' => rintval($question['q_other'])
        ];

        // 默认值
        $page = !empty($param['page']) ? intval($param['page']) : 1;
        $limit = !empty($param['limit']) ? intval($param['limit']) : 5;

        // 根据不同的问题类型,查询组织返回数据
        if (in_array($question['q_type'], self::OPTION_TYPE)) {
            // -----单选、多选、下拉、性别-----

            // 获取统计结果数据并格式化
            $result_data = $this->get_option_answer_result($question, $page, $limit);

            // 合并组装返回结果
            $result = array_merge($res_data, $result_data);
        } elseif (self::SCORE_TYPE == $question['q_type']) {
            // -----评分类型----

            // 获取统计结果数据并格式化
            $result_data = $this->get_stars_answer_result($question);
            // 合并组装返回结果
            $result = array_merge($res_data, $result_data);
        } elseif (in_array($question['q_type'], self::NUMBER_TYPE)) {
            // 数字、金额类型

            // 获取统计结果数据并格式化
            $result_data = $this->get_number_answer_result($question, $page, $limit, $base_info['anonymous']);
            // 合并组装返回结果
            $result = array_merge($res_data, $result_data);
        } elseif (in_array($question['q_type'], self::FILE_TYPE)) {
            // 图片、文件类型

            // 获取统计结果数据并格式化
            $result_data = $this->get_file_answer_result($question, $page, $limit, $base_info['anonymous']);
            // 合并组装返回结果
            $result = array_merge($res_data, $result_data);
        } elseif (self::NOTE_TYPE == $question['q_type']) {
            // 段落类型,无需查询,直接返回
            $res_data['q_answer_list'] = [];
            $result = $res_data;
        } else {
            // 单行文本,多行文本,日期

            // 获取统计结果数据并格式化
            $result_data = $this->get_text_answer_result($question, $page, $limit, $base_info['anonymous']);
            // 合并组装返回结果
            $result = array_merge($res_data, $result_data);
        }

        return $result;
    }

    /**
     * 获取多选,单选。下拉,性别的数据统计信息
     *
     * @param array $question 问题信息
     *
     * @return array
     */
    public function get_option_answer_result($question = [])
    {

        // 获取该试题的回答记录
        $record_list = $this->_d_record->list_by_where($question['qid']);
        // 【总参与人数】
        $q_total = count($record_list);

        // 题目选项
        $options = unserialize($question['q_field']);
        $data_x = array_column($options, 'option');

        $other_key = -1;
        // 包含其他选项
        if (self::OTHER_YES == $question['q_other']) {

            array_push($data_x, '其他');
            $other_key = array_search('其他', $data_x);
        }

        // 初始data_y为与data_x等长的数组
        $data_y = [];
        $data_y = array_pad($data_y, count($data_x), 0);

        // 初始q_option为与data_x等长的数组
        $q_option = [];
        $q_option = array_pad($q_option, count($data_x), 0);
        // 获取选项数据
        $qu_options = unserialize($question['q_field']);
        $qu_options = array_combine_by_key($qu_options, 'option');

        foreach ($record_list as $key => $record) {

            $answer_option = unserialize($record['answer']);
            // 取出答案数据
            $check_answer_option = array_column($answer_option, 'option');

            foreach ($data_x as $key_x => $val_x) {

                if (in_array($val_x, $check_answer_option)) {

                    $data_y[$key_x]++;
                }

                $q_option[$key_x] = [
                    'option' => $val_x,
                    'is_other' => self::OTHER_NO,
                    'option_img' => $qu_options[$key_x]['option_img'] ? $qu_options[$key_x]['option_img'] : '',
                    'option_img_url' => $qu_options[$key_x]['option_img'] ? imgUrl($qu_options[$key_x]['option_img']) : ''
                ];

                if (self::OTHER_YES == $question['q_other'] && !empty($record['other'])) {

                    $q_option[$other_key] = [
                        'option' => '其他',
                        'is_other' => self::OTHER_YES,
                        'option_img' => '',
                        'option_img_url' => ''
                    ];
                }
            }

            if (self::OTHER_YES == $question['q_other'] && !empty($record['other'])) {

                $data_y[$other_key]++;
            }
        }

        $total_sum = array_sum($data_y);

        foreach ($data_y as $key_y => $val_y) {
            // 百分比
            $option_percent = round(($val_y / $total_sum) * 100, 2);
            // 赋值
            $q_option[$key_y]['option_num'] = $val_y;
            $q_option[$key_y]['option_percent'] = $option_percent . '%';
        }

        return [
            'q_total' => $q_total, // 总参与人数
            'q_data_x' => $data_x, // X轴数据
            'q_data_y' => $data_y, // Y轴数据
            'q_option' => $q_option // 选项数据
        ];
    }

    /**
     * 获取评分类型的数据统计信息
     *
     * @param array $question $question 问题信息
     *
     * @return array
     */
    public function get_stars_answer_result($question = [])
    {

        // 获取评分最大值
        $stars_max = $question['q_max'];

        // 获取该试题的回答记录
        $record_list = $this->_d_record->list_by_where($question['qid']);

        // 获取总参与人数
        $res_data['q_total'] = count($record_list);

        $data_x = []; // X轴数据
        $data_y = []; // Y轴数据
        $q_option = [];

        $score = 0;
        // 循环星级选项数据
        for ($i = 1; $i <= $stars_max; $i++) {
            // 添加到X轴
            $data_x[] = $i . '分';
            // 当前选项填写人数
            $option_num = 0;
            $options_data = [];
            $options_data['is_other'] = self::OTHER_NO;
            $options_data['option'] = $i . '分';

            // 循环回答记录数据
            foreach ($record_list as $v) {
                if ($v['answer'] == $i) {
                    // 累积计算总分
                    $score += $i;
                    // 累加参加人数
                    $option_num++;
                }
            }

            // 赋值回答人数
            $options_data['option_num'] = $option_num;
            // 计算百分比
            $option_percent = round(($option_num / $res_data['q_total']) * 100, 2);
            // 赋值百分比
            $options_data['option_percent'] = $option_percent . "%";

            // 赋值y轴数据
            $data_y[] = $option_percent;
            $q_option[] = $options_data;
        }

        // 计算平均分
        $res_data['q_answer_avg'] = round(($score / $res_data['q_total']), 2);
        $res_data['q_data_x'] = $data_x;
        $res_data['q_data_y'] = $data_y;
        $res_data['q_option'] = $q_option;

        return $res_data;
    }

    /**
     * 获取数字金额类型的数据统计信息
     *
     * @param array $question $question 问题信息
     * @param int $page 页码
     * @param int $limit 每页条数
     * @param int $anonymous 是否匿名(1:实名,0匿名)
     *
     * @return array
     */
    public function get_number_answer_result($question = [], $page = 1, $limit = 5, $anonymous = 0)
    {

        // 获取该试题的回答记录
        $record_list = $this->_d_record->list_by_where($question['qid']);

        // 获取总参与人数
        $res_data['q_total'] = count($record_list);

        // 计算总分
        $total_score = 0;
        $score_list = array_column($record_list, 'answer');
        foreach ($score_list as $v) {

            $total_score += $v;
        }

        $q_answer_list = [];

        // 如果回答记录不为空,则进行分页
        if ($res_data['q_total'] > 0) {
            // 分页格式化
            $q_answer_list = $this->format_other_list($record_list, $page, $limit, $anonymous);
        }

        // 计算平均分
        $res_data['q_answer_avg'] = round(($total_score / $res_data['q_total']), 2);
        $res_data['q_answer_list'] = $q_answer_list;

        return $res_data;
    }

    /**
     * 格式化其他答案的列表
     *
     * @param array $record_list 答案详情列表
     * @param int $page 页码
     * @param int $limit 每页条数
     * @param int $anonymous 是否匿名(1:实名,0:匿名)
     *
     * @return array 返回结果
     */
    public function format_other_list($record_list, $page = 1, $limit = 5, $anonymous = 1)
    {

        $pagedata = $this->page_array($limit, $page, $record_list);

        // 回答记录ID集合
        $a_ids = array_column($pagedata, 'a_id');
        // 获取回答记录列表
        $answer_list = $this->_d_answer->list_by_conds(['a_id' => $a_ids]);
        // 转换成以a_id为key的二维数组
        $answer_list = array_combine_by_key($answer_list, 'a_id');

        $list = [];
        // 如果是匿名填写
        if (BaseinfoService::ANONYMOUS == $anonymous) {

            $i = 1;
            foreach ($pagedata as $v) {
                // 获取回答记录详情
                $answer = $answer_list[$v['a_id']];

                $list[] = [
                    'uid' => !empty($answer['uid']) ? $answer['uid'] : $answer['openid'],
                    'user_name' => '匿名' . $i,
                    'user_dp' => '',
                    'answer_content' => $v['answer'],
                    'answer_time' => $answer['created']
                ];

                $i++;
            }

            return $list;
        }

        // 获取回答记录的用户ID集合
        $uids = array_column($answer_list, 'uid');

        // 查询用户列表,将用户列表转换成以用户uid为主键的二维数组
        $user_list = $this->getUser($uids);

        $list = [];
        foreach ($pagedata as $v) {
            // 获取回答记录详情
            $answer = $answer_list[$v['a_id']];

            $user_dp = '';
            if (empty($answer['uid'])) {
                // 如果回答人uid为空,则是外部用户
                $uid = $answer['openid'];
                $user_name = $answer['username'] ? $answer['username'] : '外部用户';
            } else {
                // 根据回答人ID获取用户详情
                $user_info = $user_list[$answer['uid']];

                $uid = $answer['uid'];
                $user_name = $user_info['memUsername'];

                // 获取部门数组
                $dp_array = $user_info['dpName'];
                // 将部门名称组成一维数组
                $dp_list = array_column($dp_array, 'dpName');
                if (!empty($dp_list)) {

                    $user_dp = implode(',', $dp_list);
                }
            }

            $list[] = [
                'uid' => $uid,
                'user_name' => $user_name,
                'user_dp' => $user_dp,
                'answer_content' => $v['answer'],
                'answer_time' => $answer['created']
            ];
        }

        return $list;
    }

    /**
     * 数组分页
     *
     * @param int $limit 每页条数
     * @param int $page 页码
     * @param array $array 传入数组
     *
     * @return array 返回结果
     */
    private function page_array($limit, $page, $array)
    {

        // 判断当前页面是否为空 如果为空就表示为第一页面
        $page = empty($page) ? 1 : $page;
        // 计算每次分页的开始位置
        $start = ($page - 1) * $limit;
        $total = count($array);

        $page_data = [];
        if ($total) {

            $page_data = array_slice($array, $start, $limit);
        }

        return $page_data;
    }

    /**
     * 获取图片,文件 类型的数据统计信息
     *
     * @param array $question $question 问题信息
     * @param int $page 页码
     * @param int $limit 每页条数
     * @param int $anonymous 是否匿名(1:实名,0:匿名)
     *
     * @return array
     */
    public function get_file_answer_result($question = [], $page = 1, $limit = 5, $anonymous = 1)
    {

        // 获取该试题的回答记录
        $record_list = $this->_d_record->list_by_where($question['qid']);

        // 获取总参与人数
        $total = count($record_list);

        $q_answer_list = [];

        // 如果回答记录不为空,则进行分页
        if ($total) {
            // 分页格式化
            $q_answer_list = $this->format_other_list($record_list, $page, $limit, $anonymous);
        }

        // 循环数据,格式化图片显示
        foreach ($q_answer_list as &$v) {

            $answer_option_data = [];
            if (!empty($v['answer_content'])) {
                // 反序列化答案数据
                $answer_option_data = unserialize($v['answer_content']);
            }

            $pic_data = [];
            // 此处需要处理答案中包含的图片的显示路径
            foreach ($answer_option_data as $_v) {

                if (!empty($_v['option_img'])) {

                    $pic_data[]['imgUrl'] = imgUrl($_v['option_img']);
                }
            }

            // 删掉多余字段
            unset($v['answer_content']);
            // 新增图片列表数据
            $v['file_list'] = $pic_data;
        }

        return [
            'q_total' => $total,
            'q_answer_list' => $q_answer_list
        ];
    }

    /**
     * 获取单行文本,多行文本,日期时间 类型的数据统计信息
     *
     * @param array $question $question 问题信息
     * @param int $page 页码
     * @param int $limit 每页条数
     * @param int $anonymous 是否匿名(1:实名,0匿名)
     *
     * @return array
     */
    public function get_text_answer_result($question = [], $page = 1, $limit = 5, $anonymous = 1)
    {

        // 获取该试题的回答记录
        $record_list = $this->_d_record->list_by_where($question['qid']);
        // 获取总参与人数
        $res_data['q_total'] = count($record_list);

        $q_answer_list = [];
        // 如果回答记录不为空,则进行分页
        if ($res_data['q_total']) {
            // 分页格式化
            $q_answer_list = $this->format_other_list($record_list, $page, $limit, $anonymous);
        }

        $res_data['q_answer_list'] = $q_answer_list;

        return $res_data;

    }

    /**
     * 获取其他选项答案数据列表
     *
     * @param array $param 传入参数
     *
     * @return array
     */
    public function get_other_result_list($param = [])
    {

        // 获取该试题的回答记录
        $record_list = $this->_d_record->list_by_where($param['q_id']);

        // 获取问题基本信息
        $question = $this->_d->get($param['q_id']);
        // 获取试卷基本信息
        $base_info = $this->_d_baseinfo->get($question['qu_id']);

        // 其他选项列表数据
        $q_other_list = [];
        foreach ($record_list as $_v) {
            // 如果答案为空,则continue;
            if (empty($_v['other'])) {

                continue;
            }

            // 组装其他答案数据列表
            $q_other_list[] = [
                'a_id' => $_v['a_id'],
                'answer' => $_v['other']
            ];
        }

        // 总数
        $total = count($q_other_list);

        // 判断分页参数是否为空,为空赋默认值
        $page = !empty($params['page']) ? rintval($params['page']) : 1;
        $limit = !empty($params['limit']) ? rintval($params['limit']) : 10;

        $list = [];
        // 格式化其他选项列表数据
        if (!empty($q_other_list)) {

            $list = $this->format_other_list($q_other_list, $page, $limit, $base_info['anonymous']);
        }

        return [
            'page' => $page,
            'limit' => $limit,
            'total' => $total,
            'list' => $list
        ];
    }
}