123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- namespace app\common\model;
- use think\Db;
- class ServiceTime extends Base
- {
- public function updates(){
- $data = request()->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;
- }
- }
|