MeetingRoom.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. ->select();
  47. $lists = $lists?$lists:[];
  48. // 获取48个时间段
  49. $times = $this->getTimesDay($day);
  50. foreach ($lists as $k=>$v){
  51. $stime = $times;
  52. foreach ($stime as $kk=>$vv){
  53. $ret = db('meeting_apply')
  54. ->whereIn('status',[0,1])
  55. ->where('del',0)
  56. ->where('org_id',$orgId)
  57. ->where('start_time','<',$vv['etime'])
  58. ->where('end_time','>',$vv['stime'])
  59. ->where('meeting_room_id',$v['id'])
  60. ->find();
  61. if($ret){
  62. if($vv['status'] == 0){
  63. $stime[$kk]['status'] = 3;
  64. }else{
  65. $stime[$kk]['status'] = 2;
  66. }
  67. }
  68. }
  69. $lists[$k]['times'] = $stime;
  70. }
  71. return $lists;
  72. }
  73. private function getTimesDay($day){
  74. $times = [];
  75. $curTime = date('Y-m-d H:i:s');
  76. $nextday = date('Y-m-d',strtotime($day)+24*60*60);
  77. for ($i=0;$i<24;$i++){
  78. if($i == 23){
  79. $a = [
  80. 'stime' => $day.' '.$i.':00:00',
  81. 'etime' => $day.' '.$i.':30:00',
  82. 'status' => 0 // 0=白色 1=灰色 2=黄色(历史预订) 3=蓝色(已预订)
  83. ];
  84. $b = [
  85. 'stime' => $day.' '.$i.':30:00',
  86. 'etime' => $nextday.' '.'00:00:00',
  87. 'status' => 0 // 0=白色 1=灰色 2=黄色(历史预订) 3=蓝色(已预订)
  88. ];
  89. }else if($i>9&&$i<23){
  90. $a = [
  91. 'stime' => $day.' '.$i.':00:00',
  92. 'etime' => $day.' '.$i.':30:00',
  93. 'status' => 0 // 0=白色 1=灰色 2=黄色(历史预订) 3=蓝色(已预订)
  94. ];
  95. $b = [
  96. 'stime' => $day.' '.$i.':30:00',
  97. 'etime' => $day.' '.($i+1).':00:00',
  98. 'status' => 0 // 0=白色 1=灰色 2=黄色(历史预订) 3=蓝色(已预订)
  99. ];
  100. }else if($i == 9){
  101. $a = [
  102. 'stime' => $day.' 0'.$i.':00:00',
  103. 'etime' => $day.' 0'.$i.':30:00',
  104. 'status' => 0 // 0=白色 1=灰色 2=黄色(历史预订) 3=蓝色(已预订)
  105. ];
  106. $b = [
  107. 'stime' => $day.' 0'.$i.':30:00',
  108. 'etime' => $day.' '.'10:00:00',
  109. 'status' => 0 // 0=白色 1=灰色 2=黄色(历史预订) 3=蓝色(已预订)
  110. ];
  111. }else{
  112. $a = [
  113. 'stime' => $day.' 0'.$i.':00:00',
  114. 'etime' => $day.' 0'.$i.':30:00',
  115. 'status' => 0 // 0=白色 1=灰色 2=黄色(历史预订) 3=蓝色(已预订)
  116. ];
  117. $b = [
  118. 'stime' => $day.' 0'.$i.':30:00',
  119. 'etime' => $day.' 0'.($i+1).':00:00',
  120. 'status' => 0 // 0=白色 1=灰色 2=黄色(历史预订) 3=蓝色(已预订)
  121. ];
  122. }
  123. if($curTime > $a['stime']){
  124. $a['status'] = 1;
  125. }
  126. if($curTime > $b['stime']){
  127. $b['status'] = 1;
  128. }
  129. $times[] = $a;
  130. $times[] = $b;
  131. }
  132. return $times;
  133. }
  134. public function details($id,$time,$orgId){
  135. $info = db('meeting_room')
  136. ->field('id,title,content,address,img,approver,meeting_device_ids,limit')
  137. ->where('enable',1)
  138. ->where('del',0)
  139. ->where('org_id',$orgId)
  140. ->where('id',$id)
  141. ->find();
  142. if(!$info){
  143. $this->error = '记录不存在';
  144. return false;
  145. }
  146. $info['devices'] = db('meeting_device')
  147. ->field('id,title')
  148. ->whereIn('id',$info['meeting_device_ids'])
  149. ->select();
  150. $day = $time;
  151. $times = $this->getTimesDay($day);
  152. $newtimes = [];
  153. foreach ($times as $k=>$v){
  154. if($v['status'] == 0){
  155. $status = 0;
  156. $ret = db('meeting_apply')
  157. ->whereIn('status',[0,1])
  158. ->where('del',0)
  159. ->where('org_id',$orgId)
  160. ->where('start_time','<',$v['etime'])
  161. ->where('end_time','>',$v['stime'])
  162. ->where('meeting_room_id',$info['id'])
  163. ->find();
  164. if($ret){
  165. $status = 1;
  166. }
  167. $newtimes[] = [
  168. 'status' => $status,
  169. 'stime' => $v['stime'],
  170. 'etime' => $v['etime'],
  171. 'stimetext' => date('H:i',strtotime($v['stime'])),
  172. 'etimetext' => date('H:i',strtotime($v['etime']))
  173. ];
  174. }
  175. }
  176. $info['times'] = $newtimes;
  177. $approver = db('user')->where('id',$info['approver'])->field('head_image,real_name')->find();
  178. $info['approver_img'] =$approver?$approver['head_image']:'';
  179. $info['approver_name'] =$approver?$approver['real_name']:'';
  180. return $info;
  181. }
  182. // 保存会议室预定
  183. public function saveBook($data){
  184. Db::startTrans();
  185. try{
  186. $approver = db('meeting_room')->where('id',$data['meeting_room_id'])->value('approver');
  187. $data['approver'] = $approver;
  188. $data['create_time'] = getTime();
  189. //添加
  190. $applyId = db('meeting_apply')->insertGetId($data);
  191. if($applyId < 1){
  192. exception('操作失败');
  193. }
  194. //通知审批人员
  195. // send_push_meeting(14,[$approver],['id' => $applyId]);
  196. Db::commit();
  197. }catch (\Exception $e){
  198. Db::rollback();
  199. $this->error = $e->getMessage();
  200. return false;
  201. }
  202. return true;
  203. }
  204. }