<?php
/**
 * 更新老数据
 * User: yingcai
 * Date: 2017/10/25
 * Time: 上午10:06
 */

namespace Frontend\Controller\Temp;

use Common\Service\AnswerDetailExtendService;
use Common\Service\AnswerDetailService;
use Common\Service\AnswerService;
use Common\Service\PaperService;
use Common\Service\RandomSnapshotService;
use Common\Service\SnapshotService;
use Common\Service\TopicService;

class UpdateOldDataController extends AbstractController
{

    protected $_require_login = false;

    /** @var PaperService 试卷信息表 */
    protected $paper_s;
    /** @var RandomSnapshotService */
    protected $random_s;
    /** @var AnswerService */
    protected $answer_s;
    /** @var AnswerDetailService */
    protected $answer_detail_s;
    /** @var SnapshotService */
    protected $snapshot_s;
    /** @var TopicService */
    protected $topic_s;
    /** @var AnswerDetailExtendService */
    protected $detail_extend_s;

    public function before_action($action = '')
    {

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

            return false;
        }

        $this->paper_s = new PaperService();
        $this->random_s = new RandomSnapshotService();
        $this->answer_s = new AnswerService();
        $this->answer_detail_s = new AnswerDetailService();
        $this->snapshot_s = new SnapshotService();
        $this->topic_s = new TopicService();
        $this->detail_extend_s = new AnswerDetailExtendService();

        return true;
    }

    // 更新随机的老数据(注:oa_exam_random_snapshot表created、updated、deleted字段类型修改)
    public function Index()
    {

        $conds = [
            'ep_type' => PaperService::TOPIC_RANDOM,
            'is_old' => PaperService::OLD_DATA_STATE,
        ];

        // 获取所有老数据的考试
        $old_papers = $this->paper_s->listWithOutDomian($conds);

        foreach ($old_papers as $paper) {
            $temp_list = [];
            // 获取匹配出的题目
            $topic_list = $this->paper_s->get_temp_list_by_epid($paper['ep_id']);
            $topic_list = $this->format_topic_data($topic_list, $paper['bank_topic_data']);

            foreach ($topic_list as $key => $val) {

                $rule_data = unserialize($paper['rule']);
                // 根据规则设置分数
                $score = $this->paper_s->get_score_by_type($val['et_type'], $rule_data);
                $topic_data = [];
                $topic_data['ep_id'] = $paper['ep_id'];
                $topic_data['eb_id'] = $val['eb_id'];
                $topic_data['et_type'] = $val['et_type'];
                $topic_data['et_id'] = $val['et_id'];
                $topic_data['title'] = $val['title'];
                $topic_data['title_pic'] = $val['title_pic'];
                $topic_data['options'] = $val['options'];
                $topic_data['score'] = $score;
                $topic_data['answer'] = $val['answer'];
                $topic_data['answer_resolve'] = $val['answer_resolve'];
                $topic_data['answer_coverage'] = $val['answer_coverage'];
                $topic_data['match_type'] = $val['match_type'];
                $topic_data['answer_keyword'] = $val['answer_keyword'];
                $topic_data['order_num'] = $key;
                $topic_data['created'] = MILLI_TIME;
                $temp_list[] = $topic_data;
            }

            // 存入随机题库列表
            $this->random_s->insert_all($temp_list);
            // 更新is_old字段
            $this->paper_s->update($paper['ep_id'], ['is_old' => PaperService::NEW_DATA_STATE]);
        }
    }

    /**
     * 更新ep_id
     */
    public function updateDetailEpId()
    {

        $page = 1;
        $page_num = 5000;

        $domain = QY_DOMAIN;

        while ($page > 0) {
            // 分页
            $limit = ($page - 1) * $page_num . ',' . $page_num;

            // 实例化think下的model类
            $com_model = new \Think\Model();

            // 从answer_detail表获取ea_id
            $sql = "SELECT ea_id FROM `oa_exam_answer_detail` WHERE `ep_id` = 0 AND `et_id` = 0 AND status < 3 AND domain = '{$domain}' GROUP BY ea_id ORDER BY ea_id DESC LIMIT {$limit}";
            $ea_ids = $com_model->query($sql);

            if (empty($ea_ids)) {

                exit("PAGE:{$page} SUCCESS");
            }

            // 从answer表获取ep_id
            $ea_ids = array_column($ea_ids, 'ea_id');
            $answer_list = $this->answer_s->list_by_pks($ea_ids);

            foreach ($answer_list as $v) {

                $this->answer_detail_s->update_by_conds(['ea_id' => $v['ea_id']], ['ep_id' => $v['ep_id']]);
            }

            echo $page . "\r\n";

            $page++;

//            sleep(2);
        }
    }

    /**
     * 更新et_id
     */
    public function updateDetailEtId()
    {

        $page = 1;
        $limit = 5000;

        while ($page > 0) {
            // 查询条件
            $conds = ['et_id' => 0];
            // 分页
            $start = ($page - 1) * $limit;
            $page_option = [$start, $limit];
            // 排序
            $order_by = ['ead_id' => 'DESC'];
            // 查询字段
            $fields = 'ead_id,ep_id,et_detail,et_option';

            // 获取答题列表
            $answer_detail_list = $this->answer_detail_s->listWithOutDomian($conds, $page_option, $order_by, $fields);

            if (empty($answer_detail_list)) {

                exit("PAGE:{$page} SUCCESS");
            }

            // 初始化answer_detail_extend表数据
            $answer_detail_extend = [];
            foreach ($answer_detail_list as $val) {
                $et_detail = unserialize($val['et_detail']);

                // 获取esr_id
                if (!$et_detail['et_id']) {

                    $res = $this->list_by_rand_snapshot_conds($val['ep_id'], $et_detail, $val['et_option']);

                    if (!$res) {

                        continue;
                    } else {

                        $et_detail['et_id'] = $res['et_id'];
                        $esr_id = $res['esr_id'];
                    }
                } else {

                    $esr_id = $this->get_esr_id($val['ep_id'], $et_detail['et_id']);
                }

                $result = $this->answer_detail_s->update(
                    $val['ead_id'],
                    [
                        'et_id' => $et_detail['et_id'],
                        'esr_id' => $esr_id ? $esr_id : 0,
                    ]
                );

                if ($result) {
                    // 根据 ep_id,et_id 查询answer_detail_extend表是否存在此信息
                    $is_exist = $this->detail_extend_s->count_by_conds(
                        ['ep_id' => $val['ep_id'], 'et_id' => $et_detail['et_id']]
                    );

                    if (!$is_exist) {

                        $answer_detail_extend[] = [
                            'ep_id' => $val['ep_id'],
                            'et_id' => $et_detail['et_id'],
                            'et_option' => $val['et_option'],
                            'et_detail' => $val['et_detail'],
                        ];
                    }
                }
            }

            // 待插入数组去重
            $answer_detail_extend = $this->remove_duplicate($answer_detail_extend);
            // answer_detail_extend表数据入库
            if (!empty($answer_detail_extend)) {

                $this->detail_extend_s->insert_all($answer_detail_extend);
            }

            echo $page . "\r\n";

            $page++;

//            sleep(2);
        }
    }

    /**
     * 获取esr_id
     *
     * @param int $ep_id 试卷ID
     * @param int $et_id 题目ID
     *
     * @return int
     */
    private function get_esr_id($ep_id, $et_id)
    {

        $paper = $this->paper_s->get($ep_id);

        $conds = ['ep_id' => $ep_id, 'et_id' => $et_id];
        // 随机抽题
        if ($paper['ep_type'] == PaperService::TOPIC_RANDOM) {

            $snapshot = $this->random_s->get_by_conds($conds);
            $esr_id = $snapshot['er_id'];
        } else {
            // 非随机抽题
            $snapshot = $this->snapshot_s->get_by_conds($conds);
            $esr_id = $snapshot['es_id'];
        }

        return $esr_id;
    }

    /**
     * 根据试卷ID和题目标题从随机试题快照表查询试题
     *
     * @param int $ep_id 试卷ID
     * @param array $et_detail 试题详情
     * @param string $et_option 题目选项序列化
     *
     * @return array
     */
    private function list_by_rand_snapshot_conds($ep_id = 0, $et_detail = [], $et_option = '')
    {

        $result = [];
        $conds = [
            'ep_id' => $ep_id,
            'et_type' => $et_detail['et_type'],
            'title' => $et_detail['title'],
        ];

        $topic_list = $this->random_s->list_by_conds($conds);
        // 只有一个
        if (count($topic_list) == 1) {

            $result['et_id'] = $topic_list[0]['et_id'];
            $result['esr_id'] = $topic_list[0]['er_id'];

            // 有多个
        } elseif (count($topic_list) > 1) {

            // 组装题目选项数据
            foreach ($topic_list as $key => $topic) {
                $topic_options[$key] = $topic['options'];
            }

            // 过滤重复的选项
            $filter_topic_options = array_unique($topic_options);

            // 有重复的,记录日志
            if (count($topic_options) != count($filter_topic_options)) {

                $paper = $this->paper_s->get($ep_id);
                $file = APP_PATH . 'Data/topic_repeat.log';
                $log = [
                    'ep_id' => $ep_id,
                    'ep_name' => $paper['ep_name'],
                    'title' => $et_detail['title'],
                    'topic_list' => $topic_list,
                ];
                file_put_contents($file, var_export($log,
                        true) . ',' . PHP_EOL . '//=======================================================================' . PHP_EOL,
                    FILE_APPEND);
            }

            // 有重复的,就过滤掉重复的,et_id在重复的题目中选其中一个
            foreach ($filter_topic_options as $k => $v) {

                if ($v == $et_option) {

                    $result['et_id'] = $topic_list[$k]['et_id'];
                    $result['esr_id'] = $topic_list[$k]['er_id'];
                }
            }
        } else {
            // 题目不存在
            $paper = $this->paper_s->get($ep_id);

            if (!empty($paper['bank_data'])) {
                // 根据eb_id、et_type、title、answer取题目信息
                $eb_ids = $paper['bank_data'];
                $et_title = $et_detail['title'];
                $et_type = $et_detail['et_type'];
                $answer = $et_detail['answer'];

                // 实例化think下的model类
                $com_model = new \Think\Model();
                // 根据eb_id、et_type、title、answer获取题目列表
                $sql = "SELECT * FROM `oa_exam_topic` WHERE eb_id IN({$eb_ids}) AND et_type={$et_type} AND title='{$et_title}' AND answer='{$answer}'";
                $topic_list = $com_model->query($sql);

                $topic = [];
                foreach ($topic_list as $value) {
                    // 题目选项
                    if ($et_option == $value['options']) {

                        $topic = $value;
                        break;
                    }
                }

                if (empty($topic)) {

                    $file = APP_PATH . 'Data/delete_topic.log';
                    $log = [
                        'ep_id' => $ep_id,
                        'ep_name' => $paper['ep_name'],
                        'title' => $et_detail['title'],
                        'banks' => $paper['bank_data'],
                    ];
                    file_put_contents($file, var_export($log,
                            true) . ',' . PHP_EOL . '//=======================================================================' . PHP_EOL,
                        FILE_APPEND);
                } else {
                    // 根据 ep_id,et_id 查询answer_detail_extend表是否存在此信息
                    $is_exist = $this->detail_extend_s->count_by_conds(
                        ['ep_id' => $ep_id, 'et_id' => $topic['et_id']]
                    );

                    if (!$is_exist) {
                        // 组装answer_detail_extend表数据
                        $detail_extend_data = [
                            'ep_id' => $ep_id, // 试卷id
                            'et_id' => $topic['et_id'], // 题目id
                            'et_option' => $et_option, // 题目选项序列化
                            'et_detail' => serialize($et_detail) // 题目详情序列化
                        ];

                        // 扩展表新增数据
                        $this->detail_extend_s->insert($detail_extend_data);
                    }
                }
            }
        }

        return $result;
    }

    /**
     * 抽取根据规则格式
     *
     * @param $data array 取出题库所有题
     * @param $rule string 抽取规则
     *
     * @return array
     */
    private function format_topic_data($data = [], $rule = '')
    {

        $bank_topic_data = unserialize($rule);
        // 如果没有抽到题返回空
        if (empty($data)) {
            return [];
        }
        $list = [];
        // 数据格式
        foreach ($data as $val) {
            foreach ($bank_topic_data as $v) {
                // 抽到的符合条件的题存入题库
                if ($v['eb_id'] == $val['eb_id']) {
                    // 判断每个题库要求的题多少过滤
                    if ($v['single_count'] > 0 || $v['multiple_count'] || $v['judgment_count'] || $v['question_count'] || $v['voice_count']) {
                        $list[] = $val;
                    }
                }
            }
        }

        return $list;
    }
}