0
0

PatrolRecord.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 PatrolRecord extends Auth {
  9. public function index($mode=1) {
  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. $b = input('start', '', 'trim');
  28. $e = input('end', '', 'trim');
  29. if ($b) {
  30. $b = date('Ymd', strtotime($b));
  31. $map[] = ['pr.create_yyyymmdd', '>=', $b];
  32. }
  33. if ($e) {
  34. $e = date('Ymd', strtotime($e));
  35. $map[] = ['pr.create_yyyymmdd', '<=', $e];
  36. }
  37. $map[] = ['pr.org_id', '=', $this->orgId];
  38. $map[] = ['pr.patrol_mode', '=', $mode];
  39. $map = empty($map) ? true : $map;
  40. //数据查询
  41. $lists = Db::name('patrol_record')
  42. ->alias('pr')
  43. ->field('pr.*,pa.title,pt.title as task_title')
  44. ->join('patrol_task pt','pt.id = pr.patrol_task_id')
  45. ->Leftjoin('address pa', 'pa.id=pr.patrol_addr_id')
  46. ->where($map)
  47. ->limit($start, $length)
  48. ->order($order)
  49. ->select();
  50. foreach ($lists as $k => $v) {
  51. $lists[$k]['task_user'] = Db::name('user')
  52. ->where('id', $v['user_id'])->value('real_name');
  53. $status = 0;
  54. if($v['order_id'] > 0 ){
  55. $order = Db::name('orders')
  56. ->where('id',$v['order_id'])
  57. ->where('quality_type',$v['patrol_mode']+1)
  58. ->whereIn('order_mode',[5,6])->where('del',0)->find();
  59. if($order){
  60. $status = 2;
  61. }else{
  62. $status = 1;
  63. }
  64. }
  65. $lists[$k]['zg_type'] = $status;
  66. $lists[$k]['exception'] = 1;
  67. $checkJson = json_decode($v['check_json'],true);
  68. foreach ($checkJson as $kk=>$vv){
  69. if(isset($vv['forms'])){
  70. foreach ($vv['forms'] as $kkk=>$vvv){
  71. if(isset($vvv['status']) && $vvv['status'] == 0 && $lists[$k]['exception'] == 1){
  72. $lists[$k]['exception'] = 0;
  73. break;
  74. }
  75. }
  76. }
  77. }
  78. }
  79. //数据返回
  80. $where[] = ['pr.org_id', '=', $this->orgId];
  81. $where[] = ['pr.patrol_mode', '=', $mode];
  82. $totalCount =
  83. Db::name('patrol_record')
  84. ->alias('pr')
  85. ->field('pr.*,pa.title,pt.title as task_title')
  86. ->join('patrol_task pt','pt.id = pr.patrol_task_id')
  87. ->Leftjoin('address pa', 'pa.id=pr.patrol_addr_id')
  88. ->where($map)->count();
  89. $totalPage = ceil($totalCount / $length);
  90. $result['page'] = $page;
  91. $result['total'] = $totalPage;
  92. $result['records'] = $totalCount;
  93. $result['rows'] = $lists;
  94. return json($result);
  95. } else {
  96. $plans = model('PatrolPlan')->getListByMode($this->orgId,$mode);
  97. $this->assign('plans',$plans);
  98. $modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
  99. $this->assign('meta_title', $modeName . '记录列表');
  100. $this->assign('mode', $mode);
  101. return $this->fetch();
  102. }
  103. }
  104. // 详情
  105. public function details($id, $mode) {
  106. $meta_title = '记录详情';
  107. if (!$id) {
  108. $this->error('参数错误');
  109. }
  110. $info = Db::name('patrol_record')->where('id', $id)->find();
  111. $info['addr'] = Db::name('address')
  112. ->where('id', $info['patrol_addr_id'])->value('title');
  113. $info['task_name'] = Db::name('patrol_task')
  114. ->where('id', $info['patrol_task_id'])->value('title');
  115. $info['in_order'] = Db::name('patrol_task')
  116. ->where('id', $info['patrol_task_id'])->value('in_order');
  117. $info['task_addr_form'] = Db::name('patrol_addr_form')
  118. ->alias('a')
  119. ->join('patrol_task_addr b','a.id = b.patrol_form_id')
  120. ->where('b.id', $info['patrol_addr_form_id'])->value('a.title');
  121. $info['task_user'] = Db::name('user')->where('id', $info['user_id'])->value('real_name');
  122. $info['check_json'] = json_decode($info['check_json'], true);
  123. $info['images'] = explode(',',$info['images']);
  124. $status = 0;
  125. if($info['order_id'] > 0 ){
  126. $order = Db::name('orders')
  127. ->where('id',$info['order_id'])
  128. ->where('quality_type',$info['patrol_mode']+1)
  129. ->whereIn('order_mode',[5,6])->where('del',0)->find();
  130. if($order){
  131. $status = 2;
  132. }else{
  133. $status = 1;
  134. }
  135. }
  136. $info['zg_type'] = $status;
  137. $this->assign('meta_title', $meta_title);
  138. $this->assign('info', $info);
  139. $this->assign('mode', $mode);
  140. return $this->fetch();
  141. }
  142. //excel导出
  143. public function export($mode) {
  144. //获取excel表名
  145. $tableName = Db::name('patrol_mode')->where('id', $mode)->value('name');
  146. $meta_title = $tableName . ' 记录导出';
  147. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  148. if (request()->isGet()) {
  149. $b = input('start', '', 'trim');
  150. $e = input('end', '', 'trim');
  151. if ($b) {
  152. $b = date('Ymd', strtotime($b));
  153. $where[] = ['pr.create_yyyymmdd', '>=', $b];
  154. }
  155. if ($e) {
  156. $e = date('Ymd', strtotime($e));
  157. $where[] = ['pr.create_yyyymmdd', '<=', $e];
  158. }
  159. $title = input('title', '', 'trim');
  160. if ($title) {
  161. $where[] = ['pa.title', 'like', '%' . $title . '%'];
  162. }
  163. $plan_id = input('plan_id','','trim');
  164. if($plan_id != ''){
  165. $where[] = ['pt.plan_id','=',$plan_id];
  166. }
  167. $where[] = ['pr.patrol_mode', '=', $mode];
  168. $where[] = ['pr.org_id', '=', $this->orgId];
  169. $ret = Db::name('patrol_record')
  170. ->alias('pr')
  171. ->field('pr.*,pa.title,pt.title as task_title')
  172. ->join('patrol_task pt','pt.id = pr.patrol_task_id')
  173. ->Leftjoin('address pa', 'pa.id=pr.patrol_addr_id')
  174. ->where($where)->select();
  175. foreach ($ret as $k => $v) {
  176. $ret[$k]['addr'] = Db::name('address')->where('id', $v['patrol_addr_id'])->value('title');
  177. $ret[$k]['task_title'] = Db::name('patrol_task')->where('id', $v['patrol_task_id'])->value('title');
  178. $ret[$k]['task_user'] = Db::name('user')->where('id', $v['user_id'])->value('real_name');
  179. }
  180. //实例化PHPExcel类
  181. $objPHPExcel =new \PHPExcel();
  182. //激活当前的sheet表
  183. $objPHPExcel->setActiveSheetIndex(0);
  184. //设置表格头(即excel表格的第一行)
  185. $objPHPExcel->setActiveSheetIndex(0)
  186. ->setCellValue('A1', '编号')
  187. ->setCellValue('B1', '执行人')
  188. ->setCellValue('C1', '计划')
  189. ->setCellValue('D1', '地点')
  190. ->setCellValue('E1', '汇报内容')
  191. ->setCellValue('F1', '提交日期')
  192. ->setCellValue('G1', '检查项');
  193. // 设置表格头水平居中
  194. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  195. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  196. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  197. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  198. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  199. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  200. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
  201. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  202. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
  203. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  204. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
  205. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  206. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
  207. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  208. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
  209. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  210. //设置列水平居中
  211. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  212. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  213. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  214. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  215. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  216. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  217. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
  218. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  219. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
  220. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  221. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
  222. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  223. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
  224. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  225. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
  226. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  227. //设置单元格宽度
  228. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  229. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(30);
  230. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(30);
  231. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(30);
  232. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(30);
  233. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(30);
  234. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(90);
  235. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(30);
  236. //循环刚取出来的数组,将数据逐一添加到excel表格。
  237. for ($i = 0; $i < count($ret); $i++) {
  238. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $ret[$i]['id']);//ID
  239. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $ret[$i]['task_user']);//执行人
  240. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $ret[$i]['task_title']);//名称
  241. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $ret[$i]['addr']);//地点
  242. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $ret[$i]['content']);//汇报内容
  243. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $ret[$i]['create_time']);//提交时间
  244. $result = json_decode($ret[$i]['check_json'], true);
  245. /*foreach ($result as $kk => $vv) {
  246. $ss = $vv['title'] . ":";
  247. if ($vv['type'] == 1) {
  248. $ss .= $vv['result'];
  249. }
  250. else {
  251. if ($vv['result'] == 1) {
  252. $ss .= '☑';
  253. }
  254. else {
  255. $ss .= '□';
  256. }
  257. }
  258. $str[] = $ss;
  259. }*/
  260. $str = [];
  261. foreach ($result as $rk => $rv) {
  262. if(isset($rv['forms'])){
  263. foreach ($rv['forms'] as $kk => $vv) {
  264. $ss = $vv['title'] . ":";
  265. if ($vv['type'] == 1) {
  266. $ss .= $vv['result'];
  267. }
  268. else {
  269. if ($vv['result'] == 1) {
  270. $ss .= '☑';
  271. }
  272. else {
  273. $ss .= '□';
  274. }
  275. }
  276. $str[] = $ss;
  277. }
  278. }
  279. }
  280. $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), implode(' ; ', $str));//检查项
  281. }
  282. //设置保存的Excel表格名称
  283. $filename = $tableName . '记录_' . date('YmdHis', time()) . '.xls';
  284. //设置当前激活的sheet表格名称
  285. $objPHPExcel->getActiveSheet()->setTitle($tableName);
  286. //设置浏览器窗口下载表格
  287. ob_end_clean();
  288. header("Content-Type: application/force-download");
  289. header("Content-Type: application/octet-stream");
  290. header("Content-Type: application/download");
  291. header('Content-Disposition:inline;filename="' . $filename);
  292. //生成excel文件
  293. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  294. //下载文件在浏览器窗口
  295. return $objWriter->save('php://output');
  296. }
  297. }
  298. }