# PastReservationDayConfigController.java

    package cloud.wconnect.frontoffice.controller.pastreservation;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.frontoffice.cons.pastreservation.PastReservationErrorFiledCons;
    import cloud.wconnect.frontoffice.entity.pastreservation.PastReservationConsecutiveDay;
    import cloud.wconnect.frontoffice.entity.pastreservation.PastReservationDayLog;
    import cloud.wconnect.frontoffice.entity.pastreservation.PastReservationIntervalDay;
    import cloud.wconnect.frontoffice.pojo.dto.pastreservation.PastReservationConsecutiveDayDTO;
    import cloud.wconnect.frontoffice.pojo.dto.pastreservation.PastReservationDayConfigDTO;
    import cloud.wconnect.frontoffice.pojo.dto.pastreservation.PastReservationDayLogDTO;
    import cloud.wconnect.frontoffice.service.pastreservation.IPastReservationConsecutiveDayService;
    import cloud.wconnect.frontoffice.service.pastreservation.IPastReservationDayLogService;
    import cloud.wconnect.frontoffice.service.pastreservation.IPastReservationIntervalDayService;
    import cloud.wconnect.frontoffice.service.pastreservation.IPastReservationService;
    import jakarta.annotation.Resource;
    import jakarta.validation.Valid;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    import java.util.Collections;
    import java.util.List;
    import java.util.Map;
    import java.util.Objects;

    /**
     * @author: yyt
     * 2024-10-02
     */
    @RestController
    @RequestMapping("/past-reservations/day-config")
    public class PastReservationDayConfigController {

        @Resource
        private IPastReservationIntervalDayService pastReservationIntervalDayService;
        @Resource
        private IPastReservationConsecutiveDayService pastReservationConsecutiveDayService;
        @Resource
        private IPastReservationDayLogService pastReservationDayLogService;
        @Resource
        private IPastReservationService pastReservationService;
        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private BeanMapper beanMapper;

        /**
         * 1266 - 获取间隔天数+连续入住天数配置
         *
         * @param property 资产标识
         * @return 间隔天数+连续入住天数配置
         * @author yyt
         * @date 2024/10/2
         **/
        @ApiId(1266)
        @GetMapping
        public PastReservationDayConfigDTO get(String property) {
            Integer propertyId = sessionContext.getPropertyId(property);
            PastReservationIntervalDay pastReservationIntervalDay = pastReservationIntervalDayService.getByPropertyId(propertyId);
            Integer intervalDay = Objects.isNull(pastReservationIntervalDay) ? null : pastReservationIntervalDay.getDay();
            List<PastReservationConsecutiveDay> consecutiveDays = pastReservationConsecutiveDayService.listByPropertyId(propertyId);
            return new PastReservationDayConfigDTO(intervalDay, beanMapper.mapList(consecutiveDays, PastReservationConsecutiveDayDTO.class));
        }

        /**
         * 1267 - 设置间隔天数+连续入住天数
         *
         * @param property  资产标识
         * @param configDTO 配置
         * @return {}
         * @author yyt
         * @date 2024/10/2
         **/
        @ApiId(1267)
        @PostMapping
        public Map save(String property, @Valid @RequestBody PastReservationDayConfigDTO configDTO) {
            // 至少设置一个连续入住天数
            if (configDTO.getConsecutiveDays().stream().noneMatch(item -> Objects.nonNull(item.getDay()))) {
                throw new BadRequestException(PastReservationErrorFiledCons.PAST_RESERVATION_CONSECUTIVE_DAY_EMPTY,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "set at least one consecutive stay");
            }

            Integer propertyId = sessionContext.getPropertyId(property);

            // 不能有进行中的操作记录
            pastReservationDayLogService.checkInProgress(propertyId);

            // 插入进行中操作记录
            int userId = sessionContext.getLoginUserId();
            List<PastReservationDayLog> logs = pastReservationDayLogService.saveLogs(propertyId, configDTO, userId);
            pastReservationService.saveDayConfig(propertyId, configDTO, userId, logs);

            return Collections.emptyMap();
        }

        /**
         * 1268 - 获取间隔天数+连续入住天数操作 log
         *
         * @param property 资产标识
         * @param page     页码
         * @param size     每页显示条数
         * @return 操作 log
         * @author yyt
         * @date 2024/10/3
         **/
        @ApiId(1268)
        @GetMapping("/log")
        public ListPagedDTO<PastReservationDayLogDTO> listLog(String property, int page, int size) {
            return pastReservationDayLogService.listLogDTO(sessionContext.getTenantSymbolBO().getId(), sessionContext.getPropertyId(property), page, size);
        }

    }