EmployeeService.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Depart;
  4. use App\Model\Employee;
  5. use App\Model\EmployeeDepartPermission;
  6. use App\Model\EmployeeDetails;
  7. use App\Model\EmployeeRole;
  8. use App\Model\Role;
  9. use App\Model\RoleMenu;
  10. use App\Model\RoleMenuButton;
  11. use App\Model\SysMenu;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Hash;
  14. use Mockery\Exception;
  15. class EmployeeService extends Service
  16. {
  17. public function employeeEditOther($data,$user){
  18. list($status,$msg) = $this->employeeOtherRule($data,$user);
  19. if(!$status) return [$status,$msg];
  20. try {
  21. DB::beginTransaction();
  22. $model = new Employee();
  23. $model = $model->where('id',$user['id'])->first();
  24. $model->password = Hash::make($data['new_password']);
  25. $model->save();
  26. DB::commit();
  27. }catch (\Exception $exception){
  28. DB::rollBack();
  29. return [false, $exception->getMessage()];
  30. }
  31. return [true,''];
  32. }
  33. public function employeeOtherRule($data,$user){
  34. if(! isset($data['old_password'])) return [false,'请输入原密码'];
  35. if($data['old_password'] == "") return [false,'原密码不能为空'];
  36. if(! isset($data['new_password'])) return [false,'请输入新密码'];
  37. if($data['new_password'] == "") return [false,'新密码不能为空'];
  38. if(! isset($data['re_password'])) return [false,'请输入确认密码'];
  39. if($data['re_password'] == "") return [false,'确认密码不能为空'];
  40. if(! Hash::check($data['old_password'], $user['password'])) return [false,'原密码错误'];
  41. if($data['new_password'] == $data['old_password']) return [false,'原密码与新密码一致'];
  42. if($data['new_password'] !== $data['re_password']) return [false,'新密码与确认密码不一致'];
  43. return [true,''];
  44. }
  45. public function employeeEdit($data,$user){
  46. list($status,$msg) = $this->employeeRule($data,$user,false);
  47. if(!$status) return [$status,$msg];
  48. try {
  49. DB::beginTransaction();
  50. $model = new Employee();
  51. $model = $model->where('id',$data['id'])->first();
  52. $model->number = $data['number'] ?? "";
  53. $model->title = $data['title'] ?? "";
  54. $model->mobile = $data['mobile'] ?? "";
  55. $model->sex = $data['sex'] ?? 0;
  56. $model->education = $data['education'] ?? "";
  57. $model->id_card = $data['id_card'] ?? "";
  58. $model->major = $data['major'] ?? "";
  59. $model->p_title = $data['p_title'] ?? "";
  60. $model->state = $data['state'] ?? 0;
  61. $model->is_admin = $data['is_admin'] ?? 0;
  62. if($model->is_admin && $data['password'] !== '******') {
  63. $model->password = Hash::make($data['password']);
  64. $model->p_version = $model->p_version + 1;
  65. }
  66. $model->save();
  67. EmployeeDepartPermission::where('employee_id',$data['id'])->delete();
  68. if(isset($data['depart'])){
  69. $insert = [];
  70. foreach ($data['depart'] as $value){
  71. $insert[] = [
  72. 'employee_id' => $model->id,
  73. 'depart_id' => $value,
  74. 'top_depart_id' => $user['top_depart_id'],
  75. ];
  76. }
  77. EmployeeDepartPermission::insert($insert);
  78. }
  79. EmployeeRole::where('employee_id',$data['id'])->update([
  80. 'del_time' => time()
  81. ]);
  82. if(isset($data['role'])){
  83. $insert = [];
  84. foreach ($data['role'] as $value){
  85. $insert[] = [
  86. 'employee_id' => $model->id,
  87. 'role_id' => $value,
  88. 'crt_time' => time(),
  89. 'upd_time' => time(),
  90. ];
  91. }
  92. EmployeeRole::insert($insert);
  93. }
  94. DB::commit();
  95. }catch (\Exception $exception){
  96. DB::rollBack();
  97. return [false, $exception->getMessage()];
  98. }
  99. return [true,''];
  100. }
  101. public function employeeAdd($data,$user){
  102. list($status,$msg) = $this->employeeRule($data, $user);
  103. if(!$status) return [$status,$msg];
  104. try{
  105. DB::beginTransaction();
  106. $model = new Employee();
  107. $model->number = $data['number'] ?? "";
  108. $model->title = $data['title'] ?? "";
  109. $model->mobile = $data['mobile'] ?? "";
  110. $model->sex = $data['sex'] ?? 0;
  111. $model->education = $data['education'] ?? "";
  112. $model->id_card = $data['id_card'] ?? "";
  113. $model->major = $data['major'] ?? "";
  114. $model->p_title = $data['p_title'] ?? "";
  115. $model->state = $data['state'] ?? 0;
  116. $model->crt_id = $user['id'];
  117. $model->is_admin = $data['is_admin'] ?? 0;
  118. $model->account = $data['account'] ?? "";
  119. if($model->is_admin) $model->password = Hash::make($data['password']);
  120. $model->top_depart_id = $data['top_depart_id'];
  121. $model->save();
  122. if(isset($data['depart'])){
  123. $insert = [];
  124. foreach ($data['depart'] as $value){
  125. $insert[] = [
  126. 'employee_id' => $model->id,
  127. 'depart_id' => $value,
  128. 'top_depart_id' => $user['top_depart_id'],
  129. ];
  130. }
  131. EmployeeDepartPermission::insert($insert);
  132. }
  133. if(isset($data['role'])){
  134. $insert = [];
  135. foreach ($data['role'] as $value){
  136. $insert[] = [
  137. 'employee_id' => $model->id,
  138. 'role_id' => $value,
  139. 'crt_time' => time(),
  140. 'upd_time' => time(),
  141. ];
  142. }
  143. EmployeeRole::insert($insert);
  144. }
  145. DB::commit();
  146. }catch (Exception $e){
  147. DB::rollBack();
  148. return [false, $e->getMessage()];
  149. }
  150. return [true,''];
  151. }
  152. public function employeeDel($data){
  153. if($this->isEmpty($data,'id')) return [false,'请选择删除的数据!'];
  154. Employee::whereIn('id',$data['id'])->update([
  155. 'del_time'=>time()
  156. ]);
  157. EmployeeRole::where('del_time',0)->whereIn('employee_id',$data['id'])->update([
  158. 'del_time'=>time()
  159. ]);
  160. EmployeeDepartPermission::whereIn('employee_id',$data['id'])->delete();
  161. return [true,'删除成功'];
  162. }
  163. public function employeeDetail($data, $user){
  164. if(empty($data['id'])) return [false,'人员id不能为空'];
  165. list($status, $return) = $this->employeeList(['id' => [$data['id']]], $user);
  166. $user = $return['data'][0] ?? [];
  167. return [true, $user];
  168. }
  169. public function employeeCommon($data,$user, $field = []){
  170. if(empty($field)) $field = Employee::$field;
  171. $model = Employee::TopClear($user,$data);
  172. $model = $model->where('del_time',0)
  173. ->where('is_admin', '<>', Employee::IS_ADMIN_TWO)
  174. ->select($field)
  175. ->orderBy('id','desc');
  176. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  177. if(! empty($data['number'])) $model->where('number', 'LIKE', '%'.$data['number'].'%');
  178. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  179. if(! empty($data['mobile'])) $model->where('mobile', 'LIKE', '%'.$data['mobile'].'%');
  180. if(isset($data['is_admin'])) $model->where('is_admin', $data['is_admin']);
  181. if(! empty($data['state'])) $model->where('state', $data['state']);
  182. if(isset($data['education'])) $model->where('education', $data['education']);
  183. if(! empty($data['role'])) {
  184. $emp = EmployeeRole::where('role_id',$data['role'])
  185. ->where('del_time',0)
  186. ->select('employee_id')->get()->toArray();
  187. $model->whereIn('id',array_column($emp,'employee_id'));
  188. }
  189. if(! empty($data['depart'])) {
  190. // 1. 获取所有子部门 ID(包含自身)
  191. $allDeparts = Depart::where('del_time', 0)->select('id', 'parent_id')->get();
  192. $departIds = array_merge($this->getAllDescendants($allDeparts->toArray(), $data['depart']), [$data['depart']]);
  193. // 2. 优化:直接使用子查询,避免拉取巨大的 $employee_id 数组到内存
  194. $model->whereIn('id', function ($query) use ($departIds) {
  195. $query->select('employee_id')
  196. ->from('employee_depart_permission')
  197. ->whereIn('depart_id', $departIds);
  198. });
  199. }
  200. return $model;
  201. }
  202. /**
  203. * 产品列表
  204. * @param $data
  205. * @param $user
  206. * @return array
  207. */
  208. public function employeeList($data,$user){
  209. $model = $this->employeeCommon($data, $user);
  210. $list = $this->limit($model,'',$data);
  211. $list = $this->organizationEmployeeData($list);
  212. return [true, $list];
  213. }
  214. public function organizationEmployeeData($data)
  215. {
  216. if (empty($data['data'])) return $data;
  217. // 获取员工ID并查询扩展数据
  218. $employee_ids = array_column($data['data'], 'id');
  219. list($status, $extraMap) = $this->getEmployee($employee_ids);
  220. foreach ($data['data'] as &$item) {
  221. $id = $item['id'];
  222. $extra = $extraMap[$id] ?? null;
  223. $item['role'] = $extra['role_ids'] ?? [];
  224. $item['role_name'] = isset($extra['role_names']) ? implode(',', $extra['role_names']) : '';
  225. $item['depart'] = $extra['depart_ids'] ?? [];
  226. $item['depart_title'] = isset($extra['depart_names']) ? implode(',', $extra['depart_names']) : '';
  227. // 业务状态字段
  228. $item['is_admin_title'] = Employee::IS_ADMIN_TITLE[$item['is_admin']] ?? "";
  229. $item['state_title'] = Employee::State_Type[$item['state']] ?? "";
  230. $item['sex_title'] = Employee::SEX_TYPE[$item['sex']] ?? "";
  231. $item['education_title'] = Employee::Education[$item['education']] ?? "";
  232. $item['crt_time'] = !empty($item['crt_time']) ? date("Y-m-d", $item['crt_time']) : "";
  233. }
  234. return $data;
  235. }
  236. public function getEmployee(array $employee_ids)
  237. {
  238. if (empty($employee_ids)) return [false, []];
  239. // 1. 一次性获取所有角色
  240. $roles = DB::table('employee_role as a')
  241. ->join('role as b', 'a.role_id', '=', 'b.id')
  242. ->where('a.del_time', 0)
  243. ->where('b.del_time', 0)
  244. ->whereIn("a.employee_id", $employee_ids)
  245. ->select('a.employee_id', 'b.title', 'b.id')
  246. ->get();
  247. // 2. 一次性获取所有部门
  248. $departs = DB::table('employee_depart_permission as a')
  249. ->join('depart as b', 'a.depart_id', '=', 'b.id')
  250. ->whereIn("a.employee_id", $employee_ids)
  251. ->select('a.employee_id', 'b.title', 'b.id')
  252. ->orderBy('b.id')
  253. ->get();
  254. // 3. 结果按员工ID分组归纳
  255. $resultMap = [];
  256. foreach ($employee_ids as $id) {
  257. $resultMap[$id] = [
  258. 'role_ids' => [],
  259. 'role_names' => [],
  260. 'depart_ids' => [],
  261. 'depart_names' => []
  262. ];
  263. }
  264. foreach ($roles as $r) {
  265. $resultMap[$r->employee_id]['role_ids'][] = $r->id;
  266. $resultMap[$r->employee_id]['role_names'][] = $r->title;
  267. }
  268. foreach ($departs as $d) {
  269. $resultMap[$d->employee_id]['depart_ids'][] = $d->id;
  270. $resultMap[$d->employee_id]['depart_names'][] = $d->title;
  271. }
  272. return [true, $resultMap];
  273. }
  274. public function fillDataForExport($data, $column, $user, &$return)
  275. {
  276. if (empty($data)) return;
  277. $dataArray = is_array($data) ? $data : $data->toArray();
  278. $mainIds = array_column($dataArray, 'id');
  279. // 1. 获取详情映射 [员工ID => [ [部门编码, 部门名称], [部门编码, 部门名称] ]]
  280. $detailsMap = $this->getEmployeeDetailsMap($mainIds, $user);
  281. // 枚举映射
  282. $sexMap = \App\Model\Employee::SEX_TYPE;
  283. $eduMap = \App\Model\Employee::Education;
  284. $stateMap = \App\Model\Employee::State_Type;
  285. foreach ($dataArray as $main) {
  286. $empId = $main['id'];
  287. $details = $detailsMap[$empId] ?? [];
  288. // 2. 格式化主表信息(这些是这一人所有行共有的内容)
  289. $mainInfo = $main;
  290. $mainInfo['sex_title'] = $sexMap[$main['sex']] ?? '';
  291. $mainInfo['education_title'] = $eduMap[$main['education']] ?? '';
  292. $mainInfo['state_title'] = $stateMap[$main['state']] ?? '';
  293. if (empty($details)) {
  294. // 如果没部门,保底出一行
  295. $tempRow = [];
  296. foreach ($column as $col) {
  297. $tempRow[] = $mainInfo[$col] ?? '';
  298. }
  299. $return[] = $tempRow;
  300. } else {
  301. // 3. 核心拆分点:遍历部门明细,每一个部门都产生一个新的 $tempRow
  302. foreach ($details as $sub) {
  303. // 将员工基本信息与“当前这一个”部门信息合并
  304. $fullRowData = array_merge($mainInfo, $sub);
  305. $tempRow = [];
  306. // 严格按配置顺序取值
  307. foreach ($column as $col) {
  308. $tempRow[] = $fullRowData[$col] ?? '';
  309. }
  310. // 【重点】这里是追加,不是覆盖!
  311. // 如果张三有2个部门,这里会先后 push 两次,Excel 就会多出两行
  312. $return[] = $tempRow;
  313. }
  314. }
  315. }
  316. }
  317. public function getEmployeeDetailsMap($empIds, $user)
  318. {
  319. // 关联查询权限表和部门档案
  320. $list = DB::table('employee_depart_permission as p')
  321. ->join('depart as d', 'p.depart_id', '=', 'd.id')
  322. ->whereIn('p.employee_id', $empIds)
  323. ->where('p.top_depart_id', $user['top_depart_id'])
  324. ->where('d.del_time', 0)
  325. ->select('p.employee_id', 'd.code as depart_code', 'd.title as depart_title')
  326. ->get();
  327. $res = [];
  328. foreach ($list as $item) {
  329. // 【重点】注意这里的 [],它保证了同一个 employee_id 下可以挂载多个部门数组
  330. $res[$item->employee_id][] = [
  331. 'depart_code' => $item->depart_code,
  332. 'depart_title' => $item->depart_title,
  333. ];
  334. }
  335. return $res;
  336. }
  337. public function employeeRule(&$data, $user,$is_add = true){
  338. if(empty($data['number'])) return [false,'工号不存在'];
  339. if(empty($data['title'])) return [false,'姓名不存在'];
  340. if(! empty($data['sex']) && ! isset(Employee::SEX_TYPE[$data['sex']])) return [false, '性别不存在'];
  341. if(empty($data['mobile'])) return [false,'联系电话不能为空'];
  342. // if(! $this->isValidPhone($data['mobile'])) return [false, '手机号码格式错误'];
  343. if(! empty($data['education']) && ! isset(Employee::Education[$data['education']])) return [false, '学历不存在'];
  344. if(empty($data['id_card'])) return [false, '身份证号不能为空'];
  345. if(empty($data['depart'])) return [false,'部门不能为空'];
  346. if(empty($data['state'])) return [false,'状态不能为空'];
  347. if(! isset(Employee::State_Type[$data['state']])) return [false,'状态不存在'];
  348. if(! empty($data['is_admin'])){
  349. if(! isset(Employee::IS_ADMIN_TITLE_SIMPLE[$data['is_admin']])) return [false, 'is_admin类型不存在'];
  350. if(empty($data['password'])) return [false, '密码不能为空'];
  351. if(mb_strlen($data['password']) < 6) return [false, '密码长度不得小于6位长度'];
  352. if(empty($data['role'])) return [false, '角色不能为空'];
  353. }
  354. $data['top_depart_id'] = $user['top_depart_id'];
  355. if(! $is_add){
  356. if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
  357. $bool = Employee::where('del_time',0)
  358. ->where('id','<>',$data['id'])
  359. ->where('top_depart_id', $data['top_depart_id'])
  360. ->where('number', $data['number'])
  361. ->exists();
  362. }else{
  363. $code = Depart::where('id', $user['top_depart_id'])->value('code');
  364. $data['account'] = $code . "_" . $data['number'];
  365. $bool = Employee::where('del_time',0)
  366. ->where('top_depart_id', $data['top_depart_id'])
  367. ->where('number', $data['number'])
  368. ->exists();
  369. }
  370. if($bool) return [false,'人员工号已存在'];
  371. return [true,''];
  372. }
  373. public function roleEdit($data,$user){
  374. list($status,$msg) = $this->roleRule($data,$user, false);
  375. if(!$status) return [$status,$msg];
  376. $model = new Role();
  377. $model = $model->where('id',$data['id'])->first();
  378. $model->title = $data['title'];
  379. $model->save();
  380. return [true,''];
  381. }
  382. public function roleAdd($data,$user){
  383. list($status,$msg) = $this->roleRule($data,$user);
  384. if(!$status) return [$status,$msg];
  385. $model = new Role();
  386. $model->title = $data['title'] ;
  387. $model->top_depart_id = $data['top_depart_id'];
  388. $model->save();
  389. return [true,''];
  390. }
  391. public function roleDel($data){
  392. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  393. $bool = EmployeeRole::where('del_time',0)
  394. ->whereIn('role_id',$data['id'])
  395. ->exists();
  396. if($bool) return [false,'角色已绑定人员'];
  397. Role::where('id',$data['id'])->update([
  398. 'del_time' => time()
  399. ]);
  400. RoleMenu::where('del_time',0)->where('role_id',$data['id'])->update([
  401. 'del_time' => time()
  402. ]);
  403. RoleMenuButton::where('del_time',0)->where('role_id',$data['id'])->update([
  404. 'del_time' => time()
  405. ]);
  406. return [true, ''];
  407. }
  408. public function roleList($data,$user){
  409. $model = Role::TopClear($user,$data);
  410. $model = $model->where('del_time',0)
  411. ->select('title','crt_time','id','upd_time')
  412. ->orderBy('id','desc');
  413. if(! empty($data['title'])) $model->where('title', 'LIKE', '%' . $data['title'] . '%');
  414. $list = $this->limit($model,'',$data);
  415. return [true, $list];
  416. }
  417. public function roleRule(&$data,$user, $is_check = true){
  418. if($this->isEmpty($data,'title')) return [false,'名称不能为空'];
  419. $data['top_depart_id'] = $user['top_depart_id'];
  420. if($is_check){
  421. $bool = Role::where('title',$data['title'])
  422. ->where('top_depart_id', $data['top_depart_id'])
  423. ->where('del_time',0)
  424. ->exists();
  425. if($bool) return [false,'角色名称已存在'];
  426. }else{
  427. if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
  428. $top_depart_id = Role::where('id',$data['id'])->value('top_depart_id');
  429. $bool = Role::where('title',$data['title'])
  430. ->where('top_depart_id',$top_depart_id)
  431. ->where('id','<>',$data['id'])
  432. ->where('del_time',0)
  433. ->exists();
  434. if($bool) return [false,'角色名称已存在'];
  435. }
  436. return [true, ''];
  437. }
  438. public function roleMenu($data){
  439. if(empty($data['role_id'])) return [false,'角色不能为空!'];
  440. if(empty($data['menu'])) return [false,'菜单数据不能为空!'];
  441. DB::beginTransaction();
  442. try {
  443. RoleMenu::where('del_time',0)->where('role_id',$data['role_id'])->update(['del_time' => time()]);
  444. RoleMenuButton::where('del_time',0)->where('role_id',$data['role_id'])->update(['del_time' => time()]);
  445. $insert = $insert2 = [];
  446. foreach ($data['menu'] as $t){
  447. $insert[] = [
  448. 'role_id' => $data['role_id'],
  449. 'menu_id' => $t['menu_id'],
  450. 'type' => $t['type'],
  451. 'crt_time' => time()
  452. ];
  453. if(! empty($t['button'])){
  454. foreach ($t['button'] as $b){
  455. $insert2[] = [
  456. 'role_id' => $data['role_id'],
  457. 'menu_id' => $t['menu_id'],
  458. 'button_id' => $b,
  459. 'crt_time' => time()
  460. ];
  461. }
  462. RoleMenuButton::insert($insert2);
  463. }
  464. }
  465. RoleMenu::insert($insert);
  466. DB::commit();
  467. }catch (\Throwable $exception){
  468. DB::rollBack();
  469. return [false,$exception->getMessage()];
  470. }
  471. return [true, ''];
  472. }
  473. public function roleDetail($data){
  474. if(empty($data['role_id'])) return [false,'请选择角色'];
  475. $role = Role::where('id',$data['role_id'])
  476. ->where('del_time',0)
  477. ->select('id','title')
  478. ->first();
  479. if(empty($role)) return [false,'角色不存在或已被删除'];
  480. $role = $role->toArray();
  481. $menu = RoleMenu::where('role_id',$data['role_id'])
  482. ->where('del_time',0)
  483. ->select('menu_id','type')
  484. ->get()->toArray();
  485. $button = $this->fillRoleButton([$data['role_id']]);
  486. foreach ($menu as $key => $value){
  487. $menu[$key]['button'] = $button[$value['menu_id']] ?? [];
  488. }
  489. $role['menu'] = $menu;
  490. return [true, $role];
  491. }
  492. public function departEdit($data, $user){
  493. list($status,$msg) = $this->departRule($data,$user,false);
  494. if(!$status) return [$status,$msg];
  495. $update = $msg['data'][0];
  496. $model = new Depart();
  497. $model->where('id',$data['id'])->update($update);
  498. return [true, ''];
  499. }
  500. public function departAdd($data,$user){
  501. list($status,$msg) = $this->departRule($data,$user);
  502. if(!$status) return [$status,$msg];
  503. try {
  504. DB::beginTransaction();
  505. foreach ($msg['data'] as $value){
  506. $model = new Depart();
  507. $model->parent_id = $value['parent_id'];
  508. $model->title = $value['title'];
  509. $model->code = $value['code'];
  510. $model->top_depart_id = $value['top_depart_id'];
  511. $model->save();
  512. }
  513. DB::commit();
  514. }catch (\Exception $exception){
  515. DB::rollBack();
  516. return [false,$exception->getMessage()];
  517. }
  518. return [true,''];
  519. }
  520. public function departDel($data){
  521. list($status,$msg) = $this->checkDepartDel($data);
  522. if(! $status) return [false, $msg];
  523. Depart::whereIn('id',$data['id'])->update([
  524. 'del_time'=>time()
  525. ]);
  526. return [true,''];
  527. }
  528. public function checkDepartDel($data){
  529. if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
  530. $bool = Depart::whereIn('parent_id',$data['id'])->where('del_time',0)->exists();
  531. if($bool) return [false,'部门下有子部门'];
  532. if($this->checkDepartHasPerson($data['id'])) return [false,'部门下有人员档案'];
  533. return [true, ''];
  534. }
  535. public function departCommon($data, $user, $field = []){
  536. if(empty($field)) $field = Depart::$field;
  537. $model = Depart::TopClear($user,$data);
  538. $model = $model->where('del_time',0)
  539. ->select($field)
  540. ->orderby('id', 'asc');
  541. if(isset($data['parent_id'])) $model->where('parent_id', $data['parent_id']);
  542. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  543. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  544. return $model;
  545. }
  546. public function departList($data, $user){
  547. $model = $this->departCommon($data, $user);
  548. $list = $model->get()->toArray();
  549. $list = $this->fillDepartList($list, $user);
  550. $list_tree = $list;
  551. if(! empty($list_tree)) {
  552. $minParentId = min(array_column($list_tree, 'parent_id'));
  553. $list_tree = $this->makeTree($minParentId,$list_tree);
  554. $list_tree = $this->set_sort_circle($list_tree);
  555. }
  556. return [true,['data' => $list,'tree' => $list_tree]];
  557. }
  558. public function fillDepartList($list,$user, $is_export = false){
  559. if($is_export){
  560. $map = Depart::where('del_time',0)
  561. ->whereIn('id', array_column($list['data'], 'parent_id'))
  562. ->select('code','id', 'title')
  563. ->get()->toArray();
  564. $map = array_column($map,null,'id');
  565. foreach ($list['data'] as $key => $value){
  566. $tmp = $map[$value['parent_id']] ?? "";
  567. if($tmp['code'] == $user['top_depart_code']) {
  568. $code = $title = "";
  569. }else{
  570. $code = $tmp['code'];
  571. $title = $tmp['title'];
  572. }
  573. $list['data'][$key]['parent_code'] = $code;
  574. $list['data'][$key]['parent_title'] = $title;
  575. }
  576. }
  577. return $list;
  578. }
  579. public function departRule($data,$user, $is_check = true){
  580. if(empty($data['data'])) return [false,'数据不能为空!'];
  581. $code = array_column($data['data'],'code');
  582. $title = array_column($data['data'],'title');
  583. $code = array_map(function($val) {
  584. return $val !== null ? $val : 0;
  585. }, $code);
  586. $title = array_map(function($val) {
  587. return $val !== null ? $val : 0;
  588. }, $title);
  589. $code_count = array_count_values($code);
  590. $title_count = array_count_values($title);
  591. foreach ($code as $value){
  592. if(empty($value)) return [false,'编码不能为空!'];
  593. if($code_count[$value] > 1) return [false,'编码不能重复'];
  594. }
  595. foreach ($title as $value){
  596. if(empty($value)) return [false,'名称不能为空!'];
  597. if($title_count[$value] > 1) return [false,'名称不能重复'];
  598. }
  599. foreach ($data['data'] as $key => $value){
  600. $top_depart_id = $user['top_depart_id'];
  601. $parent_id = $value['parent_id'];
  602. if(empty($value['parent_id'])) {
  603. $parent_id = $top_depart_id;
  604. $data['data'][$key]['parent_id'] = $parent_id;
  605. }
  606. $data['data'][$key]['top_depart_id'] = $top_depart_id;
  607. $data['data'][$key]['upd_time'] = time();
  608. if($is_check){
  609. $data['data'][$key]['crt_time'] = time();
  610. $bool = Depart::whereRaw("binary code = '{$value['code']}'")
  611. ->where('top_depart_id', $top_depart_id)
  612. ->where('del_time',0)
  613. ->exists();
  614. }else{
  615. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  616. $bool = Depart::whereRaw("binary code = '{$value['code']}'")
  617. ->where('top_depart_id', $top_depart_id)
  618. ->where('id','<>',$data['id'])
  619. ->where('del_time',0)
  620. ->exists();
  621. }
  622. if($bool) return [false,'部门编码已存在'];
  623. }
  624. return [true, $data];
  625. }
  626. public function checkDepartHasPerson($depart_id = []){
  627. if(empty($depart_id)) return false;
  628. $bool = EmployeeDepartPermission::from('employee_depart_permission as a')
  629. ->leftJoin('employee as b','b.id','a.employee_id')
  630. ->where('b.del_time',0)
  631. ->whereIn('a.depart_id',$depart_id)
  632. ->exists();
  633. return $bool;
  634. }
  635. public function fillRoleButton($role_id){
  636. $button = RoleMenuButton::whereIn('role_id',$role_id)
  637. ->where('del_time',0)
  638. ->select('menu_id','button_id')
  639. ->get()->toArray();
  640. $button_map = [];
  641. foreach ($button as $value){
  642. if(! isset($button_map[$value['menu_id']])){
  643. $button_map[$value['menu_id']][] = $value['button_id'];
  644. }else{
  645. if(! in_array($value['button_id'], $button_map[$value['menu_id']])) $button_map[$value['menu_id']][] = $value['button_id'];
  646. }
  647. }
  648. return $button_map;
  649. }
  650. public function getEmployeeMap($employee_ids){
  651. $employee_ids = array_filter((array)$employee_ids);
  652. if (empty($employee_ids)) return [];
  653. return Employee::whereIn('id', $employee_ids)
  654. ->pluck('title', 'id')
  655. ->toArray();
  656. }
  657. public static function fillMenu2($menu_id, &$user){
  658. // 直接查询匹配的菜单
  659. $menuItem = SysMenu::where('del_time',0)
  660. ->where('id', $menu_id)
  661. ->first();
  662. $func = $menuItem ? $menuItem->export_file_func : "";
  663. $funcName = $menuItem ? $menuItem->title : "";
  664. $header_default = config("excel." . $func) ?? [];
  665. // $header_f = "extra_" . $menu_id;
  666. // $service = new TableHeadService();
  667. // if(method_exists($service,$header_f)) $service->$header_f($header_default);
  668. $user['e_header_default'] = $header_default;
  669. return [$func, $funcName];
  670. }
  671. }