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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzj.soopin.member.convert.AccountBillConvert;
import com.wzj.soopin.member.domain.bo.AccountBillBO;
import com.wzj.soopin.member.domain.po.AccountBill;
import com.wzj.soopin.member.domain.po.MemberAccount;
import com.wzj.soopin.member.domain.vo.AccountBillVO;
import com.wzj.soopin.member.domain.vo.AccountStatisticVO;
import com.wzj.soopin.member.enums.AccountBillChangeTypeEnum;
import com.wzj.soopin.member.enums.AccountBillSourceEnum;
import com.wzj.soopin.member.service.IMemberAccountService;
import com.wzj.soopin.transaction.mapper.AccountBillMapper;
import com.wzj.soopin.transaction.mapper.DivideDetailMapper;
import com.wzj.soopin.transaction.service.IAccountBillService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.system.domain.SysTenantAccount;
import org.dromara.system.service.ISysTenantAccountService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 会员账户表Service业务层处理
 *
 *
 * @author zcc
 */
@Service
@AllArgsConstructor
@Slf4j
public class AccountBillServiceImpl extends
    ServiceImpl<AccountBillMapper, AccountBill> implements IAccountBillService {

    private final ISysTenantAccountService tenantAccountService;
    private final DivideDetailMapper divideDetailMapper;

    private final AccountBillConvert convert;

    private final IMemberAccountService memberAccountService;

    @Override
    public AccountStatisticVO getStatistic(String tenantId) {
        //先获取账户信息
        SysTenantAccount account =tenantAccountService.getByTenantId(tenantId);
        AccountStatisticVO vo = new AccountStatisticVO();
        vo.setAccountId(account.getId());
        vo.setMoneyBalance(account.getMoneyBalance());
        vo.setMoneyIn(baseMapper.getAccountBillSum(account.getId(), 1));
        vo.setMoneyOut(baseMapper.getAccountBillSum(account.getId(), 2));
        //然后统计账户的所有入的总和
        //统计已入账金额
        vo.setCredited(divideDetailMapper.getMoney(account.getId(),1));
        //统计未入账金额
        vo.setPending(divideDetailMapper.getMoney(account.getId(),2));
        return vo;
    }


    @Override
    public   IPage<AccountBillVO> selectVoPage(  AccountBillBO bo) {
        Page<AccountBill> pageResult= baseMapper.selectPage(bo.getPage(), bo.toWrapper());

        return convert.toVO(pageResult);
    }




    @Override
    public boolean addMoney(BigDecimal money, Long memberId, AccountBillSourceEnum source, String remark) {
        MemberAccount memberAccount = memberAccountService.getMemberAccount(memberId);
        if (memberAccount == null) {
            throw new RuntimeException("用户不存在");
        }
        //检查当前用于的账户余额是否充足
        BigDecimal balance = memberAccount.getWallet();


        BigDecimal newBalance = balance.add(money);
        //锁定用户余额
        memberAccountService.updateById(memberAccount.toBuilder().wallet(newBalance).build());

        //生成账单
        AccountBill memberAccountChangeRecord = AccountBill.builder()
            .accountId(memberAccount.getId())
            .changeAmount(money)
            .moneyBalance(newBalance)
            .beforeBalance(balance)
            .afterBalance(newBalance)
            .changeType(AccountBillChangeTypeEnum.IN.getCode())
            .changeDesc(remark)
            .source(source.getCode())
            .build();
        baseMapper.insert(memberAccountChangeRecord);
        return false;
    }

    @Override
    public boolean reduceMoney(BigDecimal money, Long memberId, AccountBillSourceEnum source, String remark) {
        MemberAccount memberAccount = memberAccountService.getMemberAccount(memberId);
        if (memberAccount == null) {
            throw new RuntimeException("用户不存在");
        }
        //检查当前用于的账户余额是否充足
        BigDecimal balance = memberAccount.getWallet();
        if (balance.compareTo(money) < 0) {
            throw new RuntimeException("用户余额不足");
        }

        BigDecimal newBalance = balance.subtract(money);
        //锁定用户余额
        memberAccountService.updateById(memberAccount.toBuilder().wallet(newBalance).build());

        //生成账单
        AccountBill memberAccountChangeRecord = AccountBill.builder()
            .accountId(memberAccount.getId())
            .changeAmount(money)
            .moneyBalance(newBalance)
            .beforeBalance(balance)
            .afterBalance(newBalance)
            .changeType(AccountBillChangeTypeEnum.OUT.getCode())
            .changeDesc(remark)
            .source(source.getCode())
            .build();
        baseMapper.insert(memberAccountChangeRecord);

        return true;
    }
}
