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.KwpWalletPrepayMapper;
import com.sckw.payment.model.KwpWalletPrepay;
import com.sckw.payment.model.constant.PrePayEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * @author xucaiqin
 * @date 2023-09-22 15:49:51
 */
@Service
@RequiredArgsConstructor
public class KwpWalletPrepayService {

    private final KwpWalletPrepayMapper kwpWalletPrepayMapper;

    /**
     * 保存预付订单
     *
     * @param orderNo
     */
    public void savePrepay(String orderNo, PrePayEnum prePayEnum) {
        KwpWalletPrepay kwpWalletPrepay = new KwpWalletPrepay();
        kwpWalletPrepay.setId(new IdWorker(NumberConstant.ONE).nextId());
        kwpWalletPrepay.setOrderNo(orderNo);
        kwpWalletPrepay.setAction(prePayEnum.getStatus());
        kwpWalletPrepay.setRemark(prePayEnum.getDesc());
        kwpWalletPrepay.setStatus(2);
        kwpWalletPrepay.setCreateBy(LoginUserHolder.getUserId());
        kwpWalletPrepay.setCreateTime(LocalDateTime.now());
        kwpWalletPrepay.setUpdateBy(LoginUserHolder.getUserId());
        kwpWalletPrepay.setUpdateTime(LocalDateTime.now());
        kwpWalletPrepay.setDelFlag(Global.UN_DELETED);
        kwpWalletPrepayMapper.insert(kwpWalletPrepay);
    }

    public Integer getAction(String orderNo) {
        LambdaQueryWrapper<KwpWalletPrepay> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpWalletPrepay::getDelFlag, Global.UN_DELETED)
                .eq(KwpWalletPrepay::getOrderNo, orderNo).last("limit 1");
        KwpWalletPrepay kwpWalletPrepay = kwpWalletPrepayMapper.selectOne(wrapper);
        if(Objects.nonNull(kwpWalletPrepay)){
            return kwpWalletPrepay.getAction();
        }
        return null;
    }
}
