Entrust.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. return $this->view->fetch();
  40. }
  41. $params = $this->request->post('row/a');
  42. if (empty($params)) {
  43. $this->error(__('Parameter %s can not be empty', ''));
  44. }
  45. $params['name'] = preg_replace('/\s+/','',$params['name']);//去掉所有空格
  46. $params['user_id'] = Session::get('admin')['id'];
  47. $params['user_name'] = Session::get('admin')['username'];
  48. $params = $this->preExcludeFields($params);
  49. if ($params['is_two'] == 1){//双样
  50. for ($i=0;$i<2;$i++){
  51. $data[$i] = $params;
  52. $data[$i]['sample_no'] = $params['sample_no'].'-'.($i+1);
  53. }
  54. }else{//单样
  55. $data = $params;
  56. }
  57. $result = false;
  58. Db::startTrans();
  59. try {
  60. //是否采用模型验证
  61. if ($this->modelValidate) {
  62. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  63. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  64. $this->model->validateFailException()->validate($validate);
  65. }
  66. $result = $this->model->saveAll($data);
  67. Db::commit();
  68. } catch (ValidateException|PDOException|Exception $e) {
  69. Db::rollback();
  70. $this->error($e->getMessage());
  71. }
  72. if ($result === false) {
  73. $this->error(__('No rows were inserted'));
  74. }
  75. $this->success();
  76. }
  77. //提交到检测
  78. public function submit(){
  79. if (false === $this->request->isPost()) {
  80. $id = input('id');
  81. $judgeData = Db::name('item_judge')->column('id,name');
  82. $this->view->assign('judgeData', $judgeData);
  83. $this->view->assign('id', $id);
  84. return $this->view->fetch();
  85. }
  86. $id = input('id');//委托单id
  87. $temp_id = input('temp');//判定标准id
  88. if (empty($id) || empty($temp_id)){
  89. $this->error('参数错误');
  90. }
  91. /***
  92. * 提交后,更新此数据的 判定标准、委托状态
  93. * 根据数据文件名称绑定检测数据
  94. * 生成检测结果
  95. */
  96. $entrust = Db::name('entrust')->where('id',$id)->find();
  97. /***
  98. * 无法确认是哪台机器(GC、GCMS)检测
  99. * 1、先去找gcms表 没有查到数据 再找gc表
  100. */
  101. $data_txt = $entrust['sample_no'].'MS.D'; //如果是GCMS机台,数据文件为委托编号+MS.D
  102. $gather_gcms = Db::name('gather_txt_gcms')->where('data_txt_name',$data_txt)->find();
  103. if (empty($gather_gcms)){
  104. $data_txt_gc = $entrust['sample_no'].'.D';
  105. $gather_gc = Db::name('gather_txt_gc')->where('data_txt_name',$data_txt_gc)->find();
  106. if (empty($gather_gc)){
  107. $this->error('未获取到检测数据');
  108. }
  109. $gather = $gather_gc;
  110. $gather_tab = 'gather_txt_check_gc';
  111. }else{
  112. $gather = $gather_gcms;
  113. $gather_tab = 'gather_txt_check_gcms';
  114. }
  115. $detail_data = Db::name($gather_tab)->where('pid',$gather['id'])->field('chemical_compound,potency')->select();
  116. $is_qualified = 1;//默认合格,当有一项不合格时,判定此次检测不合格
  117. $dis= 0;//溶剂残留总量
  118. $dis_impurity_data = [];//溶剂杂质总量
  119. $ben_total_data = [];//苯系物总量
  120. $ben = 0; //苯含量
  121. $all_dis_data = []; //所有杂质含量数组
  122. $ethanol = 0; //乙醇含量
  123. $dis_impurity_arr = ['甲醇','丙酮','正丁醇','苯','2-乙氧基乙醇','4-甲基-2-戊酮','甲苯','乙酸正丁酯','乙苯','间对二甲苯','邻-二甲苯','苯乙烯','2-乙氧基乙基乙酸酯','环己酮'];
  124. $ben_arr = ['甲苯','乙苯','间对二甲苯','邻-二甲苯'];
  125. foreach ($detail_data as $key=>$value){
  126. $all_dis_data[$key] = $value['potency'];
  127. if ($value['chemical_compound'] == '乙醇'){//乙醇含量
  128. $ethanol = $value['potency'];
  129. }
  130. if ($value['chemical_compound'] == '苯'){//苯含量
  131. $ben = $value['potency'];
  132. }
  133. if (in_array($value['chemical_compound'],$dis_impurity_arr)){//溶剂杂质总量
  134. $dis_impurity_data[$value['chemical_compound']] = $value['potency'];
  135. }
  136. if (in_array($value['chemical_compound'],$ben_arr)){//苯系物总量
  137. $ben_total_data[$value['chemical_compound']] = $value['potency'];
  138. }
  139. }
  140. $judge = Db::name('item_judge_detail')->where('pid',$temp_id)->select();
  141. $sum_all_dis_data = array_sum($all_dis_data);
  142. $dis = $sum_all_dis_data - $ethanol; //残留总量
  143. $dis_impurity = array_sum($dis_impurity_data); //杂质总量
  144. $ben_total = array_sum($ben_total_data); //苯系物重量
  145. foreach ($judge as $k=>$v){
  146. if ($v['params'] == '溶剂残留总量'){
  147. if ( $dis < $v['max']){
  148. $is_qualified = 1;
  149. }else{
  150. $is_qualified = 0;
  151. }
  152. }if ($v['params'] == '溶剂杂质总量' || $v['params'] == '总量'){
  153. if ( $dis_impurity < $v['max']){
  154. $is_qualified = 1;
  155. }else{
  156. $is_qualified = 0;
  157. }
  158. }if ($v['params'] == '溶剂杂质苯系物' || $v['params'] == '苯系物'){
  159. if ( $ben_total < $v['max']){
  160. $is_qualified = 1;
  161. }else{
  162. $is_qualified = 0;
  163. }
  164. }if ($v['params'] == '溶剂杂质苯' || $v['params'] == '苯'){
  165. if ($ben < $v['max']){
  166. $is_qualified = 1;
  167. }else{
  168. $is_qualified = 0;
  169. }
  170. }
  171. }
  172. $params = [];//检测结果数据
  173. $params['entrust_no'] = $entrust['no'];
  174. $params['entrust_id'] = $entrust['id'];
  175. $params['name'] = $entrust['name'];
  176. $params['bach'] = $entrust['bach'];
  177. $params['sample_no'] = $entrust['sample_no'];
  178. $params['standard_id'] = $temp_id;
  179. $params['standard_name'] = $judge[0]['name'];
  180. $params['dis'] = $dis;
  181. $params['dis_impurity'] = $dis_impurity;
  182. $params['ben_total'] = $ben_total;
  183. $params['ben'] = $ben;
  184. $params['unit'] = 'mg/m2';
  185. $params['judge'] = $is_qualified;
  186. $params['create'] = date('Y-m-d H:i:s');
  187. $params['maker'] = Session::get('admin')['username'];
  188. $params['params'] = Db::name('item_judge')->where('id',$temp_id)->value('list_name');
  189. $params['userid'] = Session::get('admin')['id'];;
  190. $params['status'] = 0;
  191. //更新数据
  192. $entrust_update = [];
  193. $entrust_update['standard_id'] =$temp_id;
  194. $entrust_update['standard_name'] =$judge[0]['name'];
  195. $entrust_update['status'] =2;
  196. $entrust_update['gather_id'] =$gather['id'];
  197. $entrust_update['gather_tab'] = $gather_tab;
  198. //日志记录
  199. $log = [];
  200. $log['userid'] = Session::get('admin')['id'];
  201. $log['username'] = Session::get('admin')['username'];
  202. $log['operate'] = '提交委托单id值为'.$id.'到检测';
  203. $log['e_id'] = $id;
  204. $log['create'] = date('Y-m-d H:i:s');
  205. $result = true;
  206. Db::startTrans();
  207. try {
  208. /***
  209. * 提交后,更新此数据的 判定标准、委托状态
  210. * 根据批次号绑定检测数据
  211. * 生成检测结果
  212. */
  213. $entrust_res = Db::name('entrust')->where('id',$id)->update($entrust_update);
  214. $res_check = Db::name('res')->insert($params);
  215. $log_res = Db::name('entrust_log')->insert($log);
  216. if (!$entrust_res || !$res_check){
  217. $result = false;
  218. }
  219. Db::commit();
  220. } catch (ValidateException|PDOException|Exception $e) {
  221. Db::rollback();
  222. $this->error($e->getMessage());
  223. }
  224. if ($result === false) {
  225. $this->error(__('No rows were inserted'));
  226. }
  227. $this->success();
  228. }
  229. //日志查看
  230. public function log(){
  231. //设置过滤方法
  232. $this->request->filter(['strip_tags', 'trim']);
  233. $params = $this->request->param("ids");
  234. if (false === $this->request->isAjax()) {
  235. $this->view->assign('id', $params);
  236. return $this->view->fetch();
  237. }
  238. //如果发送的来源是 Selectpage,则转发到 Selectpage
  239. if ($this->request->request('keyField')) {
  240. return $this->selectpage();
  241. }
  242. $id = $this->request->param("ids");
  243. $map = [];
  244. if (!empty($params)){
  245. $map['e_id'] = $id;
  246. }
  247. // print_r($map);die;
  248. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  249. $list = Db::name('entrust_log')
  250. ->where($map)
  251. ->order($sort, $order)
  252. ->paginate($limit);
  253. $result = ['total' => $list->total(), 'rows' => $list->items()];
  254. return json($result);
  255. }
  256. //检测数据查看
  257. public function data(){
  258. $params = input('id');
  259. if (empty($params)){
  260. $this->error('参数错误');
  261. }
  262. $gather_id = Db::name('entrust')->where('id',$params)->find();
  263. if (empty($gather_id['gather_id'])){
  264. $this->error('此委托单还未提交检测,暂无检测数据');
  265. }
  266. //此处根据实际获取到的采集表的数据 gather_tab 去对应表里查数据 重新写一个gc表格页面 js加代码
  267. if ($gather_id['gather_tab'] == 'gather_txt_check_gcms'){
  268. $gather = Db::name('gather_txt_gcms')->where('id',$gather_id['gather_id'])->find();
  269. $data = Db::name('gather_txt_check_gcms')->where('pid',$gather_id['gather_id'])->select();
  270. $this->view->assign('gather', $gather);
  271. $this->view->assign('data', $data);
  272. return $this->view->fetch();
  273. }else if ($gather_id['gather_tab'] == 'gather_txt_check_gc'){
  274. $gather = Db::name('gather_txt_gc')->where('id',$gather_id['gather_id'])->find();
  275. $data = Db::name('gather_txt_check_gc')->where('pid',$gather_id['gather_id'])->select();
  276. $this->view->assign('gather', $gather);
  277. $this->view->assign('data', $data);
  278. return $this->fetch('datagc');
  279. }
  280. }
  281. //委托单
  282. public function commissionSheet(){
  283. return $this->view->fetch();
  284. }
  285. //生成委托单号
  286. public function getNo(){
  287. $params = input('temp');
  288. $company = input('company');
  289. if (empty($params) || empty($company)){
  290. $this->error('参数错误');
  291. }
  292. $num = Db::name('entrust')->where('sample_no','like','%'.$params.'%')->whereTime('create','d')->count();
  293. if ($company == '亚欣'){//亚欣送样从100开始
  294. $num = $num+100;
  295. }else{
  296. $num = $num+1;
  297. }
  298. if ($num < 10){
  299. $num = '0'.$num;
  300. }
  301. //一位样品代号+8位日期+当前样品代号第几次送样+2位检测方式(01代表vocs)
  302. $sample_no = date('Ymd').$num.'01';
  303. return array('code'=>1,'data'=>$sample_no);
  304. }
  305. }