FeeService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Fee;
  4. use Illuminate\Support\Facades\DB;
  5. class FeeService extends Service
  6. {
  7. public function feeEdit($data, $user){
  8. list($status,$msg) = $this->feeRule($data,$user,false);
  9. if(!$status) return [$status, $msg];
  10. $update = $msg['data'][0];
  11. $model = new Fee();
  12. $model->where('id',$data['id'])->update($update);
  13. return [true, ''];
  14. }
  15. public function feeAdd($data,$user){
  16. list($status,$msg) = $this->feeRule($data,$user);
  17. if(!$status) return [$status,$msg];
  18. try {
  19. DB::beginTransaction();
  20. foreach ($msg['data'] as $value){
  21. $model = new Fee();
  22. $model->parent_id = $value['parent_id'];
  23. $model->title = $value['title'];
  24. $model->code = $value['code'];
  25. $model->is_other = $value['is_other'] ?? Fee::IS_OTHER_ZERO;
  26. $model->sort = $value['sort'] ?? 0;
  27. $model->top_depart_id = $value['top_depart_id'];
  28. $model->save();
  29. }
  30. DB::commit();
  31. }catch (\Exception $exception){
  32. DB::rollBack();
  33. return [false,$exception->getMessage()];
  34. }
  35. return [true,''];
  36. }
  37. public function feeDel($data, $user){
  38. list($status,$msg) = $this->checkFeeDel($data);
  39. if(! $status) return [false, $msg];
  40. Fee::whereIn('id',$data['id'])->update([
  41. 'del_time'=>time()
  42. ]);
  43. return [true,''];
  44. }
  45. public function checkFeeDel($data){
  46. if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
  47. $bool = Fee::whereIn('parent_id', $data['id'])->where('del_time',0)->exists();
  48. if($bool) return [false,'该费用存在子类型'];
  49. return [true, ''];
  50. }
  51. public function feeCommon($data,$user, $field = []){
  52. if(empty($field)) $field = Fee::$field;
  53. $model = Fee::TopClear($user,$data);
  54. $model = $model->where('del_time',0)
  55. ->select($field)
  56. ->orderby('sort', 'asc')
  57. ->orderby('id', 'asc');
  58. if(isset($data['parent_id'])) $model->where('parent_id', $data['parent_id']);
  59. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  60. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  61. return $model;
  62. }
  63. public function feeList($data, $user){
  64. $model = $this->feeCommon($data, $user);
  65. $list = $model->get()->toArray();
  66. $list = $this->fillFeeList($list, $user);
  67. $list_tree = $list;
  68. if(! empty($list_tree)) {
  69. $minParentId = min(array_column($list_tree, 'parent_id'));
  70. $list_tree = $this->makeTree($minParentId,$list_tree);
  71. $list_tree = $this->set_sort_circle($list_tree);
  72. }
  73. return [true,['data' => $list,'tree' => $list_tree]];
  74. }
  75. public function fillFeeList($list, $user, $is_export = false){
  76. if(empty($list)) return $list;
  77. if($is_export){
  78. $map = Fee::where('del_time',0)
  79. ->whereIn('id', array_column($list['data'], 'parent_id'))
  80. ->select('code','id', 'title')
  81. ->get()
  82. ->toArray();
  83. $map = array_column($map, null, 'id');
  84. foreach ($list['data'] as $key => $value){
  85. $tmp = $map[$value['parent_id']] ?? "";
  86. $list['data'][$key]['parent_code'] = $tmp['code'] ?? "";
  87. $list['data'][$key]['parent_title'] = $tmp['title'] ?? "";
  88. $list['data'][$key]['is_other_title'] = Fee::IS_OTHER[$value['is_other']] ?? "";
  89. }
  90. }
  91. return $list;
  92. }
  93. public function feeRule($data,$user, $is_check = true){
  94. if(empty($data['data'])) return [false,'数据不能为空!'];
  95. $code = array_column($data['data'],'code');
  96. $title = array_column($data['data'],'title');
  97. $code = array_map(function($val) {
  98. return $val !== null ? $val : 0;
  99. }, $code);
  100. $title = array_map(function($val) {
  101. return $val !== null ? $val : 0;
  102. }, $title);
  103. $code_count = array_count_values($code);
  104. $title_count = array_count_values($title);
  105. foreach ($code as $value){
  106. if(empty($value)) return [false,'编码不能为空!'];
  107. if($code_count[$value] > 1) return [false,'编码不能重复'];
  108. }
  109. foreach ($title as $value){
  110. if(empty($value)) return [false,'名称不能为空!'];
  111. if($title_count[$value] > 1) return [false,'名称不能重复'];
  112. }
  113. foreach ($data['data'] as $key => $value){
  114. $top_depart_id = $user['top_depart_id'];
  115. if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = 0;
  116. $data['data'][$key]['top_depart_id'] = $top_depart_id;
  117. $data['data'][$key]['upd_time'] = time();
  118. if($is_check){
  119. $data['data'][$key]['crt_time'] = time();
  120. $bool = Fee::whereRaw("binary code = '{$value['code']}'")
  121. ->where('top_depart_id', $top_depart_id)
  122. ->where('del_time',0)
  123. ->exists();
  124. }else{
  125. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  126. $bool = Fee::whereRaw("binary code = '{$value['code']}'")
  127. ->where('top_depart_id', $top_depart_id)
  128. ->where('id','<>',$data['id'])
  129. ->where('del_time',0)
  130. ->exists();
  131. }
  132. if($bool) return [false,'编码已存在'];
  133. }
  134. return [true, $data];
  135. }
  136. }