archiveRule($data,$user); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = new Archive(); $model->month = $data['month']; $model->crt_id = $user['id']; $model->top_depart_id = $data['top_depart_id']; $model->save(); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true,'']; } public function archiveDel($data, $user){ if(empty($data['id']) || ! is_array($data['id'])) return [false,'ID不能为空或格式错误']; Archive::whereIn('id', $data['id'])->update([ 'del_time'=>time() ]); return [true,'']; } public function archiveCommon($data,$user, $field = []){ if(empty($field)) $field = Archive::$field; $model = Archive::TopClear($user,$data); $model = $model->where('del_time',0) ->select($field) ->orderby('id', 'desc'); if(! empty($data['time'][0]) && ! empty($data['time'][1])) { $return = $this->changeDateToTimeStampAboutRange($data['time']); $model->where('month','>=',$return[0]); $model->where('month','<=',$return[1]); } return $model; } public function archiveList($data, $user){ $model = $this->archiveCommon($data, $user); $list = $this->limit($model,'',$data); $list = $this->fillArchiveList($list); return [true, $list]; } public function fillArchiveList($data){ if(empty($data['data'])) return $data; $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id'))); foreach ($data['data'] as $key => $value){ $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : ''; $data['data'][$key]['month'] = $value['month'] ? date('Y-m',$value['month']) : ''; $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? ''; } return $data; } public function archiveRule(&$data,$user, $add = true){ if(empty($data['month'])) return [false, '年月不能为空']; $data['month'] = $this->changeDateToDate($data['month']); $data['top_depart_id'] = $user['top_depart_id']; $bool = Archive::where('del_time',0) ->where('top_depart_id', $data['top_depart_id']) ->where('month', $data['month']) ->exists(); if($bool) return [false, "年月" . date("Y-m") . "已归档"]; return [true, '']; } public static function isArchive($months, $user) { // 1. 统一转为数组处理 $monthArray = is_array($months) ? $months : [$months]; // 2. 将所有传入的时间转换成“当月第一天”的时间戳并去重 $normalizedMonths = array_unique(array_map(function($time) { // 如果传的是 2026-03 这种字符串,先转时间戳 $timestamp = is_numeric($time) ? $time : strtotime($time); return strtotime(date('Y-m-01', $timestamp)); }, $monthArray)); // 3. 批量查询数据库中存在的已归档月份 $archivedMonths = Archive::where('del_time', 0) ->where('top_depart_id', $user['top_depart_id']) ->whereIn('month', $normalizedMonths) ->pluck('month') ->toArray(); // 4. 如果查询结果不为空,说明命中了归档 if (!empty($archivedMonths)) { // 把命中的月份转回人类可读格式展示给用户 $errorMonths = array_map(function($ts) { return date('Y-m', $ts); }, $archivedMonths); return [false, "操作失败:月份 [" . implode(',', $errorMonths) . "] 已归档锁定。"]; } return [true, '']; } }