Entrust.php 33 KB

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