TeamService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Employee;
  4. use App\Model\Team;
  5. use App\Model\TeamDetails;
  6. use Illuminate\Support\Facades\DB;
  7. class TeamService extends Service
  8. {
  9. public function teamEdit($data,$user){
  10. list($status,$msg) = $this->teamRule($data, $user, false);
  11. if(!$status) return [$status,$msg];
  12. try {
  13. DB::beginTransaction();
  14. $model = Team::where('id',$data['id'])->first();
  15. $model->code = $data['code'] ?? '';
  16. $model->title = $data['title'] ?? '';
  17. $model->mark = $data['mark'] ?? '';
  18. $model->state = $data['state'] ?? 0;
  19. $model->charge_id = $data['charge_id'] ?? 0;
  20. $model->save();
  21. $time = time();
  22. TeamDetails::where('del_time',0)
  23. ->where('team_id', $model->id)
  24. ->update(['del_time' => $time]);
  25. $this->saveDetail($model->id, $time, $data);
  26. DB::commit();
  27. }catch (\Exception $exception){
  28. DB::rollBack();
  29. return [false,$exception->getMessage()];
  30. }
  31. return [true, ''];
  32. }
  33. public function teamAdd($data,$user){
  34. list($status,$msg) = $this->teamRule($data, $user);
  35. if(!$status) return [$status,$msg];
  36. try {
  37. DB::beginTransaction();
  38. $model = new Team();
  39. $model->code = $data['code'] ?? '';
  40. $model->title = $data['title'] ?? '';
  41. $model->mark = $data['mark'] ?? '';
  42. $model->state = $data['state'] ?? 0;
  43. $model->charge_id = $data['charge_id'] ?? 0;
  44. $model->crt_id = $user['id'];
  45. $model->top_depart_id = $user['top_depart_id'];
  46. $model->save();
  47. $this->saveDetail($model->id, time(), $data);
  48. DB::commit();
  49. }catch (\Exception $exception){
  50. DB::rollBack();
  51. return [false,$exception->getMessage()];
  52. }
  53. return [true, ''];
  54. }
  55. private function saveDetail($id, $time, $data){
  56. if(! empty($data['man_list'])){
  57. $unit = [];
  58. foreach ($data['man_list'] as $value){
  59. $unit[] = [
  60. 'team_id' => $id,
  61. 'data_id' => $value['data_id'],
  62. 'top_depart_id' => $value['top_depart_id'],
  63. 'crt_time' => $time,
  64. ];
  65. }
  66. if(! empty($unit)) TeamDetails::insert($unit);
  67. }
  68. }
  69. private function getDetail($id){
  70. $data = TeamDetails::where('del_time',0)
  71. ->where('team_id', $id)
  72. ->get()->toArray();
  73. $map = Employee::whereIn('id', array_unique(array_column($data,'data_id')))
  74. ->select('title','id','number')
  75. ->get()->toArray();
  76. $map = array_column($map,null,'id');
  77. $unit = [];
  78. foreach ($data as $value){
  79. $tmp = $map[$value['data_id']] ?? [];
  80. $unit[] = [
  81. 'data_id' => $value['data_id'],
  82. 'data_title' => $tmp['title'],
  83. 'data_code' => $tmp['number'],
  84. ];
  85. }
  86. $detail = [
  87. 'man_list' => $unit,
  88. ];
  89. return $detail;
  90. }
  91. public function teamDel($data){
  92. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  93. try {
  94. DB::beginTransaction();
  95. $time = time();
  96. Team::where('del_time',0)
  97. ->whereIn('id',$data['id'])
  98. ->update(['del_time' => $time]);
  99. TeamDetails::where('del_time',0)
  100. ->whereIn('team_id', $data['id'])
  101. ->update(['del_time' => $time]);
  102. DB::commit();
  103. }catch (\Exception $exception){
  104. DB::rollBack();
  105. return [false,$exception->getMessage()];
  106. }
  107. return [true, ''];
  108. }
  109. public function teamDetail($data, $user){
  110. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  111. $customer = Team::where('del_time',0)
  112. ->where('id',$data['id'])
  113. ->first();
  114. if(empty($customer)) return [false,'团队不存在或已被删除'];
  115. $customer = $customer->toArray();
  116. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title');
  117. $customer['charge_name'] = Employee::where('id',$customer['charge_id'])->value('title');
  118. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  119. $customer['state_title'] = Employee::State_Type[$customer['state']] ?? '';
  120. $details = $this->getDetail($data['id']);
  121. $customer = array_merge($customer, $details);
  122. return [true, $customer];
  123. }
  124. public function teamCommon($data,$user, $field = []){
  125. if(empty($field)) $field = Team::$field;
  126. $model = Team::TopClear($user,$data);
  127. $model = $model->where('del_time',0)
  128. ->select($field)
  129. ->orderby('id', 'desc');
  130. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  131. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  132. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  133. if(isset($data['state'])) $model->where('state', $data['state']);
  134. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  135. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  136. $model->where('crt_time','>=',$return[0]);
  137. $model->where('crt_time','<=',$return[1]);
  138. }
  139. return $model;
  140. }
  141. public function teamList($data,$user){
  142. $model = $this->teamCommon($data, $user);
  143. $list = $this->limit($model,'',$data);
  144. $list = $this->fillData($list);
  145. return [true, $list];
  146. }
  147. public function teamRule(&$data, $user, $is_add = true){
  148. if(empty($data['code'])) return [false, '团队编码不能为空'];
  149. if(! is_numeric($data['code'])) return [false, '团队编码请输入数字'];
  150. if(empty($data['title'])) return [false, '团队名称不能为空'];
  151. if(! isset($data['state'])) return [false, '状态不能为空'];
  152. if(! isset(Team::State_Type[$data['state']])) return [false, '状态不存在'];
  153. if(empty($data['man_list'])) return [false, '人员不能为空'];
  154. foreach ($data['man_list'] as $key => $value){
  155. if(empty($value['data_id'])) return [false, '人员不能为空'];
  156. $data['man_list'][$key]['top_depart_id'] = $user['top_depart_id'];
  157. }
  158. list($status, $msg) = $this->checkArrayRepeat($data['man_list'],'data_id','人员');
  159. if(! $status) return [false, $msg];
  160. if($is_add){
  161. $bool = Team::where('code',$data['code'])
  162. ->where('top_depart_id', $user['top_depart_id'])
  163. ->where('del_time',0)
  164. ->exists();
  165. }else{
  166. if(empty($data['id'])) return [false,'ID不能为空'];
  167. $bool = Team::where('code',$data['code'])
  168. ->where('top_depart_id', $user['top_depart_id'])
  169. ->where('id','<>',$data['id'])
  170. ->where('del_time',0)
  171. ->exists();
  172. }
  173. if($bool) return [false, '团队编码已存在'];
  174. return [true, ''];
  175. }
  176. public function fillData($data){
  177. if(empty($data['data'])) return $data;
  178. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_merge_recursive(array_column($data['data'],'charge_id'), array_column($data['data'],'crt_id'))));
  179. foreach ($data['data'] as $key => $value){
  180. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  181. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  182. $data['data'][$key]['charge_name'] = $emp[$value['charge_id']] ?? '';
  183. $data['data'][$key]['state_title'] = Team::State_Type[$value['state']] ?? "";
  184. }
  185. return $data;
  186. }
  187. /**
  188. * 填充项目导出数据(主表与明细平铺)
  189. */
  190. public function fillDataForExport($data, $column, $user, &$return)
  191. {
  192. if (empty($data)) return;
  193. $dataArray = is_array($data) ? $data : $data->toArray();
  194. $mainIds = array_column($dataArray, 'id');
  195. // 1. 获取详情映射 [team_id => [ [type_title=>..., code_2=>..., title=>...], ... ]]
  196. $detailsMap = $this->getDetailsMap($mainIds, $user);
  197. // 2. 获取主表状态映射
  198. $stateMap = \App\Model\Team::State_Type;
  199. $empMap = Employee::whereIn('id', array_unique(array_column($dataArray,'charge_id')))->get()->keyBy('id');
  200. foreach ($dataArray as $main) {
  201. $teamId = $main['id'];
  202. $details = $detailsMap[$teamId] ?? [];
  203. // 3. 提取并格式化主表信息
  204. $mainInfo = $main;
  205. $mainInfo['state_title'] = $stateMap[$main['state'] ?? ''] ?? '';
  206. $tmpEmp = $empMap[$main['charge_id']] ?? null;
  207. $mainInfo['charge_number'] = $tmpEmp ? $tmpEmp->number : '';
  208. $mainInfo['charge_name'] = $tmpEmp ? $tmpEmp->title : '';
  209. if (empty($details)) {
  210. // 如果单据没有明细,保底出一行
  211. $tempRow = [];
  212. foreach ($column as $col) {
  213. $tempRow[] = $mainInfo[$col] ?? '';
  214. }
  215. $return[] = $tempRow;
  216. } else {
  217. // 4. 核心平铺逻辑:每一行明细都带上主表信息
  218. foreach ($details as $sub) {
  219. // 合并主表数据和详情数据(sub 中包含 type_title, code_2, title 等)
  220. $fullRowData = array_merge($mainInfo, $sub);
  221. $tempRow = [];
  222. foreach ($column as $col) {
  223. $tempRow[] = $fullRowData[$col] ?? '';
  224. }
  225. $return[] = $tempRow;
  226. }
  227. }
  228. }
  229. }
  230. /**
  231. * 获取明细聚合映射表
  232. */
  233. public function getDetailsMap($mainIds, $user)
  234. {
  235. $details = TeamDetails::where('del_time', 0)
  236. ->whereIn('team_id', $mainIds)
  237. ->where('top_depart_id', $user['top_depart_id'])
  238. ->get();
  239. if ($details->isEmpty()) return [];
  240. // 1. 批量提取关联 ID
  241. $empIds = $details->pluck('data_id')->unique();
  242. // 2. 批量获取档案 Map
  243. $empMap = Employee::whereIn('id', $empIds)->get()->keyBy('id');
  244. $res = [];
  245. foreach ($details as $team) {
  246. $detailRow = [];
  247. $emp = $empMap[$team->data_id] ?? null;
  248. $detailRow['code_2'] = $emp ? $emp->number : '';
  249. $detailRow['title_2'] = $emp ? $emp->title : '';
  250. // 归档到对应的主表 ID 下
  251. $res[$team->team_id][] = $detailRow;
  252. }
  253. return $res;
  254. }
  255. }