ArchiveService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Archive;
  4. use Illuminate\Support\Facades\DB;
  5. class ArchiveService extends Service
  6. {
  7. public function archiveAdd($data,$user){
  8. list($status,$msg) = $this->archiveRule($data,$user);
  9. if(!$status) return [$status,$msg];
  10. try {
  11. DB::beginTransaction();
  12. $model = new Archive();
  13. $model->month = $data['month'];
  14. $model->crt_id = $user['id'];
  15. $model->top_depart_id = $data['top_depart_id'];
  16. $model->save();
  17. DB::commit();
  18. }catch (\Exception $exception){
  19. DB::rollBack();
  20. return [false,$exception->getMessage()];
  21. }
  22. return [true,''];
  23. }
  24. public function archiveDel($data, $user){
  25. if(empty($data['id']) || ! is_array($data['id'])) return [false,'ID不能为空或格式错误'];
  26. Archive::whereIn('id', $data['id'])->update([
  27. 'del_time'=>time()
  28. ]);
  29. return [true,''];
  30. }
  31. public function archiveCommon($data,$user, $field = []){
  32. if(empty($field)) $field = Archive::$field;
  33. $model = Archive::TopClear($user,$data);
  34. $model = $model->where('del_time',0)
  35. ->select($field)
  36. ->orderby('id', 'desc');
  37. if(! empty($data['time'][0]) && ! empty($data['time'][1])) {
  38. $return = $this->changeDateToTimeStampAboutRange($data['time']);
  39. $model->where('month','>=',$return[0]);
  40. $model->where('month','<=',$return[1]);
  41. }
  42. return $model;
  43. }
  44. public function archiveList($data, $user){
  45. $model = $this->archiveCommon($data, $user);
  46. $list = $this->limit($model,'',$data);
  47. $list = $this->fillArchiveList($list);
  48. return [true, $list];
  49. }
  50. public function fillArchiveList($data){
  51. if(empty($data['data'])) return $data;
  52. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  53. foreach ($data['data'] as $key => $value){
  54. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  55. $data['data'][$key]['month'] = $value['month'] ? date('Y-m',$value['month']) : '';
  56. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  57. }
  58. return $data;
  59. }
  60. public function archiveRule(&$data,$user, $add = true){
  61. if(empty($data['month'])) return [false, '年月不能为空'];
  62. $data['month'] = $this->changeDateToDate($data['month']);
  63. $data['top_depart_id'] = $user['top_depart_id'];
  64. $bool = Archive::where('del_time',0)
  65. ->where('top_depart_id', $data['top_depart_id'])
  66. ->where('month', $data['month'])
  67. ->exists();
  68. if($bool) return [false, "年月" . date("Y-m") . "已归档"];
  69. return [true, ''];
  70. }
  71. public static function isArchive($months, $user)
  72. {
  73. // 1. 统一转为数组处理
  74. $monthArray = is_array($months) ? $months : [$months];
  75. // 2. 将所有传入的时间转换成“当月第一天”的时间戳并去重
  76. $normalizedMonths = array_unique(array_map(function($time) {
  77. // 如果传的是 2026-03 这种字符串,先转时间戳
  78. $timestamp = is_numeric($time) ? $time : strtotime($time);
  79. return strtotime(date('Y-m-01', $timestamp));
  80. }, $monthArray));
  81. // 3. 批量查询数据库中存在的已归档月份
  82. $archivedMonths = Archive::where('del_time', 0)
  83. ->where('top_depart_id', $user['top_depart_id'])
  84. ->whereIn('month', $normalizedMonths)
  85. ->pluck('month')
  86. ->toArray();
  87. // 4. 如果查询结果不为空,说明命中了归档
  88. if (!empty($archivedMonths)) {
  89. // 把命中的月份转回人类可读格式展示给用户
  90. $errorMonths = array_map(function($ts) {
  91. return date('Y-m', $ts);
  92. }, $archivedMonths);
  93. return [false, "操作失败:月份 [" . implode(',', $errorMonths) . "] 已归档锁定。"];
  94. }
  95. return [true, ''];
  96. }
  97. }