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.utils.CollectionUtils;
import com.sckw.payment.dao.KwpLedgerLogisticsOrderMapper;
import com.sckw.payment.model.KwpLedgerLogisticsOrder;
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 KwpLedgerLogisticsOrderService {
    private final KwpLedgerLogisticsOrderMapper logisticsOrderMapper;

    /**
     * 根据物流订单id删除物流订单
     *
     * @param lLedgerId 物流订单id
     */
    public void remove(Long lLedgerId) {
        KwpLedgerLogisticsOrder logisticsOrder = new KwpLedgerLogisticsOrder();
        logisticsOrder.setDelFlag(1);
        logisticsOrder.setUpdateTime(LocalDateTime.now());
        LambdaUpdateWrapper<KwpLedgerLogisticsOrder> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(KwpLedgerLogisticsOrder::getLLedgerId, lLedgerId);
        wrapper.eq(KwpLedgerLogisticsOrder::getDelFlag, 0);
        logisticsOrderMapper.update(logisticsOrder, wrapper);
    }

    /**
     * 保存物流订单
     *
     * @param list 物流对账单关联物流订单
     */
    public void saveList(List<KwpLedgerLogisticsOrder> list) {
        for (KwpLedgerLogisticsOrder logisticsOrder : list) {
            logisticsOrderMapper.insert(logisticsOrder);
        }
    }

    /**
     * 根据id查询
     *
     * @param id 物流对账单id
     * @return
     */
    public List<KwpLedgerLogisticsOrder> queryList(Long id) {
        List<KwpLedgerLogisticsOrder> kwpLedgerLogisticsOrders = logisticsOrderMapper.selectList(new LambdaQueryWrapper<KwpLedgerLogisticsOrder>().eq(KwpLedgerLogisticsOrder::getLLedgerId, id).eq(KwpLedgerLogisticsOrder::getDelFlag, 0));
        if (CollectionUtils.isEmpty(kwpLedgerLogisticsOrders)) {
            return new ArrayList<>();
        }
        return kwpLedgerLogisticsOrders;
    }

    /**
     * 校验当前物流订单是否已关联其它草稿单
     *
     * @param ids 物流订单id列表
     */
    public void check(List<Long> ids) {
        List<KwpLedgerLogisticsOrder> kwpLedgerLogisticsOrders = logisticsOrderMapper.selectList(new LambdaQueryWrapper<KwpLedgerLogisticsOrder>()
                .in(KwpLedgerLogisticsOrder::getLOrderId, ids)
                .eq(KwpLedgerLogisticsOrder::getDelFlag, 0)
                .select(KwpLedgerLogisticsOrder::getLLedgerId, KwpLedgerLogisticsOrder::getLOrderId, KwpLedgerLogisticsOrder::getLOrderNo));
        if (CollectionUtils.isNotEmpty(kwpLedgerLogisticsOrders)) {
            for (KwpLedgerLogisticsOrder kwpLedgerLogisticsOrder : kwpLedgerLogisticsOrders) {
                if (ids.contains(kwpLedgerLogisticsOrder.getLOrderId())) {
                    throw new BusinessException(kwpLedgerLogisticsOrder.getLOrderNo() + "已绑定其它对账单");
                }
            }
        }
    }
}
