DailyRecord.php 12 KB

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