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.order.dao.KwoTradeOrderContractMapper;
import com.sckw.order.model.KwoTradeOrderContract;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @desc: 合同信息service
 * @author: yzc
 * @date: 2023-07-13 9:48
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class KwoTradeOrderContractService {

    private final KwoTradeOrderContractMapper kwoTradeOrderContractMapper;

    /**
     * @desc: 插入
     * @author: yzc
     * @date: 2023-07-13 9:51
     * @Param contract:
     * @return: void
     */
    public void insert(KwoTradeOrderContract contract) {
        kwoTradeOrderContractMapper.insert(contract);
    }

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

    /**
     * @desc: 根据orderId获取
     * @author: yzc
     * @date: 2023-07-18 11:25
     * @Param orderId:
     * @return: com.sckw.order.model.KwoTradeOrderContract
     */
    public KwoTradeOrderContract getByOrderId(Long orderId) {
        LambdaQueryWrapper<KwoTradeOrderContract> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwoTradeOrderContract::getTOrderId, orderId).eq(KwoTradeOrderContract::getDelFlag, Global.NO).last("LIMIT 1");
        return kwoTradeOrderContractMapper.selectOne(wrapper);
    }

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

    /**
     * @desc: 根据合同id获取
     * @author: yzc
     * @date: 2023-07-20 11:47
     * @Param contractId:
     * @return: java.util.List<com.sckw.order.model.KwoTradeOrderContract>
     */
    public List<KwoTradeOrderContract> getByContractId(Long contractId) {
        LambdaQueryWrapper<KwoTradeOrderContract> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwoTradeOrderContract::getContractId, contractId).eq(KwoTradeOrderContract::getDelFlag, Global.NO);
        List<KwoTradeOrderContract> list = kwoTradeOrderContractMapper.selectList(wrapper);
        return CollectionUtils.emptyIfNull(list);
    }
}
