# HotelKpiExplainMonthlyServiceImpl.java

    package cloud.wconnect.workperformance.service.hotelkpi.impl.old;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.commonlib.util.YearMonthUtils;
    import cloud.wconnect.workperformance.entity.hotelkpi.old.HotelKpiExplainMonthly;
    import cloud.wconnect.workperformance.mapper.hotelkpi.old.HotelKpiExplainMonthlyMapper;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.old.explain.HotelKpiExplainDTO;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.old.explain.HotelKpiExplainDetailDTO;
    import cloud.wconnect.workperformance.service.hotelkpi.old.IHotelKpiExplainMonthlyService;
    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 org.apache.commons.collections.CollectionUtils;
    import org.springframework.stereotype.Service;

    import jakarta.annotation.Resource;
    import java.time.YearMonth;
    import java.util.Collections;
    import java.util.List;

    /**
     * <p>
     * Hotel KPI Explain 服务实现类
     * </p>
     *
     * @author zc
     * @since 2024-06-26
     */
    @Service("hotelKpiExplainMonthlyServiceImpl_old")
    @DS("work_performance")
    public class HotelKpiExplainMonthlyServiceImpl extends ServiceImpl<HotelKpiExplainMonthlyMapper, HotelKpiExplainMonthly> implements IHotelKpiExplainMonthlyService {

        @Resource
        private BeanMapper beanMapper;
        @Resource
        private ObjectMapper objectMapper;

        /**
         * 根据指定年份、指标获取 Explain DTO 数据
         *
         * @param propertyId  资产 id
         * @param year        目标年(yyyy)
         * @param metricsType 指标类型
         * @return HotelKpiExplainDTO 集合
         * @author zc
         * @date 2024/6/28
         */
        @Override
        public List<HotelKpiExplainDTO> listByYearAndMetricsType(int propertyId, long year, int metricsType) {
            long startMonth = YearMonthUtils.parseYearMonthToLong(YearMonth.of((int) year, 1));
            long endMonth = YearMonthUtils.parseYearMonthToLong(YearMonth.of((int) year, 12));

            List<HotelKpiExplainMonthly> explainMonthlyList = list(new LambdaQueryWrapper<HotelKpiExplainMonthly>()
                    .between(HotelKpiExplainMonthly::getMonth, startMonth, endMonth)
                    .eq(HotelKpiExplainMonthly::getMetricsType, metricsType)
                    .eq(HotelKpiExplainMonthly::getPropertyId, propertyId));
            if (CollectionUtils.isEmpty(explainMonthlyList)) {
                return Collections.emptyList();
            }
            return beanMapper.mapList(explainMonthlyList, HotelKpiExplainDTO.class, (entity, dto) -> {
                dto.setDate(entity.getMonth());
                dto.setDetailDTO(JsonUtils.convertStringToObject(objectMapper, entity.getDetail(), HotelKpiExplainDetailDTO.class));
            });
        }

    }