| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985 |
- <?php
- namespace App\Service;
- use App\Model\CalendarDetails;
- use App\Model\Depart;
- use App\Model\Employee;
- use App\Model\EmployeeDepartPermission;
- use App\Model\EmployeeDetails;
- use App\Model\EmployeeRole;
- use App\Model\EmployeeWorkRange;
- use App\Model\Role;
- use App\Model\RoleMenu;
- use App\Model\RoleMenuButton;
- use App\Model\SysMenu;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Hash;
- use Mockery\Exception;
- class EmployeeService extends Service
- {
- public function employeeEditOther($data,$user){
- list($status,$msg) = $this->employeeOtherRule($data,$user);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = new Employee();
- $model = $model->where('id',$user['id'])->first();
- $model->password = Hash::make($data['new_password']);
- $model->save();
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false, $exception->getMessage()];
- }
- return [true,''];
- }
- public function employeeOtherRule($data,$user){
- if(! isset($data['old_password'])) return [false,'请输入原密码'];
- if($data['old_password'] == "") return [false,'原密码不能为空'];
- if(! isset($data['new_password'])) return [false,'请输入新密码'];
- if($data['new_password'] == "") return [false,'新密码不能为空'];
- if(! isset($data['re_password'])) return [false,'请输入确认密码'];
- if($data['re_password'] == "") return [false,'确认密码不能为空'];
- if(! Hash::check($data['old_password'], $user['password'])) return [false,'原密码错误'];
- if($data['new_password'] == $data['old_password']) return [false,'原密码与新密码一致'];
- if($data['new_password'] !== $data['re_password']) return [false,'新密码与确认密码不一致'];
- return [true,''];
- }
- public function employeeEdit($data,$user){
- list($status,$msg) = $this->employeeRule($data,$user,false);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = new Employee();
- $model = $model->where('id',$data['id'])->first();
- $model->number = $data['number'] ?? "";
- $model->title = $data['title'] ?? "";
- $model->mobile = $data['mobile'] ?? "";
- $model->sex = $data['sex'] ?? 0;
- $model->education = $data['education'] ?? "";
- $model->id_card = $data['id_card'] ?? "";
- $model->major = $data['major'] ?? "";
- $model->p_title = $data['p_title'] ?? "";
- $model->state = $data['state'] ?? 0;
- $model->is_admin = $data['is_admin'] ?? 0;
- $model->entrust_type = $data['entrust_type'] ?? 0;
- $model->man_type = $data['man_type'] ?? 0;
- if($model->is_admin && $data['password'] !== '******') {
- $model->password = Hash::make($data['password']);
- $model->p_version = $model->p_version + 1;
- }
- $model->save();
- EmployeeDepartPermission::where('employee_id',$data['id'])->delete();
- if(isset($data['depart'])){
- $insert = [];
- foreach ($data['depart'] as $value){
- $insert[] = [
- 'employee_id' => $model->id,
- 'depart_id' => $value,
- 'top_depart_id' => $user['top_depart_id'],
- ];
- }
- EmployeeDepartPermission::insert($insert);
- }
- EmployeeRole::where('employee_id',$data['id'])->update([
- 'del_time' => time()
- ]);
- if(isset($data['role'])){
- $insert = [];
- foreach ($data['role'] as $value){
- $insert[] = [
- 'employee_id' => $model->id,
- 'role_id' => $value,
- 'crt_time' => time(),
- 'upd_time' => time(),
- ];
- }
- EmployeeRole::insert($insert);
- }
- EmployeeWorkRange::where('employee_id',$data['id'])->delete();
- if(isset($data['work_range'])){
- $insert = [];
- foreach ($data['work_range'] as $value){
- $insert[] = [
- 'employee_id' => $model->id,
- 'start_time_hour' => $value['start_time_hour'],
- 'start_time_min' => $value['start_time_min'],
- 'end_time_hour' => $value['end_time_hour'],
- 'end_time_min' => $value['end_time_min'],
- 'total_work_min' => $value['total_work_min'],
- 'top_depart_id' => $value['top_depart_id'],
- 'crt_time' => time(),
- 'upd_time' => time(),
- ];
- }
- EmployeeWorkRange::insert($insert);
- }
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false, $exception->getMessage()];
- }
- return [true,''];
- }
- public function employeeAdd($data,$user){
- list($status,$msg) = $this->employeeRule($data, $user);
- if(!$status) return [$status,$msg];
- try{
- DB::beginTransaction();
- $model = new Employee();
- $model->number = $data['number'] ?? "";
- $model->title = $data['title'] ?? "";
- $model->mobile = $data['mobile'] ?? "";
- $model->sex = $data['sex'] ?? 0;
- $model->education = $data['education'] ?? "";
- $model->id_card = $data['id_card'] ?? "";
- $model->major = $data['major'] ?? "";
- $model->p_title = $data['p_title'] ?? "";
- $model->state = $data['state'] ?? 0;
- $model->crt_id = $user['id'];
- $model->is_admin = $data['is_admin'] ?? 0;
- $model->account = $data['account'] ?? "";
- $model->entrust_type = $data['entrust_type'] ?? 0;
- $model->man_type = $data['man_type'] ?? 0;
- if($model->is_admin) $model->password = Hash::make($data['password']);
- $model->top_depart_id = $data['top_depart_id'];
- $model->save();
- if(isset($data['depart'])){
- $insert = [];
- foreach ($data['depart'] as $value){
- $insert[] = [
- 'employee_id' => $model->id,
- 'depart_id' => $value,
- 'top_depart_id' => $user['top_depart_id'],
- ];
- }
- EmployeeDepartPermission::insert($insert);
- }
- if(isset($data['role'])){
- $insert = [];
- foreach ($data['role'] as $value){
- $insert[] = [
- 'employee_id' => $model->id,
- 'role_id' => $value,
- 'crt_time' => time(),
- 'upd_time' => time(),
- ];
- }
- EmployeeRole::insert($insert);
- }
- if(isset($data['work_range'])){
- $insert = [];
- foreach ($data['work_range'] as $value){
- $insert[] = [
- 'employee_id' => $model->id,
- 'start_time_hour' => $value['start_time_hour'],
- 'start_time_min' => $value['start_time_min'],
- 'end_time_hour' => $value['end_time_hour'],
- 'end_time_min' => $value['end_time_min'],
- 'total_work_min' => $value['total_work_min'],
- 'top_depart_id' => $value['top_depart_id'],
- 'crt_time' => time(),
- 'upd_time' => time(),
- ];
- }
- EmployeeWorkRange::insert($insert);
- }
- DB::commit();
- }catch (Exception $e){
- DB::rollBack();
- return [false, $e->getMessage()];
- }
- return [true,''];
- }
- public function employeeDel($data){
- if($this->isEmpty($data,'id')) return [false,'请选择删除的数据!'];
- Employee::whereIn('id',$data['id'])->update([
- 'del_time'=>time()
- ]);
- EmployeeRole::where('del_time',0)->whereIn('employee_id',$data['id'])->update([
- 'del_time'=>time()
- ]);
- EmployeeDepartPermission::whereIn('employee_id',$data['id'])->delete();
- EmployeeWorkRange::whereIn('employee_id',$data['id'])->delete();
- return [true,'删除成功'];
- }
- public function employeeDetail($data, $user){
- if(empty($data['id'])) return [false,'人员id不能为空'];
- list($status, $return) = $this->employeeList(['id' => [$data['id']]], $user);
- $user = $return['data'][0] ?? [];
- return [true, $user];
- }
- public function employeeCommon($data,$user, $field = []){
- if(empty($field)) $field = Employee::$field;
- $model = Employee::TopClear($user,$data);
- $model = $model->where('del_time',0)
- ->where('is_admin', '<>', Employee::IS_ADMIN_TWO)
- ->select($field)
- ->orderBy('id','desc');
- if(! empty($data['id'])) $model->whereIn('id', $data['id']);
- if(! empty($data['number'])) $model->where('number', 'LIKE', '%'.$data['number'].'%');
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
- if(! empty($data['mobile'])) $model->where('mobile', 'LIKE', '%'.$data['mobile'].'%');
- if(isset($data['is_admin'])) $model->where('is_admin', $data['is_admin']);
- if(! empty($data['state'])) $model->where('state', $data['state']);
- if(isset($data['education'])) $model->where('education', $data['education']);
- if(! empty($data['role'])) {
- $emp = EmployeeRole::where('role_id',$data['role'])
- ->where('del_time',0)
- ->select('employee_id')->get()->toArray();
- $model->whereIn('id',array_column($emp,'employee_id'));
- }
- if(! empty($data['depart'])) {
- // 1. 获取所有子部门 ID(包含自身)
- $allDeparts = Depart::where('del_time', 0)->select('id', 'parent_id')->get();
- $departIds = array_merge($this->getAllDescendants($allDeparts->toArray(), $data['depart']), [$data['depart']]);
- // 2. 优化:直接使用子查询,避免拉取巨大的 $employee_id 数组到内存
- $model->whereIn('id', function ($query) use ($departIds) {
- $query->select('employee_id')
- ->from('employee_depart_permission')
- ->whereIn('depart_id', $departIds);
- });
- }
- return $model;
- }
- /**
- * 产品列表
- * @param $data
- * @param $user
- * @return array
- */
- public function employeeList($data,$user){
- $model = $this->employeeCommon($data, $user);
- $list = $this->limit($model,'',$data);
- $list = $this->organizationEmployeeData($list, $data, $user);
- return [true, $list];
- }
- public function organizationEmployeeData($data, $ergs, $user)
- {
- if (empty($data['data'])) return $data;
- // 获取员工ID并查询扩展数据
- $employee_ids = array_column($data['data'], 'id');
- list($status, $extraMap) = $this->getEmployee($employee_ids);
- $map = [];
- if(isset($ergs['search_for_month_work'])) list($status, $map) = $this->getEmployeesMonthStats($employee_ids, $ergs['search_for_month_work'], $user);
- foreach ($data['data'] as &$item) {
- $id = $item['id'];
- $extra = $extraMap[$id] ?? null;
- $item['role'] = $extra['role_ids'] ?? [];
- $item['role_name'] = isset($extra['role_names']) ? implode(',', $extra['role_names']) : '';
- $item['depart'] = $extra['depart_ids'] ?? [];
- $item['depart_title'] = isset($extra['depart_names']) ? implode(',', $extra['depart_names']) : '';
- // 业务状态字段
- $item['is_admin_title'] = Employee::IS_ADMIN_TITLE[$item['is_admin']] ?? "";
- $item['state_title'] = Employee::State_Type[$item['state']] ?? "";
- $item['sex_title'] = Employee::SEX_TYPE[$item['sex']] ?? "";
- $item['man_type_title'] = Employee::Man_Type[$item['man_type']] ?? "";
- $item['entrust_type_title'] = Employee::WT_Type[$item['entrust_type']] ?? "";
- $item['education_title'] = Employee::Education[$item['education']] ?? "";
- $item['crt_time'] = !empty($item['crt_time']) ? date("Y-m-d", $item['crt_time']) : "";
- if (isset($ergs['search_for_month_work'])) {
- $item['month_pw'] = []; // 默认空数组
- if ($status) {
- $item['month_pw'] = $map[$item['id']] ?? [];
- }
- }
- }
- return $data;
- }
- public function getEmployeesMonthStats($employee_ids, $month, $user)
- {
- $topDepartId = $user['top_depart_id'];
- // 1. 月份初始化
- if(is_numeric($month)){
- $monthStart = $month;
- }else{
- $monthStart = $this->changeDateToDate($month);
- $monthStr = date("Y-m", $monthStart);
- }
- $endTime = strtotime("+1 month", $monthStart) - 1;
- // 2. 获取当月标准工作日天数 (基数)
- $standardWorkDays = DB::table('calendar_details')
- ->where('top_depart_id', $topDepartId)
- ->where('del_time', 0)
- ->where('time', '>=', $monthStart)
- ->where('time', '<=', $endTime)
- ->where('is_work', CalendarDetails::TYPE_ONE)
- ->count();
- if ($standardWorkDays <= 0) return [false, '工作日信息未设置'];
- // 3. 批量获取个性化工时设置
- $empWorkRanges = DB::table('employee_work_range')
- ->whereIn('employee_id', $employee_ids)
- ->where('top_depart_id', $topDepartId)
- ->select('employee_id', DB::raw('SUM(total_work_min) as total_min'))
- ->groupBy('employee_id')
- ->pluck('total_min', 'employee_id')
- ->toArray();
- // 4. 获取公司通用工时设置
- $commonWorkMin = DB::table('work_range_details')
- ->where('top_depart_id', $topDepartId)
- ->where('del_time', 0)
- ->sum('total_work_min');
- if(empty($commonWorkMin)) return [false, '公司工作时段未设置'];
- // 5. 批量获取请假与加班汇总
- $actualStats = DB::table('p_leave_over_order_details as d')
- ->join('p_leave_over_order as m', 'd.main_id', '=', 'm.id')
- ->whereIn('d.employee_id', $employee_ids)
- ->where('m.order_time', '>=', $monthStart)
- ->where('m.order_time', '<=', $endTime)
- ->where('m.del_time', 0)
- ->where('d.del_time', 0)
- ->select(
- 'd.employee_id',
- DB::raw("SUM(CASE WHEN m.type = 1 THEN d.total_min ELSE 0 END) as leave_min"),
- DB::raw("SUM(CASE WHEN m.type = 2 THEN d.total_min ELSE 0 END) as overtime_min")
- )
- ->groupBy('d.employee_id')
- ->get()
- ->keyBy('employee_id');
- // 6. 核心逻辑汇总
- $result = [];
- foreach ($employee_ids as $empId) {
- // A. 确定该人员每日标准时长 (基数)
- $dailyStandardMin = isset($empWorkRanges[$empId]) ? (float)$empWorkRanges[$empId] : (float)$commonWorkMin;
- // 如果每日标准时长没设置,跳过该人防止除以0错误
- if ($dailyStandardMin <= 0) continue;
- // B. 获取加班和请假数据
- $empActual = $actualStats->get($empId);
- $leaveMin = $empActual ? (float)$empActual->leave_min : 0;
- $overtimeMin = $empActual ? (float)$empActual->overtime_min : 0;
- // 净增减工时(分)
- $netDiffMin = $overtimeMin - $leaveMin;
- // C. 计算出勤总工时 (标准总分钟 + 净增减)
- $standardMonthMin = $standardWorkDays * $dailyStandardMin;
- $finalWorkMin = $standardMonthMin + $netDiffMin;
- // D. 计算出勤总天数 (标准天数 + 净增减折算天数)
- // 计算公式:出勤天数 = (标准总分钟 + 加班分钟 - 请假分钟) / 每日标准分钟
- $finalAttendanceDays = round($finalWorkMin / $dailyStandardMin, 2);
- $result[$empId] = [
- // 'standard_days' => $standardWorkDays, // 标准工作天数
- 'attendance_days' => $finalAttendanceDays, // 出勤总天数 * (结算后)
- // 'standard_month_min' => $standardMonthMin, // 标准总工时 (分钟)
- // 'final_work_min' => $finalWorkMin, // 出勤总工时 * (结算后分钟)
- 'final_work_hour' => round($finalWorkMin / 60, 2), // 出勤总工时 * (结算后小时)
- // 'leave_min' => $leaveMin,
- // 'overtime_min' => $overtimeMin,
- // 'daily_standard_min' => $dailyStandardMin
- ];
- }
- return [true, $result];
- }
- public function getEmployee(array $employee_ids)
- {
- if (empty($employee_ids)) return [false, []];
- // 1. 一次性获取所有角色
- $roles = DB::table('employee_role as a')
- ->join('role as b', 'a.role_id', '=', 'b.id')
- ->where('a.del_time', 0)
- ->where('b.del_time', 0)
- ->whereIn("a.employee_id", $employee_ids)
- ->select('a.employee_id', 'b.title', 'b.id')
- ->get();
- // 2. 一次性获取所有部门
- $departs = DB::table('employee_depart_permission as a')
- ->join('depart as b', 'a.depart_id', '=', 'b.id')
- ->whereIn("a.employee_id", $employee_ids)
- ->select('a.employee_id', 'b.title', 'b.id')
- ->orderBy('b.id')
- ->get();
- $work_range = DB::table('employee_work_range as a')
- ->whereIn("employee_id", $employee_ids)
- ->select('employee_id','start_time_hour', 'start_time_min', 'end_time_hour', 'end_time_min')
- ->get();
- // 3. 结果按员工ID分组归纳
- $resultMap = [];
- foreach ($employee_ids as $id) {
- $resultMap[$id] = [
- 'role_ids' => [],
- 'role_names' => [],
- 'depart_ids' => [],
- 'depart_names' => [],
- 'work_range' => [],
- ];
- }
- foreach ($roles as $r) {
- $resultMap[$r->employee_id]['role_ids'][] = $r->id;
- $resultMap[$r->employee_id]['role_names'][] = $r->title;
- }
- foreach ($departs as $d) {
- $resultMap[$d->employee_id]['depart_ids'][] = $d->id;
- $resultMap[$d->employee_id]['depart_names'][] = $d->title;
- }
- foreach ($work_range as $d) {
- $resultMap[$d->employee_id]['work_range'][] = $d;
- }
- return [true, $resultMap];
- }
- public function fillDataForExport($data, $column, $user, &$return)
- {
- if (empty($data)) return;
- $dataArray = is_array($data) ? $data : $data->toArray();
- $mainIds = array_column($dataArray, 'id');
- // 1. 获取详情映射 [员工ID => [ [部门编码, 部门名称], [部门编码, 部门名称] ]]
- $detailsMap = $this->getEmployeeDetailsMap($mainIds, $user);
- // 枚举映射
- $sexMap = Employee::SEX_TYPE;
- $eduMap = Employee::Education;
- $stateMap = Employee::State_Type;
- foreach ($dataArray as $main) {
- $empId = $main['id'];
- $details = $detailsMap[$empId] ?? [];
- // 2. 格式化主表信息(这些是这一人所有行共有的内容)
- $mainInfo = $main;
- $mainInfo['sex_title'] = $sexMap[$main['sex']] ?? '';
- $mainInfo['education_title'] = $eduMap[$main['education']] ?? '';
- $mainInfo['state_title'] = $stateMap[$main['state']] ?? '';
- if (empty($details)) {
- // 如果没部门,保底出一行
- $tempRow = [];
- foreach ($column as $col) {
- $tempRow[] = $mainInfo[$col] ?? '';
- }
- $return[] = $tempRow;
- } else {
- // 3. 核心拆分点:遍历部门明细,每一个部门都产生一个新的 $tempRow
- foreach ($details as $sub) {
- // 将员工基本信息与“当前这一个”部门信息合并
- $fullRowData = array_merge($mainInfo, $sub);
- $tempRow = [];
- // 严格按配置顺序取值
- foreach ($column as $col) {
- $tempRow[] = $fullRowData[$col] ?? '';
- }
- // 【重点】这里是追加,不是覆盖!
- // 如果张三有2个部门,这里会先后 push 两次,Excel 就会多出两行
- $return[] = $tempRow;
- }
- }
- }
- }
- public function getEmployeeDetailsMap($empIds, $user)
- {
- // 关联查询权限表和部门档案
- $list = DB::table('employee_depart_permission as p')
- ->join('depart as d', 'p.depart_id', '=', 'd.id')
- ->whereIn('p.employee_id', $empIds)
- ->where('p.top_depart_id', $user['top_depart_id'])
- ->where('d.del_time', 0)
- ->select('p.employee_id', 'd.code as depart_code', 'd.title as depart_title')
- ->get();
- $res = [];
- foreach ($list as $item) {
- // 【重点】注意这里的 [],它保证了同一个 employee_id 下可以挂载多个部门数组
- $res[$item->employee_id][] = [
- 'depart_code' => $item->depart_code,
- 'depart_title' => $item->depart_title,
- ];
- }
- return $res;
- }
- public function employeeRule(&$data, $user,$is_add = true){
- if(empty($data['number'])) return [false,'工号不存在'];
- if(empty($data['title'])) return [false,'姓名不存在'];
- if(! empty($data['sex']) && ! isset(Employee::SEX_TYPE[$data['sex']])) return [false, '性别不存在'];
- if(empty($data['mobile'])) return [false,'联系电话不能为空'];
- // if(! $this->isValidPhone($data['mobile'])) return [false, '手机号码格式错误'];
- if(! empty($data['education']) && ! isset(Employee::Education[$data['education']])) return [false, '学历不存在'];
- if(empty($data['id_card'])) return [false, '身份证号不能为空'];
- if(empty($data['depart'])) return [false,'部门不能为空'];
- if(empty($data['state'])) return [false,'状态不能为空'];
- if(! isset(Employee::State_Type[$data['state']])) return [false,'状态不存在'];
- if(! empty($data['is_admin'])){
- if(! isset(Employee::IS_ADMIN_TITLE_SIMPLE[$data['is_admin']])) return [false, 'is_admin类型不存在'];
- if(empty($data['password'])) return [false, '密码不能为空'];
- if(mb_strlen($data['password']) < 6) return [false, '密码长度不得小于6位长度'];
- if(empty($data['role'])) return [false, '角色不能为空'];
- }
- if(empty($data['man_type'])) return [false,'人员类别不能为空'];
- if(! isset(Employee::Man_Type[$data['man_type']])) return [false,'人员类别不存在'];
- // if(empty($data['entrust_type'])) return [false,'委托方式不能为空'];
- // if(! isset(Employee::WT_Type[$data['entrust_type']])) return [false,'委托方式不存在'];
- $data['top_depart_id'] = $user['top_depart_id'];
- if(! empty($data['work_range'])){
- foreach ($data['work_range'] as $key => $value){
- $res = $this->checkNumber($value['start_time_hour'], 0, 'non-negative');
- if(!$res['valid']) return [false, "开始点:" . $res['error']];
- if($value['start_time_hour'] > 23) return [false, false, "开始点不合法"];
- $res = $this->checkNumber($value['start_time_min'], 0, 'non-negative');
- if(!$res['valid']) return [false, "开始分:" . $res['error']];
- if($value['start_time_min'] > 60) return [false, false, "开始点不合法"];
- $res = $this->checkNumber($value['end_time_hour'], 0, 'non-negative');
- if(!$res['valid']) return [false, "结束点:" . $res['error']];
- if($value['end_time_hour'] > 24) return [false, false, "结束点不合法"];
- $res = $this->checkNumber($value['end_time_min'], 0, 'non-negative');
- if(!$res['valid']) return [false, "结束分:" . $res['error']];
- if($value['end_time_min'] > 60) return [false, false, "结束分不合法"];
- $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']);
- $data['work_range'][$key]['top_depart_id'] = $data['top_depart_id'];
- }
- }
- if(! $is_add){
- if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
- $bool = Employee::where('del_time',0)
- ->where('id','<>',$data['id'])
- ->where('top_depart_id', $data['top_depart_id'])
- ->where('number', $data['number'])
- ->exists();
- }else{
- $code = Depart::where('id', $user['top_depart_id'])->value('code');
- $data['account'] = $code . "_" . $data['number'];
- $bool = Employee::where('del_time',0)
- ->where('top_depart_id', $data['top_depart_id'])
- ->where('number', $data['number'])
- ->exists();
- }
- if($bool) return [false,'人员工号已存在'];
- return [true,''];
- }
- public function roleEdit($data,$user){
- list($status,$msg) = $this->roleRule($data,$user, false);
- if(!$status) return [$status,$msg];
- $model = new Role();
- $model = $model->where('id',$data['id'])->first();
- $model->title = $data['title'];
- $model->save();
- return [true,''];
- }
- public function roleAdd($data,$user){
- list($status,$msg) = $this->roleRule($data,$user);
- if(!$status) return [$status,$msg];
- $model = new Role();
- $model->title = $data['title'] ;
- $model->top_depart_id = $data['top_depart_id'];
- $model->save();
- return [true,''];
- }
- public function roleDel($data){
- if($this->isEmpty($data,'id')) return [false,'ID必须!'];
- $bool = EmployeeRole::where('del_time',0)
- ->whereIn('role_id',$data['id'])
- ->exists();
- if($bool) return [false,'角色已绑定人员'];
- Role::where('id',$data['id'])->update([
- 'del_time' => time()
- ]);
- RoleMenu::where('del_time',0)->where('role_id',$data['id'])->update([
- 'del_time' => time()
- ]);
- RoleMenuButton::where('del_time',0)->where('role_id',$data['id'])->update([
- 'del_time' => time()
- ]);
- return [true, ''];
- }
- public function roleList($data,$user){
- $model = Role::TopClear($user,$data);
- $model = $model->where('del_time',0)
- ->select('title','crt_time','id','upd_time')
- ->orderBy('id','desc');
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%' . $data['title'] . '%');
- $list = $this->limit($model,'',$data);
- return [true, $list];
- }
- public function roleRule(&$data,$user, $is_check = true){
- if($this->isEmpty($data,'title')) return [false,'名称不能为空'];
- $data['top_depart_id'] = $user['top_depart_id'];
- if($is_check){
- $bool = Role::where('title',$data['title'])
- ->where('top_depart_id', $data['top_depart_id'])
- ->where('del_time',0)
- ->exists();
- if($bool) return [false,'角色名称已存在'];
- }else{
- if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
- $top_depart_id = Role::where('id',$data['id'])->value('top_depart_id');
- $bool = Role::where('title',$data['title'])
- ->where('top_depart_id',$top_depart_id)
- ->where('id','<>',$data['id'])
- ->where('del_time',0)
- ->exists();
- if($bool) return [false,'角色名称已存在'];
- }
- return [true, ''];
- }
- public function roleMenu($data){
- if(empty($data['role_id'])) return [false,'角色不能为空!'];
- if(empty($data['menu'])) return [false,'菜单数据不能为空!'];
- DB::beginTransaction();
- try {
- RoleMenu::where('del_time',0)->where('role_id',$data['role_id'])->update(['del_time' => time()]);
- RoleMenuButton::where('del_time',0)->where('role_id',$data['role_id'])->update(['del_time' => time()]);
- $insert = $insert2 = [];
- foreach ($data['menu'] as $t){
- $insert[] = [
- 'role_id' => $data['role_id'],
- 'menu_id' => $t['menu_id'],
- 'type' => $t['type'],
- 'crt_time' => time()
- ];
- if(! empty($t['button'])){
- foreach ($t['button'] as $b){
- $insert2[] = [
- 'role_id' => $data['role_id'],
- 'menu_id' => $t['menu_id'],
- 'button_id' => $b,
- 'crt_time' => time()
- ];
- }
- RoleMenuButton::insert($insert2);
- }
- }
- RoleMenu::insert($insert);
- DB::commit();
- }catch (\Throwable $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- public function roleDetail($data){
- if(empty($data['role_id'])) return [false,'请选择角色'];
- $role = Role::where('id',$data['role_id'])
- ->where('del_time',0)
- ->select('id','title')
- ->first();
- if(empty($role)) return [false,'角色不存在或已被删除'];
- $role = $role->toArray();
- $menu = RoleMenu::where('role_id',$data['role_id'])
- ->where('del_time',0)
- ->select('menu_id','type')
- ->get()->toArray();
- $button = $this->fillRoleButton([$data['role_id']]);
- foreach ($menu as $key => $value){
- $menu[$key]['button'] = $button[$value['menu_id']] ?? [];
- }
- $role['menu'] = $menu;
- return [true, $role];
- }
- public function departEdit($data, $user){
- list($status,$msg) = $this->departRule($data,$user,false);
- if(!$status) return [$status,$msg];
- $update = $msg['data'][0];
- $model = new Depart();
- $model->where('id',$data['id'])->update($update);
- return [true, ''];
- }
- public function departAdd($data,$user){
- list($status,$msg) = $this->departRule($data,$user);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- foreach ($msg['data'] as $value){
- $model = new Depart();
- $model->parent_id = $value['parent_id'];
- $model->title = $value['title'];
- $model->code = $value['code'];
- $model->top_depart_id = $value['top_depart_id'];
- $model->save();
- }
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true,''];
- }
- public function departDel($data){
- list($status,$msg) = $this->checkDepartDel($data);
- if(! $status) return [false, $msg];
- Depart::whereIn('id',$data['id'])->update([
- 'del_time'=>time()
- ]);
- return [true,''];
- }
- public function checkDepartDel($data){
- if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
- $bool = Depart::whereIn('parent_id',$data['id'])->where('del_time',0)->exists();
- if($bool) return [false,'部门下有子部门'];
- if($this->checkDepartHasPerson($data['id'])) return [false,'部门下有人员档案'];
- return [true, ''];
- }
- public function departCommon($data, $user, $field = []){
- if(empty($field)) $field = Depart::$field;
- $model = Depart::TopClear($user,$data);
- $model = $model->where('del_time',0)
- ->select($field)
- ->orderby('id', 'asc');
- if(isset($data['parent_id'])) $model->where('parent_id', $data['parent_id']);
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
- if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
- return $model;
- }
- public function departList($data, $user){
- $model = $this->departCommon($data, $user);
- $list = $model->get()->toArray();
- $list = $this->fillDepartList($list, $user);
- $list_tree = $list;
- if(! empty($list_tree)) {
- $minParentId = min(array_column($list_tree, 'parent_id'));
- $list_tree = $this->makeTree($minParentId,$list_tree);
- $list_tree = $this->set_sort_circle($list_tree);
- }
- return [true,['data' => $list,'tree' => $list_tree]];
- }
- public function fillDepartList($list,$user, $is_export = false){
- if($is_export){
- $map = Depart::where('del_time',0)
- ->whereIn('id', array_column($list['data'], 'parent_id'))
- ->select('code','id', 'title')
- ->get()->toArray();
- $map = array_column($map,null,'id');
- foreach ($list['data'] as $key => $value){
- $tmp = $map[$value['parent_id']] ?? "";
- if($tmp['code'] == $user['top_depart_code']) {
- $code = $title = "";
- }else{
- $code = $tmp['code'];
- $title = $tmp['title'];
- }
- $list['data'][$key]['parent_code'] = $code;
- $list['data'][$key]['parent_title'] = $title;
- }
- }
- return $list;
- }
- public function departRule($data,$user, $is_check = true){
- if(empty($data['data'])) return [false,'数据不能为空!'];
- $code = array_column($data['data'],'code');
- $title = array_column($data['data'],'title');
- $code = array_map(function($val) {
- return $val !== null ? $val : 0;
- }, $code);
- $title = array_map(function($val) {
- return $val !== null ? $val : 0;
- }, $title);
- $code_count = array_count_values($code);
- $title_count = array_count_values($title);
- foreach ($code as $value){
- if(empty($value)) return [false,'编码不能为空!'];
- if($code_count[$value] > 1) return [false,'编码不能重复'];
- }
- foreach ($title as $value){
- if(empty($value)) return [false,'名称不能为空!'];
- if($title_count[$value] > 1) return [false,'名称不能重复'];
- }
- foreach ($data['data'] as $key => $value){
- $top_depart_id = $user['top_depart_id'];
- $parent_id = $value['parent_id'];
- if(empty($value['parent_id'])) {
- $parent_id = $top_depart_id;
- $data['data'][$key]['parent_id'] = $parent_id;
- }
- $data['data'][$key]['top_depart_id'] = $top_depart_id;
- $data['data'][$key]['upd_time'] = time();
- if($is_check){
- $data['data'][$key]['crt_time'] = time();
- $bool = Depart::whereRaw("binary code = '{$value['code']}'")
- ->where('top_depart_id', $top_depart_id)
- ->where('del_time',0)
- ->exists();
- }else{
- if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
- $bool = Depart::whereRaw("binary code = '{$value['code']}'")
- ->where('top_depart_id', $top_depart_id)
- ->where('id','<>',$data['id'])
- ->where('del_time',0)
- ->exists();
- }
- if($bool) return [false,'部门编码已存在'];
- }
- return [true, $data];
- }
- public function checkDepartHasPerson($depart_id = []){
- if(empty($depart_id)) return false;
- $bool = EmployeeDepartPermission::from('employee_depart_permission as a')
- ->leftJoin('employee as b','b.id','a.employee_id')
- ->where('b.del_time',0)
- ->whereIn('a.depart_id',$depart_id)
- ->exists();
- return $bool;
- }
- public function fillRoleButton($role_id){
- $button = RoleMenuButton::whereIn('role_id',$role_id)
- ->where('del_time',0)
- ->select('menu_id','button_id')
- ->get()->toArray();
- $button_map = [];
- foreach ($button as $value){
- if(! isset($button_map[$value['menu_id']])){
- $button_map[$value['menu_id']][] = $value['button_id'];
- }else{
- if(! in_array($value['button_id'], $button_map[$value['menu_id']])) $button_map[$value['menu_id']][] = $value['button_id'];
- }
- }
- return $button_map;
- }
- public function getEmployeeMap($employee_ids){
- $employee_ids = array_filter((array)$employee_ids);
- if (empty($employee_ids)) return [];
- return Employee::whereIn('id', $employee_ids)
- ->pluck('title', 'id')
- ->toArray();
- }
- public static function fillMenu2($menu_id, &$user){
- // 直接查询匹配的菜单
- $menuItem = SysMenu::where('del_time',0)
- ->where('id', $menu_id)
- ->first();
- $func = $menuItem ? $menuItem->export_file_func : "";
- $funcName = $menuItem ? $menuItem->title : "";
- $header_default = config("excel." . $func) ?? [];
- // $header_f = "extra_" . $menu_id;
- // $service = new TableHeadService();
- // if(method_exists($service,$header_f)) $service->$header_f($header_default);
- $user['e_header_default'] = $header_default;
- return [$func, $funcName];
- }
- }
|