EmployeeService.php 38 KB

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