# AdRequestMirrorServiceImpl.java

    package cloud.wconnect.frontoffice.service.adjustment.impl;

    import cloud.wconnect.frontoffice.entity.adjustment.AdRequest;
    import cloud.wconnect.frontoffice.entity.adjustment.AdRequestMirror;
    import cloud.wconnect.frontoffice.mapper.adjustment.AdRequestMirrorMapper;
    import cloud.wconnect.frontoffice.service.adjustment.IAdRequestCommentMirrorService;
    import cloud.wconnect.frontoffice.service.adjustment.IAdRequestMirrorService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import java.util.Collections;
    import java.util.List;
    import java.util.stream.Collectors;

    /**
     * <p>
     * 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2026-02-11
     */
    @Service
    @DS("logbook")
    public class AdRequestMirrorServiceImpl extends ServiceImpl<AdRequestMirrorMapper, AdRequestMirror> implements IAdRequestMirrorService {

        @Autowired
        private IAdRequestCommentMirrorService adRequestCommentMirrorService;

        /**
         * 批量更新为未配对
         *
         * @param adjustmentIds 调账id集合
         * @return void
         * @author lichen
         * @date 2026/2/11
         */
        @Override
        public void updateNonMatchedByIds(List<Long> adjustmentIds) {
            if (CollectionUtils.isEmpty(adjustmentIds)) return;

            // 查询存在的数据
            List<AdRequestMirror> adRequestMirrors = listByIds(adjustmentIds);

            // 更新为未配对
            List<AdRequestMirror> updateBatch = adRequestMirrors.stream().map(item -> {
                AdRequestMirror adRequestMirror = new AdRequestMirror();
                adRequestMirror.setId(item.getId());
                adRequestMirror.setBusinessDate(0);
                adRequestMirror.setMatchTime(0L);
                adRequestMirror.setMatchType(0);
                return adRequestMirror;
            }).collect(Collectors.toList());
            updateBatchById(updateBatch);
        }

        /**
         * 根据调账编号查询备份数据
         *
         * @param adjNumbers 调账编号集合
         * @return java.util.List<cloud.wconnect.frontoffice.entity.adjustment.AdRequestMirror>
         * @author lichen
         * @date 2026/2/11
         */
        @Override
        public List<AdRequestMirror> listByAdjNumbers(List<String> adjNumbers) {
            if (CollectionUtils.isEmpty(adjNumbers)) return Collections.emptyList();
            return list(new LambdaQueryWrapper<AdRequestMirror>().in(AdRequestMirror::getAdjNumber, adjNumbers));
        }

        /**
         * 批量更新为已配对
         *
         * @param matchedAdjs 已配对数据
         * @author lichen
         * @date 2026/2/11
         */
        @Override
        public void updateMatchedByIds(List<AdRequest> matchedAdjs) {
            if (CollectionUtils.isEmpty(matchedAdjs)) return;
            List<Long> matchedAdjIds = matchedAdjs.stream().map(AdRequest::getId).collect(Collectors.toList());

            // 查询存在的备份数据
            List<AdRequestMirror> existMirrors = listByIds(matchedAdjIds);
            if (CollectionUtils.isEmpty(existMirrors)) return;

            List<AdRequestMirror> updateBatch = matchedAdjs.stream().map(item -> {
                AdRequestMirror adRequestMirror = new AdRequestMirror();
                adRequestMirror.setId(item.getId());
                adRequestMirror.setBusinessDate(item.getBusinessDate());
                adRequestMirror.setMatchTime(item.getMatchTime());
                adRequestMirror.setMatchType(item.getMatchType());
                adRequestMirror.setAccountNumber(item.getAccountNumber());

                if (item.getMatchType() == AdRequest.MATCH_BY_GUEST_NAME_ROOM_NUMBER_TRX_CODE_ADJUSTED_AMOUNT) {
                    // 回填ConfNumber
                    adRequestMirror.setConfNumber(item.getConfNumber());
                } else {
                    // 回填GuestName
                    adRequestMirror.setGuestName(item.getGuestName());
                    // 回填RoomNumber
                    adRequestMirror.setRoomNumber(item.getRoomNumber());
                }

                return adRequestMirror;
            }).collect(Collectors.toList());
            updateBatchById(updateBatch);
        }

        /**
         * 批量删除未配对数据
         *
         * @param existUnmatchedIds 已存在未配对数据id集合
         * @author lichen
         * @date 2026/2/11
         */
        @Override
        public void removeNonMatchedByIds(List<Long> existUnmatchedIds) {
            if (CollectionUtils.isEmpty(existUnmatchedIds)) return;

            removeBatchByIds(existUnmatchedIds);

            adRequestCommentMirrorService.removeBatchByAdjustmentIds(existUnmatchedIds);
        }
    }