PatrolPlan.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace app\common\model;
  3. use think\Exception;
  4. use think\Db;
  5. class PatrolPlan extends Base {
  6. public function updates($userId){
  7. $post = request()->post();
  8. $post['org_id'] = cur_org_id();
  9. $post['user_id'] = $userId;
  10. $ids = isset($post['id'])?$post['id']:0;
  11. unset($post['id']);
  12. $validate = new \app\common\validate\PatrolPlan();
  13. $result = $validate->check($post,[]);
  14. if(true !== $result){
  15. $this->error = $validate->getError();
  16. return false;
  17. }
  18. Db::startTrans();
  19. try{
  20. $curDate = date('Y-m-d H:i:s');
  21. $data = [
  22. "title" => $post['title'],
  23. "start_time" => $post['start_time'],
  24. "end_time" => $post['end_time'],
  25. "content" => $post['content'],
  26. "patrol_mode" => $post['mode'],
  27. "status" => 0,
  28. "org_id" => $post['org_id'],
  29. "user_id" => $post['user_id'],
  30. "create_time" => $curDate,
  31. "in_order" => $post['in_order'],
  32. 'cate_id' => isset($post['cate_id'])?$post['cate_id']:0,
  33. "lines" => $post['in_order'] == 1&&$post['lines']?$post['lines']:''
  34. ];
  35. if($post['start_time'] == date('Y-m-d')){
  36. $data['status'] = 1; // 进行中
  37. }
  38. if($ids >0){
  39. Db::name('patrol_plan')->where('id',$ids)
  40. ->update($data);
  41. $planId = $ids;
  42. }else{
  43. $planId = Db::name('patrol_plan')->insertGetId($data);
  44. }
  45. if(!$planId){
  46. \exception('操作失败');
  47. }
  48. $content = json_decode($post['content'],true);
  49. $tasks = $content['tasks']['list']; // 任务数组
  50. $addrForms = $content['addrForms'];
  51. $addrForms = list_sort_by($addrForms,'sorts','asc'); // 排序
  52. $nforms = [];
  53. foreach ($addrForms as $k=>$v){
  54. $pfms = [];
  55. foreach ($v['forms'] as $kk=>$vv){
  56. $pfms[] = $vv['id'];
  57. }
  58. $nforms[] = [
  59. 'address_id' => $v['addrId'],
  60. 'patrol_forms' => implode(',',$pfms),
  61. 'sorts' => (int)$v['sorts']
  62. ];
  63. }
  64. if($ids >0){
  65. $oldTask = Db::name('patrol_task')
  66. ->where('plan_id',$ids)
  67. ->where('del',0)
  68. ->where('status',0)
  69. ->column('id');
  70. if($oldTask){
  71. $rr = Db::name('patrol_task')
  72. ->where('id','in',$oldTask)
  73. ->update([
  74. 'del'=>1,
  75. 'del_user_id'=>$userId,
  76. 'del_time'=>getTime(),
  77. ]);
  78. if(!$rr){
  79. \exception('删除旧数据失败');
  80. }
  81. $rr = Db::name('patrol_task_addr')
  82. ->where('patrol_task_id','in',$oldTask)
  83. ->delete();
  84. if(!$rr){
  85. \exception('删除旧数据失败');
  86. }
  87. $rr = Db::name('patrol_task_user')
  88. ->where('patrol_task_id','in',$oldTask)
  89. ->delete();
  90. if(!$rr){
  91. \exception('删除旧数据失败');
  92. }
  93. }
  94. }
  95. foreach ($tasks as $k=>$v){
  96. foreach ($v['list'] as $kk=>$vv){
  97. if($content['dateType']==4){//按月
  98. list($a,$b) = explode('~',$vv['day']);
  99. $stime = $a.' '.$v['time']['stime'];
  100. $etime = $b.' '.$v['time']['etime'];
  101. }else{
  102. $stime = $vv['day'].' '.$v['time']['stime'];
  103. $etime = $vv['day'].' '.$v['time']['etime'];
  104. if($v['time']['stype'] == 1){ // 次日
  105. $nextday = date('Y-m-d',strtotime($vv['day']) + $v['time']['sday']*24*60*60);
  106. $etime = $nextday.' '.$v['time']['etime'];
  107. }
  108. }
  109. if(($etime >date('Y-m-d H:i:s') && $ids >0) || $ids==0){
  110. $d = [
  111. 'org_id' => $post['org_id'],
  112. 'title' => $post['title'],
  113. 'start_time' => $stime,
  114. 'end_time' => $etime,
  115. 'status' => 0,
  116. 'create_time' => $curDate,
  117. 'patrol_mode' => $post['mode'],
  118. 'update_time' => $curDate,
  119. 'plan_id' => $planId,
  120. "cate_id" =>isset($post['cate_id'])?$post['cate_id']:0,
  121. 'create_yyyy' => date('Y',strtotime($stime)),
  122. 'create_yyyymm' => date('Ym',strtotime($stime)),
  123. 'create_yyyymmdd' => date('Ymd',strtotime($stime)),
  124. "in_order" => $post['in_order'],
  125. "lines" => $post['in_order'] == 1&&$post['lines']?$post['lines']:''
  126. ];
  127. $taskId = Db::name('patrol_task')->insertGetId($d);
  128. if(!$taskId){
  129. \exception('操作失败');
  130. }
  131. foreach ($nforms as $key=>$val){
  132. $nforms[$key]['patrol_task_id'] = $taskId;
  133. }
  134. $fromRet = Db::name('patrol_task_addr')->insertAll($nforms);
  135. if($fromRet != count($nforms)){
  136. \exception('操作失败');
  137. }
  138. $uds = [];
  139. foreach ($vv['userIds'] as $key=>$val){
  140. $uds[] = [
  141. 'patrol_task_id' => $taskId,
  142. 'user_id' => $val
  143. ];
  144. }
  145. $userRet = Db::name('patrol_task_user')->insertAll($uds);
  146. if($userRet != count($uds)){
  147. \exception('操作失败');
  148. }
  149. }
  150. }
  151. }
  152. Db::commit();
  153. return true;
  154. }catch (\Exception $e){
  155. // 回滚事务
  156. $this->error = $e->getMessage();
  157. Db::rollback();
  158. return false;
  159. }
  160. }
  161. // 获取组织某一类型巡更计划
  162. public function getListByMode($orgId,$mode){
  163. $lists = Db::name('patrol_plan')
  164. ->where('del',0)
  165. ->where('patrol_mode',$mode)
  166. ->where('org_id',$orgId)
  167. ->field('id,title,start_time,end_time,status')
  168. ->order('id desc')
  169. ->select();
  170. return $lists?$lists:[];
  171. }
  172. }