package com.jkcredit.invoice.hub.service.recharge;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkcredit.invoice.hub.mapper.RechargeMapper;
import com.jkcredit.invoice.hub.mapper.UserBalanceMapper;
import com.jkcredit.invoice.hub.model.dto.recharge.RechargeDto;
import com.jkcredit.invoice.hub.model.po.recharge.RechargePo;
import com.jkcredit.invoice.hub.model.po.userBalance.UserBalancePo;
import com.jkcredit.invoice.hub.model.vo.recharge.RechargeVo;
import com.jkcredit.invoice.hub.service.base.BaseService;
import com.jkcredit.invoice.hub.spi.lang.exception.ServiceException;
import com.jkcredit.invoice.hub.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @description:
 * @author: xusonglin
 * @create: 2020/1/12 20:49
 * @version: V1.0
 **/
@Service
@Slf4j
@Transactional(readOnly = true)
public class RechargeServiceImpl extends BaseService implements RechargeService {
    @Resource
    RechargeMapper rechargeMapper;
    @Resource
    UserBalanceMapper userBalanceMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void recharge(RechargeDto rechargeDto) {
        validate(rechargeDto);
        UserBalancePo userBalancePo = userBalanceMapper.selectById(rechargeDto.getUserId());
        RechargePo rechargePo = new RechargePo();
        BeanUtil.copyProperties(rechargePo, rechargeDto);
        try {
            if (userBalancePo == null) {
                userBalancePo = new UserBalancePo();
                userBalancePo.setUserId(rechargeDto.getUserId());
                userBalancePo.setBalance(rechargeDto.getMoney());
                userBalancePo.setUpdateTime(new Date());
                userBalanceMapper.insert(userBalancePo);
                rechargePo.setBeforeBalance(BigDecimal.ZERO);
            } else {
                rechargePo.setBeforeBalance(userBalancePo.getBalance());
                userBalancePo.setBalance(userBalancePo.getBalance().add(rechargeDto.getMoney()));
                userBalanceMapper.updateById(userBalancePo);
            }

            rechargePo.setCreateTime(new Date());
            rechargePo.setCreateUser(rechargeDto.getCreateUser());
            rechargeMapper.insert(rechargePo);
        } catch (Exception e) {
            log.error("新增余额表失败，失败原因：{}", e.getMessage());
            throw new ServiceException("新增余额表失败");
        }
    }

    @Override
    public IPage<RechargeVo> getRechargeRecords(Page page, RechargeDto rechargeDto) {
        IPage<RechargePo> rechargePoIPage = rechargeMapper.getRechargeRecords(page, rechargeDto);
        return rechargePoIPage.convert(this::convert);
    }

    private RechargeVo convert(RechargePo po) {
        RechargeVo vo = new RechargeVo();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }
}
