Entrust.php 32 KB

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