package com.jeequan.jeepay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.db.entity.MemberRechargeRecord;
import com.jeequan.jeepay.service.mapper.MemberRechargeRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 会员充值记录表 服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2023-04-12
 */
@Slf4j
@Service
public class MemberRechargeRecordService extends ServiceImpl<MemberRechargeRecordMapper, MemberRechargeRecord> {

    @Autowired private MemberAccountHistoryService memberAccountHistoryService;
    @Autowired private MemberRechargeRecordMapper memberRechargeRecordMapper;


    public boolean updateInit2Ing(String rechargeRecordId, String payOrderId) {
        MemberRechargeRecord updateRecord = new MemberRechargeRecord();
        updateRecord.setState(MemberRechargeRecord.STATE_ING);
        updateRecord.setPayOrderId(payOrderId);

        return update(updateRecord, new LambdaUpdateWrapper<MemberRechargeRecord>()
                .eq(MemberRechargeRecord::getRechargeRecordId, rechargeRecordId).eq(MemberRechargeRecord::getState, MemberRechargeRecord.STATE_INIT));
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean updateIng2SuccessAndMbrAccount(String rechargeRecordId, String payOrderId) {

        MemberRechargeRecord dbRecord = this.getById(rechargeRecordId);
        Long afterBalance = memberAccountHistoryService.updateMbrAccount4RechagreRecord(dbRecord);

        MemberRechargeRecord updateRecord = new MemberRechargeRecord();
        updateRecord.setState(MemberRechargeRecord.STATE_SUCCESS);
        updateRecord.setPayOrderId(payOrderId);
        updateRecord.setSuccessTime(new Date());
        updateRecord.setAfterBalance(afterBalance);

        boolean result = update(updateRecord, new LambdaUpdateWrapper<MemberRechargeRecord>()
                .eq(MemberRechargeRecord::getRechargeRecordId, rechargeRecordId).eq(MemberRechargeRecord::getState, MemberRechargeRecord.STATE_ING));
        if (!result) {
            log.info("更新充值记录状态[充值中--->充值成功]失败，充值记录ID={}", rechargeRecordId);
            throw new BizException("update fail");
        }


        return true;
    }

    public boolean updateInit2Fail(String rechargeRecordId, String errCode, String errMsg) {
        MemberRechargeRecord updateRecord = new MemberRechargeRecord();
        updateRecord.setState(MemberRechargeRecord.STATE_FAIL);
        updateRecord.setErrCode(errCode);
        updateRecord.setErrMsg(errMsg);

        return update(updateRecord, new LambdaUpdateWrapper<MemberRechargeRecord>()
                .eq(MemberRechargeRecord::getRechargeRecordId, rechargeRecordId).eq(MemberRechargeRecord::getState, MemberRechargeRecord.STATE_INIT));
    }

    public boolean updateIng2Fail(String rechargeRecordId, String errCode, String errMsg) {
        MemberRechargeRecord updateRecord = new MemberRechargeRecord();
        updateRecord.setState(MemberRechargeRecord.STATE_FAIL);
        updateRecord.setErrCode(errCode);
        updateRecord.setErrMsg(errMsg);

        return update(updateRecord, new LambdaUpdateWrapper<MemberRechargeRecord>()
                .eq(MemberRechargeRecord::getRechargeRecordId, rechargeRecordId).eq(MemberRechargeRecord::getState, MemberRechargeRecord.STATE_ING));
    }

    public Map count(LambdaQueryWrapper<MemberRechargeRecord> wrapper) {
        return memberRechargeRecordMapper.count(wrapper);
    }


    /**
     * @author: xiaoyu
     * @date: 2023/4/18 10:17
     * @describe: 查询条件
     */
    public void selectParams(MemberRechargeRecord rechargeRecord, LambdaQueryWrapper<MemberRechargeRecord> wrapper) {
        // 商户号
        wrapper.eq(StringUtils.isNotEmpty(rechargeRecord.getMchNo()), MemberRechargeRecord::getMchNo, rechargeRecord.getMchNo());
        // 充值记录ID
        wrapper.eq(StringUtils.isNotEmpty(rechargeRecord.getMbrId()), MemberRechargeRecord::getMbrId, rechargeRecord.getMbrId());
        // 会员ID
        wrapper.eq(StringUtils.isNotEmpty(rechargeRecord.getRechargeRecordId()), MemberRechargeRecord::getRechargeRecordId, rechargeRecord.getRechargeRecordId());
        // 会员名称快照
        wrapper.like(StringUtils.isNotEmpty(rechargeRecord.getMbrName()), MemberRechargeRecord::getMbrName, rechargeRecord.getMbrName());
        // 支付金额
        wrapper.eq(rechargeRecord.getPayAmount() != null, MemberRechargeRecord::getPayAmount, rechargeRecord.getPayAmount());
        // 充值状态
        wrapper.eq(rechargeRecord.getState() != null, MemberRechargeRecord::getState, rechargeRecord.getState());
        // 支付订单号
        wrapper.eq(StringUtils.isNotEmpty(rechargeRecord.getPayOrderId()), MemberRechargeRecord::getPayOrderId, rechargeRecord.getPayOrderId());
        // 手机号
        wrapper.like(StringUtils.isNotEmpty(rechargeRecord.getMbrTel()), MemberRechargeRecord::getMbrTel, rechargeRecord.getMbrTel());
        // 支付方式代码
        wrapper.eq(StringUtils.isNotEmpty(rechargeRecord.getWayCode()), MemberRechargeRecord::getWayCode, rechargeRecord.getWayCode());
        // 支付方式代码分类
        wrapper.eq(StringUtils.isNotEmpty(rechargeRecord.getWayCodeType()), MemberRechargeRecord::getWayCodeType, rechargeRecord.getWayCodeType());
        // 时间范围条件
        Date[] dateRange = rechargeRecord.buildQueryDateRange();
        if (dateRange[0] != null) {
            wrapper.ge(MemberRechargeRecord::getCreatedAt, dateRange[0]);
        }
        if (dateRange[1] != null) {
            wrapper.le(MemberRechargeRecord::getCreatedAt, dateRange[1]);
        }
    }
}
