package com.wzj.soopin.transaction.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzj.soopin.member.domain.po.AccountBill;
import com.wzj.soopin.member.domain.po.MemberAccount;
import com.wzj.soopin.member.enums.AccountBillChangeTypeEnum;
import com.wzj.soopin.member.enums.AccountBillSourceEnum;
import com.wzj.soopin.transaction.service.IAccountBillService;
import com.wzj.soopin.member.service.IMemberAccountService;
import com.wzj.soopin.transaction.domain.po.Charge;
import com.wzj.soopin.transaction.enums.ChargeStatus;
import com.wzj.soopin.transaction.mapper.ChargeMapper;
import com.wzj.soopin.transaction.service.IChargeService;
import com.wzj.soopin.transaction.util.SnowFlake;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.ServiceException;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 会员封禁
 *
 * @author zcc
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ChargeServiceImpl extends ServiceImpl<ChargeMapper, Charge> implements IChargeService {

    private final IMemberAccountService memberAccountService;

    private final IAccountBillService accountBillService;

    @Override
    public boolean audit(Long id, String status) {
        Charge charge = getById(id);
        //调用三方充值接口
        boolean chargeSuccess = true;
        //充值成功后更新会员账户余额
        if (chargeSuccess) {
            //更新会员账户余额
            MemberAccount memberAccount = memberAccountService.getById(charge.getMemberId());
            memberAccount.setWallet(memberAccount.getWallet().add(charge.getMoney()));
            memberAccountService.updateById(memberAccount);
            //生成充值记录
            AccountBill accountBill = AccountBill.builder()
                .accountId(charge.getMemberId())
                .changeAmount(charge.getMoney())
                .changeType(AccountBillChangeTypeEnum.IN.getCode())
                .changeDesc("充值")
                .source(AccountBillSourceEnum.RECHARGE.getCode())
                .build();
            accountBillService.save(accountBill);
            return true;
        }
        //生成充值记录
        return false;
    }


    @Override
    public boolean charge(Charge charge) {
        //判断充值金额不能为零
        if (charge.getMoney()==null||charge.getMoney().compareTo(BigDecimal.ZERO) == 0) {
            throw new ServiceException("充值金额不能为零");
        }
        //状态为待充值
        charge.setStatus(ChargeStatus.WAITING.getCode());
        charge.setType(1);
        //手续费
        charge.setFee(new BigDecimal("0.01").multiply(charge.getMoney()));
        //实际金额
        charge.setActualMoney(charge.getMoney().subtract(charge.getFee()));

        //设置充值信息
        if (charge.getMemberId() == null) {
            throw new ServiceException("充值用户不能为空");
        }
        charge.setCode(SnowFlake.createStr("RC"));
        //保存充值记录
        save(charge);
        return false;
    }

    @Override
    public Charge getByCode(String code) {
        return baseMapper.selectOne(new LambdaQueryWrapper<Charge>().eq(Charge::getCode, code));
    }

    @Override
    public boolean paySuccess(String code, String payNo, Integer method) {
        Charge charge = getByCode(code);
        if(charge==null){
            log.error("充值记录不存在,code:{}",code);
            return false;
        }
        charge.setStatus(ChargeStatus.PENDING.getCode());
//        charge.setPayNo(payNo);
        charge.setMethod(method);
        return updateById(charge);
    }
}
