# SalesKpiMonthlyReportServiceImpl.java

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

    import cloud.wconnect.workperformance.entity.saleskpi.SalesKpiMonthlyReport;
    import cloud.wconnect.workperformance.mapper.saleskpi.SalesKpiMonthlyReportMapper;
    import cloud.wconnect.workperformance.pojo.dto.saleskpi.monthly.trans.SalesKpiTransMonthlyDTO;
    import cloud.wconnect.workperformance.service.saleskpi.ISalesKpiMonthlyReportService;
    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.google.common.collect.Lists;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.Comparator;
    import java.util.List;

    /**
     * <p>
     *  服务实现类
     * </p>
     *
     * @author zc
     * @since 2024-06-13
     */
    @Service
    @DS("work_performance")
    public class SalesKpiMonthlyReportServiceImpl extends ServiceImpl<SalesKpiMonthlyReportMapper, SalesKpiMonthlyReport> implements ISalesKpiMonthlyReportService {

        /**
         * 保存采集的 Monthly Report 功能指标数据
         *
         * @param propertyId          资产 id
         * @param transMonthlyDTOList 指标数据
         * @author zc
         * @date 2024/6/13
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void saveBatch(int propertyId, List<SalesKpiTransMonthlyDTO> transMonthlyDTOList) {
            List<SalesKpiMonthlyReport> monthlyReportList = Lists.newArrayList();
            transMonthlyDTOList.forEach(item -> {
                item.getData().forEach(dataItem -> {
                    SalesKpiMonthlyReport salesKpiMonthlyReport = new SalesKpiMonthlyReport();
                    salesKpiMonthlyReport.setPropertyId(propertyId);
                    salesKpiMonthlyReport.setMonthly(item.getMonthly());
                    salesKpiMonthlyReport.setDataSource(dataItem.getDataSource());
                    salesKpiMonthlyReport.setTarget(dataItem.getTarget());
                    salesKpiMonthlyReport.setActual(dataItem.getActual());

                    monthlyReportList.add(salesKpiMonthlyReport);
                });
            });
            if (CollectionUtils.isNotEmpty(monthlyReportList)) {
                // 按唯一索引字段排序，避免死锁
                monthlyReportList.sort(Comparator.comparing(SalesKpiMonthlyReport::getPropertyId)
                        .thenComparing(SalesKpiMonthlyReport::getMonthly)
                        .thenComparing(SalesKpiMonthlyReport::getDataSource));

                baseMapper.insertOrUpdateBatchByUnique(monthlyReportList);
            }
        }

        /**
         * 获取指定日期范围的 Monthly Report 数据
         *
         * @param propertyId 资产 id
         * @param startMonth 开始月（yyyyMM）
         * @param endMonth   结束月（yyyyMM）
         * @return SalesKpiMonthlyReport 集合
         * @author zc
         * @date 2024/6/15
         */
        @Override
        public List<SalesKpiMonthlyReport> list(int propertyId, long startMonth, long endMonth) {
            return list(new LambdaQueryWrapper<SalesKpiMonthlyReport>()
                    .eq(SalesKpiMonthlyReport::getPropertyId, propertyId)
                    .between(SalesKpiMonthlyReport::getMonthly, startMonth, endMonth));
        }
    }