package com.sckw.payment.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sckw.core.common.enums.NumberConstant;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.IdWorker;
import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.payment.dao.KwpTradeFreezeMapper;
import com.sckw.payment.model.KwpTradeFreeze;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author xucaiqin
 * @date 2023-09-20 17:32:24
 */
@Service
@RequiredArgsConstructor
public class KwpTradeFreezeService {
    private final KwpTradeFreezeMapper kwpTradeFreezeMapper;

    /**
     * 通过贸易订单id查询
     *
     * @param tOrderId 贸易订单id
     * @return 冻结订单号
     */
    public KwpTradeFreeze getByTOrderId(Long tOrderId) {
        LambdaQueryWrapper<KwpTradeFreeze> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpTradeFreeze::getTOrderId, tOrderId)
                .eq(KwpTradeFreeze::getDelFlag, Global.UN_DELETED).last("limit 1");
        return kwpTradeFreezeMapper.selectOne(wrapper);
    }

    public void save(Long tOrderId, String orderNo) {
        KwpTradeFreeze kwpTradeFreeze = new KwpTradeFreeze();
        kwpTradeFreeze.setId(new IdWorker(NumberConstant.ONE).nextId());
        kwpTradeFreeze.setTOrderId(tOrderId);
        kwpTradeFreeze.setOrderNo(orderNo);
        kwpTradeFreeze.setStatus(0);
        kwpTradeFreeze.setCreateBy(LoginUserHolder.getUserId());
        kwpTradeFreeze.setCreateTime(LocalDateTime.now());
        kwpTradeFreeze.setUpdateBy(LoginUserHolder.getUserId());
        kwpTradeFreeze.setUpdateTime(LocalDateTime.now());
        kwpTradeFreeze.setDelFlag(Global.UN_DELETED);
        kwpTradeFreezeMapper.insert(kwpTradeFreeze);
    }

}
