FeeService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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->top_depart_id = $value['top_depart_id'];
  26. $model->save();
  27. }
  28. DB::commit();
  29. }catch (\Exception $exception){
  30. DB::rollBack();
  31. return [false,$exception->getMessage()];
  32. }
  33. return [true,''];
  34. }
  35. public function feeDel($data, $user){
  36. list($status,$msg) = $this->checkFeeDel($data);
  37. if(! $status) return [false, $msg];
  38. Fee::whereIn('id',$data['id'])->update([
  39. 'del_time'=>time()
  40. ]);
  41. return [true,''];
  42. }
  43. public function checkFeeDel($data){
  44. if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
  45. $bool = Fee::whereIn('parent_id', $data['id'])->where('del_time',0)->exists();
  46. if($bool) return [false,'该费用存在子类型'];
  47. return [true, ''];
  48. }
  49. public function feeCommon($data,$user, $field = []){
  50. if(empty($field)) $field = Fee::$field;
  51. $model = Fee::TopClear($user,$data);
  52. $model = $model->where('del_time',0)
  53. ->select($field)
  54. ->orderby('id', 'asc');
  55. if(isset($data['parent_id'])) $model->where('parent_id', $data['parent_id']);
  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 feeList($data, $user){
  61. $model = $this->feeCommon($data, $user);
  62. $list = $model->get()->toArray();
  63. $list = $this->fillFeeList($list, $user);
  64. $list_tree = $list;
  65. if(! empty($list_tree)) {
  66. $minParentId = min(array_column($list_tree, 'parent_id'));
  67. $list_tree = $this->makeTree($minParentId,$list_tree);
  68. $list_tree = $this->set_sort_circle($list_tree);
  69. }
  70. return [true,['data' => $list,'tree' => $list_tree]];
  71. }
  72. public function fillFeeList($list,$user, $is_export = false){
  73. if(empty($list)) return $list;
  74. if($is_export){
  75. $map = Fee::where('del_time',0)
  76. ->whereIn('id', array_column($list['data'], 'parent_id'))
  77. ->pluck('code','id')
  78. ->toArray();
  79. foreach ($list['data'] as $key => $value){
  80. $list['data'][$key]['parent_code'] = $map[$value['parent_id']] ?? "";
  81. }
  82. }
  83. return $list;
  84. }
  85. public function feeRule($data,$user, $is_check = true){
  86. if(empty($data['data'])) return [false,'数据不能为空!'];
  87. $code = array_column($data['data'],'code');
  88. $title = array_column($data['data'],'title');
  89. $code = array_map(function($val) {
  90. return $val !== null ? $val : 0;
  91. }, $code);
  92. $title = array_map(function($val) {
  93. return $val !== null ? $val : 0;
  94. }, $title);
  95. $code_count = array_count_values($code);
  96. $title_count = array_count_values($title);
  97. foreach ($code as $value){
  98. if(empty($value)) return [false,'编码不能为空!'];
  99. if($code_count[$value] > 1) return [false,'编码不能重复'];
  100. }
  101. foreach ($title as $value){
  102. if(empty($value)) return [false,'名称不能为空!'];
  103. if($title_count[$value] > 1) return [false,'名称不能重复'];
  104. }
  105. foreach ($data['data'] as $key => $value){
  106. $top_depart_id = $user['top_depart_id'];
  107. if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = 0;
  108. $data['data'][$key]['top_depart_id'] = $top_depart_id;
  109. $data['data'][$key]['upd_time'] = time();
  110. if($is_check){
  111. $data['data'][$key]['crt_time'] = time();
  112. $bool = Fee::whereRaw("binary code = '{$value['code']}'")
  113. ->where('top_depart_id', $top_depart_id)
  114. ->where('del_time',0)
  115. ->exists();
  116. }else{
  117. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  118. $bool = Fee::whereRaw("binary code = '{$value['code']}'")
  119. ->where('top_depart_id', $top_depart_id)
  120. ->where('id','<>',$data['id'])
  121. ->where('del_time',0)
  122. ->exists();
  123. }
  124. if($bool) return [false,'编码已存在'];
  125. }
  126. return [true, $data];
  127. }
  128. }