TeamService.php 11 KB

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