|
|
@@ -0,0 +1,316 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Service;
|
|
|
+
|
|
|
+use App\Model\Employee;
|
|
|
+use App\Model\Team;
|
|
|
+use App\Model\TeamDetails;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+
|
|
|
+class TeamService extends Service
|
|
|
+{
|
|
|
+ public function teamEdit($data,$user){
|
|
|
+ list($status,$msg) = $this->teamRule($data, $user, false);
|
|
|
+ if(!$status) return [$status,$msg];
|
|
|
+
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+
|
|
|
+ $model = Team::where('id',$data['id'])->first();
|
|
|
+ $model->code = $data['code'] ?? '';
|
|
|
+ $model->title = $data['title'] ?? '';
|
|
|
+ $model->mark = $data['mark'] ?? '';
|
|
|
+ $model->state = $data['state'] ?? 0;
|
|
|
+ $model->charge_id = $data['charge_id'] ?? 0;
|
|
|
+ $model->save();
|
|
|
+
|
|
|
+ $time = time();
|
|
|
+ TeamDetails::where('del_time',0)
|
|
|
+ ->where('team_id', $model->id)
|
|
|
+ ->update(['del_time' => $time]);
|
|
|
+ $this->saveDetail($model->id, $time, $data);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Exception $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false,$exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function teamAdd($data,$user){
|
|
|
+ list($status,$msg) = $this->teamRule($data, $user);
|
|
|
+ if(!$status) return [$status,$msg];
|
|
|
+
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+
|
|
|
+ $model = new Team();
|
|
|
+ $model->code = $data['code'] ?? '';
|
|
|
+ $model->title = $data['title'] ?? '';
|
|
|
+ $model->mark = $data['mark'] ?? '';
|
|
|
+ $model->state = $data['state'] ?? 0;
|
|
|
+ $model->charge_id = $data['charge_id'] ?? 0;
|
|
|
+ $model->crt_id = $user['id'];
|
|
|
+ $model->top_depart_id = $user['top_depart_id'];
|
|
|
+ $model->save();
|
|
|
+
|
|
|
+ $this->saveDetail($model->id, time(), $data);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Exception $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false,$exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ private function saveDetail($id, $time, $data){
|
|
|
+ if(! empty($data['man_list'])){
|
|
|
+ $unit = [];
|
|
|
+ foreach ($data['man_list'] as $value){
|
|
|
+ $unit[] = [
|
|
|
+ 'team_id' => $id,
|
|
|
+ 'data_id' => $value['data_id'],
|
|
|
+ 'crt_time' => $time,
|
|
|
+ 'top_depart_id' => $value['top_depart_id'],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ if(! empty($unit)) TeamDetails::insert($unit);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function getDetail($id){
|
|
|
+ $data = TeamDetails::where('del_time',0)
|
|
|
+ ->where('team_id', $id)
|
|
|
+ ->get()->toArray();
|
|
|
+
|
|
|
+ $map = Employee::whereIn('id', array_unique(array_column($data,'data_id')))
|
|
|
+ ->select('title','id','number')
|
|
|
+ ->get()->toArray();
|
|
|
+ $map = array_column($map,null,'id');
|
|
|
+
|
|
|
+ $unit = $receipt = [];
|
|
|
+ foreach ($data as $value){
|
|
|
+ $tmp = $map[$value['data_id']] ?? [];
|
|
|
+ $unit[] = [
|
|
|
+ 'type' => $value['type'],
|
|
|
+ 'data_id' => $value['data_id'],
|
|
|
+ 'data_title' => $tmp['title'],
|
|
|
+ 'data_code' => $tmp['number'],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ $detail = [
|
|
|
+ 'man_list' => $unit,
|
|
|
+ ];
|
|
|
+
|
|
|
+ return $detail;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function teamDel($data){
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
|
|
|
+
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+ $time = time();
|
|
|
+
|
|
|
+ Team::where('del_time',0)
|
|
|
+ ->whereIn('id',$data['id'])
|
|
|
+ ->update(['del_time' => $time]);
|
|
|
+
|
|
|
+ TeamDetails::where('del_time',0)
|
|
|
+ ->whereIn('team_id', $data['id'])
|
|
|
+ ->update(['del_time' => $time]);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Exception $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false,$exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function teamDetail($data, $user){
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
|
|
|
+ $customer = Team::where('del_time',0)
|
|
|
+ ->where('id',$data['id'])
|
|
|
+ ->first();
|
|
|
+ if(empty($customer)) return [false,'团队不存在或已被删除'];
|
|
|
+ $customer = $customer->toArray();
|
|
|
+ $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title');
|
|
|
+ $customer['charge_name'] = Employee::where('id',$customer['charge_id'])->value('title');
|
|
|
+ $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
|
|
|
+ $customer['state_title'] = Employee::State_Type[$customer['state']] ?? '';
|
|
|
+
|
|
|
+ $details = $this->getDetail($data['id']);
|
|
|
+ $customer = array_merge($customer, $details);
|
|
|
+
|
|
|
+ return [true, $customer];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function teamCommon($data,$user, $field = []){
|
|
|
+ if(empty($field)) $field = Team::$field;
|
|
|
+
|
|
|
+ $model = Team::TopClear($user,$data);
|
|
|
+ $model = $model->where('del_time',0)
|
|
|
+ ->select($field)
|
|
|
+ ->orderby('id', 'desc');
|
|
|
+
|
|
|
+ if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
|
|
|
+ if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
|
|
|
+ if(! empty($data['id'])) $model->whereIn('id', $data['id']);
|
|
|
+ if(isset($data['state'])) $model->where('state', $data['state']);
|
|
|
+ if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
|
|
|
+ $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
|
+ $model->where('crt_time','>=',$return[0]);
|
|
|
+ $model->where('crt_time','<=',$return[1]);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $model;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function teamList($data,$user){
|
|
|
+ $model = $this->teamCommon($data, $user);
|
|
|
+ $list = $this->limit($model,'',$data);
|
|
|
+ $list = $this->fillData($list);
|
|
|
+
|
|
|
+ return [true, $list];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function teamRule(&$data, $user, $is_add = true){
|
|
|
+ if(empty($data['code'])) return [false, '团队编码不能为空'];
|
|
|
+ if(! is_numeric($data['code'])) return [false, '团队编码请输入数字'];
|
|
|
+ if(empty($data['title'])) return [false, '团队名称不能为空'];
|
|
|
+ if(! isset($data['state'])) return [false, '状态不能为空'];
|
|
|
+ if(! isset(Team::State_Type[$data['state']])) return [false, '状态不存在'];
|
|
|
+ if(empty($data['man_list'])) return [false, '人员不能为空'];
|
|
|
+ foreach ($data['man_list'] as $key => $value){
|
|
|
+ if(empty($value['type'])) return [false, '类型不能为空'];
|
|
|
+ if(empty($value['data_id'])) return [false, '人员不能为空'];
|
|
|
+ $data['man_list'][$key]['top_depart_id'] = $data['top_depart_id'];
|
|
|
+ }
|
|
|
+ list($status, $msg) = $this->checkArrayRepeat($data['man_list'],'data_id','人员');
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
+
|
|
|
+ if($is_add){
|
|
|
+ $bool = Team::where('code',$data['code'])
|
|
|
+ ->where('top_depart_id', $data['top_depart_id'])
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->exists();
|
|
|
+ }else{
|
|
|
+ if(empty($data['id'])) return [false,'ID不能为空'];
|
|
|
+ $bool = Team::where('code',$data['code'])
|
|
|
+ ->where('top_depart_id', $data['top_depart_id'])
|
|
|
+ ->where('id','<>',$data['id'])
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->exists();
|
|
|
+ }
|
|
|
+ if($bool) return [false, '团队编码已存在'];
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function fillData($data){
|
|
|
+ if(empty($data['data'])) return $data;
|
|
|
+
|
|
|
+ $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_merge_recursive(array_column($data['data'],'charge_id'), array_column($data['data'],'crt_id'))));
|
|
|
+ foreach ($data['data'] as $key => $value){
|
|
|
+ $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
|
|
|
+ $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
|
|
|
+ $data['data'][$key]['charge_name'] = $emp[$value['charge_id']] ?? '';
|
|
|
+ $data['data'][$key]['state_title'] = Team::State_Type[$value['state']] ?? "";
|
|
|
+ }
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 填充项目导出数据(主表与明细平铺)
|
|
|
+ */
|
|
|
+ public function fillDataForExport($data, $column, $user, &$return)
|
|
|
+ {
|
|
|
+ if (empty($data)) return;
|
|
|
+
|
|
|
+ $dataArray = is_array($data) ? $data : $data->toArray();
|
|
|
+ $mainIds = array_column($dataArray, 'id');
|
|
|
+
|
|
|
+ // 1. 获取详情映射 [team_id => [ [type_title=>..., code_2=>..., title=>...], ... ]]
|
|
|
+ $detailsMap = $this->getDetailsMap($mainIds, $user);
|
|
|
+
|
|
|
+ // 2. 获取主表状态映射
|
|
|
+ $stateMap = \App\Model\Team::State_Type;
|
|
|
+
|
|
|
+ $empMap = Employee::whereIn('id', array_unique(array_column($dataArray,'charge_id')))->get()->keyBy('id');
|
|
|
+
|
|
|
+ foreach ($dataArray as $main) {
|
|
|
+ $teamId = $main['id'];
|
|
|
+ $details = $detailsMap[$teamId] ?? [];
|
|
|
+
|
|
|
+ // 3. 提取并格式化主表信息
|
|
|
+ $mainInfo = $main;
|
|
|
+ $mainInfo['state_title'] = $stateMap[$main['state'] ?? ''] ?? '';
|
|
|
+
|
|
|
+ $tmpEmp = $empMap[$main['charge_id']] ?? null;
|
|
|
+ $mainInfo['charge_number'] = $tmpEmp ? $tmpEmp->number : '';
|
|
|
+ $mainInfo['charge_name'] = $tmpEmp ? $tmpEmp->title : '';
|
|
|
+
|
|
|
+ if (empty($details)) {
|
|
|
+ // 如果单据没有明细,保底出一行
|
|
|
+ $tempRow = [];
|
|
|
+ foreach ($column as $col) {
|
|
|
+ $tempRow[] = $mainInfo[$col] ?? '';
|
|
|
+ }
|
|
|
+ $return[] = $tempRow;
|
|
|
+ } else {
|
|
|
+ // 4. 核心平铺逻辑:每一行明细都带上主表信息
|
|
|
+ foreach ($details as $sub) {
|
|
|
+ // 合并主表数据和详情数据(sub 中包含 type_title, code_2, title 等)
|
|
|
+ $fullRowData = array_merge($mainInfo, $sub);
|
|
|
+
|
|
|
+ $tempRow = [];
|
|
|
+ foreach ($column as $col) {
|
|
|
+ $tempRow[] = $fullRowData[$col] ?? '';
|
|
|
+ }
|
|
|
+ $return[] = $tempRow;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取明细聚合映射表
|
|
|
+ */
|
|
|
+ public function getDetailsMap($mainIds, $user)
|
|
|
+ {
|
|
|
+ $details = TeamDetails::where('del_time', 0)
|
|
|
+ ->whereIn('team_id', $mainIds)
|
|
|
+ ->where('top_depart_id', $user['top_depart_id'])
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ if ($details->isEmpty()) return [];
|
|
|
+
|
|
|
+ // 1. 批量提取关联 ID
|
|
|
+ $empIds = $details->pluck('data_id')->unique();
|
|
|
+
|
|
|
+ // 2. 批量获取档案 Map
|
|
|
+ $empMap = Employee::whereIn('id', $empIds)->get()->keyBy('id');
|
|
|
+
|
|
|
+ $res = [];
|
|
|
+ foreach ($details as $team) {
|
|
|
+ $detailRow = [];
|
|
|
+
|
|
|
+ $emp = $empMap[$team->data_id] ?? null;
|
|
|
+ $detailRow['code_2'] = $emp ? $emp->number : '';
|
|
|
+ $detailRow['title_2'] = $emp ? $emp->title : '';
|
|
|
+
|
|
|
+ // 归档到对应的主表 ID 下
|
|
|
+ $res[$team->team_id][] = $detailRow;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $res;
|
|
|
+ }
|
|
|
+}
|