package com.hzs.activity.pick.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzs.activity.pick.mapper.AcPickMemberLogMapper;
import com.hzs.activity.pick.service.IAcPickMemberLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.activity.pick.vo.AcPickMemberLogVO;
import com.hzs.common.domain.activity.pick.AcPickMemberLog;
import com.hzs.common.domain.activity.pick.ext.AcPickExt;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 会员提货记录 服务实现类
 *
 * @author hzs
 * @since 2023-05-15
 */
@Service
public class AcPickMemberLogServiceImpl extends ServiceImpl<AcPickMemberLogMapper, AcPickMemberLog> implements IAcPickMemberLogService {

    @Override
    public List<AcPickMemberLogVO> queryMemberLog(Long pkMember) {
        return baseMapper.queryMemberLog(pkMember);
    }

    @Override
    public int insertBatch(List<AcPickExt> acPickExtList, Integer quantity, Integer source) {
        return baseMapper.insertBatch(acPickExtList, quantity, source);
    }

    @Override
    public int insertBatchPick(List<AcPickExt> acPickExtList, Integer source) {
        return baseMapper.insertBatchPick(acPickExtList, source);
    }

    @Override
    public int deleteBatch(List<AcPickMemberLog> acPickMemberLogList) {
        return baseMapper.deleteBatch(acPickMemberLogList);
    }

    @Override
    public List<AcPickMemberLog> queryPickMemberLog(List<SaOrderExt> saOrderExtList) {
        List<AcPickMemberLog> retPickMemberLogList = new ArrayList<>();

        Set<String> orderKey = new HashSet<>();
        saOrderExtList.forEach(saOrderExt -> orderKey.add(saOrderExt.getOrderCode() + saOrderExt.getPkMember()));

        Set<String> orderCodeSet = saOrderExtList.stream().distinct().map(SaOrderExt::getOrderCode).collect(Collectors.toSet());
        LambdaQueryWrapper<AcPickMemberLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AcPickMemberLog::getPickOrder, orderCodeSet);
        List<AcPickMemberLog> acPickMemberLogList = baseMapper.selectList(queryWrapper);

        for (AcPickMemberLog acPickMemberLog : acPickMemberLogList) {
            if (!orderKey.contains(acPickMemberLog.getPickOrder() + acPickMemberLog.getPkMember())) {
                retPickMemberLogList.add(acPickMemberLog);
            }
        }
        return retPickMemberLogList;
    }
}
