# DbDbPmsManagerFlashServiceImpl.java

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

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.SystemPlatformEnum;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.commonlib.util.TransactionUtils;
    import cloud.wconnect.workperformance.entity.database.DbPmsManagerFlash;
    import cloud.wconnect.workperformance.mapper.database.DbPmsManagerFlashMapper;
    import cloud.wconnect.workperformance.pojo.bo.database.managerflash.DbPmsManagerFlashBO;
    import cloud.wconnect.workperformance.pojo.bo.database.managerflash.DbPmsManagerFlashItemBO;
    import cloud.wconnect.workperformance.pojo.vo.database.managerflash.DbPmsManagerFlashVO;
    import cloud.wconnect.workperformance.service.database.IDbPmsManagerFlashService;
    import cloud.wconnect.workperformance.service.database.IDbPmsMarketCodeStatisticsService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import jakarta.annotation.Resource;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.stereotype.Service;

    import java.time.LocalDate;
    import java.util.Collections;
    import java.util.List;
    import java.util.Objects;
    import java.util.concurrent.Executor;

    /**
     * <p>
     * Database Manager Flash 服务实现类
     * </p>
     *
     * @author zc
     * @since 2025-12-02
     */
    @Service
    @DS("database")
    public class DbDbPmsManagerFlashServiceImpl extends ServiceImpl<DbPmsManagerFlashMapper, DbPmsManagerFlash> implements IDbPmsManagerFlashService {

        @Resource
        private IDbPmsMarketCodeStatisticsService marketCodeStatisticsService;
        @Resource
        private ObjectMapper objectMapper;
        @Resource
        private Executor executor;

        /**
         * 获取指定日期的 SC Rooms
         *
         * @param propertyDTO 资产
         * @param dataDate    数据日期
         * @return PmsManagerFlashItemBO
         * @author zc
         * @date 2026/1/12
         */
        @Override
        public DbPmsManagerFlashItemBO getScRooms(PropertyDTO propertyDTO, long dataDate) {
            String scOccDesc = getScOccDesc(propertyDTO);
            List<DbPmsManagerFlashItemBO> managerFlashItemBOS = getBoListByDate(propertyDTO.getId(), dataDate);
            return managerFlashItemBOS.stream().filter(item ->
                            scOccDesc.equals(item.getDesc()))
                    .findFirst().orElse(new DbPmsManagerFlashItemBO());
        }

        /**
         * 获取 SC Occ Desc
         *
         * @param propertyDTO 资产
         * @return java.lang.String
         * @author zc
         * @date 2026/1/13
         */
        private String getScOccDesc(PropertyDTO propertyDTO) {
            if (propertyDTO.getPmsType().equals(SystemPlatformEnum.OPERA.getDesc())) {
                return "Total Rooms in Hotel minus OOO Rooms";
            } else {
                return "Total Rooms to Sell (Physical Rooms - OOO)";
            }
        }

        /**
         * 处理报告数据
         *
         * @param propertyDTO 资产
         * @param bo          报告数据
         * @param operatorId  操作人 id
         * @author zc
         * @date 2025/12/02
         */
        @Override
        public void handle(PropertyDTO propertyDTO, DbPmsManagerFlashBO bo, int operatorId) {
            LocalDate localDate = DateTimeUtils.strToLocalDate(bo.getDateStr(), BaseConstant.DATE_FORMAT_YYYY_MM_DD);
            long date = Long.parseLong(DateTimeUtils.localDateToStr(localDate, BaseConstant.DATE_FORMAT_YYYYMMDD));

            DbPmsManagerFlash managerFlash = getByDate(propertyDTO.getId(), date);
            if (Objects.isNull(managerFlash)) {
                managerFlash = new DbPmsManagerFlash();
                managerFlash.setPropertyId(propertyDTO.getId());
                managerFlash.setDate(date);
            }
            managerFlash.setData(JsonUtils.convertObjListToString(objectMapper, bo.getData()));
            managerFlash.setUpdateBy(operatorId);
            managerFlash.setUpdateTime(DateTimeUtils.nowTimestamp());

            saveOrUpdate(managerFlash);

            // 触发重新计算 Market code statistics
            TransactionUtils.afterCommitAsyncExecute(executor, () -> {
                String scOccDesc = getScOccDesc(propertyDTO);
                DbPmsManagerFlashItemBO scRoomsBO = bo.getData().stream()
                        .filter(item -> scOccDesc.equals(item.getDesc()))
                        .findFirst().orElse(null);
                marketCodeStatisticsService.recalculateByManagerFlash(propertyDTO, date, scRoomsBO, operatorId);
            });
        }

        /**
         * 获取指定资产指定日期的 Database PMS Manager Flash 数据
         *
         * @param propertyId 资产 id
         * @param date       日期（yyyyMMdd）
         * @return DbPmsManagerFlashItemBO 集合
         * @author zc
         * @date 2025/12/15
         */
        @Override
        public List<DbPmsManagerFlashItemBO> getBoListByDate(int propertyId, long date) {
            DbPmsManagerFlash managerFlash = getByDate(propertyId, date);
            if (Objects.isNull(managerFlash)) {
                return Collections.emptyList();
            }
            return JsonUtils.convertStringToList(objectMapper, managerFlash.getData(), DbPmsManagerFlashItemBO.class);
        }

        /**
         * 获取指定日期的数据
         *
         * @param propertyId 资产 id
         * @param date       日期（yyyyMMdd）
         * @return DbPmsManagerFlash
         * @author zc
         * @date 2026/1/12
         */
        private DbPmsManagerFlash getByDate(int propertyId, long date) {
            return getOne(new LambdaQueryWrapper<DbPmsManagerFlash>()
                    .eq(DbPmsManagerFlash::getPropertyId, propertyId)
                    .eq(DbPmsManagerFlash::getDate, date));
        }

        /**
         * 获取 Manager Flash 数据详情
         *
         * @param propertyId 资产 id
         * @param date       日期（yyyyMMdd）
         * @return DbPmsManagerFlashVO 集合
         * @author zc
         * @date 2026/1/12
         */
        @Override
        public List<DbPmsManagerFlashVO> getDetail(int propertyId, long date) {
            DbPmsManagerFlash managerFlash = getByDate(propertyId, date);
            if (Objects.isNull(managerFlash)) {
                return Collections.emptyList();
            }
            return JsonUtils.convertStringToList(objectMapper, managerFlash.getData(), DbPmsManagerFlashVO.class).stream()
                    .filter(item -> StringUtils.isNotBlank(item.getDesc())).toList();
        }
    }