Entrust.php 19 KB

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