UpdateOldDataController.class.php 13.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
<?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;
    }
}