model = new \app\admin\model\Entrust; $this->view->assign("statusList", $this->model->getStatusList()); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 * * @return string|Json * @throws \think\Exception * @throws DbException */ public function index() { //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if (false === $this->request->isAjax()) { return $this->view->fetch(); } //如果发送的来源是 Selectpage,则转发到 Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } [$where, $sort, $order, $offset, $limit] = $this->buildparams(); $user_id = Session::get('admin')['id']; if ($user_id == 1){//超级管理员 $list = $this->model ->where($where) ->order($sort, $order) ->paginate($limit); }else{ $userinfo = Db::name('admin')->where('id',$user_id)->find(); $pidList = Db::name('company')->where('pid',$userinfo['company'])->select(); $map = []; if (!empty($pidList)){//总公司 $pid = []; foreach ($pidList as $key=>$value){ $pid[$key] = $value['id']; } $map['work_unit'] = array('in',$pid); }else{//分公司 $map['work_unit'] = $userinfo['company']; } $list = $this->model ->where($where) ->where($map) ->order($sort, $order) ->paginate($limit); } $result = ['total' => $list->total(), 'rows' => $list->items()]; return json($result); } /** * 添加 * * @return string * @throws \think\Exception */ public function add() { if (false === $this->request->isPost()) { return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $params['name'] = preg_replace('/\s+/','',$params['name']);//去掉所有空格 $params['user_id'] = Session::get('admin')['id']; $userinfo = Db::name('admin')->where('id',$params['user_id'])->find(); $params['user_name'] = $userinfo['username']; $params['work_unit'] = $userinfo['company']; $params['work_name'] = Db::name('company')->where('id',$userinfo['company'])->value('name'); $is_exit = false;//默认样品编号唯一 if ($params['is_two'] == 1){//双样 for ($i=0;$i<2;$i++){ $data[$i] = $params; $data[$i]['sample_no'] = $params['sample_no'].'-'.($i+1); $is_exit = Db::name('entrust')->where('sample_no',$data[$i]['sample_no'])->find(); if ($is_exit){ $is_exit = true; } } }else{//单样 $data[0] = $params; $is_exit = Db::name('entrust')->where('sample_no',$params['sample_no'])->find(); if ($is_exit){ $is_exit = true; } } if ($is_exit === true){ $this->error('样品编号已存在,请关闭此页面后重新打开!'); } $result = false; Db::startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; $this->model->validateFailException()->validate($validate); } $result = $this->model->saveAll($data); Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result === false) { $this->error(__('No rows were inserted')); } $this->success(); } /** * 编辑 * * @param $ids * @return string * @throws DbException * @throws \think\Exception */ public function edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } if (false === $this->request->isPost()) { $company = ['美浓','翌星','亚新','涂料','其他']; $this->view->assign('row', $row); $this->view->assign('company', $company); return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $params = $this->preExcludeFields($params); $result = false; Db::startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; $row->validateFailException()->validate($validate); } $result = $row->allowField(true)->save($params); Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if (false === $result) { $this->error(__('No rows were updated')); } $this->success(); } //提交到检测 public function submit(){ if (false === $this->request->isPost()) { $id = input('id'); $judgeData = Db::name('item_judge')->column('id,name'); $this->view->assign('judgeData', $judgeData); $this->view->assign('id', $id); return $this->view->fetch(); } $id = input('id');//委托单id $temp_id = input('temp');//判定标准id if (empty($id) || empty($temp_id)){ $this->error('参数错误'); } /*** * 提交后,更新此数据的 判定标准、委托状态 * 根据数据文件名称绑定检测数据 * 生成检测结果 */ $entrust = Db::name('entrust')->where('id',$id)->find(); /*** * 无法确认是哪台机器(GC、GCMS)检测 * 1、先去找gcms表 没有查到数据 再找gc表 */ $data_txt = $entrust['sample_no'].'MS.D'; //如果是GCMS机台,数据文件为委托编号+MS.D $gather_gcms = Db::name('gather_txt_gcms')->where('data_txt_name',$data_txt)->order('id desc')->find(); if (empty($gather_gcms)){ $data_txt_gc = $entrust['sample_no'].'.D'; $gather_gc = Db::name('gather_txt_gc')->where('data_txt_name',$data_txt_gc)->order('id desc')->find(); if (empty($gather_gc)){ $this->error('未获取到检测数据'); } $gather = $gather_gc; $gather_tab = 'gather_txt_check_gc'; }else{ $gather = $gather_gcms; $gather_tab = 'gather_txt_check_gcms'; } $detail_data = Db::name($gather_tab)->where('pid',$gather['id'])->field('chemical_compound,potency')->select(); $is_qualified = 1;//默认合格,当有一项不合格时,判定此次检测不合格 $dis= 0;//溶剂残留总量 $dis_impurity_data = [];//溶剂杂质总量 $ben_total_data = [];//苯系物总量 $ben = 0; //苯含量 $all_dis_data = []; //所有杂质含量数组 $ethanol = 0; //乙醇含量 $dis_impurity_arr = ['甲醇','丙酮','正丁醇','苯','2-乙氧基乙醇','4-甲基-2-戊酮','甲苯','乙酸正丁酯','乙苯','间对二甲苯','邻-二甲苯','苯乙烯','2-乙氧基乙基乙酸酯','环己酮']; $ben_arr = ['甲苯','乙苯','间对二甲苯','邻-二甲苯']; foreach ($detail_data as $key=>$value){ $all_dis_data[$key] = $value['potency']; if ($value['chemical_compound'] == '乙醇'){//乙醇含量 $ethanol = $value['potency']; } if ($value['chemical_compound'] == '苯'){//苯含量 $ben = $value['potency']; } if (in_array($value['chemical_compound'],$dis_impurity_arr)){//溶剂杂质总量 $dis_impurity_data[$value['chemical_compound']] = $value['potency']; } if (in_array($value['chemical_compound'],$ben_arr)){//苯系物总量 $ben_total_data[$value['chemical_compound']] = $value['potency']; } } $judge = Db::name('item_judge_detail')->where('pid',$temp_id)->select(); $sum_all_dis_data = array_sum($all_dis_data); $dis = $sum_all_dis_data - $ethanol; //残留总量 $dis_impurity = array_sum($dis_impurity_data); //杂质总量 $ben_total = array_sum($ben_total_data); //苯系物重量 foreach ($judge as $k=>$v){ if ($v['params'] == '溶剂残留总量'){ if ( $dis < $v['max']){ $is_qualified = 1; }else{ $is_qualified = 0; } }if ($v['params'] == '溶剂杂质总量' || $v['params'] == '总量'){ if ( $dis_impurity < $v['max']){ $is_qualified = 1; }else{ $is_qualified = 0; } }if ($v['params'] == '溶剂杂质苯系物' || $v['params'] == '苯系物'){ if ( $ben_total < $v['max']){ $is_qualified = 1; }else{ $is_qualified = 0; } }if ($v['params'] == '溶剂杂质苯' || $v['params'] == '苯'){ if ($ben < $v['max']){ $is_qualified = 1; }else{ $is_qualified = 0; } } } $params = [];//检测结果数据 $params['entrust_no'] = $entrust['no']; $params['entrust_id'] = $entrust['id']; $params['name'] = $entrust['name']; $params['bach'] = $entrust['bach']; $params['sample_no'] = $entrust['sample_no']; $params['standard_id'] = $temp_id; $params['standard_name'] = $judge[0]['name']; $params['dis'] = $dis; $params['dis_impurity'] = $dis_impurity; $params['ben_total'] = $ben_total; $params['ben'] = $ben; $params['unit'] = 'mg/m2'; $params['judge'] = $is_qualified; $params['create'] = date('Y-m-d H:i:s'); $params['maker'] = Session::get('admin')['username']; $params['params'] = Db::name('item_judge')->where('id',$temp_id)->value('list_name'); $params['userid'] = Session::get('admin')['id'];; $params['status'] = 0; //更新数据 $entrust_update = []; $entrust_update['standard_id'] =$temp_id; $entrust_update['standard_name'] =$judge[0]['name']; $entrust_update['status'] =2; $entrust_update['gather_id'] =$gather['id']; $entrust_update['gather_tab'] = $gather_tab; //日志记录 $log = []; $log['userid'] = Session::get('admin')['id']; $log['username'] = Session::get('admin')['username']; $log['operate'] = '提交委托单id值为'.$id.'到检测'; $log['e_id'] = $id; $log['create'] = date('Y-m-d H:i:s'); $result = true; Db::startTrans(); try { /*** * 提交后,更新此数据的 判定标准、委托状态 * 根据批次号绑定检测数据 * 生成检测结果 */ $entrust_res = Db::name('entrust')->where('id',$id)->update($entrust_update); $res_check = Db::name('res')->insert($params); $log_res = Db::name('entrust_log')->insert($log); if (!$entrust_res || !$res_check){ $result = false; } Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result === false) { $this->error(__('No rows were inserted')); } $this->success(); } //日志查看 public function log(){ //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); $params = $this->request->param("ids"); if (false === $this->request->isAjax()) { $this->view->assign('id', $params); return $this->view->fetch(); } //如果发送的来源是 Selectpage,则转发到 Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } $id = $this->request->param("ids"); $map = []; if (!empty($params)){ $map['e_id'] = $id; } // print_r($map);die; [$where, $sort, $order, $offset, $limit] = $this->buildparams(); $list = Db::name('entrust_log') ->where($map) ->order($sort, $order) ->paginate($limit); $result = ['total' => $list->total(), 'rows' => $list->items()]; return json($result); } //检测数据查看 public function data(){ $params = input('id'); if (empty($params)){ $this->error('参数错误'); } $gather_id = Db::name('entrust')->where('id',$params)->find(); if (empty($gather_id['gather_id'])){ $this->error('此委托单还未提交检测,暂无检测数据'); } //此处根据实际获取到的采集表的数据 gather_tab 去对应表里查数据 重新写一个gc表格页面 js加代码 if ($gather_id['gather_tab'] == 'gather_txt_check_gcms'){ $gather = Db::name('gather_txt_gcms')->where('id',$gather_id['gather_id'])->find(); $data = Db::name('gather_txt_check_gcms')->where('pid',$gather_id['gather_id'])->select(); $this->view->assign('gather', $gather); $this->view->assign('data', $data); $this->view->assign('id', $params); return $this->view->fetch(); }else if ($gather_id['gather_tab'] == 'gather_txt_check_gc'){ $gather = Db::name('gather_txt_gc')->where('id',$gather_id['gather_id'])->find(); $data = Db::name('gather_txt_check_gc')->where('pid',$gather_id['gather_id'])->select(); $this->view->assign('gather', $gather); $this->view->assign('data', $data); $this->view->assign('id', $params); return $this->fetch('datagc'); } } //检测数据确认 public function dataSure(){ $params = input('id'); if (empty($params)){ $this->error('参数错误'); } $gather_id = Db::name('entrust')->where('id',$params)->find(); if (empty($gather_id['gather_id'])){ $this->error('此委托单还未提交检测,暂无检测数据'); } if ($gather_id['gather_tab'] == 'gather_txt_check_gcms'){ Db::name('gather_txt_gcms')->where('id',$gather_id['gather_id'])->setField('status',1); }else{ Db::name('gather_txt_gc')->where('id',$gather_id['gather_id'])->setField('status',1); } $this->success('确认成功'); } //委托单 public function commissionSheet(){ return $this->view->fetch(); } //生成委托单号 public function getNo(){ $params = input('temp'); $company = input('company'); if (empty($params) || empty($company)){ $this->error('参数错误'); } $num = Db::name('entrust')->where('sample_no','like','%'.$params.'%')->whereTime('create','d')->count(); if ($company == '亚欣'){//亚欣送样从100开始 $num = $num+100; }else{ $num = $num+1; } if ($num < 10){ $num = '0'.$num; } //一位样品代号+8位日期+当前样品代号第几次送样+2位检测方式(01代表vocs) $sample_no = date('Ymd').$num.'01'; return array('code'=>1,'data'=>$sample_no); } }