FeeService.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. // 1. 验证数据与获取清洗后的数据
  9. list($status, $msg) = $this->feeRule($data, $user, false);
  10. if(!$status) return [$status, $msg];
  11. $update = $msg['data'][0];
  12. try {
  13. // 2. 开启事务
  14. DB::beginTransaction();
  15. // 3. 更新当前节点本身
  16. $model = new Fee();
  17. $model->where('id', $data['id'])->update($update);
  18. // 4. 判断如果是顶级节点(parent_id == 0),则更新下面所有的子集
  19. if (isset($update['parent_id']) && $update['parent_id'] == 0) {
  20. // 获取当前顶级节点最新的两个字段值
  21. $isOther = $update['is_other'] ?? Fee::IS_OTHER_ZERO;
  22. $isDirect = $update['is_direct'] ?? Fee::IS_DIRECT_ZERO;
  23. // 调用递归方法,向下同步所有后代节点
  24. $this->syncChildrenAttributes($data['id'], $isOther, $isDirect);
  25. }
  26. DB::commit();
  27. return [true, ''];
  28. } catch (\Exception $exception) {
  29. DB::rollBack();
  30. return [false, $exception->getMessage()];
  31. }
  32. }
  33. /**
  34. * 辅助方法:递归更新所有子集的 is_other 和 is_direct 属性
  35. * * @param int $parentId 当前父级ID
  36. * @param int $isOther
  37. * @param int $isDirect
  38. */
  39. public function syncChildrenAttributes($parentId, $isOther, $isDirect) {
  40. // 1. 找出当前节点下的所有直接子节点
  41. $children = Fee::where('parent_id', $parentId)
  42. ->where('del_time', 0)
  43. ->get();
  44. // 如果没有子节点了,直接退出递归
  45. if ($children->isEmpty()) {
  46. return;
  47. }
  48. // 2. 批量更新这些直接子节点的这两个字段
  49. Fee::where('parent_id', $parentId)
  50. ->where('del_time', 0)
  51. ->update([
  52. 'is_other' => $isOther,
  53. 'is_direct' => $isDirect,
  54. ]);
  55. // 3. 递归向下:让每个子节点再去更新它们自己的子节点(孙子辈)
  56. foreach ($children as $child) {
  57. $this->syncChildrenAttributes($child->id, $isOther, $isDirect);
  58. }
  59. }
  60. public function feeAdd($data,$user){
  61. list($status,$msg) = $this->feeRule($data,$user);
  62. if(!$status) return [$status,$msg];
  63. try {
  64. DB::beginTransaction();
  65. foreach ($msg['data'] as $value){
  66. $model = new Fee();
  67. $model->parent_id = $value['parent_id'];
  68. $model->title = $value['title'];
  69. $model->code = $value['code'];
  70. $model->is_other = $value['is_other'] ?? Fee::IS_OTHER_ZERO;
  71. $model->is_direct = $value['is_direct'] ?? Fee::IS_DIRECT_ZERO;
  72. $model->sort = $value['sort'] ?? 0;
  73. $model->top_depart_id = $value['top_depart_id'];
  74. $model->save();
  75. }
  76. DB::commit();
  77. }catch (\Exception $exception){
  78. DB::rollBack();
  79. return [false,$exception->getMessage()];
  80. }
  81. return [true,''];
  82. }
  83. public function feeDel($data, $user){
  84. list($status,$msg) = $this->checkFeeDel($data);
  85. if(! $status) return [false, $msg];
  86. Fee::whereIn('id',$data['id'])->update([
  87. 'del_time'=>time()
  88. ]);
  89. return [true,''];
  90. }
  91. public function checkFeeDel($data){
  92. if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
  93. $bool = Fee::whereIn('parent_id', $data['id'])->where('del_time',0)->exists();
  94. if($bool) return [false,'该费用存在子类型'];
  95. return [true, ''];
  96. }
  97. public function feeCommon($data,$user, $field = []){
  98. if(empty($field)) $field = Fee::$field;
  99. $model = Fee::TopClear($user,$data);
  100. $model = $model->where('del_time',0)
  101. ->select($field)
  102. ->orderby('sort', 'asc')
  103. ->orderby('id', 'asc');
  104. if(isset($data['parent_id'])) $model->where('parent_id', $data['parent_id']);
  105. if(isset($data['is_direct'])) $model->where('is_direct', $data['is_direct']);
  106. if(isset($data['is_other'])) $model->where('is_other', $data['is_other']);
  107. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  108. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  109. return $model;
  110. }
  111. public function feeList($data, $user){
  112. $model = $this->feeCommon($data, $user);
  113. $list = $model->get()->toArray();
  114. $list = $this->fillFeeList($list, $user);
  115. $list_tree = $list;
  116. if(! empty($list_tree)) {
  117. $minParentId = min(array_column($list_tree, 'parent_id'));
  118. $list_tree = $this->makeTree($minParentId,$list_tree);
  119. $list_tree = $this->set_sort_circle($list_tree);
  120. }
  121. return [true,['data' => $list,'tree' => $list_tree]];
  122. }
  123. public function fillFeeList($list, $user, $is_export = false){
  124. if(empty($list)) return $list;
  125. if($is_export){
  126. $map = Fee::where('del_time',0)
  127. ->whereIn('id', array_column($list['data'], 'parent_id'))
  128. ->select('code','id', 'title')
  129. ->get()
  130. ->toArray();
  131. $map = array_column($map, null, 'id');
  132. foreach ($list['data'] as $key => $value){
  133. $tmp = $map[$value['parent_id']] ?? "";
  134. $list['data'][$key]['parent_code'] = $tmp['code'] ?? "";
  135. $list['data'][$key]['parent_title'] = $tmp['title'] ?? "";
  136. $list['data'][$key]['is_other_title'] = Fee::IS_OTHER[$value['is_other']] ?? "";
  137. $list['data'][$key]['is_direct_title'] = Fee::IS_DIRECT[$value['is_direct']] ?? "";
  138. }
  139. }
  140. return $list;
  141. }
  142. public function feeRule($data,$user, $is_check = true){
  143. if(empty($data['data'])) return [false,'数据不能为空!'];
  144. $code = array_column($data['data'],'code');
  145. $title = array_column($data['data'],'title');
  146. $code = array_map(function($val) {
  147. return $val !== null ? $val : 0;
  148. }, $code);
  149. $title = array_map(function($val) {
  150. return $val !== null ? $val : 0;
  151. }, $title);
  152. $code_count = array_count_values($code);
  153. $title_count = array_count_values($title);
  154. foreach ($code as $value){
  155. if(empty($value)) return [false,'编码不能为空!'];
  156. if($code_count[$value] > 1) return [false,'编码不能重复'];
  157. }
  158. foreach ($title as $value){
  159. if(empty($value)) return [false,'名称不能为空!'];
  160. if($title_count[$value] > 1) return [false,'名称不能重复'];
  161. }
  162. foreach ($data['data'] as $key => $value){
  163. $top_depart_id = $user['top_depart_id'];
  164. if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = 0;
  165. // 1. 基础互斥校验
  166. $isOther = $value['is_other'] ?? Fee::IS_OTHER_ZERO;
  167. $isDirect = $value['is_direct'] ?? Fee::IS_DIRECT_ZERO;
  168. if ($isOther && $isDirect) return [false, '是否其他费用和是否直接投入费用不能同时开启'];
  169. $data['data'][$key]['top_depart_id'] = $top_depart_id;
  170. $data['data'][$key]['upd_time'] = time();
  171. if($is_check){
  172. $data['data'][$key]['crt_time'] = time();
  173. $bool = Fee::whereRaw("binary code = '{$value['code']}'")
  174. ->where('top_depart_id', $top_depart_id)
  175. ->where('del_time',0)
  176. ->exists();
  177. }else{
  178. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  179. $bool = Fee::whereRaw("binary code = '{$value['code']}'")
  180. ->where('top_depart_id', $top_depart_id)
  181. ->where('id','<>',$data['id'])
  182. ->where('del_time',0)
  183. ->exists();
  184. }
  185. if($bool) return [false,'编码已存在'];
  186. }
  187. return [true, $data];
  188. }
  189. }