Res.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  5. use \think\Db;
  6. use \think\Session;
  7. /**
  8. * 检测结果管理管理
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Res extends Backend
  13. {
  14. /**
  15. * Res模型对象
  16. * @var \app\admin\model\Res
  17. */
  18. protected $model = null;
  19. protected $searchFields = 'entrust_no,sample_no,name,bach,standard_name';
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\Res;
  24. $this->view->assign("statusList", $this->model->getStatusList());
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 查看
  33. *
  34. * @return string|Json
  35. * @throws \think\Exception
  36. * @throws DbException
  37. */
  38. public function index()
  39. {
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if (false === $this->request->isAjax()) {
  43. return $this->view->fetch();
  44. }
  45. //如果发送的来源是 Selectpage,则转发到 Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  50. $user_id = Session::get('admin')['id'];
  51. if ($user_id == 1){//超级管理员
  52. $list = $this->model
  53. ->where($where)
  54. ->order($sort, $order)
  55. ->paginate($limit);
  56. }else{
  57. $userinfo = Db::name('admin')->where('id',$user_id)->find();
  58. $pidList = Db::name('company')->where('pid',$userinfo['company'])->select();
  59. $map = [];
  60. $map['delete_time'] = null;
  61. if (!empty($pidList)){//总公司
  62. $pid = [];
  63. foreach ($pidList as $key=>$value){
  64. $pid[$key] = $value['id'];
  65. }
  66. $map['work_unit'] = array('in',$pid);
  67. }else{//分公司
  68. $map['work_unit'] = $userinfo['company'];
  69. }
  70. $list = $this->model
  71. ->where($where)
  72. ->where($map)
  73. ->order($sort, $order)
  74. ->paginate($limit);
  75. }
  76. $result = ['total' => $list->total(), 'rows' => $list->items()];
  77. return json($result);
  78. }
  79. //检测数据编辑
  80. public function edit($ids = null)
  81. {
  82. $row = $this->model->get($ids);
  83. if ($row['judge'] == 0){
  84. $row['judge'] = '不合格';
  85. }else{
  86. $row['judge'] = '合格';
  87. }
  88. if (!$row) {
  89. $this->error(__('No Results were found'));
  90. }
  91. $adminIds = $this->getDataLimitAdminIds();
  92. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  93. $this->error(__('You have no permission'));
  94. }
  95. if (false === $this->request->isPost()) {
  96. $this->view->assign('row', $row);
  97. return $this->view->fetch();
  98. }
  99. $params = $this->request->post('row/a');
  100. if (empty($params)) {
  101. $this->error(__('Parameter %s can not be empty', ''));
  102. }
  103. $params = $this->preExcludeFields($params);
  104. if ($params['judge'] == '合格'){
  105. $params['judge'] = '1';
  106. }else{
  107. $params['judge'] = '0';
  108. }
  109. $result = false;
  110. Db::startTrans();
  111. try {
  112. //是否采用模型验证
  113. if ($this->modelValidate) {
  114. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  115. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  116. $row->validateFailException()->validate($validate);
  117. }
  118. $result = $row->allowField(true)->save($params);
  119. Db::commit();
  120. } catch (ValidateException|PDOException|Exception $e) {
  121. Db::rollback();
  122. $this->error($e->getMessage());
  123. }
  124. if (false === $result) {
  125. $this->error(__('No rows were updated'));
  126. }
  127. $this->success();
  128. }
  129. //检测数据查看
  130. public function data(){
  131. $params = input('id');
  132. if (empty($params)){
  133. $this->error('参数错误');
  134. }
  135. $entrust_id = Db::name('res')->where('id',$params)->find();
  136. $gather_id = Db::name('entrust')->where('id',$entrust_id['entrust_id'])->find();
  137. $this->view->assign('row',$entrust_id);
  138. if (empty($gather_id['gather_id'])){
  139. $this->error('此委托单还未提交检测,暂无检测数据');
  140. }
  141. //此处根据实际获取到的采集表的数据 gather_tab 去对应表里查数据 重新写一个gc表格页面 js加代码
  142. if ($gather_id['gather_tab'] == 'gather_txt_check_gcms'){
  143. $gather = Db::name('gather_txt_gcms')->where('id',$gather_id['gather_id'])->find();
  144. $data = Db::name('gather_txt_check_gcms')->where('pid',$gather_id['gather_id'])->select();
  145. $this->view->assign('gather', $gather);
  146. $this->view->assign('data', $data);
  147. $this->view->assign('id', $params);
  148. return $this->view->fetch();
  149. }else if ($gather_id['gather_tab'] == 'gather_txt_check_gc'){
  150. $gather = Db::name('gather_txt_gc')->where('id',$gather_id['gather_id'])->find();
  151. $data = Db::name('gather_txt_check_gc')->where('pid',$gather_id['gather_id'])->select();
  152. $this->view->assign('gather', $gather);
  153. $this->view->assign('data', $data);
  154. $this->view->assign('id', $params);
  155. return $this->fetch('datagc');
  156. }
  157. }
  158. //批量审核
  159. public function couse(){
  160. $id = input('ids');
  161. $this->view->assign('id',$id);
  162. $row = input('row/a');
  163. return $this->fetch();
  164. }
  165. public function couses(){
  166. if ($this->request->isPost()){
  167. $id = input('id');
  168. $status = input('status');
  169. $couse = input('couse');
  170. $ids = explode(',',$id);
  171. for ($i=0;$i<count($ids);$i++){
  172. Db::name('res')->where('id',$ids[$i])->update(['status'=>$status,'couse'=>$couse]);
  173. }
  174. return $this->success();
  175. }else{
  176. return $this->error('参数错误');
  177. }
  178. }
  179. //数据报告导出
  180. public function report(){
  181. if ($this->request->isGet()){
  182. $judge=null;
  183. $data=array();
  184. $ids = input('id');
  185. $id = explode(',',$ids);
  186. for ($i=0;$i<count($id);$i++){
  187. $res = Db::name('res')->where('id',$id[$i])->find();
  188. if ($res['judge'] == 1){
  189. $judge = '合格';
  190. }else{
  191. $judge = '不合格';
  192. }
  193. $entrust = Db::name('entrust')->where('id',$res['entrust_id'])->find();
  194. // $gather = Db::name($entrust['gather_tab'])->where('pid',$entrust['gather_id'])->select();
  195. $gather = Db::name($entrust['gather_tab'])->where('pid',$entrust['gather_id'])->column('potency','chemical_compound');
  196. $data[$i] = [
  197. 'name' => $entrust['name'],
  198. 'sell_bach' => $entrust['sell_bach'],
  199. 'bach' => $entrust['bach'],
  200. 'massage' => $entrust['sample_info'],
  201. 'user_name' => $entrust['send_sample'],
  202. 'create' => $res['create'],
  203. 'resname' => $entrust['sample_no'],
  204. 'dis' => $res['dis'],
  205. 'dis_impurity' => $res['dis_impurity'],
  206. 'ben_total' => $res['ben_total'],
  207. 'ben' => $res['ben'],
  208. 'judge' => $judge,
  209. 'maker' => $entrust['receive_sample'],
  210. 'remark' => '',
  211. '0' => isset($gather['甲醇']) ? $gather['甲醇'] : '',
  212. '1' => isset($gather['乙醇']) ? $gather['乙醇'] : '',
  213. '2' => isset($gather['异丙醇']) ? $gather['异丙醇'] : '',
  214. '3' => isset($gather['丙酮']) ? $gather['丙酮'] : '',
  215. '4' => isset($gather['正丙醇']) ? $gather['正丙醇'] : '',
  216. '5' => isset($gather['丁酮']) ? $gather['丁酮'] : '',
  217. '6' => isset($gather['乙酸乙酯']) ? $gather['乙酸乙酯'] : '',
  218. '7' => isset($gather['乙酸异丙酯']) ? $gather['乙酸异丙酯'] : '',
  219. '8' => isset($gather['正丁醇']) ? $gather['正丁醇'] : '',
  220. '9' => isset($gather['苯']) ? $gather['苯'] : '',
  221. '10' => isset($gather['1-甲氧基-2-丙醇']) ? $gather['1-甲氧基-2-丙醇'] : '',
  222. '11' => isset($gather['乙酸正丙酯']) ? $gather['乙酸正丙酯'] : '',
  223. '12' => isset($gather['2-乙氧基乙醇']) ? $gather['2-乙氧基乙醇'] : '',
  224. '13' => isset($gather['4-甲基-2-戊酮']) ? $gather['4-甲基-2-戊酮'] : '',
  225. '14' => isset($gather['1-乙氧基-2-丙醇']) ? $gather['1-乙氧基-2-丙醇'] : '',
  226. '15' => isset($gather['甲苯']) ? $gather['甲苯'] : '',
  227. '16' => isset($gather['乙酸正丁酯']) ? $gather['乙酸正丁酯'] : '',
  228. '17' => isset($gather['乙苯']) ? $gather['乙苯'] : '',
  229. '18' => isset($gather['间对二甲苯']) ? $gather['间对二甲苯'] : '',
  230. '19' => isset($gather['邻-二甲苯']) ? $gather['邻-二甲苯'] : '',
  231. '20' => isset($gather['苯乙烯']) ? $gather['苯乙烯'] : '',
  232. '21' => isset($gather['2-乙氧基乙基乙酸酯']) ? $gather['2-乙氧基乙基乙酸酯'] : '',
  233. '22' => isset($gather['环己酮']) ? $gather['环己酮'] : '',
  234. '23' => isset($gather['丁二酸二甲酯']) ? $gather['丁二酸二甲酯'] : '',
  235. '24' => isset($gather['戊二酸二甲酯']) ? $gather['戊二酸二甲酯'] : '',
  236. '25' => isset($gather['己二酸二甲酯']) ? $gather['己二酸二甲酯'] : '',
  237. ];
  238. }
  239. $this->exceloutput($data);
  240. }else{
  241. $this->error('参数错误');
  242. }
  243. }
  244. /**
  245. * 搜索栏检测项目下拉列表
  246. */
  247. public function companyselect(){
  248. $company = Db::name('item_judge')->where('delete',null)->order('weigh asc')->column('name,name');
  249. if (empty($company)){
  250. return $this->error('没有检测项目数据');
  251. }else{
  252. return json($company);
  253. }
  254. }
  255. /**
  256. * 搜索栏检测设备精准查找
  257. */
  258. public function machineselect(){
  259. echo "<pre>";
  260. print_r(input(''));
  261. echo "</pre>";
  262. }
  263. //excel格式
  264. public function exceloutput($data){
  265. vendor("phpoffice/phpspreadsheet/src/PhpSpreadsheet/Spreadsheet");
  266. $objexcel = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  267. $sheet = $objexcel->getActiveSheet();
  268. //设置全局单元格垂直居中
  269. $objexcel->getDefaultStyle()->getAlignment()->setVertical(Alignment::HORIZONTAL_CENTER);
  270. //设置全局单元格水平居中
  271. $objexcel->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  272. //设置全局单元格自动换行
  273. $objexcel->getDefaultStyle()->getAlignment()->setWrapText(true);
  274. //设置单元格宽度
  275. $sheet->getDefaultColumnDimension()->setWidth(18);
  276. //合并单元格,设置表头格式
  277. $sheet->mergeCells('A1:AN1');
  278. $sheet->mergeCells('A2:A3');
  279. $sheet->mergeCells('B2:B3');
  280. $sheet->mergeCells('C2:C3');
  281. $sheet->mergeCells('D2:D3');
  282. $sheet->mergeCells('E2:E3');
  283. $sheet->mergeCells('F2:F3');
  284. $sheet->mergeCells('G2:G3');
  285. $sheet->mergeCells('H2:H3');
  286. $sheet->mergeCells('I2:K2');
  287. $sheet->mergeCells('L2:L3');
  288. $sheet->mergeCells('M2:M3');
  289. $sheet->mergeCells('N2:N3');
  290. $sheet->mergeCells('O2:O3');
  291. $sheet->mergeCells('P2:P3');
  292. $sheet->mergeCells('Q2:Q3');
  293. $sheet->mergeCells('R2:R3');
  294. $sheet->mergeCells('S2:S3');
  295. $sheet->mergeCells('T2:T3');
  296. $sheet->mergeCells('U2:U3');
  297. $sheet->mergeCells('V2:V3');
  298. $sheet->mergeCells('W2:W3');
  299. $sheet->mergeCells('X2:X3');
  300. $sheet->mergeCells('Y2:Y3');
  301. $sheet->mergeCells('Z2:Z3');
  302. $sheet->mergeCells('AA2:AA3');
  303. $sheet->mergeCells('AB2:AB3');
  304. $sheet->mergeCells('AC2:AC3');
  305. $sheet->mergeCells('AD2:AD3');
  306. $sheet->mergeCells('AE2:AE3');
  307. $sheet->mergeCells('AF2:AF3');
  308. $sheet->mergeCells('AG2:AG3');
  309. $sheet->mergeCells('AH2:AH3');
  310. $sheet->mergeCells('AI2:AI3');
  311. $sheet->mergeCells('AJ2:AJ3');
  312. $sheet->mergeCells('AK2:AK3');
  313. $sheet->mergeCells('AL2:AL3');
  314. $sheet->mergeCells('AM2:AM3');
  315. $sheet->mergeCells('AN2:AN3');
  316. //设置表头
  317. $sheet->setCellValue('A2', '产品名称');
  318. $sheet->setCellValue('B2','销售订单号');
  319. $sheet->setCellValue('C2','生产批次号');
  320. $sheet->setCellValue('D2','样品信息');
  321. $sheet->setCellValue('E2','送样人');
  322. $sheet->setCellValue('F2','检测日期');
  323. $sheet->setCellValue('G2','检测编号');
  324. $sheet->setCellValue('H2','溶剂残留总量 ≤70.0mg/m²');
  325. $sheet->setCellValue('I2','溶剂杂质');
  326. $sheet->setCellValue('I3','溶剂杂质总量 ≤7.0mg/m²');
  327. $sheet->setCellValue('J3','苯系物总量 ≤0.7mg/m²');
  328. $sheet->setCellValue('K3','苯含量 ≤0.010mg/m²');
  329. $sheet->setCellValue('L2','判定结论');
  330. $sheet->setCellValue('M2','检测人');
  331. $sheet->setCellValue('N2','备注');
  332. $sheet->setCellValue('O2','甲醇');
  333. $sheet->setCellValue('P2','乙醇');
  334. $sheet->setCellValue('Q2','异丙醇');
  335. $sheet->setCellValue('R2','丙酮');
  336. $sheet->setCellValue('S2','正丙醇');
  337. $sheet->setCellValue('T2','丁酮');
  338. $sheet->setCellValue('U2','乙酸乙酯');
  339. $sheet->setCellValue('V2','乙酸异丙酯');
  340. $sheet->setCellValue('W2','正丁醇');
  341. $sheet->setCellValue('X2','苯');
  342. $sheet->setCellValue('Y2','1-甲氧基-2-丙醇');
  343. $sheet->setCellValue('Z2','乙酸正丙酯');
  344. $sheet->setCellValue('AA2','2-乙氧基乙醇');
  345. $sheet->setCellValue('AB2','4-甲基-2-戊酮');
  346. $sheet->setCellValue('AC2','1-乙氧基-2-丙醇');
  347. $sheet->setCellValue('AD2','甲苯');
  348. $sheet->setCellValue('AE2','乙酸正丁酯');
  349. $sheet->setCellValue('AF2','乙苯');
  350. $sheet->setCellValue('AG2','间对-二甲苯');
  351. $sheet->setCellValue('AH2','邻-二甲苯');
  352. $sheet->setCellValue('AI2','苯乙烯');
  353. $sheet->setCellValue('AJ2','2-乙氧基乙基乙酸酯');
  354. $sheet->setCellValue('AK2','环己酮');
  355. $sheet->setCellValue('AL2','丁二酸二甲酯');
  356. $sheet->setCellValue('AM2','戊二酸二甲酯');
  357. $sheet->setCellValue('AN2','己二酸二甲酯');
  358. //设置A列宽度
  359. $sheet->getColumnDimension('A')->setWidth(25);
  360. //插入表格内容
  361. for ($i=0;$i<count($data);$i++){
  362. $k = $i+4;
  363. $sheet->getCell('A'.$k)->setValue($data[$i]['name']);
  364. $sheet->getCell('B'.$k)->setValue($data[$i]['sell_bach']);
  365. $sheet->getCell('C'.$k)->setValue($data[$i]['bach']);
  366. $sheet->getCell('D'.$k)->setValue($data[$i]['massage']);
  367. $sheet->getCell('E'.$k)->setValue($data[$i]['user_name']);
  368. $sheet->getCell('F'.$k)->setValue($data[$i]['create']);
  369. $sheet->getCell('G'.$k)->setValue($data[$i]['resname']);
  370. $sheet->getCell('H'.$k)->setValue($data[$i]['dis']);
  371. $sheet->getCell('I'.$k)->setValue($data[$i]['dis_impurity']);
  372. $sheet->getCell('J'.$k)->setValue($data[$i]['ben_total']);
  373. $sheet->getCell('K'.$k)->setValue($data[$i]['ben']);
  374. $sheet->getCell('L'.$k)->setValue($data[$i]['judge']);
  375. $sheet->getCell('M'.$k)->setValue($data[$i]['maker']);
  376. $sheet->getCell('N'.$k)->setValue($data[$i]['remark']);
  377. $sheet->getCell('O'.$k)->setValue($data[$i]['0']);
  378. $sheet->getCell('P'.$k)->setValue($data[$i][1]);
  379. $sheet->getCell('Q'.$k)->setValue($data[$i][2]);
  380. $sheet->getCell('R'.$k)->setValue($data[$i][3]);
  381. $sheet->getCell('S'.$k)->setValue($data[$i][4]);
  382. $sheet->getCell('T'.$k)->setValue($data[$i][5]);
  383. $sheet->getCell('U'.$k)->setValue($data[$i][6]);
  384. $sheet->getCell('V'.$k)->setValue($data[$i][7]);
  385. $sheet->getCell('W'.$k)->setValue($data[$i][8]);
  386. $sheet->getCell('X'.$k)->setValue($data[$i][9]);
  387. $sheet->getCell('Y'.$k)->setValue($data[$i][10]);
  388. $sheet->getCell('Z'.$k)->setValue($data[$i][11]);
  389. $sheet->getCell('AA'.$k)->setValue($data[$i][12]);
  390. $sheet->getCell('AB'.$k)->setValue($data[$i][13]);
  391. $sheet->getCell('AC'.$k)->setValue($data[$i][14]);
  392. $sheet->getCell('AD'.$k)->setValue($data[$i][15]);
  393. $sheet->getCell('AE'.$k)->setValue($data[$i][16]);
  394. $sheet->getCell('AF'.$k)->setValue($data[$i][17]);
  395. $sheet->getCell('AG'.$k)->setValue($data[$i][18]);
  396. $sheet->getCell('AH'.$k)->setValue($data[$i][19]);
  397. $sheet->getCell('AI'.$k)->setValue($data[$i][20]);
  398. $sheet->getCell('AJ'.$k)->setValue($data[$i][21]);
  399. $sheet->getCell('AK'.$k)->setValue($data[$i][22]);
  400. $sheet->getCell('AL'.$k)->setValue($data[$i][23]);
  401. $sheet->getCell('AM'.$k)->setValue($data[$i][24]);
  402. $sheet->getCell('AN'.$k)->setValue($data[$i][25]);
  403. //不合格字体设置红色
  404. $start = 'A'.$k; $xend = 'AN'.$k;
  405. if ($data[$i]['judge'] == '不合格'){
  406. $sheet->getStyle("$start:$xend")->getFont()->getColor()
  407. ->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
  408. }
  409. }
  410. //excel文件格式
  411. $filename = '数据报告'.date('ymdhis',time()).'.xlsx';
  412. $filename = iconv("utf-8","gb2312",$filename);
  413. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  414. header('Content-Disposition: attachment;filename="'.$filename.'"');
  415. header('Cache-Control: max-age=0');
  416. header("Content-Type:text/html;charset=utf-8");
  417. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objexcel, 'Xlsx');
  418. $writer->save('php://output');
  419. exit;
  420. }
  421. }