94 lines
4.6 KiB
Java

package github.benjamin.equipreservebackend.controller;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import github.benjamin.equipreservebackend.entity.Reservation;
import github.benjamin.equipreservebackend.response.ResponseResult;
import github.benjamin.equipreservebackend.service.ReservationService;
import github.benjamin.equipreservebackend.vo.ReservationStatsVO;
import github.benjamin.equipreservebackend.vo.ReservationVO;
import github.benjamin.equipreservebackend.vo.UserReservationVO;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/reservation")
@RequiredArgsConstructor
public class ReservationController {
private final ReservationService reservationService;
@PreAuthorize("hasAnyRole('USER', 'LEADER', 'DEVICE_ADMIN')")
@PostMapping
public ResponseResult<?> addReservation(@RequestBody Reservation reservation) {
reservationService.addReservation(reservation);
return ResponseResult.success();
}
@PreAuthorize("hasAnyRole('USER', 'LEADER', 'DEVICE_ADMIN')")
@GetMapping("/{userId}")
public ResponseResult<Page<UserReservationVO>> getUserReservation(@PathVariable("userId") Long userId,
@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer size) {
Page<Reservation> pageRequest = new Page<>(page, size);
Page<UserReservationVO> res = reservationService.getUserReservationVO(userId, pageRequest);
return ResponseResult.success(res);
}
@PreAuthorize("hasAnyRole('LEADER', 'DEVICE_ADMIN')")
@GetMapping("/approval/{id}")
public ResponseResult<Page<ReservationVO>> getReservation(@PathVariable("id") Long id,
@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer size) {
return ResponseResult.success(reservationService.getReservationVO(id, page, size));
}
@PreAuthorize("hasRole('ADMIN')")
@GetMapping("/stats")
public ResponseResult<List<ReservationStatsVO>> getReservationStats(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate start,
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
return ResponseResult.success(reservationService.getReservationStats(start, end));
}
@PreAuthorize("hasRole('ADMIN')")
@GetMapping("/stats/export")
public void exportReservationStats(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate start,
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
HttpServletResponse response) throws IOException {
String fileName = String.format("使用人统计_%s-%s.xlsx", start.format(DateTimeFormatter.ISO_DATE), end.format(DateTimeFormatter.ISO_DATE));
String encodedName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + encodedName);
// 查询数据
List<ReservationStatsVO> data = reservationService.getReservationStats(start, end);
// 使用EasyExcel写入response流
EasyExcel.write(response.getOutputStream(), ReservationStatsVO.class)
.sheet("使用人统计")
.doWrite(data);
}
@PreAuthorize("hasRole('DEVICE_ADMIN')")
@PostMapping("/endTime/{reservationId}")
public ResponseResult<?> updateEndTime(@PathVariable Long reservationId,
@RequestBody Map<String, String> body) {
LocalDate endTime = LocalDate.parse(body.get("endTime"));
reservationService.updateEndTime(reservationId, endTime);
return ResponseResult.success();
}
}