Tasks.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace app\admin\controller\manage;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Exception;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 质控任务管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Tasks extends Backend
  14. {
  15. /**
  16. * Tasks模型对象
  17. * @var \app\admin\model\manage\Tasks
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\manage\Tasks;
  24. }
  25. protected $noNeedRight = ['start','complete','getnums'];
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. public function start()
  32. {
  33. $id = $this->request->post('id');
  34. $task = $this->model->get($id);
  35. $ins = ['institution_id'=>$task['institution_id']];
  36. $exam = Db::table('exams')->where($ins);
  37. if(!empty($task['exam_class']))
  38. {
  39. $exam = $exam->where('exam_class','in',explode(',',$task['exam_class']));
  40. }
  41. if(!empty($task['datetime_area']))
  42. {
  43. $datetime = explode('-',$task['datetime_area']);
  44. $exam = $exam->whereBetween('exam_datetime',$datetime);
  45. }
  46. if(!empty($task['order_type']))
  47. {
  48. //抽取顺序类型 1随机 2检查时间正序 3检查时间倒序
  49. switch ($task['order_type'])
  50. {
  51. case 1:
  52. $exam = $exam->orderRand();
  53. break;
  54. case 2:
  55. $exam = $exam->order('exam_datetime');
  56. break;
  57. case 3:
  58. $exam = $exam->order('exam_datetime desc');
  59. break;
  60. }
  61. }
  62. $list = $exam->limit($task['num'])->select();
  63. $count = count($list);
  64. $arrs = [];
  65. foreach ($list as $k=>$v)
  66. {
  67. $reportTime = Db::table('report')->where('exam_id',$v['id'])->where('type',1)->value('report_datetime');
  68. $arr = ['exam_id'=>$v['id'],'type'=>$task['type'],'task_id'=>$id,'task_type'=>2,'report_datetime'=>$reportTime];
  69. $arrs[] = $arr;
  70. }
  71. Db::table('qc_task_exams')->insertAll($arrs);
  72. $this->model->where('id',$id)->update(['status'=>1,'exam_num'=>$count]);
  73. $this->success('success');
  74. }
  75. public function complete()
  76. {
  77. $id = $this->request->post('id');
  78. $this->model->where('id',$id)->update(['status'=>2]);
  79. $this->success('success');
  80. }
  81. /**
  82. * 添加
  83. */
  84. public function add()
  85. {
  86. if ($this->request->isPost()) {
  87. $params = $this->request->post("row/a");
  88. if ($params) {
  89. $params = $this->preExcludeFields($params);
  90. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  91. $params[$this->dataLimitField] = $this->auth->id;
  92. }
  93. $result = false;
  94. Db::startTrans();
  95. try {
  96. //是否采用模型验证
  97. if ($this->modelValidate) {
  98. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  99. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  100. $this->model->validateFailException(true)->validate($validate);
  101. }
  102. if(!empty($params['datetime_area'][0] ?? ''))
  103. {
  104. $params['datetime_area'] = implode('-',$params['datetime_area']);
  105. }else{
  106. $params['datetime_area'] = '';
  107. }
  108. if(!empty($params['institution_id']))
  109. {
  110. $params['institution_name'] = Db::table('institution')->where('id',$params['institution_id'])->value('name');
  111. }
  112. if(empty($params['name']))
  113. {
  114. $type = '';
  115. switch ($params['type']){
  116. case 1:
  117. $type = '技术质控';
  118. break;
  119. case 2:
  120. $type = '诊断质控';
  121. break;
  122. case 3:
  123. $type = '技术+诊断质控';
  124. break;
  125. }
  126. $params['name'] = ($params['institution_name'] ?? '').$params['datetime_area'].$type;
  127. }
  128. $result = $this->model->allowField(true)->save($params);
  129. Db::commit();
  130. } catch (ValidateException $e) {
  131. Db::rollback();
  132. $this->error($e->getMessage());
  133. } catch (PDOException $e) {
  134. Db::rollback();
  135. $this->error($e->getMessage());
  136. } catch (Exception $e) {
  137. Db::rollback();
  138. $this->error($e->getMessage());
  139. }
  140. if ($result !== false) {
  141. $this->success();
  142. } else {
  143. $this->error(__('No rows were inserted'));
  144. }
  145. }
  146. $this->error(__('Parameter %s can not be empty', ''));
  147. }
  148. return $this->view->fetch();
  149. }
  150. /**
  151. * 编辑
  152. */
  153. public function edit($ids = null)
  154. {
  155. $row = $this->model->get($ids);
  156. if(!empty($row['datetime_area']))
  157. {
  158. $row['datetime_area'] = explode('-',$row['datetime_area']);
  159. }
  160. if (!$row) {
  161. $this->error(__('No Results were found'));
  162. }
  163. $adminIds = $this->getDataLimitAdminIds();
  164. if (is_array($adminIds)) {
  165. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  166. $this->error(__('You have no permission'));
  167. }
  168. }
  169. if ($this->request->isPost()) {
  170. $params = $this->request->post("row/a");
  171. if ($params) {
  172. $params = $this->preExcludeFields($params);
  173. $result = false;
  174. Db::startTrans();
  175. try {
  176. //是否采用模型验证
  177. if ($this->modelValidate) {
  178. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  179. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  180. $row->validateFailException(true)->validate($validate);
  181. }
  182. if(!empty($params['datetime_area'][0] ?? ''))
  183. {
  184. $params['datetime_area'] = implode('-',$params['datetime_area']);
  185. }else{
  186. $params['datetime_area'] = '';
  187. }
  188. if(!empty($params['institution_id']))
  189. {
  190. $params['institution_name'] = Db::table('institution')->where('id',$params['institution_id'])->value('name');
  191. }
  192. $result = $row->allowField(true)->save($params);
  193. Db::commit();
  194. } catch (ValidateException $e) {
  195. Db::rollback();
  196. $this->error($e->getMessage());
  197. } catch (PDOException $e) {
  198. Db::rollback();
  199. $this->error($e->getMessage());
  200. } catch (Exception $e) {
  201. Db::rollback();
  202. $this->error($e->getMessage());
  203. }
  204. if ($result !== false) {
  205. $this->success();
  206. } else {
  207. $this->error(__('No rows were updated'));
  208. }
  209. }
  210. $this->error(__('Parameter %s can not be empty', ''));
  211. }
  212. $this->view->assign("row", $row);
  213. return $this->view->fetch();
  214. }
  215. public function getNums($ids)
  216. {
  217. $row = $this->model->get($ids);
  218. if($row['exam_num'] !== $row['qc_num'])
  219. {
  220. return false;
  221. }
  222. return true;
  223. }
  224. /**
  225. * 查看
  226. */
  227. public function index()
  228. {
  229. //设置过滤方法
  230. $this->request->filter(['strip_tags']);
  231. if ($this->request->isAjax()) {
  232. //如果发送的来源是Selectpage,则转发到Selectpage
  233. if ($this->request->request('keyField')) {
  234. return $this->selectpage();
  235. }
  236. // 过滤机构
  237. $childInsIds = $this->auth->getMyInsId();
  238. if($childInsIds == false){
  239. $more = false;
  240. } else {
  241. $more = ['institution_id' => ['in', $childInsIds]];
  242. }
  243. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  244. $total = $this->model
  245. ->where($where)
  246. ->where($more)
  247. ->order($sort, $order)
  248. ->count();
  249. $list = $this->model
  250. ->where($where)
  251. ->where($more)
  252. ->order($sort, $order)
  253. ->limit($offset, $limit)
  254. ->select();
  255. $list = collection($list)->toArray();
  256. $result = array("total" => $total, "rows" => $list);
  257. return json($result);
  258. }
  259. return $this->view->fetch();
  260. }
  261. }