FeeService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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){
  73. if(empty($list)) return $list;
  74. // foreach ($list as $key => $value){
  75. //
  76. // }
  77. return $list;
  78. }
  79. public function feeRule($data,$user, $is_check = true){
  80. if(empty($data['data'])) return [false,'数据不能为空!'];
  81. $code = array_column($data['data'],'code');
  82. $title = array_column($data['data'],'title');
  83. $code = array_map(function($val) {
  84. return $val !== null ? $val : 0;
  85. }, $code);
  86. $title = array_map(function($val) {
  87. return $val !== null ? $val : 0;
  88. }, $title);
  89. $code_count = array_count_values($code);
  90. $title_count = array_count_values($title);
  91. foreach ($code as $value){
  92. if(empty($value)) return [false,'编码不能为空!'];
  93. if($code_count[$value] > 1) return [false,'编码不能重复'];
  94. }
  95. foreach ($title as $value){
  96. if(empty($value)) return [false,'名称不能为空!'];
  97. if($title_count[$value] > 1) return [false,'名称不能重复'];
  98. }
  99. foreach ($data['data'] as $key => $value){
  100. $top_depart_id = $user['top_depart_id'];
  101. if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = 0;
  102. $data['data'][$key]['top_depart_id'] = $top_depart_id;
  103. $data['data'][$key]['upd_time'] = time();
  104. if($is_check){
  105. $data['data'][$key]['crt_time'] = time();
  106. $bool = Fee::whereRaw("binary code = '{$value['code']}'")
  107. ->where('top_depart_id', $top_depart_id)
  108. ->where('del_time',0)
  109. ->exists();
  110. }else{
  111. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  112. $bool = Fee::whereRaw("binary code = '{$value['code']}'")
  113. ->where('top_depart_id', $top_depart_id)
  114. ->where('id','<>',$data['id'])
  115. ->where('del_time',0)
  116. ->exists();
  117. }
  118. if($bool) return [false,'编码已存在'];
  119. }
  120. return [true, $data];
  121. }
  122. }