Res.php 19 KB

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