123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- namespace app\common\model;
- use think\Exception;
- use think\Db;
- class PatrolPlan extends Base {
- public function updates($userId){
- $post = request()->post();
- $post['org_id'] = cur_org_id();
- $post['user_id'] = $userId;
- $ids = isset($post['id'])?$post['id']:0;
- unset($post['id']);
- $validate = new \app\common\validate\PatrolPlan();
- $result = $validate->check($post,[]);
- if(true !== $result){
- $this->error = $validate->getError();
- return false;
- }
- Db::startTrans();
- try{
- $curDate = date('Y-m-d H:i:s');
- $data = [
- "title" => $post['title'],
- "start_time" => $post['start_time'],
- "end_time" => $post['end_time'],
- "content" => $post['content'],
- "patrol_mode" => $post['mode'],
- "status" => 0,
- "org_id" => $post['org_id'],
- "user_id" => $post['user_id'],
- "create_time" => $curDate,
- "in_order" => $post['in_order'],
- 'cate_id' => isset($post['cate_id'])?$post['cate_id']:0,
- "lines" => $post['in_order'] == 1&&$post['lines']?$post['lines']:''
- ];
- if($post['start_time'] == date('Y-m-d')){
- $data['status'] = 1; // 进行中
- }
- if($ids >0){
- Db::name('patrol_plan')->where('id',$ids)
- ->update($data);
- $planId = $ids;
- }else{
- $planId = Db::name('patrol_plan')->insertGetId($data);
- }
- if(!$planId){
- \exception('操作失败');
- }
- $content = json_decode($post['content'],true);
- $tasks = $content['tasks']['list']; // 任务数组
- $addrForms = $content['addrForms'];
- $addrForms = list_sort_by($addrForms,'sorts','asc'); // 排序
- $nforms = [];
- foreach ($addrForms as $k=>$v){
- $pfms = [];
- foreach ($v['forms'] as $kk=>$vv){
- $pfms[] = $vv['id'];
- }
- $nforms[] = [
- 'address_id' => $v['addrId'],
- 'patrol_forms' => implode(',',$pfms),
- 'sorts' => (int)$v['sorts']
- ];
- }
- if($ids >0){
- $oldTask = Db::name('patrol_task')
- ->where('plan_id',$ids)
- ->where('del',0)
- ->where('status',0)
- ->column('id');
- if($oldTask){
- $rr = Db::name('patrol_task')
- ->where('id','in',$oldTask)
- ->update([
- 'del'=>1,
- 'del_user_id'=>$userId,
- 'del_time'=>getTime(),
- ]);
- if(!$rr){
- \exception('删除旧数据失败');
- }
- $rr = Db::name('patrol_task_addr')
- ->where('patrol_task_id','in',$oldTask)
- ->delete();
- if(!$rr){
- \exception('删除旧数据失败');
- }
- $rr = Db::name('patrol_task_user')
- ->where('patrol_task_id','in',$oldTask)
- ->delete();
- if(!$rr){
- \exception('删除旧数据失败');
- }
- }
- }
- foreach ($tasks as $k=>$v){
- foreach ($v['list'] as $kk=>$vv){
- if($content['dateType']==4){//按月
- list($a,$b) = explode('~',$vv['day']);
- $stime = $a.' '.$v['time']['stime'];
- $etime = $b.' '.$v['time']['etime'];
- }else{
- $stime = $vv['day'].' '.$v['time']['stime'];
- $etime = $vv['day'].' '.$v['time']['etime'];
- if($v['time']['stype'] == 1){ // 次日
- $nextday = date('Y-m-d',strtotime($vv['day']) + $v['time']['sday']*24*60*60);
- $etime = $nextday.' '.$v['time']['etime'];
- }
- }
- if(($etime >date('Y-m-d H:i:s') && $ids >0) || $ids==0){
- $d = [
- 'org_id' => $post['org_id'],
- 'title' => $post['title'],
- 'start_time' => $stime,
- 'end_time' => $etime,
- 'status' => 0,
- 'create_time' => $curDate,
- 'patrol_mode' => $post['mode'],
- 'update_time' => $curDate,
- 'plan_id' => $planId,
- "cate_id" =>isset($post['cate_id'])?$post['cate_id']:0,
- 'create_yyyy' => date('Y',strtotime($stime)),
- 'create_yyyymm' => date('Ym',strtotime($stime)),
- 'create_yyyymmdd' => date('Ymd',strtotime($stime)),
- "in_order" => $post['in_order'],
- "lines" => $post['in_order'] == 1&&$post['lines']?$post['lines']:''
- ];
- $taskId = Db::name('patrol_task')->insertGetId($d);
- if(!$taskId){
- \exception('操作失败');
- }
- foreach ($nforms as $key=>$val){
- $nforms[$key]['patrol_task_id'] = $taskId;
- }
- $fromRet = Db::name('patrol_task_addr')->insertAll($nforms);
- if($fromRet != count($nforms)){
- \exception('操作失败');
- }
- $uds = [];
- foreach ($vv['userIds'] as $key=>$val){
- $uds[] = [
- 'patrol_task_id' => $taskId,
- 'user_id' => $val
- ];
- }
- $userRet = Db::name('patrol_task_user')->insertAll($uds);
- if($userRet != count($uds)){
- \exception('操作失败');
- }
- }
- }
- }
- Db::commit();
- return true;
- }catch (\Exception $e){
- // 回滚事务
- $this->error = $e->getMessage();
- Db::rollback();
- return false;
- }
- }
- // 获取组织某一类型巡更计划
- public function getListByMode($orgId,$mode){
- $lists = Db::name('patrol_plan')
- ->where('del',0)
- ->where('patrol_mode',$mode)
- ->where('org_id',$orgId)
- ->field('id,title,start_time,end_time,status')
- ->order('id desc')
- ->select();
- return $lists?$lists:[];
- }
- }
|