You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
7.1 KiB
PHP

<?php
namespace App\Http\Controllers\API\Admin;
use App\Http\Controllers\Controller;
use App\Services\UserService;
use Illuminate\Http\Request;
use App\Lib\Rs;
use Illuminate\Support\Facades\DB;
class HolidayController extends Controller
{
private $userService;
public function __construct(UserService $userService)
{
$this->userService = $userService;
}
/**
* 获取节假日列表
*/
public function list(Request $request)
{
$payload = $request->attributes->get('payload');
if (!$payload) {
return Rs::error('用户信息无效', 401);
}
$year = $request->input('year', null);
$month = $request->input('month', null);
$type = $request->input('type', null);
$status = $request->input('status', null);
$page = $request->input('page', 1);
$pageSize = $request->input('page_size', 20);
$query = DB::table('holiday')
->where('deleted', 0);
if ($year !== null && $year !== '') {
$query->whereYear('date', $year);
}
if ($month !== null && $month !== '') {
$query->whereMonth('date', $month);
}
if ($type !== null && $type !== '') {
$query->where('type', $type);
}
if ($status !== null && $status !== '') {
$query->where('status', $status);
}
$total = $query->count();
$holidays = $query->orderBy('date', 'asc')
->skip(($page - 1) * $pageSize)
->take($pageSize)
->get();
$data = [
'list' => $holidays,
'total' => $total,
'page' => $page,
'page_size' => $pageSize,
'total_pages' => ceil($total / $pageSize)
];
return Rs::success($data);
}
/**
* 保存节假日(新增或更新)
*/
public function Save(Request $request)
{
$payload = $request->attributes->get('payload');
if (!$payload) {
return Rs::error('用户信息无效', 401);
}
$data = $request->validate([
'id' => 'nullable|integer',
'date' => 'required|date',
'type' => 'required|integer|in:1,2',
'name' => 'nullable|string|max:50',
'status' => 'required|integer|in:0,1',
]);
if (!isset($data['id']) || $data['id'] === null) {
$data['id'] = 0;
}
// 检查同一日期是否已存在(排除当前记录和已删除的)
$exists = DB::table('holiday')
->where('date', $data['date'])
->where('deleted', 0);
if ($data['id'] > 0) {
$exists->where('id', '!=', $data['id']);
}
if ($exists->exists()) {
return Rs::error('该日期已存在节假日记录');
}
try {
$values = [
'date' => $data['date'],
'type' => $data['type'],
'name' => $data['name'] ?? '',
'status' => $data['status'],
];
if ($data['id'] == 0) {
$values['created_at'] = now();
$values['updated_at'] = now();
$id = DB::table('holiday')->insertGetId($values);
} else {
$values['updated_at'] = now();
DB::table('holiday')->where('id', $data['id'])->update($values);
$id = $data['id'];
}
return Rs::success(['id' => $id]);
} catch (\Exception $e) {
return Rs::error('保存失败:' . $e->getMessage());
}
}
/**
* 批量保存节假日
*/
public function batchSave(Request $request)
{
$payload = $request->attributes->get('payload');
if (!$payload) {
return Rs::error('用户信息无效', 401);
}
$data = $request->validate([
'holidays' => 'required|array|min:1',
'holidays.*.date' => 'required|date',
'holidays.*.type' => 'required|integer|in:1,2',
'holidays.*.name' => 'nullable|string|max:50',
'holidays.*.status' => 'required|integer|in:0,1',
]);
try {
DB::beginTransaction();
$count = 0;
foreach ($data['holidays'] as $holiday) {
// 检查是否已存在
$exists = DB::table('holiday')
->where('date', $holiday['date'])
->where('deleted', 0)
->first();
if ($exists) {
// 更新已有记录
DB::table('holiday')->where('id', $exists->id)->update([
'type' => $holiday['type'],
'name' => $holiday['name'] ?? $exists->name,
'status' => $holiday['status'],
'updated_at' => now(),
]);
} else {
// 新增
DB::table('holiday')->insert([
'date' => $holiday['date'],
'type' => $holiday['type'],
'name' => $holiday['name'] ?? '',
'status' => $holiday['status'],
'deleted' => 0,
'created_at' => now(),
'updated_at' => now(),
]);
}
$count++;
}
DB::commit();
return Rs::success(['count' => $count], "成功保存 {$count} 条节假日记录");
} catch (\Exception $e) {
DB::rollBack();
return Rs::error('批量保存失败:' . $e->getMessage());
}
}
/**
* 删除节假日
*/
public function Delete(Request $request)
{
$payload = $request->attributes->get('payload');
if (!$payload) {
return Rs::error('用户信息无效', 401);
}
$data = $request->validate([
'id' => 'required|integer',
]);
try {
$deleted = DB::table('holiday')
->where('id', $data['id'])
->update([
'deleted' => 1,
'updated_at' => now(),
]);
if ($deleted) {
return Rs::success();
}
return Rs::error('删除失败:记录不存在');
} catch (\Exception $e) {
return Rs::error('删除失败:' . $e->getMessage());
}
}
/**
* 获取指定年份所有节假日(用于前端日历展示)
*/
public function getByYear(Request $request)
{
$payload = $request->attributes->get('payload');
if (!$payload) {
return Rs::error('用户信息无效', 401);
}
$year = $request->input('year', date('Y'));
$holidays = DB::table('holiday')
->where('deleted', 0)
->where('status', 1)
->whereYear('date', $year)
->select('id', 'date', 'type', 'name', 'status')
->orderBy('date', 'asc')
->get();
return Rs::success($holidays);
}
}