0
0

GreenTaskRecord.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 GreenTaskRecord 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[] = ['ga.title', 'like', '%' . $title . '%'];
  22. }
  23. $plan_id = input('plan_id','','trim');
  24. if($plan_id != ''){
  25. $map[] = ['gt.plan_id','=',$plan_id];
  26. }
  27. $map[] = ['gtr.org_id', '=', $this->orgId];
  28. $map = empty($map) ? true : $map;
  29. //数据查询
  30. $lists = Db::name('green_task_record')
  31. ->alias('gtr')
  32. ->field('gtr.*,ga.title,gt.title as task_title')
  33. ->join('green_task gt','gt.id = gtr.green_task_id')
  34. ->Leftjoin('green_addr ga', 'ga.id=gtr.green_addr_id')
  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('green_task_record')
  45. ->alias('gtr')
  46. ->field('gtr.*,ga.title,gt.title as task_title')
  47. ->join('green_task gt','gt.id = gtr.green_task_id')
  48. ->Leftjoin('green_addr ga', 'ga.id=gtr.green_addr_id')
  49. ->where($map)
  50. ->count();
  51. $totalPage = ceil($totalCount / $length);
  52. $result['page'] = $page;
  53. $result['total'] = $totalPage;
  54. $result['records'] = $totalCount;
  55. $result['rows'] = $lists;
  56. return json($result);
  57. } else {
  58. $plans = model('GreenPlan')->getList($this->orgId);
  59. $this->assign('plans',$plans);
  60. $this->assign('meta_title', '绿化养护任务记录列表');
  61. return $this->fetch();
  62. }
  63. }
  64. // 详情
  65. public function details($id) {
  66. $meta_title = '记录详情';
  67. if (!$id) {
  68. $this->error('参数错误');
  69. }
  70. $info = Db::name('green_task_record')->where('id', $id)->find();
  71. $info['addr'] = Db::name('green_addr')
  72. ->where('id', $info['green_addr_id'])->value('title');
  73. $info['task_name'] = Db::name('green_task')
  74. ->where('id', $info['green_task_id'])->value('title');
  75. $info['task_addr_form'] = Db::name('green_addr_form')
  76. ->alias('a')
  77. ->join('green_task_addr b','a.id = b.green_form_id')
  78. ->where('b.id', $info['green_task_addr_id'])
  79. ->value('a.title');
  80. $info['task_user'] = Db::name('user')->where('id', $info['user_id'])->value('real_name');
  81. $info['check_json'] =$info['check_json']? json_decode($info['check_json'], true):[];
  82. $this->assign('meta_title', $meta_title);
  83. $this->assign('info', $info);
  84. return $this->fetch();
  85. }
  86. //excel导出
  87. public function export() {
  88. //获取excel表名
  89. $meta_title = '绿化养护任务记录导出';
  90. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  91. if (request()->isPost()) {
  92. $start = input('start');
  93. $end = input('end');
  94. $where[] = ['create_yyyymmdd', '>=', $start];
  95. $where[] = ['create_yyyymmdd', '<=', $end];
  96. $where[] = ['org_id', '=', $this->orgId];
  97. $ret = Db::name('green_task_record')->where($where)->select();
  98. foreach ($ret as $k => $v) {
  99. $ret[$k]['addr'] = Db::name('green_addr')->where('id', $v['green_addr_id'])->value('title');
  100. $ret[$k]['task_title'] = Db::name('green_task')->where('id', $v['green_task_id'])->value('title');
  101. $ret[$k]['task_user'] = Db::name('user')->where('id', $v['user_id'])->value('real_name');
  102. }
  103. //实例化PHPExcel类
  104. $objPHPExcel =new \PHPExcel();
  105. //激活当前的sheet表
  106. $objPHPExcel->setActiveSheetIndex(0);
  107. //设置表格头(即excel表格的第一行)
  108. $objPHPExcel->setActiveSheetIndex(0)
  109. ->setCellValue('A1', '编号')
  110. ->setCellValue('B1', '执行人')
  111. ->setCellValue('C1', '计划')
  112. ->setCellValue('D1', '地点')
  113. ->setCellValue('E1', '汇报内容')
  114. ->setCellValue('F1', '检查项');
  115. // 设置表格头水平居中
  116. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  117. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  118. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  119. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  120. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  121. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  122. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
  123. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  124. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
  125. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  126. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
  127. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  128. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
  129. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  130. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
  131. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  132. //设置列水平居中
  133. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  134. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  135. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  136. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  137. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  138. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  139. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
  140. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  141. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
  142. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  143. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
  144. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  145. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
  146. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  147. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
  148. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  149. //设置单元格宽度
  150. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  151. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(30);
  152. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(30);
  153. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(30);
  154. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(30);
  155. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(60);
  156. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(30);
  157. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(30);
  158. //循环刚取出来的数组,将数据逐一添加到excel表格。
  159. for ($i = 0; $i < count($ret); $i++) {
  160. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $ret[$i]['id']);//ID
  161. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $ret[$i]['task_user']);//执行人
  162. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $ret[$i]['task_title']);//名称
  163. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $ret[$i]['addr']);//地点
  164. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $ret[$i]['content']);//汇报内容
  165. $result = json_decode($v['check_json'], true);
  166. $str = array();
  167. foreach ($result as $kk => $vv) {
  168. $ss = $vv['title'] . ":";
  169. if ($vv['type'] == 1) {
  170. $ss .= $vv['result'];
  171. }
  172. else {
  173. if ($vv['result'] == 1) {
  174. $ss .= '☑';
  175. }
  176. else {
  177. $ss .= '□';
  178. }
  179. }
  180. $str[] = $ss;
  181. }
  182. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), implode(' ; ', $str));//检查项
  183. }
  184. //设置保存的Excel表格名称
  185. $filename = '绿化养护任务记录_' . date('YmdHis', time()) . '.xls';
  186. //设置当前激活的sheet表格名称
  187. $objPHPExcel->getActiveSheet()->setTitle('绿化养护任务记录');
  188. //设置浏览器窗口下载表格
  189. ob_end_clean();
  190. header("Content-Type: application/force-download");
  191. header("Content-Type: application/octet-stream");
  192. header("Content-Type: application/download");
  193. header('Content-Disposition:inline;filename="' . $filename);
  194. //生成excel文件
  195. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  196. //下载文件在浏览器窗口
  197. return $objWriter->save('php://output');
  198. }
  199. $this->assign('s',date('Ymd'));
  200. $this->assign('e',date('Ymd',time()+(3*86400)));
  201. $this->assign('meta_title', $meta_title);
  202. return $this->fetch();
  203. }
  204. }