package com.sckw.payment.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sckw.core.exception.BusinessException;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.payment.dao.KwpLedgerTradeOrderMapper;
import com.sckw.payment.model.KwpLedgerTradeOrder;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xucaiqin
 * @date 2023-07-10 16:38:36
 */
@Service
@AllArgsConstructor
public class KwpLedgerTradeOrderService {
    private final KwpLedgerTradeOrderMapper tradeOrderMapper;

    /**
     * 删除贸易结算单关联的贸易订单
     *
     * @param tLedgerId 结算单id
     */
    public void remove(Long tLedgerId) {
        KwpLedgerTradeOrder kwpLedgerTradeOrder = new KwpLedgerTradeOrder();
        kwpLedgerTradeOrder.setDelFlag(Global.DELETED);
        kwpLedgerTradeOrder.setUpdateTime(LocalDateTime.now());
        LambdaUpdateWrapper<KwpLedgerTradeOrder> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(KwpLedgerTradeOrder::getTLedgerId, tLedgerId);
        wrapper.eq(KwpLedgerTradeOrder::getDelFlag, Global.UN_DELETED);
        tradeOrderMapper.update(kwpLedgerTradeOrder, wrapper);
    }

    /**
     * 批量保存贸易结算单关联的贸易订单
     *
     * @param list 订单关联数据
     */
    public void saveList(List<KwpLedgerTradeOrder> list) {
        if (!CollectionUtils.isEmpty(list)) {
            for (KwpLedgerTradeOrder kwpLedgerTradeOrder : list) {
                tradeOrderMapper.insert(kwpLedgerTradeOrder);
            }
        }
    }

    /**
     * 查询关联的订单数据
     *
     * @param id 对账单id
     * @return
     */
    public List<KwpLedgerTradeOrder> queryList(Long id) {
        List<KwpLedgerTradeOrder> kwpLedgerLogisticsOrders = tradeOrderMapper.selectList(new LambdaQueryWrapper<KwpLedgerTradeOrder>().eq(KwpLedgerTradeOrder::getTLedgerId, id).eq(KwpLedgerTradeOrder::getDelFlag, 0));

        if (CollectionUtils.isEmpty(kwpLedgerLogisticsOrders)) {
            return new ArrayList<>();
        }
        return kwpLedgerLogisticsOrders;
    }

    /**
     * 校验订单是否绑定其它订单
     *
     * @param ids
     */
    public void check(List<Long> ids) {
        List<KwpLedgerTradeOrder> kwpLedgerLogisticsOrders = tradeOrderMapper.selectList(new LambdaQueryWrapper<KwpLedgerTradeOrder>()
                .in(KwpLedgerTradeOrder::getTOrderId, ids)
                .eq(KwpLedgerTradeOrder::getDelFlag, Global.UN_DELETED)
                .select(KwpLedgerTradeOrder::getTLedgerId, KwpLedgerTradeOrder::getTOrderId, KwpLedgerTradeOrder::getTOrderNo));
        if (CollectionUtils.isNotEmpty(kwpLedgerLogisticsOrders)) {
            for (KwpLedgerTradeOrder kwpLedgerTradeOrder : kwpLedgerLogisticsOrders) {
                if (ids.contains(kwpLedgerTradeOrder.getTOrderId())) {
                    throw new BusinessException(kwpLedgerTradeOrder.getTOrderNo() + "已绑定其它对账单");
                }
            }
        }
    }
}
