Entrust.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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,sample_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. $map['work_unit'] = $userinfo['company'];
  69. $list = $this->model
  70. ->where($where)
  71. ->where($map)
  72. ->order($sort, $order)
  73. ->paginate($limit);
  74. }
  75. $result = ['total' => $list->total(), 'rows' => $list->items()];
  76. return json($result);
  77. }
  78. /**
  79. * 搜索栏检测项目下拉列表
  80. */
  81. public function companyselect(){
  82. $company = Db::name('item_judge')->where('delete',null)->order('weigh asc')->column('name,name');
  83. if (empty($company)){
  84. return $this->error('没有检测项目数据');
  85. }else{
  86. return json($company);
  87. }
  88. }
  89. /**
  90. * 添加
  91. *
  92. * @return string
  93. * @throws \think\Exception
  94. */
  95. public function add()
  96. {
  97. if (false === $this->request->isPost()) {
  98. $user_id = Session::get('admin')['id'];
  99. $nickname = Session::get('admin')['nickname'];
  100. $company_id = Db::name('admin')->where('id',$user_id)->value('company');
  101. $company = Db::name('company')->where('pid',$company_id)->column('id,name');
  102. if (empty($company)){
  103. $company = Db::name('company')->where('id',$company_id)->column('id,name');
  104. }
  105. // $judgeData = Db::name('item_judge')->whereNull('delete')->order('weigh asc')->column('id,name');
  106. $judgeData = Db::name('item_judge')->whereNull('delete')->order('weigh asc')->select();
  107. $receiveData = Db::name('receive_people')->where('company_id',$company_id)->column('id,name');
  108. $nick_id = Db::name('receive_people')->where('name',$nickname)->value('id');
  109. $this->assign('company',$company);
  110. $this->view->assign('judgeData', $judgeData);
  111. $this->view->assign('nickname', $nick_id);
  112. $this->view->assign('receiveData', $receiveData);
  113. return $this->view->fetch();
  114. }
  115. $params = $this->request->post('row/a');
  116. if (empty($params)) {
  117. $this->error(__('Parameter %s can not be empty', ''));
  118. }
  119. $params['name'] = preg_replace('/\s+/','',$params['name']);//去掉所有空格
  120. $params['user_id'] = Session::get('admin')['id'];
  121. $userinfo = Db::name('admin')->where('id',$params['user_id'])->find();
  122. $params['project'] = implode(',',$params['project']);
  123. $params['report_grant'] = implode(',',$params['report_grant']);
  124. $params['sample_stand'] = implode(',',$params['sample_stand']);
  125. $params['manufacturer'] = preg_replace('/\s+/','',$params['manufacturer']);
  126. $params['standard_id'] = preg_replace('/\s+/','',$params['standard_id']);
  127. $judge = Db::name('item_judge')->where('id',$params['standard_id'])->find();
  128. $params['standard_name'] = $judge['name'];
  129. $params['receive_sample'] = Db::name('receive_people')->where('id',$params['receive_sample'])->value('name');
  130. $params['user_name'] = $userinfo['username'];
  131. $params['work_unit'] = $userinfo['company'];
  132. $params['work_name'] = Db::name('company')->where('id',$userinfo['company'])->value('name');
  133. $params['company'] = Db::name('company')->where('id',$params['company'])->value('name');
  134. $is_exit = false;//默认样品编号唯一
  135. if ($params['is_two'] == 1){//双样
  136. for ($i=0;$i<2;$i++){
  137. $data[$i] = $params;
  138. $data[$i]['sample_no'] = $params['sample_no'].'-'.($i+1);
  139. $is_exit = Db::name('entrust')->where('sample_no',$data[$i]['sample_no'])->find();
  140. if ($is_exit){
  141. $is_exit = true;
  142. }
  143. }
  144. }else{//单样
  145. $data[0] = $params;
  146. $is_exit = Db::name('entrust')->where('sample_no',$params['sample_no'])->find();
  147. if ($is_exit){
  148. $is_exit = true;
  149. }
  150. }
  151. if ($is_exit === true){
  152. $this->error('样品编号已存在,请关闭此页面后重新打开!');
  153. }
  154. $result = false;
  155. Db::startTrans();
  156. try {
  157. //是否采用模型验证
  158. if ($this->modelValidate) {
  159. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  160. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  161. $this->model->validateFailException()->validate($validate);
  162. }
  163. $result = $this->model->saveAll($data);
  164. Db::commit();
  165. } catch (ValidateException|PDOException|Exception $e) {
  166. Db::rollback();
  167. $this->error($e->getMessage());
  168. }
  169. if ($result === false) {
  170. $this->error(__('No rows were inserted'));
  171. }
  172. $this->success();
  173. }
  174. /**
  175. * 编辑
  176. *
  177. * @param $ids
  178. * @return string
  179. * @throws DbException
  180. * @throws \think\Exception
  181. */
  182. public function edit($ids = null)
  183. {
  184. $row = $this->model->get($ids);
  185. if (!$row) {
  186. $this->error(__('No Results were found'));
  187. }
  188. $adminIds = $this->getDataLimitAdminIds();
  189. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  190. $this->error(__('You have no permission'));
  191. }
  192. if (false === $this->request->isPost()) {
  193. $user_id = Session::get('admin')['id'];
  194. $company_id = Db::name('admin')->where('id',$user_id)->value('company');
  195. $pid = Db::name('company')->where('id',$company_id)->value('pid');
  196. $company = Db::name('company')->where('pid',$pid)->column('id,name');
  197. $report_grant = explode(',',$row['report_grant']);
  198. $sample_stand = explode(',',$row['sample_stand']);
  199. $receiveData = Db::name('receive_people')->where('company_id',$company_id)->column('id,name');
  200. $this->view->assign('receiveData', $receiveData);
  201. $this->assign('company',$company);
  202. $this->assign('report_grant',$report_grant);
  203. $this->assign('sample_stand',$sample_stand);
  204. $this->view->assign('row', $row);
  205. //修改判定标准
  206. // $judgeData = Db::name('item_judge')->whereNull('delete')->column('id,name');
  207. $judgeData = Db::name('item_judge')->whereNull('delete')->order('weigh asc')->select();
  208. $this->view->assign('judgeData', $judgeData);
  209. return $this->view->fetch();
  210. }
  211. $params = $this->request->post('row/a');
  212. if (empty($params)) {
  213. $this->error(__('Parameter %s can not be empty', ''));
  214. }
  215. $params['project'] = implode(',',$params['project']);
  216. $params['report_grant'] = implode(',',$params['report_grant']);
  217. $params['sample_stand'] = implode(',',$params['sample_stand']);
  218. $item_judge = Db::name('item_judge')->field('id,name')->where('id',$params['standard_id'])->find();
  219. $params['standard_id'] = $item_judge['id'];
  220. $params['standard_name'] = $item_judge['name'];
  221. $params['receive_sample'] = Db::name('receive_people')->where('id',$params['receive_sample'])->value('name');
  222. $params = $this->preExcludeFields($params);
  223. $result = false;
  224. Db::startTrans();
  225. try {
  226. //是否采用模型验证
  227. if ($this->modelValidate) {
  228. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  229. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  230. $row->validateFailException()->validate($validate);
  231. }
  232. $result = $row->allowField(true)->save($params);
  233. Db::commit();
  234. } catch (ValidateException|PDOException|Exception $e) {
  235. Db::rollback();
  236. $this->error($e->getMessage());
  237. }
  238. if (false === $result) {
  239. $this->error(__('No rows were updated'));
  240. }
  241. $this->success();
  242. }
  243. /**
  244. * 删除
  245. *
  246. * @param $ids
  247. * @return void
  248. * @throws DbException
  249. * @throws DataNotFoundException
  250. * @throws ModelNotFoundException
  251. */
  252. public function del($ids = null)
  253. {
  254. if (false === $this->request->isPost()) {
  255. $this->error(__("Invalid parameters"));
  256. }
  257. $ids = $ids ?: $this->request->post("ids");
  258. if (empty($ids)) {
  259. $this->error(__('Parameter %s can not be empty', 'ids'));
  260. }
  261. $idList = explode(',',$ids);
  262. $canDel = true;
  263. foreach ($idList as $key => $value){
  264. $row = $this->model->get($ids);
  265. if (!empty($row['gather_id'])){
  266. $tab = $row['gather_tab'] == 'gather_txt_check_gc'?'gather_txt_gc':'gather_txt_gcms';
  267. $id = $row['gather_id'];
  268. $res = Db::name($tab)->where('id',$id)->find();
  269. if ($res['status'] == 1){
  270. $canDel = false;
  271. }
  272. }
  273. }
  274. if ($canDel === false){
  275. $this->error('检测数据已确认,无法删除!');
  276. }
  277. $pk = $this->model->getPk();
  278. $adminIds = $this->getDataLimitAdminIds();
  279. if (is_array($adminIds)) {
  280. $this->model->where($this->dataLimitField, 'in', $adminIds);
  281. }
  282. $list = $this->model->where($pk, 'in', $ids)->select();
  283. $count = 0;
  284. Db::startTrans();
  285. try {
  286. foreach ($list as $item) {
  287. $count += $item->delete();
  288. }
  289. Db::commit();
  290. } catch (PDOException|Exception $e) {
  291. Db::rollback();
  292. $this->error($e->getMessage());
  293. }
  294. if ($count) {
  295. $this->success();
  296. }
  297. $this->error(__('No rows were deleted'));
  298. }
  299. //提交到检测
  300. public function submit()
  301. {
  302. $idList = explode(',', input('id'));//委托单id
  303. if (empty($idList)){
  304. $this->error('参数错误');
  305. }
  306. //检测结果数据
  307. $params = [];
  308. //更新数据
  309. $entrust_update = [];
  310. //日志记录
  311. $log = [];
  312. foreach ($idList as $ke => $id) {
  313. $temp_id[$ke] = Db::name('entrust')->where('id', $id)->value('standard_id');
  314. if (empty($temp_id[$ke])) {
  315. $this->error('序号为'.$id . '未获取到判断标准,请先编辑确认标准');
  316. }
  317. /***
  318. * 提交后,更新此数据的 判定标准、委托状态
  319. * 根据数据文件名称绑定检测数据
  320. * 生成检测结果
  321. */
  322. $entrust = Db::name('entrust')->where('id', $id)->find();
  323. /***
  324. * 无法确认是哪台机器(GC、GCMS)检测
  325. * 1、先去找gcms表 没有查到数据 再找gc表
  326. */
  327. $data_txt = $entrust['sample_no'] . 'MS.D'; //如果是GCMS机台,数据文件为委托编号+MS.D或MSR.D
  328. $gather_gcms = Db::name('gather_txt_gcms')->where('data_txt_name', ['=',$data_txt],['=',$entrust['sample_no'].'MSR.D'],'or')->order('id desc')->find();
  329. /**
  330. * 先都查找一边,按时间先后顺序判定哪个结果是最新的
  331. * 1、gc和gcms都没有数据 返回错误->没获取到检测数据
  332. * 2、gc或gcms中有一个有数据,判定这个数据为检测数据
  333. * 3、gc和gcms都有数据,将创建时间转换为时间戳,判断大小,时间戳大的检测数据此委托单的检测数据
  334. */
  335. $data_txt_gc = $entrust['sample_no'] . '.D';
  336. $gather_gc = Db::name('gather_txt_gc')->where('data_txt_name', ['=',$data_txt_gc],['=',$entrust['sample_no'].'R.D'],'or')->order('id desc')->find();
  337. // if (empty($gather_gcms)) {
  338. // $data_txt_gc = $entrust['sample_no'] . '.D';
  339. // $gather_gc = Db::name('gather_txt_gc')->where('data_txt_name', ['=',$data_txt_gc],['=',$entrust['sample_no'].'R.D'],'or')->order('id desc')->find();
  340. // if (empty($gather_gc)) {
  341. // $this->error('序号为'.$id.'的样品未获取到检测数据,请取消选中');
  342. // }
  343. // $gather = $gather_gc;
  344. // $gather_tab = 'gather_txt_check_gc';
  345. // } else {
  346. // $gather = $gather_gcms;
  347. // $gather_tab = 'gather_txt_check_gcms';
  348. // }
  349. $gather_tab = '';
  350. $gather = '';
  351. if (empty($gather_gcms) && empty($gather_gc)) {
  352. $this->error('没获取到检测数据');
  353. }else if(empty($gather_gcms) && !empty($gather_gc)){
  354. $gather = $gather_gc;
  355. $gather_tab = 'gather_txt_check_gc';
  356. }else if(empty($gather_gc) && !empty($gather_gcms)){
  357. $gather = $gather_gcms;
  358. $gather_tab = 'gather_txt_check_gcms';
  359. }else{
  360. $gcms_time = strtotime($gather_gcms['create']);//gcms创建时间转换为时间戳
  361. $gc_time = strtotime($gather_gc['create']); //gc创建时间转换为时间戳
  362. if($gcms_time > $gc_time){
  363. //gcms时间最新
  364. $gather = $gather_gcms;
  365. $gather_tab = 'gather_txt_check_gcms';
  366. }else{
  367. //gc时间最新
  368. $gather = $gather_gc;
  369. $gather_tab = 'gather_txt_check_gc';
  370. }
  371. }
  372. $detail_data = Db::name($gather_tab)->where('pid', $gather['id'])->field('chemical_compound,potency')->select();
  373. $is_qualified = [];//默认合格,当有一项不合格时,判定此次检测不合格
  374. $dis = 0;//溶剂残留总量
  375. $dis_impurity_data = [];//溶剂杂质总量
  376. $ben_total_data = [];//苯系物总量
  377. $ben = 0; //苯含量
  378. $all_dis_data = []; //所有杂质含量数组
  379. $ethanol = 0; //乙醇含量
  380. $methanol = 0; //甲醇含量
  381. $ethylacetate = 0; //乙酸乙酯含量
  382. $dis_impurity_arr = ['甲醇', '丙酮', '正丁醇', '苯', '2-乙氧基乙醇', '4-甲基-2-戊酮', '甲苯', '乙酸正丁酯', '乙苯', '间对二甲苯', '邻-二甲苯', '苯乙烯', '2-乙氧基乙基乙酸酯', '环己酮'];
  383. $ben_arr = ['甲苯', '乙苯', '间对二甲苯', '邻-二甲苯'];
  384. foreach ($detail_data as $key => $value) {
  385. $all_dis_data[$key] = $value['potency'];
  386. if ($value['chemical_compound'] == '乙醇') {//乙醇含量
  387. $ethanol = $value['potency'];
  388. }
  389. if ($value['chemical_compound'] == '苯') {//苯含量
  390. $ben = $value['potency'];
  391. }
  392. if (in_array($value['chemical_compound'], $dis_impurity_arr)) {//溶剂杂质总量
  393. $dis_impurity_data[$value['chemical_compound']] = $value['potency'];
  394. }
  395. if (in_array($value['chemical_compound'], $ben_arr)) {//苯系物总量
  396. $ben_total_data[$value['chemical_compound']] = $value['potency'];
  397. }
  398. if ($value['chemical_compound'] == '甲醇') {//甲醇含量
  399. $methanol = $value['potency'];
  400. }
  401. if ($value['chemical_compound'] == '乙酸乙酯') {//乙酸乙酯含量
  402. $ethylacetate = $value['potency'];
  403. }
  404. }
  405. $judge = Db::name('item_judge_detail')->where('pid', $temp_id[$ke])->select();
  406. $class = Db::name('item_judge')->where('id', $temp_id[$ke])->value('class');//获取判定标准类别
  407. $sum_all_dis_data = array_sum($all_dis_data);
  408. if ($ethanol == '-'){
  409. $ethanol = 0;
  410. }
  411. if ($methanol == '-'){
  412. $methanol = 0;
  413. }
  414. if ($ethylacetate == '-'){
  415. $ethylacetate = 0;
  416. }
  417. if ($class == 1) {//常规类 26项减去乙醇 杂质总量14项
  418. $dis = $sum_all_dis_data - $ethanol; //残留总量
  419. $dis_impurity = array_sum($dis_impurity_data); //杂质总量
  420. } else if ($class == 2) {//特殊类1 26项减去乙醇、甲醇、乙酸乙酯,杂质总量减去甲醇
  421. $dis = $sum_all_dis_data - $ethanol - $methanol - $ethylacetate; //残留总量
  422. $dis_impurity = array_sum($dis_impurity_data) - $methanol; //杂质总量
  423. } else {//特殊类2 26项减去乙醇 杂质总量14项减去甲醇
  424. $dis = $sum_all_dis_data - $ethanol; //残留总量
  425. $dis_impurity = array_sum($dis_impurity_data) - $methanol; //杂质总量
  426. }
  427. $ben_total = array_sum($ben_total_data); //苯系物重量
  428. /***
  429. * 如果判定合格,continue
  430. * 如果判定不合格,定义一个变量$str,在else 拼接不合格原因
  431. * 假如溶剂残留总量不合格,str = ‘不合格原因:溶剂残留总量检测值:$dis大于标准值:$v['max']’;
  432. */
  433. $str = '不合格原因:';
  434. foreach ($judge as $k => $v) {
  435. if ($v['params'] == '溶剂残留总量') {
  436. if ($dis < $v['max']) {
  437. $is_qualified[$k] = 1;
  438. } else {
  439. $is_qualified[$k] = 0;
  440. $str = $str. '溶剂残留总量检测值:'.$dis.'大于标准值:'.$v['max'].'。';
  441. }
  442. }
  443. if ($v['params'] == '溶剂杂质总量' || $v['params'] == '总量') {
  444. if ($dis_impurity < $v['max']) {
  445. $is_qualified[$k] = 1;
  446. } else {
  447. $is_qualified[$k] = 0;
  448. $str = $str. '溶剂杂质总量检测值:'.$dis_impurity.'大于标准值:'.$v['max'].'。';
  449. }
  450. }
  451. if ($v['params'] == '溶剂杂质苯系物' || $v['params'] == '苯系物总量' || $v['params'] == '苯系物') {
  452. if ($ben_total < $v['max']) {
  453. $is_qualified[$k] = 1;
  454. } else {
  455. $is_qualified[$k] = 0;
  456. $str = $str. '溶剂杂质苯系物检测值:'.$ben_total.'大于标准值:'.$v['max'].'。';
  457. }
  458. }
  459. if ($v['params'] == '溶剂杂质苯' || $v['params'] == '苯含量' || $v['params'] == '苯') {
  460. if ($ben < $v['max']) {
  461. $is_qualified[$k] = 1;
  462. } else {
  463. $is_qualified[$k] = 0;
  464. $str = $str. '溶剂杂质苯检测值:'.$ben.'大于标准值:'.$v['max'].'。';
  465. }
  466. }
  467. }
  468. $is_pass = array_sum($is_qualified);//4项都合格,sum=4才判定合格 不等于4就说明有不合格的
  469. $params[$ke]['judge'] = 0;
  470. if ($is_pass == 4) {
  471. $params[$ke]['judge'] = 1;
  472. $str = '';
  473. }
  474. $params[$ke]['entrust_no'] = $entrust['no'];
  475. $params[$ke]['entrust_id'] = $entrust['id'];
  476. $params[$ke]['machine'] = 'GCMS';
  477. if ($gather_tab == 'gather_txt_check_gc') {
  478. $params[$ke]['machine'] = 'GC';
  479. }
  480. $params[$ke]['name'] = $entrust['name'];
  481. $params[$ke]['bach'] = $entrust['bach'];
  482. $params[$ke]['sell_bach'] = $entrust['sell_bach'];
  483. $params[$ke]['sample_no'] = $entrust['sample_no'];
  484. $params[$ke]['standard_id'] = $temp_id[$ke];
  485. $params[$ke]['standard_name'] = $judge[0]['name'];
  486. if (is_numeric($dis)){
  487. $params[$ke]['dis'] = number_format($dis,'2','.','');
  488. }else{
  489. $params[$ke][$dis] = $dis;
  490. }
  491. if (is_numeric($dis_impurity)){
  492. $params[$ke]['dis_impurity'] = number_format($dis_impurity,'2','.','');
  493. }else{
  494. $params[$ke]['dis_impurity'] = $dis_impurity;
  495. }
  496. if (is_numeric($ben_total)){
  497. $params[$ke]['ben_total'] = number_format($ben_total,'2','.','');
  498. }else{
  499. $params[$ke]['ben_total'] = $ben_total;
  500. }
  501. if (is_numeric($ben)){
  502. $params[$ke]['ben'] = number_format($ben,'4','.','');
  503. }else{
  504. $params[$ke]['ben'] = $ben;
  505. }
  506. $params[$ke]['unit'] = 'mg/m2';
  507. $params[$ke]['remark'] = $str;
  508. $params[$ke]['create'] = date('Y-m-d H:i:s');
  509. $params[$ke]['maker'] = Session::get('admin')['username'];
  510. $params[$ke]['params'] = Db::name('item_judge')->where('id', $temp_id[$ke])->value('list_name');
  511. $params[$ke]['userid'] = Session::get('admin')['id'];;
  512. $params[$ke]['status'] = 0;
  513. $params[$ke]['work_unit'] = $entrust['work_unit'];
  514. $params[$ke]['sample_info'] = $entrust['sample_info'];
  515. $params[$ke]['work_name'] = $entrust['work_name'];
  516. $params[$ke]['send_sample'] = $entrust['send_sample'];
  517. // $entrust_update[$ke]['standard_id'] = $temp_id[$ke];
  518. // $entrust_update[$ke]['standard_name'] = $judge[0]['name'];
  519. $entrust_update[$ke]['status'] = 2;
  520. $entrust_update[$ke]['gather_id'] = $gather['id'];
  521. $entrust_update[$ke]['gather_tab'] = $gather_tab;
  522. $log[$ke]['userid'] = Session::get('admin')['id'];
  523. $log[$ke]['username'] = Session::get('admin')['username'];
  524. $log[$ke]['operate'] = '提交委托单id值为' . $id . '到检测';
  525. $log[$ke]['content'] = '选用检测标准为:"' . $judge[0]['name']. '",检测数据为:' . $gather_tab . '/' . $gather['id'];
  526. $log[$ke]['e_id'] = $id;
  527. $log[$ke]['create'] = date('Y-m-d H:i:s');
  528. }
  529. $result = 0;
  530. Db::startTrans();
  531. try {
  532. /***
  533. * 提交后,更新此数据的 判定标准、委托状态
  534. * 根据批次号绑定检测数据
  535. * 生成检测结果
  536. */
  537. $record = [];
  538. for ($i=0;$i<count($idList);$i++){
  539. $entrust_res = Db::name('entrust')->where('id', $idList[$i])->update($entrust_update[$i]);
  540. // Db::name('res')->where('entrust_id',$idList[$i])->update(['delete_time'=>date('Y-m-d H:i:s',time())]);
  541. // $res_check = Db::name('res')->insert($params[$i]);
  542. $entrust_id = Db::name('res')->where('entrust_id',$idList[$i])->order('id desc')->find();
  543. if (empty($entrust_id)) {
  544. $res_check = Db::name('res')->insert($params[$i]);
  545. } else {
  546. if ($entrust_id['status'] != 1){
  547. $data =Db::name('res')
  548. ->where('id',$entrust_id['id'])
  549. ->update(['delete_time'=>date('Y-m-d H:i:s',time())]);
  550. $res_check = Db::name('res')->insert($params[$i]);
  551. }else{
  552. $res_check = true;
  553. }
  554. }
  555. $log_res = Db::name('entrust_log')->insert($log[$i]);
  556. if (!$log_res || !$res_check) {
  557. $record[$i] = $idList[$i];
  558. }else{
  559. $result += 1;
  560. }
  561. }
  562. Db::commit();
  563. } catch (ValidateException|PDOException|Exception $e) {
  564. Db::rollback();
  565. $this->error($e->getMessage());
  566. }
  567. if ($result !== count($idList)) {
  568. $res_log = implode(',',$record);
  569. $this->error(__('序号为'.$res_log.'的委托单没有提交成功,请重新提交'));
  570. }
  571. $this->success();
  572. }
  573. //日志查看
  574. public function log(){
  575. //设置过滤方法
  576. $this->request->filter(['strip_tags', 'trim']);
  577. $params = $this->request->param("ids");
  578. if (false === $this->request->isAjax()) {
  579. $this->view->assign('id', $params);
  580. return $this->view->fetch();
  581. }
  582. //如果发送的来源是 Selectpage,则转发到 Selectpage
  583. if ($this->request->request('keyField')) {
  584. return $this->selectpage();
  585. }
  586. $id = $this->request->param("ids");
  587. $map = [];
  588. if (!empty($params)){
  589. $map['e_id'] = $id;
  590. }
  591. // print_r($map);die;
  592. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  593. $list = Db::name('entrust_log')
  594. ->where($map)
  595. ->order($sort, $order)
  596. ->paginate($limit);
  597. $result = ['total' => $list->total(), 'rows' => $list->items()];
  598. return json($result);
  599. }
  600. //检测数据查看
  601. public function data(){
  602. $params = input('id');
  603. if (empty($params)){
  604. $this->error('参数错误');
  605. }
  606. $gather_id = Db::name('entrust')->where('id',$params)->find();
  607. if (empty($gather_id['gather_id'])){
  608. $this->error('此委托单还未提交检测,暂无检测数据');
  609. }
  610. //此处根据实际获取到的采集表的数据 gather_tab 去对应表里查数据 重新写一个gc表格页面 js加代码
  611. if ($gather_id['gather_tab'] == 'gather_txt_check_gcms'){
  612. $gather = Db::name('gather_txt_gcms')->where('id',$gather_id['gather_id'])->find();
  613. $data = Db::name('gather_txt_check_gcms')->where('pid',$gather_id['gather_id'])->select();
  614. $this->view->assign('gather', $gather);
  615. $this->view->assign('data', $data);
  616. $this->view->assign('id', $params);
  617. return $this->view->fetch();
  618. }else if ($gather_id['gather_tab'] == 'gather_txt_check_gc'){
  619. $gather = Db::name('gather_txt_gc')->where('id',$gather_id['gather_id'])->find();
  620. $data = Db::name('gather_txt_check_gc')->where('pid',$gather_id['gather_id'])->select();
  621. $this->view->assign('gather', $gather);
  622. $this->view->assign('data', $data);
  623. $this->view->assign('id', $params);
  624. return $this->fetch('datagc');
  625. }
  626. }
  627. //检测数据确认
  628. public function dataSure(){
  629. $params = input('id');
  630. if (empty($params)){
  631. $this->error('参数错误');
  632. }
  633. $gather_id = Db::name('entrust')->where('id',$params)->find();
  634. if (empty($gather_id['gather_id'])){
  635. $this->error('此委托单还未提交检测,暂无检测数据');
  636. }
  637. if ($gather_id['gather_tab'] == 'gather_txt_check_gcms'){
  638. Db::name('gather_txt_gcms')->where('id',$gather_id['gather_id'])->setField('status',1);
  639. }else{
  640. Db::name('gather_txt_gc')->where('id',$gather_id['gather_id'])->setField('status',1);
  641. }
  642. $this->success('确认成功');
  643. }
  644. //委托单页面
  645. public function commissionsheet(){
  646. //获取样品名称
  647. $id = input('id');
  648. if (empty($id)){
  649. $this->error('参数错误');
  650. }
  651. $this->assign('name',Db::name('entrust')->where('id',$id)->find());
  652. //委托单上传
  653. return $this->view->fetch();
  654. }
  655. //委托单上传
  656. public function commissionsheetup(){
  657. $params = [];
  658. if (true === $this->request->isPost()){
  659. $params['entrust_user'] = input('entrust_user');
  660. $params['entrust_time'] = input('entrust_time');
  661. $params['entrust_id'] = input('entrust_id');
  662. $params['name'] = input('name');
  663. $params['status'] = input('status');
  664. $params['num'] = input('num');
  665. $params['project'] = input('project');
  666. $params['no'] = input('no');
  667. $params['requirement'] = input('requirement');
  668. $params['reportnumber'] = input('reportnumber');
  669. $params['deal'] = input('deal');
  670. $params['condition'] = input('condition');
  671. $params['isue'] = input('isue');
  672. $params['testcost'] = input('testcost');
  673. $params['standard'] = input('standard');
  674. }
  675. $judgment = Db::name('entrust_print')->where('entrust_user',$params['entrust_user'])
  676. ->where('entrust_id',$params['entrust_id'])
  677. ->where('entrust_time',$params['entrust_time'])
  678. ->where('name',$params['name'])
  679. ->where('no',$params['no'])->find();
  680. if (empty($judgment)){
  681. $res = Db::name('entrust_print')->insert($params);
  682. if (!empty($res)){
  683. $this->success();
  684. }
  685. }else{
  686. $this->error('该委托单已存在,不需要重新添加');
  687. }
  688. }
  689. //委托单批量打印页面
  690. public function printing(){
  691. $datas=$res = array();
  692. if ($this->request->isGet()){
  693. $ids =explode(',',$this->request->get('ids')) ;
  694. for($i=0;$i<count($ids);$i++){
  695. $datas[$i] = Db::name('entrust')->where('id',$ids[$i])->find();
  696. $res['entrust_id'][$i]=$datas[$i]['no'];
  697. $res['set'][$i]['name'] = $datas[$i]['name'];
  698. $res['set'][$i]['no'] = $datas[$i]['sample_no'];
  699. }
  700. $res['entrust_time'] = $datas[0]['create'];
  701. $res['entrust_id'] = implode('、',array_unique($res['entrust_id']));
  702. $this->view->assign('row',$res);
  703. }
  704. return $this->view->fetch();
  705. }
  706. //委托单批量上传
  707. public function printingup(){
  708. $params = [];
  709. if (true === $this->request->isPost()){
  710. $sampledata = input('sampledata/a');
  711. for ($i=0;$i<count($sampledata);$i++){
  712. $params['entrust_user'] = input('entrust_user');
  713. $params['entrust_time'] = input('entrust_time');
  714. $params['entrust_id'] = input('entrust_id');
  715. $params['requirement'] = input('requirement');
  716. $params['reportnumber'] = input('reportnumber');
  717. $params['deal'] = input('deal');
  718. $params['condition'] = input('condition');
  719. $params['isue'] = input('isue');
  720. $params['testcost'] = input('testcost');
  721. $params['standard'] = input('standard');
  722. $params['name'] = $sampledata[$i][0];
  723. $params['status'] = $sampledata[$i][1];
  724. $params['num'] = $sampledata[$i][2];
  725. $params['project'] = $sampledata[$i][3];
  726. $params['no'] = $sampledata[$i][4];
  727. $res[$i] = Db::name('entrust_print')->insert($params);
  728. }
  729. if (!empty($res)){
  730. $this->success();
  731. }
  732. }
  733. }
  734. //生成委托单号
  735. public function getNo(){
  736. $params = input('temp');
  737. $company = input('company');
  738. if (empty($params) || empty($company)){
  739. $this->error('参数错误');
  740. }
  741. $beginTime = date('Y-m-d 00:00:00');
  742. $endTime = date('Y-m-d 23:59:59');
  743. $num = Db::name('entrust')->where('sample_no','like','%'.$params.'%')->where('create','between',[$beginTime,$endTime])->count();
  744. // if ($company == '亚欣'){//亚欣送样从100开始
  745. // $num = $num+100;
  746. // }else{
  747. // $num = $num+1;
  748. // }
  749. $num = $num+1;
  750. if ($num < 10){
  751. $num = '0'.$num;
  752. }
  753. //一位样品代号+8位日期+当前样品代号第几次送样
  754. $sample_no = date('Ymd').$num;
  755. return array('code'=>1,'data'=>$sample_no);
  756. }
  757. }