Res.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. ->whereNull('delete_time')
  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. * 搜索栏检测项目下拉列表
  181. */
  182. public function companyselect(){
  183. $company = Db::name('item_judge')->where('delete',null)->order('weigh asc')->column('name,name');
  184. if (empty($company)){
  185. return $this->error('没有检测项目数据');
  186. }else{
  187. return json($company);
  188. }
  189. }
  190. //数据报告导出
  191. public function report(){
  192. if ($this->request->isGet()){
  193. $judge=null;
  194. $data=array();
  195. $ids = input('id');
  196. $id = explode(',',$ids);
  197. for ($i=0;$i<count($id);$i++){
  198. $res = Db::name('res')->where('id',$id[$i])->find();
  199. if ($res['judge'] == 1){
  200. $judge = '合格';
  201. }else{
  202. $judge = '不合格';
  203. }
  204. $entrust = Db::name('entrust')->where('id',$res['entrust_id'])->find();
  205. // $gather = Db::name($entrust['gather_tab'])->where('pid',$entrust['gather_id'])->select();
  206. $gather = Db::name($entrust['gather_tab'])->where('pid',$entrust['gather_id'])->column('potency','chemical_compound');
  207. $data[$i] = [
  208. 'name' => $entrust['name'],
  209. 'sell_bach' => $entrust['sell_bach'],
  210. 'bach' => $entrust['bach'],
  211. 'massage' => $entrust['sample_info'],
  212. 'user_name' => $entrust['send_sample'],
  213. 'create' => $res['create'],
  214. 'resname' => $entrust['sample_no'],
  215. 'dis' => $res['dis'],
  216. 'dis_impurity' => $res['dis_impurity'],
  217. 'ben_total' => $res['ben_total'],
  218. 'ben' => $res['ben'],
  219. 'judge' => $judge,
  220. 'maker' => $entrust['receive_sample'],
  221. 'remark' => '',
  222. '0' => isset($gather['甲醇']) ? $gather['甲醇'] : '',
  223. '1' => isset($gather['乙醇']) ? $gather['乙醇'] : '',
  224. '2' => isset($gather['异丙醇']) ? $gather['异丙醇'] : '',
  225. '3' => isset($gather['丙酮']) ? $gather['丙酮'] : '',
  226. '4' => isset($gather['正丙醇']) ? $gather['正丙醇'] : '',
  227. '5' => isset($gather['丁酮']) ? $gather['丁酮'] : '',
  228. '6' => isset($gather['乙酸乙酯']) ? $gather['乙酸乙酯'] : '',
  229. '7' => isset($gather['乙酸异丙酯']) ? $gather['乙酸异丙酯'] : '',
  230. '8' => isset($gather['正丁醇']) ? $gather['正丁醇'] : '',
  231. '9' => isset($gather['苯']) ? $gather['苯'] : '',
  232. '10' => isset($gather['1-甲氧基-2-丙醇']) ? $gather['1-甲氧基-2-丙醇'] : '',
  233. '11' => isset($gather['乙酸正丙酯']) ? $gather['乙酸正丙酯'] : '',
  234. '12' => isset($gather['2-乙氧基乙醇']) ? $gather['2-乙氧基乙醇'] : '',
  235. '13' => isset($gather['4-甲基-2-戊酮']) ? $gather['4-甲基-2-戊酮'] : '',
  236. '14' => isset($gather['1-乙氧基-2-丙醇']) ? $gather['1-乙氧基-2-丙醇'] : '',
  237. '15' => isset($gather['甲苯']) ? $gather['甲苯'] : '',
  238. '16' => isset($gather['乙酸正丁酯']) ? $gather['乙酸正丁酯'] : '',
  239. '17' => isset($gather['乙苯']) ? $gather['乙苯'] : '',
  240. '18' => isset($gather['间对二甲苯']) ? $gather['间对二甲苯'] : '',
  241. '19' => isset($gather['邻-二甲苯']) ? $gather['邻-二甲苯'] : '',
  242. '20' => isset($gather['苯乙烯']) ? $gather['苯乙烯'] : '',
  243. '21' => isset($gather['2-乙氧基乙基乙酸酯']) ? $gather['2-乙氧基乙基乙酸酯'] : '',
  244. '22' => isset($gather['环己酮']) ? $gather['环己酮'] : '',
  245. '23' => isset($gather['丁二酸二甲酯']) ? $gather['丁二酸二甲酯'] : '',
  246. '24' => isset($gather['戊二酸二甲酯']) ? $gather['戊二酸二甲酯'] : '',
  247. '25' => isset($gather['己二酸二甲酯']) ? $gather['己二酸二甲酯'] : '',
  248. ];
  249. }
  250. $this->exceloutput($data);
  251. }else{
  252. $this->error('参数错误');
  253. }
  254. }
  255. //excel格式
  256. public function exceloutput($data){
  257. vendor("phpoffice/phpspreadsheet/src/PhpSpreadsheet/Spreadsheet");
  258. $objexcel = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  259. $sheet = $objexcel->getActiveSheet();
  260. //设置全局单元格垂直居中
  261. $objexcel->getDefaultStyle()->getAlignment()->setVertical(Alignment::HORIZONTAL_CENTER);
  262. //设置全局单元格水平居中
  263. $objexcel->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  264. //设置全局单元格自动换行
  265. $objexcel->getDefaultStyle()->getAlignment()->setWrapText(true);
  266. //设置单元格宽度
  267. $sheet->getDefaultColumnDimension()->setWidth(18);
  268. //合并单元格,设置表头格式
  269. $sheet->mergeCells('A1:AN1');
  270. $sheet->mergeCells('A2:A3');
  271. $sheet->mergeCells('B2:B3');
  272. $sheet->mergeCells('C2:C3');
  273. $sheet->mergeCells('D2:D3');
  274. $sheet->mergeCells('E2:E3');
  275. $sheet->mergeCells('F2:F3');
  276. $sheet->mergeCells('G2:G3');
  277. $sheet->mergeCells('H2:H3');
  278. $sheet->mergeCells('I2:K2');
  279. $sheet->mergeCells('L2:L3');
  280. $sheet->mergeCells('M2:M3');
  281. $sheet->mergeCells('N2:N3');
  282. $sheet->mergeCells('O2:O3');
  283. $sheet->mergeCells('P2:P3');
  284. $sheet->mergeCells('Q2:Q3');
  285. $sheet->mergeCells('R2:R3');
  286. $sheet->mergeCells('S2:S3');
  287. $sheet->mergeCells('T2:T3');
  288. $sheet->mergeCells('U2:U3');
  289. $sheet->mergeCells('V2:V3');
  290. $sheet->mergeCells('W2:W3');
  291. $sheet->mergeCells('X2:X3');
  292. $sheet->mergeCells('Y2:Y3');
  293. $sheet->mergeCells('Z2:Z3');
  294. $sheet->mergeCells('AA2:AA3');
  295. $sheet->mergeCells('AB2:AB3');
  296. $sheet->mergeCells('AC2:AC3');
  297. $sheet->mergeCells('AD2:AD3');
  298. $sheet->mergeCells('AE2:AE3');
  299. $sheet->mergeCells('AF2:AF3');
  300. $sheet->mergeCells('AG2:AG3');
  301. $sheet->mergeCells('AH2:AH3');
  302. $sheet->mergeCells('AI2:AI3');
  303. $sheet->mergeCells('AJ2:AJ3');
  304. $sheet->mergeCells('AK2:AK3');
  305. $sheet->mergeCells('AL2:AL3');
  306. $sheet->mergeCells('AM2:AM3');
  307. $sheet->mergeCells('AN2:AN3');
  308. //设置表头
  309. $sheet->setCellValue('A2', '产品名称');
  310. $sheet->setCellValue('B2','销售订单号');
  311. $sheet->setCellValue('C2','生产批次号');
  312. $sheet->setCellValue('D2','样品信息');
  313. $sheet->setCellValue('E2','送样人');
  314. $sheet->setCellValue('F2','检测日期');
  315. $sheet->setCellValue('G2','检测编号');
  316. $sheet->setCellValue('H2','溶剂残留总量 ≤70.0mg/m²');
  317. $sheet->setCellValue('I2','溶剂杂质');
  318. $sheet->setCellValue('I3','溶剂杂质总量 ≤7.0mg/m²');
  319. $sheet->setCellValue('J3','苯系物总量 ≤0.7mg/m²');
  320. $sheet->setCellValue('K3','苯含量 ≤0.010mg/m²');
  321. $sheet->setCellValue('L2','判定结论');
  322. $sheet->setCellValue('M2','检测人');
  323. $sheet->setCellValue('N2','备注');
  324. $sheet->setCellValue('O2','甲醇');
  325. $sheet->setCellValue('P2','乙醇');
  326. $sheet->setCellValue('Q2','异丙醇');
  327. $sheet->setCellValue('R2','丙酮');
  328. $sheet->setCellValue('S2','正丙醇');
  329. $sheet->setCellValue('T2','丁酮');
  330. $sheet->setCellValue('U2','乙酸乙酯');
  331. $sheet->setCellValue('V2','乙酸异丙酯');
  332. $sheet->setCellValue('W2','正丁醇');
  333. $sheet->setCellValue('X2','苯');
  334. $sheet->setCellValue('Y2','1-甲氧基-2-丙醇');
  335. $sheet->setCellValue('Z2','乙酸正丙酯');
  336. $sheet->setCellValue('AA2','2-乙氧基乙醇');
  337. $sheet->setCellValue('AB2','4-甲基-2-戊酮');
  338. $sheet->setCellValue('AC2','1-乙氧基-2-丙醇');
  339. $sheet->setCellValue('AD2','甲苯');
  340. $sheet->setCellValue('AE2','乙酸正丁酯');
  341. $sheet->setCellValue('AF2','乙苯');
  342. $sheet->setCellValue('AG2','间对-二甲苯');
  343. $sheet->setCellValue('AH2','邻-二甲苯');
  344. $sheet->setCellValue('AI2','苯乙烯');
  345. $sheet->setCellValue('AJ2','2-乙氧基乙基乙酸酯');
  346. $sheet->setCellValue('AK2','环己酮');
  347. $sheet->setCellValue('AL2','丁二酸二甲酯');
  348. $sheet->setCellValue('AM2','戊二酸二甲酯');
  349. $sheet->setCellValue('AN2','己二酸二甲酯');
  350. //设置A列宽度
  351. $sheet->getColumnDimension('A')->setWidth(25);
  352. //插入表格内容
  353. for ($i=0;$i<count($data);$i++){
  354. $k = $i+4;
  355. $sheet->getCell('A'.$k)->setValue($data[$i]['name']);
  356. $sheet->getCell('B'.$k)->setValue($data[$i]['sell_bach']);
  357. $sheet->getCell('C'.$k)->setValue($data[$i]['bach']);
  358. $sheet->getCell('D'.$k)->setValue($data[$i]['massage']);
  359. $sheet->getCell('E'.$k)->setValue($data[$i]['user_name']);
  360. $sheet->getCell('F'.$k)->setValue($data[$i]['create']);
  361. $sheet->getCell('G'.$k)->setValue($data[$i]['resname']);
  362. $sheet->getCell('H'.$k)->setValue($data[$i]['dis']);
  363. $sheet->getCell('I'.$k)->setValue($data[$i]['dis_impurity']);
  364. $sheet->getCell('J'.$k)->setValue($data[$i]['ben_total']);
  365. $sheet->getCell('K'.$k)->setValue($data[$i]['ben']);
  366. $sheet->getCell('L'.$k)->setValue($data[$i]['judge']);
  367. $sheet->getCell('M'.$k)->setValue($data[$i]['maker']);
  368. $sheet->getCell('N'.$k)->setValue($data[$i]['remark']);
  369. $sheet->getCell('O'.$k)->setValue($data[$i]['0']);
  370. $sheet->getCell('P'.$k)->setValue($data[$i][1]);
  371. $sheet->getCell('Q'.$k)->setValue($data[$i][2]);
  372. $sheet->getCell('R'.$k)->setValue($data[$i][3]);
  373. $sheet->getCell('S'.$k)->setValue($data[$i][4]);
  374. $sheet->getCell('T'.$k)->setValue($data[$i][5]);
  375. $sheet->getCell('U'.$k)->setValue($data[$i][6]);
  376. $sheet->getCell('V'.$k)->setValue($data[$i][7]);
  377. $sheet->getCell('W'.$k)->setValue($data[$i][8]);
  378. $sheet->getCell('X'.$k)->setValue($data[$i][9]);
  379. $sheet->getCell('Y'.$k)->setValue($data[$i][10]);
  380. $sheet->getCell('Z'.$k)->setValue($data[$i][11]);
  381. $sheet->getCell('AA'.$k)->setValue($data[$i][12]);
  382. $sheet->getCell('AB'.$k)->setValue($data[$i][13]);
  383. $sheet->getCell('AC'.$k)->setValue($data[$i][14]);
  384. $sheet->getCell('AD'.$k)->setValue($data[$i][15]);
  385. $sheet->getCell('AE'.$k)->setValue($data[$i][16]);
  386. $sheet->getCell('AF'.$k)->setValue($data[$i][17]);
  387. $sheet->getCell('AG'.$k)->setValue($data[$i][18]);
  388. $sheet->getCell('AH'.$k)->setValue($data[$i][19]);
  389. $sheet->getCell('AI'.$k)->setValue($data[$i][20]);
  390. $sheet->getCell('AJ'.$k)->setValue($data[$i][21]);
  391. $sheet->getCell('AK'.$k)->setValue($data[$i][22]);
  392. $sheet->getCell('AL'.$k)->setValue($data[$i][23]);
  393. $sheet->getCell('AM'.$k)->setValue($data[$i][24]);
  394. $sheet->getCell('AN'.$k)->setValue($data[$i][25]);
  395. //不合格字体设置红色
  396. $start = 'A'.$k; $xend = 'AN'.$k;
  397. if ($data[$i]['judge'] == '不合格'){
  398. $sheet->getStyle("$start:$xend")->getFont()->getColor()
  399. ->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
  400. }
  401. }
  402. //excel文件格式
  403. $filename = '数据报告'.date('ymdhis',time()).'.xlsx';
  404. $filename = iconv("utf-8","gb2312",$filename);
  405. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  406. header('Content-Disposition: attachment;filename="'.$filename.'"');
  407. header('Cache-Control: max-age=0');
  408. header("Content-Type:text/html;charset=utf-8");
  409. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objexcel, 'Xlsx');
  410. $writer->save('php://output');
  411. exit;
  412. }
  413. }