123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- namespace app\common\model;
- use think\Db;
- use think\Exception;
- class ModeCate extends Base
- {
- protected $createTime = 'create_time';
- protected $updateTime = 'update_time';
- protected $table = 'mode_cate';
- protected $validateName = 'ModeCate';
- public function updates($orgId){
- $data = request()->post();
- $data['org_id'] = cur_org_id();
- $result = validate($this->validateName)->check($data,[],'');
- if(true !== $result){
- $this->error = validate($this->validateName)->getError();
- return false;
- }
- $busIds = $data['bus_ids'] ? explode(',',$data['bus_ids']) :[];
- unset($data['bus_ids']);
- $busIds1 = $data['bus_ids1'] ? explode(',',$data['bus_ids1']) :[];
- unset($data['bus_ids1']);
- if($data['mode'] == 2 && !$busIds){
- $this->error = '保洁类型不能为空';
- return false;
- }
- if($data['mode'] == 3 && !$busIds1){
- $this->error = '运送类型不能为空';
- return false;
- }
- $id = $data['id'];
- unset($data['id']);
- Db::startTrans();
- try {
- if($id > 0){
- if($data['mode'] == 2){
- $atable = 'cleaning_type b';
- }else{
- $atable = 'convey_cate b';
- }
- $busIdsAll = Db::name('mode_cate_type')
- ->where('mode',$data['mode'])
- ->where('cate_id','<>',$id)
- ->column('bus_id');
- if($data['mode'] == 2){
- $intersectIds = array_intersect($busIds,$busIdsAll);
- }else{
- $intersectIds = array_intersect($busIds1,$busIdsAll);
- }
- $cleaningTypeTitle = Db::name('mode_cate_type')
- ->alias('mct')
- ->join($atable,'b.id=mct.bus_id')
- ->where('mct.mode',$data['mode'])
- ->where('mct.bus_id','in',$intersectIds)
- ->column('b.title');
- if($cleaningTypeTitle){
- $cleaningTypeTitles = $cleaningTypeTitle ? implode(',',$cleaningTypeTitle):'';
- $this->error = $cleaningTypeTitles.'已存在';
- return false;
- }
- Db::name('mode_cate_type')->where('mode',$data['mode'])->where('cate_id',$id)->delete();
- if($busIds){
- $tat = [
- 'cate_id'=>$id,
- 'mode'=>$data['mode'],
- 'create_time'=>getTime(),
- ];
- foreach ($busIds as $k=>$v){
- $tat['bus_id'] = $v;
- $add = Db::name('mode_cate_type')->insert($tat);
- if(!$add){
- $this->error = '操作失败1';
- return false;
- }
- }
- }
- if($busIds1){
- $tat = [
- 'cate_id'=>$id,
- 'mode'=>$data['mode'],
- 'create_time'=>getTime(),
- ];
- foreach ($busIds1 as $k=>$v){
- $tat['bus_id'] = $v;
- $add = Db::name('mode_cate_type')->insert($tat);
- if(!$add){
- $this->error = '操作失败2';
- return false;
- }
- }
- }
- $ret = $this->allowField(true)->save($data,['id'=>$id]);
- if(!$ret){
- $this->error = '修改失败';
- return false;
- }
- }else{
- if($data['mode'] == 2){
- $atable = 'cleaning_type b';
- }else{
- $atable = 'convey_cate b';
- }
- $cleaningTypeTitle = Db::name('mode_cate_type')
- ->alias('mct')
- ->join($atable,'b.id=mct.bus_id')
- ->where('mct.mode',$data['mode'])
- ->where('mct.bus_id','in',$busIds)
- ->column('b.title');
- if($cleaningTypeTitle){
- $cleaningTypeTitles = $cleaningTypeTitle ? implode(',',$cleaningTypeTitle):'';
- $this->error = $cleaningTypeTitles.'已存在';
- return false;
- }
- $id = Db::name($this->table)->insertGetId($data);
- if(!$id){
- $this->error = '操作失败';
- return false;
- }
- if($data['mode'] == 2 && $busIds){
- $dt = [
- 'cate_id'=>$id,
- 'mode'=>$data['mode'],
- 'create_time'=>getTime(),
- ];
- foreach ($busIds as $k=>$v){
- $dt['bus_id'] = $v;
- $add = Db::name('mode_cate_type')->insert($dt);
- if(!$add){
- $this->error = '操作失败1';
- return false;
- }
- }
- }
- if($data['mode'] == 3 && $busIds1){
- $dt = [
- 'cate_id'=>$id,
- 'mode'=>$data['mode'],
- 'create_time'=>getTime(),
- ];
- foreach ($busIds1 as $k=>$v){
- $dt['bus_id'] = $v;
- $add = Db::name('mode_cate_type')->insert($dt);
- if(!$add){
- $this->error = '操作失败1';
- return false;
- }
- }
- }
- }
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- $this->error = $e->getMessage();
- return false;
- }
- }
- }
|