DeleteDataController.class.php
4.23 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
<?php
/**
* 删除状态为已删除的和初始化的试卷数据
* User: yingcai
* Date: 2017/11/7
* Time: 下午4:45
*/
namespace Frontend\Controller\Temp;
use Common\Service\PaperService;
use Think\Exception;
class DeleteDataController extends AbstractController
{
protected $_require_login = false;
/** @var PaperService 实例化试卷表对象 */
protected $paper_service;
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
$this->paper_service = new PaperService();
return true;
}
public function Index()
{
set_time_limit(0);
// 实例化think下的model类
$com_model = new \Think\Model();
// 初始化状态过期时间
$deadline = MILLI_TIME - 86400*1000;
// 从paper表获取已删除的或者试卷状态为初始化且超过有效期的试卷ID
$sql = "SELECT ep_id FROM `oa_exam_paper` WHERE `status` = 3 OR (`exam_status` = 0 AND `created` < {$deadline})";
$list = $com_model->query($sql);
// 需要被删除的时间ID集合
$ep_ids = array_column($list, 'ep_id');
$ep_ids_str = implode(',', $ep_ids);
// 查询被删除的题库
$bank_sql = "SELECT eb_id FROM `oa_exam_bank` WHERE `status` = 3";
$bank_list = $com_model->query($bank_sql);
$eb_ids = array_column($bank_list, 'eb_id');
$eb_ids_str = implode(',', $eb_ids);
try {
$this->paper_service->start_trans();
if (!empty($ep_ids_str)) {
// 删除答卷
$del_answer_sql = "DELETE FROM `oa_exam_answer` WHERE `ep_id` IN ({$ep_ids_str})";
$com_model->execute($del_answer_sql);
// 删除答卷详情
$del_answer_detail_sql = "DELETE FROM `oa_exam_answer_detail` WHERE `ep_id` IN ({$ep_ids_str})";
$com_model->execute($del_answer_detail_sql);
// 删除答卷详情扩展
$del_answer_detail_extend_sql = "DELETE FROM `oa_exam_answer_detail_extend` WHERE `ep_id` IN ({$ep_ids_str})";
$com_model->execute($del_answer_detail_extend_sql);
// 删除试卷快照(非随机抽题)
$del_snapshot_sql = "DELETE FROM `oa_exam_snapshot` WHERE `ep_id` IN ({$ep_ids_str})";
$com_model->execute($del_snapshot_sql);
// 删除随机试卷快照
$del_random_snapshot_sql = "DELETE FROM `oa_exam_random_snapshot` WHERE `ep_id` IN ({$ep_ids_str})";
$com_model->execute($del_random_snapshot_sql);
// 删除试卷临时备选题目
$del_paper_temp_sql = "DELETE FROM `oa_exam_paper_temp` WHERE `ep_id` IN ({$ep_ids_str})";
$com_model->execute($del_paper_temp_sql);
// 删除随机答卷临时表数据
$del_answer_temp = "DELETE FROM `oa_exam_answer_temp` WHERE `ep_id` IN ({$ep_ids_str})";
$com_model->execute($del_answer_temp);
// 删除随机答卷详情临时表数据
$del_answer_detail_temp = "DELETE FROM `oa_exam_answer_detail_temp` WHERE `ep_id` IN ({$ep_ids_str})";
$com_model->execute($del_answer_detail_temp);
// 删除试题统计
$del_statistics_sql = "DELETE FROM `oa_exam_statistics` WHERE `ep_id` IN ({$ep_ids_str})";
$com_model->execute($del_statistics_sql);
// 删除试卷
$del_paper_sql = "DELETE FROM `oa_exam_paper` WHERE `ep_id` IN ({$ep_ids_str})";
$com_model->execute($del_paper_sql);
}
if (!empty($eb_ids_str)) {
// 删除题库
$del_bank_sql = "DELETE FROM `oa_exam_bank` WHERE `eb_id` IN ({$eb_ids_str})";
$com_model->execute($del_bank_sql);
// 删除题目
$del_topic_sql = "DELETE FROM `oa_exam_topic` WHERE `eb_id` IN ({$eb_ids_str})";
$com_model->execute($del_topic_sql);
}
$this->paper_service->commit();
} catch (Exception $e) {
$this->paper_service->rollback();
}
}
}