Entrust.php 16 KB

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