ModeCate.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Exception;
  5. class ModeCate extends Base
  6. {
  7. protected $createTime = 'create_time';
  8. protected $updateTime = 'update_time';
  9. protected $table = 'mode_cate';
  10. protected $validateName = 'ModeCate';
  11. public function updates($orgId){
  12. $data = request()->post();
  13. $data['org_id'] = cur_org_id();
  14. $result = validate($this->validateName)->check($data,[],'');
  15. if(true !== $result){
  16. $this->error = validate($this->validateName)->getError();
  17. return false;
  18. }
  19. $busIds = $data['bus_ids'] ? explode(',',$data['bus_ids']) :[];
  20. unset($data['bus_ids']);
  21. $busIds1 = $data['bus_ids1'] ? explode(',',$data['bus_ids1']) :[];
  22. unset($data['bus_ids1']);
  23. if($data['mode'] == 2 && !$busIds){
  24. $this->error = '保洁类型不能为空';
  25. return false;
  26. }
  27. if($data['mode'] == 3 && !$busIds1){
  28. $this->error = '运送类型不能为空';
  29. return false;
  30. }
  31. $id = $data['id'];
  32. unset($data['id']);
  33. Db::startTrans();
  34. try {
  35. if($id > 0){
  36. if($data['mode'] == 2){
  37. $atable = 'cleaning_type b';
  38. }else{
  39. $atable = 'convey_cate b';
  40. }
  41. $busIdsAll = Db::name('mode_cate_type')
  42. ->where('mode',$data['mode'])
  43. ->where('cate_id','<>',$id)
  44. ->column('bus_id');
  45. if($data['mode'] == 2){
  46. $intersectIds = array_intersect($busIds,$busIdsAll);
  47. }else{
  48. $intersectIds = array_intersect($busIds1,$busIdsAll);
  49. }
  50. $cleaningTypeTitle = Db::name('mode_cate_type')
  51. ->alias('mct')
  52. ->join($atable,'b.id=mct.bus_id')
  53. ->where('mct.mode',$data['mode'])
  54. ->where('mct.bus_id','in',$intersectIds)
  55. ->column('b.title');
  56. if($cleaningTypeTitle){
  57. $cleaningTypeTitles = $cleaningTypeTitle ? implode(',',$cleaningTypeTitle):'';
  58. $this->error = $cleaningTypeTitles.'已存在';
  59. return false;
  60. }
  61. Db::name('mode_cate_type')->where('mode',$data['mode'])->where('cate_id',$id)->delete();
  62. if($busIds){
  63. $tat = [
  64. 'cate_id'=>$id,
  65. 'mode'=>$data['mode'],
  66. 'create_time'=>getTime(),
  67. ];
  68. foreach ($busIds as $k=>$v){
  69. $tat['bus_id'] = $v;
  70. $add = Db::name('mode_cate_type')->insert($tat);
  71. if(!$add){
  72. $this->error = '操作失败1';
  73. return false;
  74. }
  75. }
  76. }
  77. if($busIds1){
  78. $tat = [
  79. 'cate_id'=>$id,
  80. 'mode'=>$data['mode'],
  81. 'create_time'=>getTime(),
  82. ];
  83. foreach ($busIds1 as $k=>$v){
  84. $tat['bus_id'] = $v;
  85. $add = Db::name('mode_cate_type')->insert($tat);
  86. if(!$add){
  87. $this->error = '操作失败2';
  88. return false;
  89. }
  90. }
  91. }
  92. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  93. if(!$ret){
  94. $this->error = '修改失败';
  95. return false;
  96. }
  97. }else{
  98. if($data['mode'] == 2){
  99. $atable = 'cleaning_type b';
  100. }else{
  101. $atable = 'convey_cate b';
  102. }
  103. $cleaningTypeTitle = Db::name('mode_cate_type')
  104. ->alias('mct')
  105. ->join($atable,'b.id=mct.bus_id')
  106. ->where('mct.mode',$data['mode'])
  107. ->where('mct.bus_id','in',$busIds)
  108. ->column('b.title');
  109. if($cleaningTypeTitle){
  110. $cleaningTypeTitles = $cleaningTypeTitle ? implode(',',$cleaningTypeTitle):'';
  111. $this->error = $cleaningTypeTitles.'已存在';
  112. return false;
  113. }
  114. $id = Db::name($this->table)->insertGetId($data);
  115. if(!$id){
  116. $this->error = '操作失败';
  117. return false;
  118. }
  119. if($data['mode'] == 2 && $busIds){
  120. $dt = [
  121. 'cate_id'=>$id,
  122. 'mode'=>$data['mode'],
  123. 'create_time'=>getTime(),
  124. ];
  125. foreach ($busIds as $k=>$v){
  126. $dt['bus_id'] = $v;
  127. $add = Db::name('mode_cate_type')->insert($dt);
  128. if(!$add){
  129. $this->error = '操作失败1';
  130. return false;
  131. }
  132. }
  133. }
  134. if($data['mode'] == 3 && $busIds1){
  135. $dt = [
  136. 'cate_id'=>$id,
  137. 'mode'=>$data['mode'],
  138. 'create_time'=>getTime(),
  139. ];
  140. foreach ($busIds1 as $k=>$v){
  141. $dt['bus_id'] = $v;
  142. $add = Db::name('mode_cate_type')->insert($dt);
  143. if(!$add){
  144. $this->error = '操作失败1';
  145. return false;
  146. }
  147. }
  148. }
  149. }
  150. Db::commit();
  151. return true;
  152. } catch (\Exception $e) {
  153. Db::rollback();
  154. $this->error = $e->getMessage();
  155. return false;
  156. }
  157. }
  158. }