UpdateNewDataController.class.php
3.6 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
<?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++;
}
}
}