# CbCasePublishLogServiceImpl.java

    package cloud.wconnect.finance.service.chargeback.impl;

    import cloud.wconnect.commonlib.cons.OperationCons;
    import cloud.wconnect.finance.entity.chargeback.CbCasePublishLog;
    import cloud.wconnect.finance.mapper.chargeback.CbCasePublishLogMapper;
    import cloud.wconnect.finance.service.chargeback.ICbCasePublishLogService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.stereotype.Service;

    import jakarta.annotation.Nullable;
    import java.util.*;
    import java.util.stream.Collectors;

    /**
     * <p>
     * 财务 / 酒店 publish 记录 服务实现类
     * </p>
     *
     * @author yyt
     * @since 2023-08-22
     */
    @Service
    @DS("finance")
    public class CbCasePublishLogServiceImpl extends ServiceImpl<CbCasePublishLogMapper, CbCasePublishLog> implements ICbCasePublishLogService {

        /**
         * 清空日志
         *
         * @param caseId case id
         * @param stages 阶段
         * @author yyt
         * @date 2023/9/7
         */
        @Override
        public void clearLog(Long caseId, @Nullable List<Integer> stages) {
            remove(new QueryWrapper<CbCasePublishLog>().lambda().eq(CbCasePublishLog::getCaseId, caseId)
                    .in(CollectionUtils.isNotEmpty(stages), CbCasePublishLog::getStage, stages));
        }

        /**
         * 获取 case 指定批次的 推送 log
         *
         * @param caseId  case id
         * @param batchId batch id
         * @return 推送 log
         * @author yyt
         * @date 2023/9/11
         */
        @Override
        public CbCasePublishLog getLog(Long caseId, Long batchId) {
            return getOne(new QueryWrapper<CbCasePublishLog>().lambda().eq(CbCasePublishLog::getCaseId, caseId)
                    .eq(CbCasePublishLog::getBatchId, batchId));
        }

        /**
         * 获取 case 各个阶段最新推送的日志
         *
         * @param caseId       case id
         * @param excludeStage 排除阶段
         * @return 各个阶段最新推送的日志
         * @author yyt
         * @date 2024/2/20
         **/
        @Override
        public List<CbCasePublishLog> listLatestLog(Long caseId, @Nullable Integer excludeStage) {
            List<CbCasePublishLog> publishLogs = list(new QueryWrapper<CbCasePublishLog>().lambda()
                    .eq(CbCasePublishLog::getCaseId, caseId)
                    .ne(Objects.nonNull(excludeStage), CbCasePublishLog::getStage, excludeStage)
                    .eq(CbCasePublishLog::getOperationType, OperationCons.PUBLISH));
            if (CollectionUtils.isEmpty(publishLogs)) {
                return Collections.emptyList();
            }

            Map<Integer, CbCasePublishLog> publishLogByStage = publishLogs.stream()
                    .collect(Collectors.groupingBy(CbCasePublishLog::getStage,
                            Collectors.collectingAndThen(Collectors.toList(), item -> {
                                item.sort(Comparator.comparing(CbCasePublishLog::getNotifiedTime));
                                return item.get(item.size() - 1);
                            })));
            return new ArrayList<>(publishLogByStage.values());
        }

    }