package com.sckw.order.serivce;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.StringUtils;
import com.sckw.order.dao.KwoTradeOrderUnitMapper;
import com.sckw.order.model.KwoTradeOrderUnit;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * @desc: 交易订单采购/销售企业信息service
 * @author: yzc
 * @date: 2023-06-25 14:28
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class KwoTradeOrderUnitService {

    private final KwoTradeOrderUnitMapper kwoTradeOrderUnitMapper;

    /**
     * @desc: 批量插入
     * @author: yzc
     * @date: 2023-07-13 9:52
     * @Param list:
     * @return: void
     */
    public void insertBatch(List<KwoTradeOrderUnit> list) {
        kwoTradeOrderUnitMapper.insertBatch(list);
    }

    /**
     * @desc: 企业匹配
     * @author: yzc
     * @date: 2023-07-13 16:56
     * @Param orderId:
     * @Param entId:
     * @Param unitType:
     * @return: void
     */
    public Boolean entMatch(Long orderId, Long entId, String unitType) {
        LambdaQueryWrapper<KwoTradeOrderUnit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwoTradeOrderUnit::getTopEntId, entId).eq(KwoTradeOrderUnit::getTOrderId, orderId)
                .eq(StringUtils.isNotBlank(unitType), KwoTradeOrderUnit::getUnitType, unitType)
                .eq(KwoTradeOrderUnit::getDelFlag, Global.NO).last("LIMIT 1");
        return Objects.isNull(kwoTradeOrderUnitMapper.selectOne(wrapper));
    }

    /**
     * @desc: 批量匹配
     * @author: yzc
     * @date: 2023-07-18 16:46
     * @Param orderIds:
     * @Param entId:
     * @Param unitType:
     * @return: java.lang.Boolean
     */
    public Boolean entBatchMatch(List<Long> orderIds, Long entId, String unitType) {
        LambdaQueryWrapper<KwoTradeOrderUnit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwoTradeOrderUnit::getTopEntId, entId).in(KwoTradeOrderUnit::getTOrderId, orderIds)
                .eq(KwoTradeOrderUnit::getUnitType, unitType).eq(KwoTradeOrderUnit::getDelFlag, Global.NO);
        List<KwoTradeOrderUnit> list = kwoTradeOrderUnitMapper.selectList(wrapper);
        return CollectionUtils.isEmpty(list) || orderIds.size() != list.size();
    }

    /**
     * @desc: 根据orderId删除
     * @author: yzc
     * @date: 2023-07-14 14:49
     * @Param orderId:
     * @return: void
     */
    public void delByOrderId(Long orderId) {
        LambdaUpdateWrapper<KwoTradeOrderUnit> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(KwoTradeOrderUnit::getTOrderId, orderId).eq(KwoTradeOrderUnit::getDelFlag, Global.NO)
                .set(KwoTradeOrderUnit::getDelFlag, Global.YES);
        kwoTradeOrderUnitMapper.update(null, wrapper);
    }

    /**
     * @desc: 根据orderId获取
     * @author: yzc
     * @date: 2023-07-18 11:23
     * @Param torderId:
     * @return: java.util.List<com.sckw.order.model.KwoTradeOrderUnit>
     */
    public List<KwoTradeOrderUnit> getByOrderId(Long torderId) {
        LambdaQueryWrapper<KwoTradeOrderUnit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwoTradeOrderUnit::getTOrderId, torderId).eq(KwoTradeOrderUnit::getDelFlag, Global.NO);
        List<KwoTradeOrderUnit> list = kwoTradeOrderUnitMapper.selectList(wrapper);
        return CollectionUtils.emptyIfNull(list);
    }

    /**
     * @desc: 根据orderIds删除
     * @author: yzc
     * @date: 2023-07-18 16:49
     * @Param orderIds:
     * @return: void
     */
    public void delByOrderIds(List<Long> orderIds) {
        LambdaUpdateWrapper<KwoTradeOrderUnit> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(KwoTradeOrderUnit::getTOrderId, orderIds).eq(KwoTradeOrderUnit::getDelFlag, Global.NO)
                .set(KwoTradeOrderUnit::getDelFlag, Global.YES);
        kwoTradeOrderUnitMapper.update(null, wrapper);
    }

    /**
     * @desc: 根据一级企业匹配
     * @author: yzc
     * @date: 2023-08-02 18:06
     * @Param entId:
     * @return: java.util.List<com.sckw.order.model.KwoTradeOrderUnit>
     */
    public List<KwoTradeOrderUnit> getByTopEntId(Long entId) {
        LambdaQueryWrapper<KwoTradeOrderUnit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwoTradeOrderUnit::getTopEntId, entId).eq(KwoTradeOrderUnit::getDelFlag, Global.NO);
        List<KwoTradeOrderUnit> list = kwoTradeOrderUnitMapper.selectList(wrapper);
        return CollectionUtils.emptyIfNull(list);
    }

    /**
     * @desc: 否关联企业  true是，false否
     * @author: yzc
     * @date: 2023-08-16 16:22
     * @Param firstEndId:
     * @Param secondEntId:
     * @return: java.lang.Boolean
     */
    public Set<Long> associateEntOrderId(Long firstEndId, Long secondEntId) {
        List<Long> entIds = Arrays.asList(firstEndId, secondEntId);
        LambdaQueryWrapper<KwoTradeOrderUnit> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(KwoTradeOrderUnit::getEntId, entIds).eq(KwoTradeOrderUnit::getDelFlag, Global.NO);
        List<KwoTradeOrderUnit> list = kwoTradeOrderUnitMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptySet();
        }
        Map<Long, Set<Long>> map = list.stream().collect(Collectors.groupingBy(KwoTradeOrderUnit::getTOrderId,
                Collectors.mapping(KwoTradeOrderUnit::getEntId, Collectors.toSet())));
        Set<Long> orderIds = new HashSet<>(map.keySet().size());
        map.forEach((k,v)->{
            if (v.containsAll(entIds)){
                orderIds.add(k);
            }
        });
        return orderIds;
    }
}
