post(); $result = validate('ServiceTime')->check($data,[],''); if(true !== $result){ $this->error = validate('ServiceTime')->getError(); return false; } if($data['start'] > $data['end']){ $this->error = '开始时间不能大于结束时间'; return false; } $id = $data['id']; unset($data['id']); if($id > 0){ $data['update_time'] = date('Y-m-d H:i:s'); $info = db('service_time') ->where('org_id',cur_org_id()) ->where('del','=',0) ->where('id','<>',$id) ->where('day','=',$data['day']) ->where('start','<',$data['end']) ->where('end','>',$data['start']) ->find(); if($info) { $this->error = '时间有重复'; return false; } $ret = $this->allowField(true)->save($data,['id'=>$id]); if(!$ret){ $this->error = '操作失败'; return false; } }else{ $days = $data['days']; $sData = [[ 'org_id' => cur_org_id(), 'service_id' => $data['service_id'], 'start' => $data['start'], 'end' => $data['end'], 'limit' => $data['limit'], 'day' => $data['day'], 'enable' => $data['enable'], 'create_time' => date('Y-m-d H:i:s') ]]; if($days > 0){ for($i=1;$i<=$days;$i++){ $day = date('Y-m-d',strtotime($data['day']) + $i*24*60*60); $info = Db::name('service_time') ->where('org_id',cur_org_id()) ->where('del','=',0) ->where('day','=',$day) ->where('start','<',$data['end']) ->where('end','>',$data['start']) ->find(); if($info){ $this->error = '时间有重复'; return false; } $sData[] = [ 'org_id' => cur_org_id(), 'service_id' => $data['service_id'], 'start' => $data['start'], 'end' => $data['end'], 'limit' => $data['limit'], 'day' => $day, 'enable' => $data['enable'], 'create_time' => date('Y-m-d H:i:s') ]; } } // 启动事务 Db::startTrans(); try{ $add = Db::name('service_time')->insertAll($sData); if(!$add){ exception('操作失败'); } // 提交事务 Db::commit(); }catch (\Exception $e) { Db::rollback(); $this->error = $e->getMessage(); return false; } } return true; } }