PaperTempService.class.php
4.26 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
<?php
/**
* 试卷题目对应关系表
* @author: houyingcai
* @email: 594609175@qq.com
* @date : 2017-05-19 17:50:41
* @version $Id$
*/
namespace Common\Service;
use Common\Model\BankModel;
use Common\Model\PaperTempModel;
class PaperTempService extends AbstractService
{
/** @var BankModel */
protected $_d_bank;
/** @var PaperTempModel */
protected $_d_temp;
// 构造方法
public function __construct()
{
$this->_d = new PaperTempModel();
$this->_d_bank = new BankModel();
$this->_d_temp = new PaperTempModel();
parent::__construct();
}
/**
* 获取备选题列表
*
* @author:daijun
* @param array $params
*
* @return array|bool
*/
public function get_temp_list($params = [])
{
// 默认值
$page = !empty($params['page']) ? intval($params['page']) : 1;
$limit = !empty($params['limit']) ? intval($params['limit']) : self::DEFAULT_LIMIT_ADMIN;
// 分页
list($start, $limit) = page_limit($page, $limit);
// 排序
$order_option = ['a.order_num' => 'ASC'];
// 组装查询条件
$where['a.ep_id'] = $params['ep_id'];
if (!empty($params['title'])) {
$where['b.title like ?'] = '%' . trim($params['title']) . '%';
}
if (!empty($params['et_type'])) {
$where['b.et_type'] = intval($params['et_type']);
}
if (!empty($params['eb_name'])) {
// 去题库表查询题库ID集合
$eb_list = $this->_d_bank->list_by_conds(['eb_name like ?' => '%' . trim($params['eb_name']) . '%']);
// 查询题库ID 初始化
$where['b.eb_id'] = 0;
// 如果符合条件的题库存在
if (!empty($eb_list)) {
$eb_ids = array_column($eb_list, 'eb_id');
$where['b.eb_id'] = $eb_ids;
}
}
// 查询所有的题库信息
$eb_list = $this->_d_bank->list_all();
$eb_data = array_column($eb_list, null, 'eb_id');
// 联合查询总数
$total = $this->_d->count_by_where($where);
$topic_list = [];
if ($total) {
// 分页参数
$page_option = [$start, $limit];
// 查询字段
$fields = 'a.epd_id,a.et_id,b.title,b.eb_id,b.et_type,a.score';
// 关联查询需要的相关信息
$topic_list = $this->_d->list_by_where($where, $page_option, $order_option, $fields);
}
// 循环格式化
foreach ($topic_list as $k => $v) {
$topic_list[$k]['epd_id'] = intval($v['epd_id']);
$topic_list[$k]['et_id'] = intval($v['et_id']);
$topic_list[$k]['eb_id'] = intval($v['eb_id']);
$topic_list[$k]['title'] = $v['title'];
$topic_list[$k]['eb_name'] = $eb_data[$v['eb_id']]['eb_name'];
$topic_list[$k]['et_type'] = intval($v['et_type']);
$topic_list[$k]['score'] = intval($v['score']);
}
// 组装返回数据
return [
'total' => intval($total),
'limit' => intval($limit),
'page' => intval($page),
'list' => $topic_list
];
}
/**
* 编辑试卷题目分数
*
* @param array $list 题目分数编辑列表
* + int epd_id 主键id
* + int score 题目分数
*
* @return bool
*/
public function save_score($list = [])
{
try {
// 开始事务
$this->start_trans();
// 保存试卷题目分数
foreach ($list as $value) {
$this->_d->update($value['epd_id'], ['score' => $value['score']]);
}
// 提交事务
$this->commit();
} catch (\Think\Exception $e) {
\Think\Log::record($e);
// 事务回滚
$this->_set_error($e->getMessage(), $e->getCode());
$this->rollback();
return false;
} catch (\Exception $e) {
\Think\Log::record($e);
$this->_set_error($e->getMessage(), $e->getCode());
// 事务回滚
$this->rollback();
return false;
}
return true;
}
}