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.model.constant.Global;
import com.sckw.payment.dao.KwpLedgerLogisticsUnitMapper;
import com.sckw.payment.model.KwpLedgerLogisticsUnit;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * @author xucaiqin
 * @date 2023-07-13 17:32:34
 */
@Service
@AllArgsConstructor
public class KwpLedgerLogisticsUnitService {
    private final KwpLedgerLogisticsUnitMapper logisticsUnitMapper;

    /**
     * 批量保存对账单关联企业信息
     *
     * @param logisticsUnits 企业信息
     */
    public void saveList(List<KwpLedgerLogisticsUnit> logisticsUnits) {
        if (!CollectionUtils.isEmpty(logisticsUnits)) {
            for (KwpLedgerLogisticsUnit logisticsUnit : logisticsUnits) {
                logisticsUnitMapper.insert(logisticsUnit);
            }
        }
    }

    /**
     * 删除对账单关联的企业信息
     *
     * @param lLedgerId
     */
    public void remove(Long lLedgerId) {
        KwpLedgerLogisticsUnit logisticsUnit = new KwpLedgerLogisticsUnit();
        logisticsUnit.setDelFlag(Global.DELETED);
        logisticsUnit.setUpdateTime(LocalDateTime.now());
        LambdaUpdateWrapper<KwpLedgerLogisticsUnit> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(KwpLedgerLogisticsUnit::getLLedgerId, lLedgerId);
        wrapper.eq(KwpLedgerLogisticsUnit::getDelFlag, Global.UN_DELETED);
        logisticsUnitMapper.update(logisticsUnit, wrapper);
    }

    public KwpLedgerLogisticsUnit queryCustomerEnt(Long id, Integer unitType) {
        LambdaQueryWrapper<KwpLedgerLogisticsUnit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpLedgerLogisticsUnit::getLLedgerId, id)
                .eq(KwpLedgerLogisticsUnit::getDelFlag, Global.UN_DELETED)
                .eq(KwpLedgerLogisticsUnit::getUnitType, unitType).last("limit 1");
        KwpLedgerLogisticsUnit kwpLedgerTradeUnit = logisticsUnitMapper.selectOne(wrapper);
        if (Objects.isNull(kwpLedgerTradeUnit)) {
            return new KwpLedgerLogisticsUnit();
        }
        return kwpLedgerTradeUnit;
    }
}
