Entrust.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use \think\Session;
  5. use \think\Db;
  6. /**
  7. * 委托管理管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Entrust extends Backend
  12. {
  13. /**
  14. * Entrust模型对象
  15. * @var \app\admin\model\Entrust
  16. */
  17. protected $model = null;
  18. protected $searchFields = 'no,name,bach,sell_bach,company';
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\Entrust;
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 添加
  32. *
  33. * @return string
  34. * @throws \think\Exception
  35. */
  36. public function add()
  37. {
  38. if (false === $this->request->isPost()) {
  39. $num = Db::name('entrust')->whereTime('create', 'd')->count();
  40. $num = $num + 1;
  41. if ($num < 10){
  42. $num = '0'.$num;
  43. }
  44. $sample_no = date('Ymd').$num;
  45. $this->view->assign('sample_no', $sample_no);
  46. return $this->view->fetch();
  47. }
  48. $params = $this->request->post('row/a');
  49. if (empty($params)) {
  50. $this->error(__('Parameter %s can not be empty', ''));
  51. }
  52. $params['name'] = preg_replace('/\s+/','',$params['name']);//去掉所有空格
  53. $params['user_id'] = Session::get('admin')['id'];
  54. $params['user_name'] = Session::get('admin')['username'];
  55. $params = $this->preExcludeFields($params);
  56. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  57. $params[$this->dataLimitField] = $this->auth->id;
  58. }
  59. $result = false;
  60. Db::startTrans();
  61. try {
  62. //是否采用模型验证
  63. if ($this->modelValidate) {
  64. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  65. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  66. $this->model->validateFailException()->validate($validate);
  67. }
  68. $result = $this->model->allowField(true)->save($params);
  69. Db::commit();
  70. } catch (ValidateException|PDOException|Exception $e) {
  71. Db::rollback();
  72. $this->error($e->getMessage());
  73. }
  74. if ($result === false) {
  75. $this->error(__('No rows were inserted'));
  76. }
  77. $this->success();
  78. }
  79. //提交到检测
  80. public function submit(){
  81. if (false === $this->request->isPost()) {
  82. $id = input('id');
  83. $judgeData = Db::name('item_judge')->column('id,name');
  84. $this->view->assign('judgeData', $judgeData);
  85. $this->view->assign('id', $id);
  86. return $this->view->fetch();
  87. }
  88. $id = input('id');//委托单id
  89. $temp_id = input('temp');//判定标准id
  90. if (empty($id) || empty($temp_id)){
  91. $this->error('参数错误');
  92. }
  93. /***
  94. * 提交后,更新此数据的 判定标准、委托状态
  95. * 根据批次号绑定检测数据
  96. * 生成检测结果
  97. */
  98. $entrust = Db::name('entrust')->where('id',$id)->find();
  99. /***
  100. * 无法确认是哪台机器(GC、GCMS)检测
  101. * 1、先去找gc表 没有查到数据 再找gcms表
  102. */
  103. $gather = Db::name('gather_txt_gcms')->where('bach',$entrust['bach'])->find();
  104. if (empty($gather)){
  105. $this->error('未获取到检测数据');
  106. }
  107. $detail_data = Db::name('gather_txt_check_gcms')->where('pid',$gather['id'])->field('chemical_compound,potency')->select();
  108. $gather_tab = 'gather_txt_gcms';//默认gcms表,后续根据实际查到的数据 更改
  109. $is_qualified = 1;//默认合格,当有一项不合格时,判定此次检测不合格
  110. $dis= 0;//溶剂残留总量
  111. $dis_impurity_data = [];//溶剂杂质总量
  112. $ben_total_data = [];//苯系物总量
  113. $ben = 0; //苯含量
  114. $all_dis_data = []; //所有杂质含量数组
  115. $ethanol = 0; //乙醇含量
  116. $dis_impurity_arr = ['甲醇','丙酮','正丁醇','苯','2-乙氧基乙醇','4-甲基-2-戊酮','甲苯','乙酸正丁酯','乙苯','间对二甲苯','邻-二甲苯','苯乙烯','2-乙氧基乙基乙酸酯','环己酮'];
  117. $ben_arr = ['甲苯','乙苯','间对二甲苯','邻-二甲苯'];
  118. foreach ($detail_data as $key=>$value){
  119. $all_dis_data[$key] = $value['potency'];
  120. if ($value['chemical_compound'] == '乙醇'){//乙醇含量
  121. $ethanol = $value['potency'];
  122. }
  123. if ($value['chemical_compound'] == '苯'){//苯含量
  124. $ben = $value['potency'];
  125. }
  126. if (in_array($value['chemical_compound'],$dis_impurity_arr)){//溶剂杂质总量
  127. $dis_impurity_data[$value['chemical_compound']] = $value['potency'];
  128. }
  129. if (in_array($value['chemical_compound'],$ben_arr)){//苯系物总量
  130. $ben_total_data[$value['chemical_compound']] = $value['potency'];
  131. }
  132. }
  133. $judge = Db::name('item_judge_detail')->where('pid',$temp_id)->select();
  134. $sum_all_dis_data = array_sum($all_dis_data);
  135. $dis = $sum_all_dis_data - $ethanol; //残留总量
  136. $dis_impurity = array_sum($dis_impurity_data); //杂质总量
  137. $ben_total = array_sum($ben_total_data); //苯系物重量
  138. foreach ($judge as $k=>$v){
  139. // $v['min'] = $v['min'] * 10000;
  140. // $v['max'] = $v['max'] * 10000;
  141. if ($v['params'] == '溶剂残留总量'){
  142. if ( $dis < $v['max']){
  143. $is_qualified = 1;
  144. }else{
  145. $is_qualified = 0;
  146. }
  147. }if ($v['params'] == '溶剂杂质总量' || $v['params'] == '总量'){
  148. if ( $dis_impurity < $v['max']){
  149. $is_qualified = 1;
  150. }else{
  151. $is_qualified = 0;
  152. }
  153. }if ($v['params'] == '溶剂杂质苯系物' || $v['params'] == '苯系物'){
  154. // $ben_total_ten_thousand = $ben_total * 10000;
  155. if ( $ben_total < $v['max']){
  156. $is_qualified = 1;
  157. }else{
  158. $is_qualified = 0;
  159. }
  160. }if ($v['params'] == '溶剂杂质苯' || $v['params'] == '苯'){
  161. if ($ben < $v['max']){
  162. $is_qualified = 1;
  163. }else{
  164. $is_qualified = 0;
  165. }
  166. }
  167. }
  168. $params = [];//检测结果数据
  169. $params['entrust_no'] = $entrust['no'];
  170. $params['entrust_id'] = $entrust['id'];
  171. $params['name'] = $entrust['name'];
  172. $params['bach'] = $entrust['bach'];
  173. $params['sample_no'] = $entrust['sample_no'];
  174. $params['standard_id'] = $temp_id;
  175. $params['standard_name'] = $judge[0]['name'];
  176. $params['dis'] = $dis;
  177. $params['dis_impurity'] = $dis_impurity;
  178. $params['ben_total'] = $ben_total;
  179. $params['ben'] = $ben;
  180. $params['unit'] = 'mg/m2';
  181. $params['judge'] = $is_qualified;
  182. $params['create'] = date('Y-m-d H:i:s');
  183. $params['maker'] = Session::get('admin')['username'];
  184. $params['params'] = Db::name('item_judge')->where('id',$temp_id)->value('list_name');
  185. $params['userid'] = Session::get('admin')['id'];;
  186. $params['status'] = 0;
  187. //更新数据
  188. $entrust_update = [];
  189. $entrust_update['standard_id'] =$temp_id;
  190. $entrust_update['standard_name'] =$judge[0]['name'];
  191. $entrust_update['status'] =2;
  192. $entrust_update['gather_id'] =$gather['id'];
  193. $entrust_update['gather_tab'] = $gather_tab;
  194. //日志记录
  195. $log = [];
  196. $log['userid'] = Session::get('admin')['id'];
  197. $log['username'] = Session::get('admin')['username'];
  198. $log['operate'] = '提交委托单id值为'.$id.'到检测';
  199. $log['e_id'] = $id;
  200. $log['create'] = date('Y-m-d H:i:s');
  201. $result = true;
  202. Db::startTrans();
  203. try {
  204. /***
  205. * 提交后,更新此数据的 判定标准、委托状态
  206. * 根据批次号绑定检测数据
  207. * 生成检测结果
  208. */
  209. $entrust_res = Db::name('entrust')->where('id',$id)->update($entrust_update);
  210. $res_check = Db::name('res_check')->insert($params);
  211. $log_res = Db::name('entrust_log')->insert($log);
  212. if (!$entrust_res || !$res_check){
  213. $result = false;
  214. }
  215. Db::commit();
  216. } catch (ValidateException|PDOException|Exception $e) {
  217. Db::rollback();
  218. $this->error($e->getMessage());
  219. }
  220. if ($result === false) {
  221. $this->error(__('No rows were inserted'));
  222. }
  223. $this->success();
  224. }
  225. //日志查看
  226. public function log(){
  227. //设置过滤方法
  228. $this->request->filter(['strip_tags', 'trim']);
  229. $params = $this->request->param("ids");
  230. if (false === $this->request->isAjax()) {
  231. $this->view->assign('id', $params);
  232. return $this->view->fetch();
  233. }
  234. //如果发送的来源是 Selectpage,则转发到 Selectpage
  235. if ($this->request->request('keyField')) {
  236. return $this->selectpage();
  237. }
  238. $id = $this->request->param("ids");
  239. $map = [];
  240. if (!empty($params)){
  241. $map['e_id'] = $id;
  242. }
  243. // print_r($map);die;
  244. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  245. $list = Db::name('entrust_log')
  246. ->where($map)
  247. ->order($sort, $order)
  248. ->paginate($limit);
  249. $result = ['total' => $list->total(), 'rows' => $list->items()];
  250. return json($result);
  251. }
  252. //检测数据查看
  253. public function data(){
  254. $params = input('id');
  255. if (empty($params)){
  256. $this->error('参数错误');
  257. }
  258. $gather_id = Db::name('entrust')->where('id',$params)->find();
  259. if (empty($gather_id['gather_id'])){
  260. $this->error('此委托单还未提交检测,暂无检测数据','entrust/index');
  261. }
  262. //此处根据实际获取到的采集表的数据 gather_tab 去对应表里查数据 重新写一个gc表格页面 js加代码
  263. if ($gather_id['gather_tab'] == 'gather_txt_gcms'){
  264. $gather = Db::name('gather_txt_gcms')->where('id',$gather_id['gather_id'])->find();
  265. $data = Db::name('gather_txt_check_gcms')->where('pid',$gather_id['gather_id'])->select();
  266. $this->view->assign('gather', $gather);
  267. $this->view->assign('data', $data);
  268. return $this->view->fetch();
  269. }else{
  270. $gather = Db::name('gather_txt')->where('id',$gather_id['gather_id'])->find();
  271. $data = Db::name('gather_txt_check')->where('pid',$gather_id['gather_id'])->select();
  272. $this->view->assign('gather', $gather);
  273. $this->view->assign('data', $data);
  274. return $this->view->fetch('data_gc.html');//此处页面没写
  275. }
  276. }
  277. //委托单
  278. public function commissionSheet(){
  279. return $this->view->fetch();
  280. }
  281. }