# HotelKpiWconnectAdjustmentServiceImpl.java

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

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.pojo.bo.adj.AdjBO;
    import cloud.wconnect.workperformance.entity.hotelkpi.HotelKpiWconnectAdjustment;
    import cloud.wconnect.workperformance.mapper.hotelkpi.HotelKpiWconnectAdjustmentMapper;
    import cloud.wconnect.workperformance.service.hotelkpi.IHotelKpiWconnectAdjustmentService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import jakarta.annotation.Resource;
    import org.apache.commons.collections4.CollectionUtils;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.Comparator;
    import java.util.List;
    import java.util.Set;
    import java.util.stream.Collectors;

    /**
     * <p>
     * Hotel KPI w-connect Adjustment 数据 服务实现类
     * </p>
     *
     * @author zc
     * @since 2024-07-20
     */
    @Service
    @DS("work_performance")
    public class HotelKpiWconnectAdjustmentServiceImpl extends ServiceImpl<HotelKpiWconnectAdjustmentMapper, HotelKpiWconnectAdjustment> implements IHotelKpiWconnectAdjustmentService {

        @Resource
        private BeanMapper beanMapper;

        /**
         * 保存数据
         *
         * @param propertyId 资产 id
         * @param adjBOList  adjustment 数据
         * @param updateTime 更新时间（毫秒级别时间戳）
         * @author zc
         * @date 2024/7/20
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void save(int propertyId, List<AdjBO> adjBOList, long updateTime) {
            if (CollectionUtils.isEmpty(adjBOList)) {
                return;
            }
            List<HotelKpiWconnectAdjustment> adjustmentList = beanMapper.mapList(adjBOList,
                    HotelKpiWconnectAdjustment.class, (bo, item) -> {
                        item.setPropertyId(propertyId);
                        item.setUpdateTime(updateTime);
                    });

            // 按唯一索引字段排序，避免死锁
            adjustmentList.sort(Comparator.comparing(HotelKpiWconnectAdjustment::getPropertyId)
                    .thenComparing(HotelKpiWconnectAdjustment::getAdjNumber));

            baseMapper.insertOrUpdateBatchByUnique(adjustmentList);
        }

        /**
         * 删除数据
         *
         * @param propertyId 资产 id
         * @param adjBOList  adjustment 数据
         * @author zc
         * @date 2024/7/20
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void delete(int propertyId, List<AdjBO> adjBOList) {
            if (CollectionUtils.isEmpty(adjBOList)) {
                return;
            }

            Set<String> adjNumList = adjBOList.stream()
                    .map(AdjBO::getAdjNumber).collect(Collectors.toSet());

            remove(new QueryWrapper<HotelKpiWconnectAdjustment>().lambda()
                    .eq(HotelKpiWconnectAdjustment::getPropertyId, propertyId)
                    .in(HotelKpiWconnectAdjustment::getAdjNumber, adjNumList));
        }

        /**
         * 根据指定日期范围获取数据
         *
         * @param propertyId 资产 id
         * @param startDate  开始日期（yyyyMMdd）
         * @param endDate    结束日期（yyyyMMdd）
         * @return HotelKpiWconnectAdjustment 集合
         * @author zc
         * @date 2024/8/6
         */
        @Override
        public List<HotelKpiWconnectAdjustment> listByDateRange(int propertyId, long startDate, long endDate) {
            return list(new LambdaQueryWrapper<HotelKpiWconnectAdjustment>()
                    .eq(HotelKpiWconnectAdjustment::getPropertyId, propertyId)
                    // businessDate等于0的为未配对数据，此处查询已配对数据
                    .between(HotelKpiWconnectAdjustment::getBusinessDate, startDate, endDate));
        }

    }