0
0

ConveyPlanRecord.php 10 KB

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