MeetingApply.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. class MeetingApply extends Auth
  5. {
  6. public function index(){
  7. if(request()->isAjax()){
  8. //分页参数
  9. $length = input('rows',10,'intval'); //每页条数
  10. $page = input('page',1,'intval'); //第几页
  11. $start = ($page - 1) * $length; //分页开始位置
  12. //排序
  13. $sortRow = input('sidx','id','trim'); //排序列
  14. $sort = input('sord','desc','trim'); //排序方式
  15. $order = $sortRow.' '.$sort;
  16. $title = input('title','','trim');
  17. if($title){
  18. $map[] = ['u.real_name','like','%'.$title.'%'];
  19. }
  20. $room_id = input('room_id','','trim');
  21. if($room_id){
  22. $map[] = ['ma.meeting_room_id','=',$room_id];
  23. }
  24. $map[] = ['ma.del','=',0];
  25. $map[] = ['ma.org_id','=',cur_org_id()];
  26. $map= empty($map) ? true: $map;
  27. //数据查询
  28. $lists = db('meeting_apply')
  29. ->alias('ma')
  30. ->field('ma.*,u.real_name')
  31. ->join('user u','u.id=ma.user_id')
  32. ->limit($start,$length)
  33. ->where($map)
  34. ->order('id desc')
  35. ->select();
  36. foreach ($lists as $k=>$v){
  37. $lists[$k]['roomName'] = db('meeting_room')->where('id',$v['meeting_room_id'])->value('title');
  38. }
  39. //数据返回
  40. $totalCount = db('meeting_apply')->alias('ma')
  41. ->field('ma.*,u.real_name')
  42. ->join('user u','u.id=ma.user_id')->where($map)->count();
  43. $totalPage = ceil($totalCount/$length);
  44. $result['page'] = $page;
  45. $result['total'] = $totalPage;
  46. $result['records'] = $totalCount;
  47. $result['rows'] = $lists;
  48. return json($result);
  49. }else{
  50. $roomList = db('meeting_room')->where('org_id',cur_org_id())->where('enable',1)->where('del',0)->select();
  51. $this->assign('roomList',$roomList);
  52. return $this->fetch();
  53. }
  54. }
  55. /**
  56. * 删除记录
  57. * @param int $id
  58. */
  59. public function del($id=0){
  60. if(!$id){
  61. $this->error('参数错误');
  62. }
  63. $res = db('meeting_apply')->where('id',$id)->setField('del',1);
  64. if($res){
  65. $this->success('删除成功');
  66. }else{
  67. $this->error('删除失败');
  68. }
  69. }
  70. /**
  71. * 改变字段值
  72. * @param int $fv
  73. * @param string $fn
  74. * @param int $fv
  75. */
  76. public function changeField($id=0,$fn='',$fv=0){
  77. if(!$fn||!$id){
  78. $this->error('参数错误');
  79. }
  80. $res = db('meeting_apply')->where('id',$id)->setField($fn,$fv);
  81. if($res){
  82. $this->success('操作成功');
  83. }else{
  84. $this->error('操作失败');
  85. }
  86. }
  87. //同意
  88. public function agree($id=0){
  89. if(request()->isPost()){
  90. $res = model('MeetingApply')->agree();
  91. if($res){
  92. $this->success('操作成功',url('index'));
  93. }else{
  94. $this->error(model('MeetingApply')->getError());
  95. }
  96. }else{
  97. if($id){
  98. $info = db('meeting_apply')->where('id',$id)->find();
  99. $this->assign('info',$info);
  100. }
  101. $userList = model('work_type_mode')->getRolesUserApp(12,cur_org_id(),-1);
  102. foreach ($userList as $k=>$v){
  103. $userList[$k]['title']= $v['real_name'];
  104. }
  105. $this->assign('userList',$userList);
  106. return $this->fetch();
  107. }
  108. }
  109. //拒绝
  110. public function refuse($id=0){
  111. if(request()->isPost()){
  112. $res = model('MeetingApply')->refuse();
  113. if($res){
  114. $this->success('操作成功',url('index'));
  115. }else{
  116. $this->error(model('MeetingApply')->getError());
  117. }
  118. }else{
  119. if($id){
  120. $info = db('meeting_apply')->where('id',$id)->find();
  121. $this->assign('info',$info);
  122. }
  123. return $this->fetch();
  124. }
  125. }
  126. public function details($id=0){
  127. if($id < 1 ){
  128. $this->error('参数错误');
  129. }
  130. $info = db('meeting_apply')->where('id',$id)->find();
  131. $info['meeting_room'] = db('meeting_room')->where('id',$info['meeting_room_id'])->value('title');
  132. //申请人
  133. $info['apply_user'] = db('user')->where('id',$info['user_id'])->value('real_name');
  134. //审批人
  135. $info['approver_user'] = db('user')->where('id',$info['approver'])->value('real_name');
  136. //参会设备
  137. $meeting_devices = db('meeting_device')->whereIn('id',$info['meeting_devices'])->where('del',0)->select();
  138. $deviceName = [];
  139. foreach ($meeting_devices as $k=>$v){
  140. $deviceName[] = $v['title'];
  141. }
  142. $info['meeting_devices'] = $deviceName?implode(',',$deviceName):'';
  143. //参会人员
  144. $cUser= db('user')->whereIn('id',$info['user_ids'])->where('del',0)->select();
  145. $userName = [];
  146. foreach ($cUser as $k=>$v){
  147. $userName[] = $v['real_name'];
  148. }
  149. $info['user_name'] = $userName?implode(',',$userName):'';
  150. $this->assign('info',$info);
  151. return $this->fetch();
  152. }
  153. //excel导出
  154. public function export() {
  155. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  156. if (request()->isGet()) {
  157. $where[] = ['org_id', '=', $this->orgId];
  158. $where[] = ['del', '=', 0];
  159. $ret = Db::name('meeting_apply')->where($where)->select();
  160. foreach ($ret as $k => $v) {
  161. $ret[$k]['apply_user'] = db('user')->where('id',$v['user_id'])->value('real_name');
  162. $ret[$k]['room_name'] = db('meeting_room')->where('id',$v['meeting_room_id'])->value('title');
  163. }
  164. //实例化PHPExcel类
  165. $objPHPExcel =new \PHPExcel();
  166. //激活当前的sheet表
  167. $objPHPExcel->setActiveSheetIndex(0);
  168. //设置表格头(即excel表格的第一行)
  169. $objPHPExcel->setActiveSheetIndex(0)
  170. ->setCellValue('A1', '编号')
  171. ->setCellValue('B1', '申请人')
  172. ->setCellValue('B1', '会议室名称')
  173. ->setCellValue('C1', '开始时间')
  174. ->setCellValue('D1', '结束时间')
  175. ->setCellValue('E1', '状态');
  176. // 设置表格头水平居中
  177. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  178. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  179. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  180. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  181. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  182. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  183. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
  184. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  185. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
  186. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  187. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
  188. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  189. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
  190. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  191. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
  192. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  193. //设置列水平居中
  194. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  195. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  196. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  197. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  198. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  199. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  200. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
  201. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  202. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
  203. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  204. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
  205. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  206. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
  207. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  208. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
  209. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  210. //设置单元格宽度
  211. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
  212. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(30);
  213. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(30);
  214. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(30);
  215. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(30);
  216. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(60);
  217. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(30);
  218. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(30);
  219. //循环刚取出来的数组,将数据逐一添加到excel表格。
  220. for ($i = 0; $i < count($ret); $i++) {
  221. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $ret[$i]['id']);//ID
  222. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $ret[$i]['apply_user']);
  223. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $ret[$i]['room_name']);
  224. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $ret[$i]['start_time']);
  225. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $ret[$i]['end_time']);
  226. if($ret[$i]['status'] == 0){
  227. $objPHPExcel->getActiveSheet()->setCellValue('f' . ($i + 2), '审核中');
  228. }elseif ($ret[$i]['status'] == 1){
  229. $objPHPExcel->getActiveSheet()->setCellValue('f' . ($i + 2), '同意');
  230. }elseif ($ret[$i]['status'] == 2){
  231. $objPHPExcel->getActiveSheet()->setCellValue('f' . ($i + 2), '拒绝');
  232. }elseif ($ret[$i]['status'] == 3){
  233. $objPHPExcel->getActiveSheet()->setCellValue('f' . ($i + 2), '取消');
  234. }
  235. }
  236. //设置保存的Excel表格名称
  237. $filename = '会议室申请记录_' . date('YmdHis', time()) . '.xls';
  238. //设置当前激活的sheet表格名称
  239. $objPHPExcel->getActiveSheet()->setTitle('会议室申请');
  240. //设置浏览器窗口下载表格
  241. ob_end_clean();
  242. header("Content-Type: application/force-download");
  243. header("Content-Type: application/octet-stream");
  244. header("Content-Type: application/download");
  245. header('Content-Disposition:inline;filename="' . $filename);
  246. //生成excel文件
  247. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  248. //下载文件在浏览器窗口
  249. return $objWriter->save('php://output');
  250. }
  251. }
  252. }