feeRule($data,$user,false); if(!$status) return [$status, $msg]; $update = $msg['data'][0]; $model = new Fee(); $model->where('id',$data['id'])->update($update); return [true, '']; } public function feeAdd($data,$user){ list($status,$msg) = $this->feeRule($data,$user); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); foreach ($msg['data'] as $value){ $model = new Fee(); $model->parent_id = $value['parent_id']; $model->title = $value['title']; $model->code = $value['code']; $model->is_other = $value['is_other'] ?? Fee::IS_OTHER_ZERO; $model->sort = $value['sort'] ?? 0; $model->top_depart_id = $value['top_depart_id']; $model->save(); } DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true,'']; } public function feeDel($data, $user){ list($status,$msg) = $this->checkFeeDel($data); if(! $status) return [false, $msg]; Fee::whereIn('id',$data['id'])->update([ 'del_time'=>time() ]); return [true,'']; } public function checkFeeDel($data){ if($this->isEmpty($data,'id')) return [false,'ID不能为空']; $bool = Fee::whereIn('parent_id', $data['id'])->where('del_time',0)->exists(); if($bool) return [false,'该费用存在子类型']; return [true, '']; } public function feeCommon($data,$user, $field = []){ if(empty($field)) $field = Fee::$field; $model = Fee::TopClear($user,$data); $model = $model->where('del_time',0) ->select($field) ->orderby('sort', 'asc') ->orderby('id', 'asc'); if(isset($data['parent_id'])) $model->where('parent_id', $data['parent_id']); if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%'); if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%'); return $model; } public function feeList($data, $user){ $model = $this->feeCommon($data, $user); $list = $model->get()->toArray(); $list = $this->fillFeeList($list, $user); $list_tree = $list; if(! empty($list_tree)) { $minParentId = min(array_column($list_tree, 'parent_id')); $list_tree = $this->makeTree($minParentId,$list_tree); $list_tree = $this->set_sort_circle($list_tree); } return [true,['data' => $list,'tree' => $list_tree]]; } public function fillFeeList($list, $user, $is_export = false){ if(empty($list)) return $list; if($is_export){ $map = Fee::where('del_time',0) ->whereIn('id', array_column($list['data'], 'parent_id')) ->select('code','id', 'title') ->get() ->toArray(); $map = array_column($map, null, 'id'); foreach ($list['data'] as $key => $value){ $tmp = $map[$value['parent_id']] ?? ""; $list['data'][$key]['parent_code'] = $tmp['code'] ?? ""; $list['data'][$key]['parent_title'] = $tmp['title'] ?? ""; $list['data'][$key]['is_other_title'] = Fee::IS_OTHER[$value['is_other']] ?? ""; } } return $list; } public function feeRule($data,$user, $is_check = true){ if(empty($data['data'])) return [false,'数据不能为空!']; $code = array_column($data['data'],'code'); $title = array_column($data['data'],'title'); $code = array_map(function($val) { return $val !== null ? $val : 0; }, $code); $title = array_map(function($val) { return $val !== null ? $val : 0; }, $title); $code_count = array_count_values($code); $title_count = array_count_values($title); foreach ($code as $value){ if(empty($value)) return [false,'编码不能为空!']; if($code_count[$value] > 1) return [false,'编码不能重复']; } foreach ($title as $value){ if(empty($value)) return [false,'名称不能为空!']; if($title_count[$value] > 1) return [false,'名称不能重复']; } foreach ($data['data'] as $key => $value){ $top_depart_id = $user['top_depart_id']; if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = 0; $data['data'][$key]['top_depart_id'] = $top_depart_id; $data['data'][$key]['upd_time'] = time(); if($is_check){ $data['data'][$key]['crt_time'] = time(); $bool = Fee::whereRaw("binary code = '{$value['code']}'") ->where('top_depart_id', $top_depart_id) ->where('del_time',0) ->exists(); }else{ if($this->isEmpty($data,'id')) return [false,'id不能为空!']; $bool = Fee::whereRaw("binary code = '{$value['code']}'") ->where('top_depart_id', $top_depart_id) ->where('id','<>',$data['id']) ->where('del_time',0) ->exists(); } if($bool) return [false,'编码已存在']; } return [true, $data]; } }