QualityRecord.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace app\admin\controller;
  3. use function think\__include_file;
  4. use think\App;
  5. use think\Db;
  6. use think\Env;
  7. use think\facade\Url;
  8. class QualityRecord extends Auth {
  9. public function index() {
  10. if (request()->isAjax()) {
  11. //分页参数
  12. $length = input('rows', 10, 'intval'); //每页条数
  13. $page = input('page', 1, 'intval'); //第几页
  14. $start = ($page - 1) * $length; //分页开始位置
  15. //排序
  16. $sortRow = input('sidx', 'sort', 'trim'); //排序列
  17. $sort = input('sord', 'asc', 'trim'); //排序方式
  18. $order = $sortRow . ' ' . $sort . ' ,id desc';
  19. $title = input('title', '', 'trim');
  20. if ($title) {
  21. $map[] = ['pa.title', 'like', '%' . $title . '%'];
  22. }
  23. $plan_id = input('plan_id','','trim');
  24. if($plan_id != ''){
  25. $map[] = ['pt.plan_id','=',$plan_id];
  26. }
  27. $map[] = ['pr.org_id', '=', $this->orgId];
  28. $map = empty($map) ? true : $map;
  29. //数据查询
  30. $lists = Db::name('quality_record')
  31. ->alias('pr')
  32. ->join('quality_task pt','pt.id = pr.quality_task_id')
  33. ->Leftjoin('quality_group pa', 'pa.id=pr.quality_group_id')
  34. ->field('pr.*,pa.title,pt.title as task_title')
  35. ->where($map)
  36. ->limit($start, $length)
  37. ->order($order)
  38. ->select();
  39. foreach ($lists as $k => $v) {
  40. $lists[$k]['task_user'] = Db::name('user')
  41. ->where('id', $v['user_id'])->value('real_name');
  42. }
  43. //数据返回
  44. $totalCount = Db::name('quality_record')
  45. ->alias('pr')
  46. ->join('quality_task pt','pt.id = pr.quality_task_id')
  47. ->Leftjoin('quality_group pa', 'pa.id=pr.quality_group_id')
  48. ->where($map)->count();
  49. $totalPage = ceil($totalCount / $length);
  50. $result['page'] = $page;
  51. $result['total'] = $totalPage;
  52. $result['records'] = $totalCount;
  53. $result['rows'] = $lists;
  54. return json($result);
  55. } else {
  56. $plans = model('QualityPlan')->getListByMode($this->orgId);
  57. $this->assign('plans',$plans);
  58. $this->assign('meta_title','品控记录列表');
  59. return $this->fetch();
  60. }
  61. }
  62. // 详情
  63. public function details($id) {
  64. $meta_title = '记录详情';
  65. if (!$id) {
  66. $this->error('参数错误');
  67. }
  68. $info = Db::name('quality_record')->where('id', $id)->find();
  69. $info['addr'] = Db::name('quality_group')->where('id', $info['quality_group_id'])->value('title');
  70. $info['task_name'] = Db::name('quality_task')->where('id', $info['quality_task_id'])->value('title');
  71. $info['task_addr_form'] = Db::name('quality_group')
  72. ->alias('a')
  73. ->join('quality_task_addr b','a.id = b.quality_group_id')
  74. ->where('b.id', $info['quality_addr_id'])->value('a.title');
  75. $info['task_user'] = Db::name('user')->where('id', $info['user_id'])->value('real_name');
  76. $info['check_json'] = json_decode($info['check_json'], true);
  77. $info['images'] = explode(',',$info['images']);
  78. $this->assign('meta_title', $meta_title);
  79. $this->assign('info', $info);
  80. // $this->assign('mode', $mode);
  81. return $this->fetch();
  82. }
  83. //excel导出
  84. public function export() {
  85. //获取excel表名
  86. $meta_title = '品控记录导出';
  87. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  88. if (request()->isPost()) {
  89. $start = input('start');
  90. $end = input('end');
  91. $where[] = ['create_yyyymmdd', '>=', $start];
  92. $where[] = ['create_yyyymmdd', '<=', $end];
  93. $where[] = ['org_id', '=', $this->orgId];
  94. $ret = Db::name('quality_record')->where($where)->select();
  95. foreach ($ret as $k => $v) {
  96. $ret[$k]['addr'] = Db::name('quality_group')->where('id', $v['quality_group_id'])->value('title');
  97. $ret[$k]['task_title'] = Db::name('quality_task')->where('id', $v['quality_task_id'])->value('title');
  98. $ret[$k]['task_user'] = Db::name('user')->where('id', $v['user_id'])->value('real_name');
  99. }
  100. //实例化PHPExcel类
  101. $objPHPExcel =new \PHPExcel();
  102. //激活当前的sheet表
  103. $objPHPExcel->setActiveSheetIndex(0);
  104. //设置表格头(即excel表格的第一行)
  105. $objPHPExcel->setActiveSheetIndex(0)
  106. ->setCellValue('A1', '编号')
  107. ->setCellValue('B1', '执行人')
  108. ->setCellValue('C1', '任务')
  109. ->setCellValue('D1', '检查内容')
  110. ->setCellValue('E1', '汇报内容')
  111. ->setCellValue('F1', '检查项');
  112. // 设置表格头水平居中
  113. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  114. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  115. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  116. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  117. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  118. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  119. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
  120. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  121. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
  122. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  123. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
  124. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  125. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
  126. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  127. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
  128. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  129. //设置列水平居中
  130. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  131. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  132. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  133. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  134. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  135. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  136. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
  137. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  138. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
  139. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  140. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
  141. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  142. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
  143. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  144. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
  145. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  146. //设置单元格宽度
  147. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  148. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(30);
  149. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(30);
  150. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(30);
  151. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(30);
  152. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(60);
  153. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(30);
  154. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(30);
  155. //循环刚取出来的数组,将数据逐一添加到excel表格。
  156. for ($i = 0; $i < count($ret); $i++) {
  157. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $ret[$i]['id']);//ID
  158. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $ret[$i]['task_user']);//执行人
  159. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $ret[$i]['task_title']);//名称
  160. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $ret[$i]['addr']);//地点
  161. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $ret[$i]['content']);//汇报内容
  162. $result = json_decode($v['check_json'], true);
  163. $str = array();
  164. /*foreach ($result as $kk => $vv) {
  165. $ss = $vv['title'] . ":";
  166. if ($vv['type'] == 1) {
  167. $ss .= $vv['result'];
  168. }
  169. else {
  170. if ($vv['result'] == 1) {
  171. $ss .= '☑';
  172. }
  173. else {
  174. $ss .= '□';
  175. }
  176. }
  177. $str[] = $ss;
  178. }*/
  179. foreach ($result as $rk => $rv) {
  180. foreach ($rv['forms'] as $kk => $vv) {
  181. $ss = $vv['title'] . ":";
  182. if ($vv['type'] == 1) {
  183. $ss .= $vv['result'];
  184. }
  185. else {
  186. if ($vv['result'] == 1) {
  187. $ss .= '☑';
  188. }
  189. else {
  190. $ss .= '□';
  191. }
  192. }
  193. $str[] = $ss;
  194. }
  195. }
  196. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), implode(' ; ', $str));//检查项
  197. }
  198. //设置保存的Excel表格名称
  199. $filename = '品控记录_' . date('YmdHis', time()) . '.xls';
  200. //设置当前激活的sheet表格名称
  201. $objPHPExcel->getActiveSheet()->setTitle('品控记录');
  202. //设置浏览器窗口下载表格
  203. ob_end_clean();
  204. header("Content-Type: application/force-download");
  205. header("Content-Type: application/octet-stream");
  206. header("Content-Type: application/download");
  207. header('Content-Disposition:inline;filename="' . $filename);
  208. //生成excel文件
  209. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  210. //下载文件在浏览器窗口
  211. return $objWriter->save('php://output');
  212. }
  213. $this->assign('meta_title', $meta_title);
  214. return $this->fetch();
  215. }
  216. }