# RevenueDailySummaryTaServiceImpl.java

    package cloud.wconnect.rateshop.service.revenue.impl;

    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.UpdateLogUtil;
    import cloud.wconnect.rateshop.entity.revenue.RevenueDailySummaryTa;
    import cloud.wconnect.rateshop.mapper.revenue.RevenueDailySummaryTaMapper;
    import cloud.wconnect.rateshop.pojo.bo.revenue.RoomDayBO;
    import cloud.wconnect.rateshop.service.revenue.IRevenueDailySummaryTaService;
    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.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.List;
    import java.util.Map;
    import java.util.Objects;
    import java.util.function.Function;
    import java.util.stream.Collectors;

    /**
     * <p>
     * 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2024-03-23
     */
    @Service
    @DS("rate_shop")
    public class RevenueDailySummaryTaServiceImpl extends ServiceImpl<RevenueDailySummaryTaMapper, RevenueDailySummaryTa> implements IRevenueDailySummaryTaService {

        @Autowired
        private UpdateLogUtil updateLogUtil;

        /**
         * 根据资产ID和目标日期查询TA
         *
         * @param propertyId 资产ID
         * @param targetDate 目标日期时间戳
         * @return cloud.wconnect.rateshop.entity.revenue.RevenueDailySummaryTa
         * @author lichen
         * @date 2024/3/26
         */
        @Override
        public RevenueDailySummaryTa getByTheDate(Integer propertyId, long targetDate) {
            return getOne(new LambdaQueryWrapper<RevenueDailySummaryTa>()
                    .eq(RevenueDailySummaryTa::getPropertyId, propertyId)
                    .eq(RevenueDailySummaryTa::getTheDate, targetDate));
        }

        /**
         * 保存RoomDay
         *
         * @param propertyDTO   资产DTO
         * @param roomDayBOList roomDay数据集合
         * @return void
         * @author lichen
         * @date 2024/3/27
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void saveOrUpdate(PropertyDTO propertyDTO, List<RoomDayBO> roomDayBOList) {
            if (CollectionUtils.isEmpty(roomDayBOList)) return;
            List<Long> dateList = roomDayBOList.stream().map(RoomDayBO::getTheDate).toList();

            //查询已经存在的RoomDay
            Map<Long, RevenueDailySummaryTa> date2ExistTa = list(new LambdaQueryWrapper<RevenueDailySummaryTa>()
                    .eq(RevenueDailySummaryTa::getPropertyId, propertyDTO.getId())
                    .in(RevenueDailySummaryTa::getTheDate, dateList))
                    .stream().collect(Collectors.toMap(RevenueDailySummaryTa::getTheDate, Function.identity()));

            List<RevenueDailySummaryTa> entityList = Lists.newArrayListWithCapacity(roomDayBOList.size());
            for (RoomDayBO roomDayBO : roomDayBOList) {
                Long theDate = roomDayBO.getTheDate();

                RevenueDailySummaryTa revenueDailySummaryTa = date2ExistTa.get(theDate);
                if (Objects.nonNull(revenueDailySummaryTa)) {//已经存在时，更新
                    //更新前的操作日志
                    String oriUpdateLog = revenueDailySummaryTa.getUpdateLog();

                    revenueDailySummaryTa.setRoomDay(roomDayBO.getRoomDay());
                    revenueDailySummaryTa.setUpdateLog(updateLogUtil.append(oriUpdateLog, 0));

                }else{//不存在时，新增
                    revenueDailySummaryTa = new RevenueDailySummaryTa();
                    revenueDailySummaryTa.setPropertyId(propertyDTO.getId());
                    revenueDailySummaryTa.setRoomDay(roomDayBO.getRoomDay());
                    revenueDailySummaryTa.setTheDate(theDate);
                    revenueDailySummaryTa.setUpdateLog(updateLogUtil.get(0));
                }
                entityList.add(revenueDailySummaryTa);
            }

            //批量保存或更新
            saveOrUpdateBatch(entityList);
        }
    }