# ScheduleWorkDayServiceImpl.java

    package cloud.wconnect.workperformance.service.schedule.impl;

    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.workperformance.entity.schedule.ScheduleWorkDay;
    import cloud.wconnect.workperformance.mapper.schedule.ScheduleWorkDayMapper;
    import cloud.wconnect.workperformance.pojo.dto.schedule.ScheduleWorkDayDTO;
    import cloud.wconnect.workperformance.service.schedule.IScheduleWorkDayService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.google.common.collect.Maps;
    import org.springframework.stereotype.Service;

    import java.time.ZonedDateTime;
    import java.time.temporal.ChronoField;
    import java.util.Map;
    import java.util.Objects;

    /**
     * <p>
     * 每周开始工作日 服务实现类
     * </p>
     *
     * @author yyt
     * @since 2023-06-28
     */
    @Service
    @DS("work_performance")
    public class ScheduleWorkDayServiceImpl extends ServiceImpl<ScheduleWorkDayMapper, ScheduleWorkDay> implements IScheduleWorkDayService {

        public static final String START_DATE = "startDate";

        public static final String END_DATE = "endDate";

        /**
         * 获取工作开始日
         *
         * @param propertyId 资产 id
         * @return 工作开始日
         * @author yyt
         * @date 2023/6/29
         */
        @Override
        public ScheduleWorkDay getByPropertyId(Integer propertyId) {
            return getOne(new QueryWrapper<ScheduleWorkDay>().lambda().eq(ScheduleWorkDay::getPropertyId, propertyId));
        }

        /**
         * 新增、编辑工作日
         *
         * @param scheduleWorkDay 工作日
         * @author yyt
         * @date 2023/6/29
         */
        @Override
        public void insertOrUpdate(ScheduleWorkDay scheduleWorkDay) {
            baseMapper.insertOrUpdateByUniqueKey(scheduleWorkDay);
        }

        /**
         * 计算有效日期范围
         *
         * @param propertyDTO 资产
         * @return 有效日期范围
         * @author yyt
         * @date 2023/7/5
         */
        @Override
        public Map<String, Long> calculateValidDateRange(PropertyDTO propertyDTO) {
            /**
             * 处理有效日期范围（两个周期）逻辑：
             * 1、默认时间范围：当天 ~ 未来两周
             * 2、当配置了 周开始工作日后，范围：周开始工作日 ~ 未来两周
             */
            String timezone = propertyDTO.getTimezone();
            ZonedDateTime startDateZoneDate = DateTimeUtils.dateNow(timezone);
            ZonedDateTime endDateZoneDate = startDateZoneDate.plusDays(13);

            Integer propertyId = propertyDTO.getId();
            ScheduleWorkDay workDay = getOne(new QueryWrapper<ScheduleWorkDay>().lambda()
                    .eq(ScheduleWorkDay::getPropertyId, propertyId));
            if (Objects.nonNull(workDay)) {
                startDateZoneDate = startDateZoneDate.with(ChronoField.DAY_OF_WEEK, workDay.getWorkDay());
                endDateZoneDate = startDateZoneDate.plusDays(13);
            }
            Map<String, Long> dateRangeMap = Maps.newHashMapWithExpectedSize(2);
            dateRangeMap.put(START_DATE, startDateZoneDate.toEpochSecond());
            dateRangeMap.put(END_DATE, endDateZoneDate.toEpochSecond());
            return dateRangeMap;
        }

        /**
         * 获取工作日
         *
         * @param propertyId 资产 id
         * @return 工作日
         * @author yyt
         * @date 2023/7/5
         */
        @Override
        public ScheduleWorkDayDTO getWorkDayDTO(int propertyId) {
            ScheduleWorkDay scheduleWorkDay = getByPropertyId(propertyId);
            ScheduleWorkDayDTO scheduleWorkDayDTO = new ScheduleWorkDayDTO();
            scheduleWorkDayDTO.setWorkDay(Objects.isNull(scheduleWorkDay) ? null : scheduleWorkDay.getWorkDay());
            return scheduleWorkDayDTO;
        }

        /**
         * 保存工作日
         *
         * @param propertyId         资产 id
         * @param scheduleWorkDayDTO 工作日
         * @param userId             操作人
         * @author yyt
         * @date 2023/7/5
         */
        @Override
        public void save(Integer propertyId, ScheduleWorkDayDTO scheduleWorkDayDTO, int userId) {
            ScheduleWorkDay scheduleWorkDay = new ScheduleWorkDay();
            scheduleWorkDay.setPropertyId(propertyId).setWorkDay(scheduleWorkDayDTO.getWorkDay())
                    .setUpdatedBy(userId).setUpdatedTime(DateTimeUtils.nowTimestamp());
            insertOrUpdate(scheduleWorkDay);
        }

    }