package org.jeecg.modules.rechargeInfoLog.service.impl;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.RedisKey;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.jeecg.modules.rechargeInfoLog.entity.TbRechargeInfoLog;
import org.jeecg.modules.rechargeInfoLog.mapper.TbRechargeInfoLogMapper;
import org.jeecg.modules.rechargeInfoLog.service.ITbRechargeInfoLogService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @Description: tb_recharge_info_log
 * @Author: jeecg-boot
 * @Date:   2024-12-12
 * @Version: V1.0
 */
@Service
public class TbRechargeInfoLogServiceImpl extends ServiceImpl<TbRechargeInfoLogMapper, TbRechargeInfoLog> implements ITbRechargeInfoLogService {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private IHanHaiMemberService hanHaiMemberService;


    @Transactional
    @Override
    public Result<?> audit(String id, boolean status, String checkInfo) {
        Integer n = (Integer) redisUtil.get(RedisKey.MER_WITHDRAWAL_AUDIT + id);
        if(n != null){
            throw new JeecgBootException("请勿重复点击！");
        }
        redisUtil.set(RedisKey.MER_WITHDRAWAL_AUDIT + id,1,5);
        TbRechargeInfoLog rechargeInfoLog = this.getById(id);
        if(rechargeInfoLog==null) {
            throw new JeecgBootException("未找到数据");
        }

        Long count = this.lambdaQuery()
                .eq(TbRechargeInfoLog::getWithdrawalId, rechargeInfoLog.getId())
                .count();
        if(count != null && count > 0){
            throw new JeecgBootException("此提现已处理过不通过，请勿重新点击");
        }

        HanHaiMember account = hanHaiMemberService.getById(rechargeInfoLog.getMemberId());
        TbRechargeInfoLog rechargeInfoLog1 = new TbRechargeInfoLog();
        rechargeInfoLog1.setId(rechargeInfoLog.getId());
        rechargeInfoLog1.setState(1);
        if(!status){
            //审核不通过退回余额
            rechargeInfoLog1.setState(3);
            rechargeInfoLog1.setCheckInfo(checkInfo);

            BigDecimal money = rechargeInfoLog.getMoney().add(rechargeInfoLog.getServiceCharge());
            HanHaiMember cmMemberAccount2 = new HanHaiMember();
            cmMemberAccount2.setId(account.getId());
            cmMemberAccount2.setPrice(account.getPrice().add(money));
            boolean b = hanHaiMemberService.updateById(cmMemberAccount2);
            if(!b){
                throw new JeecgBootException("操作失败，请稍后重试");
            }
            TbRechargeInfoLog merRechargeInfoLog = new TbRechargeInfoLog();
            merRechargeInfoLog.setTitle("提现未通过退款");
            merRechargeInfoLog.setMoney(money);
            merRechargeInfoLog.setUserMoney(account.getPrice());
            merRechargeInfoLog.setIsPay(1);
            merRechargeInfoLog.setDelFlag(0);
            merRechargeInfoLog.setPayTime(new Date());
            merRechargeInfoLog.setMemberId(rechargeInfoLog.getMemberId());
            merRechargeInfoLog.setType(1);
            merRechargeInfoLog.setCheckInfo(checkInfo);
            merRechargeInfoLog.setWithdrawalId(rechargeInfoLog.getId());
            boolean save = this.save(merRechargeInfoLog);
            if(!save){
                throw new JeecgBootException("操作失败");
            }
        }
        boolean b = this.updateById(rechargeInfoLog1);
        if(!b){
            throw new JeecgBootException("操作失败，请稍后重试");
        }
        return null;

    }
}
