Serial.php
7.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
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
<?php
namespace app\school\controller\project_approval;
use app\common\controller\DoubleBase;
use app\school\controller\project_common\ProjectData;
use app\school\model\ManystoreShop;
use app\school\model\project_common\ProjectRegister;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
class Serial extends DoubleBase
{
protected $containerId = null;
protected $model = null;
// 评审阶段(0:立项评审;1:中期检查;2:项目结题)
protected $reviewStage = 0;
protected $shopIdAutoCondition = false;
// 项目级别列表
protected $levels = [
'1' => '院级',
'2' => '校级',
'3' => '省级',
'4' => '国家级'
];
// 引入获取项目数据的traits
use ProjectData;
public function _initialize()
{
parent::_initialize();
$this->containerId = $_COOKIE['container_id'];
$this->model = new ProjectRegister();
}
public function index()
{
if ($this->request->isAjax())
{
$map = [];
$map['a.review_stage'] = ['IN', '1,2'];
if ($this->auth->rule_type == 1){
//所属学院或部门
$map['a.project_college'] = $this->auth->college_id;
}
$result = $this->approvalProjectData($map);
return json($result);
}
$this->assignconfig('project_level', $this->levels);
$this->assign('levels', $this->levels);
return $this->view->fetch();
}
/**
* 编辑
*/
public function edit($ids = null)
{
$row = $this->model->where(array('id'=>$ids,'school_id'=>SCHOOL_ID))->find();
$domain = strtoupper(ManystoreShop::get($this->auth->school_id)['subdomain_name']);
if (!$row) {
$this->error(__('No Results were found'));
}
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$params = $this->preExcludeFields($params);
if($domain == 'BTMC') {
$params['project_no'] = $domain.'DC-2021'.$params['project_no'];
}
if($this->model->where(['school_id' => SCHOOL_ID,'project_no' => $params['project_no']])->where('id','neq',$params['id'])->find()) {
$this->error(__('项目编号已存在'));
}
$result = false;
Db::startTrans();
try {
$result = $row->allowField(true)->save($params);
Db::commit();
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success();
} else {
$this->error(__('No rows were updated'));
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
if($domain == 'BTMC') {
$newProjectNo = substr($row['project_no'],11);
$row['project_no'] = $newProjectNo;
}
$this->view->assign("row", $row);
$this->assignconfig("domian", $domain);
$this->assign('project_levels', $this->levels);
return $this->view->fetch();
}
/**
* 项目编号
*/
public function serial()
{
if ($this->request->isAjax()) {
$ids = explode(',', $this->request->param('ids'));
$projectNos = [];
// 已经使用过的项目编号
$savedProjectNos = $this->model->column('project_no');
// 与id数量对应的唯一编号数组
$uniqueProjectNos = $this->getNoRepeatProjectNos(count($ids), $savedProjectNos);
foreach ($ids as $k => $id) {
$projectNos[] = [
'id' => $id,
'project_no' => $uniqueProjectNos[$k]
];
}
Db::startTrans();
try {
$this->model->allowField(true)->saveAll($projectNos);
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$errMsg = $e->getMessage();
// 数据重复
if (strpos($errMsg, 'Duplicate entry')) {
if (strpos($errMsg, 'project_no')) {
$errMsg = '项目编号重复';
}
}
$this->error($errMsg);
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success();
}
}
// public function serial()
// {
// if ($this->request->isAjax()) {
// $ids = explode(',', $this->request->param('ids'));
// $projectNos = [];
// // 已经使用过的项目编号
// $savedProjectNos = $this->model->column('project_no');
// // 与id数量对应的唯一编号数组
// $uniqueProjectNos = $this->getNoRepeatProjectNos(count($ids), $savedProjectNos);
//
// foreach ($ids as $k => $id) {
// $projectNos[] = [
// 'id' => $id,
// 'project_no' => $uniqueProjectNos[$k]
// ];
// }
// Db::startTrans();
// try {
// $this->model->allowField(true)->saveAll($projectNos);
// Db::commit();
// } catch (PDOException $e) {
// Db::rollback();
// $errMsg = $e->getMessage();
// // 数据重复
// if (strpos($errMsg, 'Duplicate entry')) {
// if (strpos($errMsg, 'project_no')) {
// $errMsg = '项目编号重复';
// }
// }
// $this->error($errMsg);
// } catch (Exception $e) {
// Db::rollback();
// $this->error($e->getMessage());
// }
// $this->success();
// }
// }
/**
* 获取指定个数的编号(与数据库中项目编号不重复)
* @param $len int 需要的编号个数
* @param $savedProjectNos array 已经存在的编号
* @return array
*/
private function getNoRepeatProjectNos($len, $savedProjectNos) {
$result = [];
while ($len--) {
$result[] = $this->getProjectNo();
}
$repeats = array_intersect($result, $savedProjectNos);
// 存在重复编号
if (0 < count($repeats)) {
foreach ($repeats as $k => $v) {
$result[$k] = $this->getNoRepeatNo($savedProjectNos);
}
}
return $result;
}
/**
* 递归获取唯一的项目编号
* @param $savedProjectNos array 已经存在的编号
* @return string
*/
private function getNoRepeatNo($savedProjectNos)
{
$res = $this->getProjectNo();
if (!in_array($res, $savedProjectNos)) {
return $res;
}
$this->getNoRepeatNo($savedProjectNos);
}
/**
* 生成项目编号,格式(WL + 年月日时分秒 + 微秒(6位))
* @return string
*/
private function getProjectNo()
{
return 'WL' . date('YmdHis') . $this->getMicrotime();
}
/**
* 获取当前时间的微秒数(6位)
* @return mixed|string
*/
private function getMicrotime() {
$tempTime = microtime(true);
return explode('.', number_format($tempTime - intval($tempTime), 6))[1];
}
}