PriorityService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Priority;
  4. use Illuminate\Support\Facades\DB;
  5. class PriorityService extends Service
  6. {
  7. public function priorityEdit($data, $user){
  8. list($status,$msg) = $this->priorityRule($data,$user,false);
  9. if(!$status) return [$status, $msg];
  10. try {
  11. DB::beginTransaction();
  12. $update = $msg['data'][0];
  13. $model = new Priority();
  14. $model->where('id',$data['id'])->update($update);
  15. DB::commit();
  16. }catch (\Exception $exception){
  17. DB::rollBack();
  18. return [false,$exception->getMessage()];
  19. }
  20. return [true,''];
  21. }
  22. public function priorityAdd($data,$user){
  23. list($status,$msg) = $this->priorityRule($data,$user);
  24. if(!$status) return [$status,$msg];
  25. try {
  26. DB::beginTransaction();
  27. foreach ($msg['data'] as $value){
  28. $model = new Priority();
  29. $model->title = $value['title'];
  30. $model->code = $value['code'];
  31. $model->type = $value['type'];
  32. $model->sort = $value['sort'];
  33. $model->top_depart_id = $value['top_depart_id'];
  34. $model->save();
  35. }
  36. DB::commit();
  37. }catch (\Exception $exception){
  38. DB::rollBack();
  39. return [false,$exception->getMessage()];
  40. }
  41. return [true,''];
  42. }
  43. public function priorityDel($data, $user){
  44. if(empty($data['id'])) return [false, 'ID不能为空'];
  45. Priority::whereIn('id',$data['id'])->update([
  46. 'del_time'=>time()
  47. ]);
  48. return [true,''];
  49. }
  50. public function priorityCommon($data,$user, $field = []){
  51. if(empty($field)) $field = Priority::$field;
  52. $model = Priority::TopClear($user,$data);
  53. $model = $model->where('del_time',0)
  54. ->select($field)
  55. ->orderby('id', 'asc');
  56. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  57. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  58. return $model;
  59. }
  60. public function priorityList($data, $user){
  61. $model = $this->priorityCommon($data, $user);
  62. $list = $model->get()->toArray();
  63. $list = $this->fillPriorityList($list, $user);
  64. return [true, $list];
  65. }
  66. public function fillPriorityList($list, $user, $is_export = false){
  67. if(empty($list)) return $list;
  68. if($is_export){
  69. foreach ($list['data'] as $key => $value){
  70. $list['data'][$key]['type_title'] = Priority::TYPE_TITLE[$value['type']] ?? "";
  71. }
  72. }
  73. return $list;
  74. }
  75. public function priorityRule($data,$user, $is_check = true){
  76. if(empty($data['data'])) return [false,'数据不能为空!'];
  77. $code = array_column($data['data'],'code');
  78. $title = array_column($data['data'],'title');
  79. $code = array_map(function($val) {
  80. return $val !== null ? $val : 0;
  81. }, $code);
  82. $title = array_map(function($val) {
  83. return $val !== null ? $val : 0;
  84. }, $title);
  85. $code_count = array_count_values($code);
  86. $title_count = array_count_values($title);
  87. foreach ($code as $value){
  88. if(empty($value)) return [false,'编码不能为空!'];
  89. if($code_count[$value] > 1) return [false,'编码不能重复'];
  90. }
  91. foreach ($title as $value){
  92. if(empty($value)) return [false,'名称不能为空!'];
  93. if($title_count[$value] > 1) return [false,'名称不能重复'];
  94. }
  95. foreach ($data['data'] as $key => $value){
  96. $top_depart_id = $user['top_depart_id'];
  97. $data['data'][$key]['top_depart_id'] = $top_depart_id;
  98. $data['data'][$key]['upd_time'] = time();
  99. if(! isset($value['sort'])) return [false, '排序字段sort不存在'];
  100. if (filter_var($value['sort'], FILTER_VALIDATE_INT) === false) return [false, '排序字段sort必须是整数且不能含有小数点'];
  101. if(! isset(Priority::TYPE_TITLE[$value['type']])) return [false, 'type错误'];
  102. if($is_check){
  103. $data['data'][$key]['crt_time'] = time();
  104. $bool = Priority::whereRaw("binary code = '{$value['code']}'")
  105. ->where('top_depart_id', $top_depart_id)
  106. ->where('del_time',0)
  107. ->exists();
  108. }else{
  109. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  110. $bool = Priority::whereRaw("binary code = '{$value['code']}'")
  111. ->where('top_depart_id', $top_depart_id)
  112. ->where('id','<>',$data['id'])
  113. ->where('del_time',0)
  114. ->exists();
  115. }
  116. if($bool) return [false,'编码已存在'];
  117. }
  118. return [true, $data];
  119. }
  120. }