Entrust.php 10 KB

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