UpdateNewDataController.class.php 3.6 KB
<?php
/**
 * 试卷表paper新数据-新增answer_detail_extend表数据
 * User: wanghuan
 * Date: 2017/12/5
 */

namespace Frontend\Controller\Temp;

use Common\Service\AnswerDetailService;
use Common\Service\AnswerDetailExtendService;

class UpdateNewDataController extends AbstractController
{

    // 免登录
    protected $_require_login = false;

    /** @var AnswerDetailService */
    protected $answer_detail_s;
    /** @var AnswerDetailExtendService */
    protected $detail_extend_s;

    public function before_action($action = '')
    {

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

            return false;
        }

        $this->answer_detail_s = new AnswerDetailService();
        $this->detail_extend_s = new AnswerDetailExtendService();

        return true;
    }

    public function Index()
    {

        $page = 1;
        $limit = 10000;

        // 查询条件
        $condition['et_id != ?'] = 0;

        $un_insert = 0;
        $insert = 0;

        while ($page > 0) {
            // 分页
            $start = ($page - 1) * $limit;

            // 实例化think下的model类
            $com_model = new \Think\Model();
            // 答卷详情列表(适用于数据量小的情况)
            $sql = "SELECT DISTINCT(CONCAT(`ep_id`, `et_id`)) AS `ep_et_id`,`ead_id`,`ep_id`,`et_id`,`et_detail`,`et_option`,`domain` FROM `oa_exam_answer_detail` WHERE `et_id` != 0 AND `ep_id` NOT IN (SELECT `ep_id` FROM `oa_exam_paper` WHERE `status` = 3) LIMIT {$start},{$limit}";

            // 更新2017/12/7 15:0:0之后新增的数据
            // $sql = "SELECT DISTINCT(CONCAT(`ep_id`, `et_id`)) AS `ep_et_id`,`ead_id`,`ep_id`,`et_id`,`et_detail`,`et_option` FROM `oa_exam_answer_detail` WHERE `created` > 1512630000000 LIMIT {$start},{$limit}";
            $answer_detail_list = $com_model->query($sql);

            if (empty($answer_detail_list)) {
                echo "--【repeat:" . $un_insert . "; add:" . $insert . "】--";
                exit("PAGE:{$page} SUCCESS");
            }

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

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

                if (!$is_exist) {

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

                    $insert++;
                } else {

                    $un_insert++;
                }
            }

            // answer_detail_extend表数据入库
            $old_total = count($answer_detail_extend);
            // 待插入数组去重
            $answer_detail_extend = $this->remove_duplicate($answer_detail_extend);
            $new_total = count($answer_detail_extend);

            $repeat_total = $old_total - $new_total;
            $un_insert = $un_insert + $repeat_total;
            $insert = $insert - $repeat_total;

            if (!empty($answer_detail_extend)) {

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

            echo $page . "\r\n";

            $page++;

        }
    }
}