# EosShiftConfigController.java

    package cloud.wconnect.frontoffice.controller.endofshift;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.frontoffice.cons.endofshift.EndOfShiftErrorI18nConstant;
    import cloud.wconnect.frontoffice.entity.endofshift.EosShiftConfig;
    import cloud.wconnect.frontoffice.pojo.dto.EosShiftDetailDTO;
    import cloud.wconnect.frontoffice.pojo.dto.endofshift.EosShiftConfigDTO;
    import cloud.wconnect.frontoffice.pojo.vo.endofshift.EosShiftConfigVO;
    import cloud.wconnect.frontoffice.service.endofshift.IEosShiftConfigService;
    import com.google.common.collect.ImmutableMap;
    import com.google.common.collect.Sets;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    import java.time.LocalTime;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    import java.util.Map;

    /**
     * 排班配置控制层
     *
     * @author lichen
     * @date 2025/6/18
     */
    @RestController
    @RequestMapping("/end-of-shift/shift-config")
    public class EosShiftConfigController {

        @Autowired
        private IEosShiftConfigService shiftConfigService;
        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private PropertyI18nRedisService propertyI18nRedisService;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 查询资产的排班配置
         *
         * @param property 资产
         * @param page     页码
         * @param size     每页大小
         * @return cloud.wconnect.commonlib.pojo.dto.ListPagedDTO<cloud.wconnect.frontoffice.pojo.vo.endofshift.EosShiftConfigVO>
         * @author lichen
         * @date 2025/6/24
         */
        @ApiId(1529)
        @GetMapping
        public ListPagedDTO<EosShiftConfigVO> list(String property, @RequestParam int page, @RequestParam int size) {
            int propertyId = sessionContext.getPropertyId(property);
            return shiftConfigService.pageByPropertyId(propertyId, page, size);
        }

        /**
         * 新增排班配置
         *
         * @param property 资产
         * @param dto      排班配置DTO
         * @return java.util.Map<java.lang.String, java.lang.Integer>
         * @author lichen
         * @date 2025/6/18
         */
        @ApiId(1530)
        @PostMapping
        public Map<String, Integer> save(String property, @RequestBody EosShiftConfigDTO dto) {
            int propertyId = sessionContext.getPropertyId(property);
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);
            //校验排班生效的开始日期必须大于当前日期
            Integer currentDate = Integer.valueOf(DateTimeUtils.dateNowStr(propertyDTO.getTimezone(), BaseConstant.DATE_FORMAT_YYYYMMDD));
            if (dto.getStartDate() <= currentDate) {
                throw new BadRequestException(EndOfShiftErrorI18nConstant.SHIFT_CONFIG_EFFECTIVE_DATE_MUST_BE_GREATER_THAN_CURRENT_DATE,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The effective start date must be greater than the current date");
            }

            //校验未来日期的配置是否已经存在，未来日期的配置有且只有一条
            shiftConfigService.checkFutureDateConfigExist(propertyDTO);

            //校验排班名称不能重复
            List<String> shiftNameList = dto.getShiftConfigs().stream().map(EosShiftDetailDTO::getShiftName).toList();
            if (shiftNameList.size() != Sets.newHashSet(shiftNameList).size()) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_CANNOT_REPEAT,
                        i18nMessageUtils.getErrorParamMapNeedTranslate(),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The shiftName cannot be repeated");
            }

            //班次时间按照开始时间排序
            List<EosShiftDetailDTO> sortedList = dto.getShiftConfigs().stream().sorted(Comparator.comparing(config -> {
                //开始时间转换成LocalTime
                return LocalTime.parse(config.getStartTime());
            })).toList();
            dto.setShiftConfigs(sortedList);
            //校验排班时间(1.所有班次的整个时间跨度不能大于24个小时 2.单个班次的时间跨度必须大于1个小时 3.班次之间不能有重叠)
            shiftConfigService.checkShiftTime(propertyId, dto.getShiftConfigs());

            //新增排班
            return ImmutableMap.of("id", shiftConfigService.insert(propertyId, dto, sessionContext.getLoginUserId()));
        }

        /**
         * 删除排班配置
         *
         * @param property 资产
         * @param id       删除的排班id
         * @return java.util.Map<java.lang.String, java.lang.String>
         * @author lichen
         * @date 2025/6/19
         */
        @ApiId(1531)
        @DeleteMapping("/{id:\\d+}")
        public Map<String, String> delete(String property, @PathVariable Integer id) {
            int propertyId = sessionContext.getPropertyId(property);
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);
            //校验是否存在
            EosShiftConfig shiftConfig = shiftConfigService.checkExist(id);
            //校验是否归属于资产
            shiftConfigService.checkBelong(shiftConfig, propertyId);

            //校验是否未来日期的配置，只有未来日期的配置可以删除
            if (shiftConfig.getStartDate() <= Integer.parseInt(DateTimeUtils.dateNowStr(propertyDTO.getTimezone(), BaseConstant.DATE_FORMAT_YYYYMMDD))) {
                throw new BadRequestException(EndOfShiftErrorI18nConstant.CAN_ONLY_DELETE_FUTURE_DATE_SHIFT_CONFIG,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "Only the future dates shift config can be deleted");
            }
            //删除排班
            shiftConfigService.delete(shiftConfig);
            return Collections.emptyMap();
        }

    }