ConveyPlanRecord.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. class ConveyPlanRecord extends Auth
  6. {
  7. public function __construct(App $app = null) {
  8. parent::__construct($app);
  9. $this->table = 'convey_plan_record';
  10. }
  11. public function index(){
  12. if(request()->isAjax()){
  13. //分页参数
  14. $length = input('rows',10,'intval'); //每页条数
  15. $page = input('page',1,'intval'); //第几页
  16. $start = ($page - 1) * $length; //分页开始位置
  17. //排序
  18. $sortRow = input('sidx','id','trim'); //排序列
  19. $sort = input('sord','desc','trim'); //排序方式
  20. $order = $sortRow.' '.$sort;
  21. $title = input('real_name','','trim');
  22. if($title){
  23. $user = Db::name('user')
  24. ->alias('u')
  25. ->join('user_org o','o.user_id=u.id')
  26. ->where('u.del',0)
  27. ->where('u.enable',1)
  28. ->where('u.real_name','like','%'.$title.'%')
  29. ->where('o.org_id',$this->orgId)
  30. ->column('u.id');
  31. if(empty($user)){
  32. $map[] = ['user_id','=',0];
  33. }else{
  34. $map[] = ['user_id','in',$user];
  35. }
  36. }
  37. $addr = input('addr','','trim');
  38. if($addr){
  39. $map[] = ['addr_id','=',$addr];
  40. }
  41. $s = input('start','','trim');
  42. if($s){
  43. $ss = date('Ymd',strtotime($s));
  44. $map[] = ['create_yyyymmdd','>=',$ss];
  45. }
  46. $e = input('end','','trim');
  47. if($e){
  48. $ee = date('Ymd',strtotime($e));
  49. $map[] = ['create_yyyymmdd','<=',$ee];
  50. }
  51. $map[] = ['org_id','=',$this->orgId];
  52. $map[] = ['type','=',0];
  53. $map= empty($map) ? true: $map;
  54. //数据查询
  55. $lists = db($this->table)->where($map)->limit($start,$length)->order($order)->select();
  56. foreach ($lists as $k=>$v){
  57. $lists[$k]['real_name'] = Db::name('user')
  58. ->where('id',$v['user_id'])
  59. ->value('real_name');
  60. $lists[$k]['address_title'] = Db::name('address')
  61. ->where('id',$v['addr_id'])
  62. ->value('title');
  63. }
  64. //数据返回
  65. $totalCount = db($this->table)->where($map)->count();
  66. $totalPage = ceil($totalCount/$length);
  67. $result['page'] = $page;
  68. $result['total'] = $totalPage;
  69. $result['records'] = $totalCount;
  70. $result['rows'] = $lists;
  71. return json($result);
  72. }else{
  73. $address = (new \app\common\model\Address())->getListByType(2,$this->orgId);
  74. $this->assign('address',$address);
  75. return $this->fetch();
  76. }
  77. }
  78. //excel导出
  79. public function export() {
  80. $meta_title = '循环签到记录';
  81. if (request()->isGet()) {
  82. $sortRow = input('sidx','id','trim'); //排序列
  83. $sort = input('sord','desc','trim'); //排序方式
  84. $order = $sortRow.' '.$sort;
  85. $title = input('real_name','','trim');
  86. if($title){
  87. $user = Db::name('user')
  88. ->alias('u')
  89. ->join('user_org o','o.user_id=u.id')
  90. ->where('u.del',0)
  91. ->where('u.enable',1)
  92. ->where('u.real_name','like','%'.$title.'%')
  93. ->where('o.org_id',$this->orgId)
  94. ->column('u.id');
  95. if(empty($user)){
  96. $map[] = ['user_id','=',0];
  97. }else{
  98. $map[] = ['user_id','in',$user];
  99. }
  100. }
  101. $addr = input('addr','','trim');
  102. if($addr){
  103. $map[] = ['addr_id','=',$addr];
  104. }
  105. $s = input('start','','trim');
  106. if($s){
  107. $ss = date('Ymd',strtotime($s));
  108. $map[] = ['create_yyyymmdd','>=',$ss];
  109. }
  110. $e = input('end','','trim');
  111. if($e){
  112. $ee = date('Ymd',strtotime($e));
  113. $map[] = ['create_yyyymmdd','<=',$ee];
  114. }
  115. $map[] = ['org_id','=',$this->orgId];
  116. $map= empty($map) ? true: $map;
  117. //数据查询
  118. $lists = db($this->table)->where($map)->order($order)->select();
  119. foreach ($lists as $k=>$v){
  120. $lists[$k]['real_name'] = Db::name('user')
  121. ->where('id',$v['user_id'])
  122. ->value('real_name');
  123. $lists[$k]['address_title'] = Db::name('address')
  124. ->where('id',$v['addr_id'])
  125. ->value('title');
  126. }
  127. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  128. //实例化PHPExcel类
  129. $objPHPExcel = new \PHPExcel();
  130. //激活当前的sheet表
  131. $objPHPExcel->setActiveSheetIndex(0);
  132. //设置表格头(即excel表格的第一行)
  133. $objPHPExcel->setActiveSheetIndex(0)
  134. ->setCellValue('A1', 'id')
  135. ->setCellValue('B1', '签到人')
  136. ->setCellValue('C1', '位置')
  137. ->setCellValue('D1', '时间');
  138. // 设置表格头水平居中
  139. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  140. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  141. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  142. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  143. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  144. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  145. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
  146. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  147. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
  148. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  149. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
  150. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  151. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
  152. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  153. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
  154. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  155. //设置列水平居中
  156. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  157. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  158. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  159. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  160. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  161. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  162. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
  163. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  164. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
  165. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  166. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
  167. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  168. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
  169. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  170. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
  171. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  172. //设置单元格宽度
  173. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  174. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
  175. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
  176. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
  177. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
  178. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(50);
  179. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
  180. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
  181. //循环刚取出来的数组,将数据逐一添加到excel表格。
  182. for ($i = 0; $i < count($lists); $i++) {
  183. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $lists[$i]['id']);
  184. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $lists[$i]['real_name']);
  185. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $lists[$i]['address_title']);
  186. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $lists[$i]['create_time']);
  187. }
  188. //设置保存的Excel表格名称
  189. $filename = $meta_title.'_' . date('YmdHis', time()) . '.xls';
  190. //设置当前激活的sheet表格名称
  191. $objPHPExcel->getActiveSheet()->setTitle($meta_title);
  192. //设置浏览器窗口下载表格
  193. ob_end_clean();
  194. header("Content-Type: application/force-download");
  195. header("Content-Type: application/octet-stream");
  196. header("Content-Type: application/download");
  197. header('Content-Disposition:inline;filename="' . $filename);
  198. //生成excel文件
  199. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  200. //下载文件在浏览器窗口
  201. return $objWriter->save('php://output');
  202. }
  203. }
  204. }