FeeService.php 5.6 KB

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