MeetingRoom.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class MeetingRoom extends Base
  5. {
  6. protected $createTime = 'create_time';
  7. public function updates(){
  8. $data = request()->post();
  9. $data['org_id'] = cur_org_id();
  10. $result = validate('MeetingRoom')->check($data,[],'');
  11. if(true !== $result){
  12. $this->error = validate('MeetingRoom')->getError();
  13. return false;
  14. }
  15. $id = $data['id'];
  16. unset($data['id']);
  17. if($id > 0){
  18. $data['update_time'] = date('Y-m-d H:i:s');
  19. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  20. }else{
  21. $data['create_time'] = date('Y-m-d H:i:s');
  22. $ret = $this->allowField(true)->save($data);
  23. }
  24. if(!$ret){
  25. $this->error = '操作失败';
  26. return false;
  27. }
  28. return true;
  29. }
  30. //app接口
  31. public function checkApprover($userId,$orgId){
  32. $info = db('meeting_room')->where('enable',1)->where('del',0)->where('approver',$userId)->where('org_id',$orgId)->find();
  33. if(!$info){
  34. $this->error = '不是会议室审批人';
  35. return false;
  36. }
  37. $undeal = db('meeting_apply')->where('status',0)->where('del',0)->where('approver',$userId)->where('org_id',$orgId)->count();
  38. return ['count' => $undeal];
  39. }
  40. public function getRoomByDay($day,$orgId){
  41. $lists = db('meeting_room')
  42. ->field('id,title,content,address,img,limit')
  43. ->where('enable',1)
  44. ->where('del',0)
  45. ->where('org_id',$orgId)
  46. ->order(['sort'=>'desc','id'=>'desc'])
  47. ->select();
  48. $lists = $lists?$lists:[];
  49. // 获取48个时间段
  50. $times = $this->getTimesDay($day);
  51. foreach ($lists as $k=>$v){
  52. $stime = $times;
  53. foreach ($stime as $kk=>$vv){
  54. $ret = db('meeting_apply')
  55. ->whereIn('status',[0,1])
  56. ->where('del',0)
  57. ->where('org_id',$orgId)
  58. ->where('start_time','<',$vv['etime'])
  59. ->where('end_time','>',$vv['stime'])
  60. ->where('meeting_room_id',$v['id'])
  61. ->find();
  62. if($ret){
  63. if($vv['status'] == 0){
  64. $stime[$kk]['status'] = 3;
  65. }else{
  66. $stime[$kk]['status'] = 2;
  67. }
  68. }
  69. }
  70. $lists[$k]['times'] = $stime;
  71. }
  72. return $lists;
  73. }
  74. private function getTimesDay($day){
  75. $times = [];
  76. $curTime = date('Y-m-d H:i:s');
  77. $nextday = date('Y-m-d',strtotime($day)+24*60*60);
  78. for ($i=0;$i<24;$i++){
  79. if($i == 23){
  80. $a = [
  81. 'stime' => $day.' '.$i.':00:00',
  82. 'etime' => $day.' '.$i.':30:00',
  83. 'status' => 0 // 0=白色 1=灰色 2=黄色(历史预订) 3=蓝色(已预订)
  84. ];
  85. $b = [
  86. 'stime' => $day.' '.$i.':30:00',
  87. 'etime' => $nextday.' '.'00:00:00',
  88. 'status' => 0 // 0=白色 1=灰色 2=黄色(历史预订) 3=蓝色(已预订)
  89. ];
  90. }else if($i>9&&$i<23){
  91. $a = [
  92. 'stime' => $day.' '.$i.':00:00',
  93. 'etime' => $day.' '.$i.':30:00',
  94. 'status' => 0 // 0=白色 1=灰色 2=黄色(历史预订) 3=蓝色(已预订)
  95. ];
  96. $b = [
  97. 'stime' => $day.' '.$i.':30:00',
  98. 'etime' => $day.' '.($i+1).':00:00',
  99. 'status' => 0 // 0=白色 1=灰色 2=黄色(历史预订) 3=蓝色(已预订)
  100. ];
  101. }else if($i == 9){
  102. $a = [
  103. 'stime' => $day.' 0'.$i.':00:00',
  104. 'etime' => $day.' 0'.$i.':30:00',
  105. 'status' => 0 // 0=白色 1=灰色 2=黄色(历史预订) 3=蓝色(已预订)
  106. ];
  107. $b = [
  108. 'stime' => $day.' 0'.$i.':30:00',
  109. 'etime' => $day.' '.'10:00:00',
  110. 'status' => 0 // 0=白色 1=灰色 2=黄色(历史预订) 3=蓝色(已预订)
  111. ];
  112. }else{
  113. $a = [
  114. 'stime' => $day.' 0'.$i.':00:00',
  115. 'etime' => $day.' 0'.$i.':30:00',
  116. 'status' => 0 // 0=白色 1=灰色 2=黄色(历史预订) 3=蓝色(已预订)
  117. ];
  118. $b = [
  119. 'stime' => $day.' 0'.$i.':30:00',
  120. 'etime' => $day.' 0'.($i+1).':00:00',
  121. 'status' => 0 // 0=白色 1=灰色 2=黄色(历史预订) 3=蓝色(已预订)
  122. ];
  123. }
  124. if($curTime > $a['stime']){
  125. $a['status'] = 1;
  126. }
  127. if($curTime > $b['stime']){
  128. $b['status'] = 1;
  129. }
  130. $times[] = $a;
  131. $times[] = $b;
  132. }
  133. return $times;
  134. }
  135. public function details($id,$time,$orgId){
  136. $info = db('meeting_room')
  137. ->field('id,title,content,address,img,approver,meeting_device_ids,limit')
  138. ->where('enable',1)
  139. ->where('del',0)
  140. ->where('org_id',$orgId)
  141. ->where('id',$id)
  142. ->find();
  143. if(!$info){
  144. $this->error = '记录不存在';
  145. return false;
  146. }
  147. $info['devices'] = db('meeting_device')
  148. ->field('id,title')
  149. ->whereIn('id',$info['meeting_device_ids'])
  150. ->select();
  151. $day = $time;
  152. $times = $this->getTimesDay($day);
  153. $newtimes = [];
  154. foreach ($times as $k=>$v){
  155. if($v['status'] == 0){
  156. $status = 0;
  157. $ret = db('meeting_apply')
  158. ->whereIn('status',[0,1])
  159. ->where('del',0)
  160. ->where('org_id',$orgId)
  161. ->where('start_time','<',$v['etime'])
  162. ->where('end_time','>',$v['stime'])
  163. ->where('meeting_room_id',$info['id'])
  164. ->find();
  165. if($ret){
  166. $status = 1;
  167. }
  168. $newtimes[] = [
  169. 'status' => $status,
  170. 'stime' => $v['stime'],
  171. 'etime' => $v['etime'],
  172. 'stimetext' => date('H:i',strtotime($v['stime'])),
  173. 'etimetext' => date('H:i',strtotime($v['etime']))
  174. ];
  175. }
  176. }
  177. $info['times'] = $newtimes;
  178. $approver = db('user')->where('id',$info['approver'])->field('head_image,real_name')->find();
  179. $info['approver_img'] =$approver?$approver['head_image']:'';
  180. $info['approver_name'] =$approver?$approver['real_name']:'';
  181. return $info;
  182. }
  183. // 保存会议室预定
  184. public function saveBook($data){
  185. Db::startTrans();
  186. try{
  187. $approver = db('meeting_room')->where('id',$data['meeting_room_id'])->value('approver');
  188. $data['approver'] = $approver;
  189. $data['create_time'] = getTime();
  190. $roomInfo = db('meeting_room')->where('id',$data['meeting_room_id'])->find();
  191. if($roomInfo['is_check'] == 0){
  192. $data['status'] = 1;
  193. $data['approver'] = 0;//不需要审核
  194. $data['apply_time'] = getTime();
  195. }
  196. //添加
  197. $applyId = db('meeting_apply')->insertGetId($data);
  198. if($applyId < 1){
  199. exception('操作失败');
  200. }
  201. //通知审批人员
  202. send_jpush([$approver],19,['id' => $applyId]);
  203. Db::commit();
  204. }catch (\Exception $e){
  205. Db::rollback();
  206. $this->error = $e->getMessage();
  207. return false;
  208. }
  209. return true;
  210. }
  211. }