package com.jeequan.jeepay.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeequan.jeepay.core.constants.ApiCodeEnum;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.db.entity.ChannelAccountCashoutRecord;
import com.jeequan.jeepay.service.mapper.ChannelAccountCashoutRecordMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 提现结算记录表 服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2022-11-09
 */
@Service
public class ChannelAccountCashoutRecordService extends ServiceImpl<ChannelAccountCashoutRecordMapper, ChannelAccountCashoutRecord> {

    /** 初始化提现记录 */
    public ChannelAccountCashoutRecord initRecord(String recordId, Long amount, Long queryBalanceAmount, String mchNo, String mchName, String appId, String isvNo,
                           String ifCode, String subMchId, String payOrderId, Long createUid, String createBy) {

        if (StringUtils.isNotBlank(payOrderId) && count(ChannelAccountCashoutRecord.gw().eq(ChannelAccountCashoutRecord::getPayOrderId, payOrderId)) > 0) {
            return null;
        }

        ChannelAccountCashoutRecord cashoutRecord = new ChannelAccountCashoutRecord();
        cashoutRecord.setRid(recordId).setCashoutAmount(amount).setCurrentBalance(queryBalanceAmount).setAppId(appId).setMchNo(mchNo).setMchName(mchName).setIsvNo(isvNo)
                .setIfCode(ifCode).setChannelSubMchId(subMchId).setPayOrderId(payOrderId).setCreatedUid(createUid).setCreatedBy(createBy);
        cashoutRecord.setState(ChannelAccountCashoutRecord.CASHOUT_STATE_CREATE); // 初始状态为  创建提现单

        boolean result = save(cashoutRecord);
        if (!result) {
            throw new BizException(ApiCodeEnum.SYS_OPERATION_FAIL_CREATE);
        }
        return cashoutRecord;
    }

    /** 更新提现状态  创建 -> 提现中 */
    public boolean updateInit2Ing(String recordId, String channelRid) {
        ChannelAccountCashoutRecord updateRecord = new ChannelAccountCashoutRecord();
        updateRecord.setState(ChannelAccountCashoutRecord.CASHOUT_STATE_ING);
        updateRecord.setChannelRid(channelRid);

        return update(updateRecord, new LambdaUpdateWrapper<ChannelAccountCashoutRecord>()
                .eq(ChannelAccountCashoutRecord::getRid, recordId).eq(ChannelAccountCashoutRecord::getState, ChannelAccountCashoutRecord.CASHOUT_STATE_CREATE));
    }

    /** 更新提现状态  提现中 -> 提现成功 */
    public boolean updateIng2Success(String recordId, String channelRid, String bankName, String bankAccount, String bankAccountName) {
        ChannelAccountCashoutRecord updateRecord = new ChannelAccountCashoutRecord();
        updateRecord.setState(ChannelAccountCashoutRecord.CASHOUT_STATE_SUCCESS);
        updateRecord.setChannelRid(channelRid);
        updateRecord.setBankName(bankName);
        updateRecord.setBankAccount(bankAccount);
        updateRecord.setBankAccountName(bankAccountName);
        updateRecord.setSuccessTime(new Date());

        return update(updateRecord, new LambdaUpdateWrapper<ChannelAccountCashoutRecord>()
                .eq(ChannelAccountCashoutRecord::getRid, recordId).eq(ChannelAccountCashoutRecord::getState, ChannelAccountCashoutRecord.CASHOUT_STATE_ING));
    }

    /** 更新提现状态  提现中 -> 提现失败 */
    public boolean updateIng2Fail(String recordId, String channelErrMsg) {
        ChannelAccountCashoutRecord updateRecord = new ChannelAccountCashoutRecord();
        updateRecord.setState(ChannelAccountCashoutRecord.CASHOUT_STATE_FAIL);
        updateRecord.setFailInfo(channelErrMsg);

        return update(updateRecord, new LambdaUpdateWrapper<ChannelAccountCashoutRecord>()
                .eq(ChannelAccountCashoutRecord::getRid, recordId).eq(ChannelAccountCashoutRecord::getState, ChannelAccountCashoutRecord.CASHOUT_STATE_ING));
    }

}
