java110
2022-05-21 c2614ad5c7d1e27fae68e52cd058af363464cdb4
service-acct/src/main/java/com/java110/acct/smo/impl/ShopVipAccountInnerServiceSMOImpl.java
@@ -1,17 +1,28 @@
package com.java110.acct.smo.impl;
import com.java110.acct.dao.IShopVipAccountDetailServiceDao;
import com.java110.acct.dao.IShopVipAccountServiceDao;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.base.smo.BaseServiceSMO;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.PageDto;
import com.java110.dto.account.AccountDto;
import com.java110.dto.accountDetail.AccountDetailDto;
import com.java110.dto.shopVipAccount.ShopVipAccountDto;
import com.java110.intf.acct.IShopVipAccountInnerServiceSMO;
import com.java110.po.account.AccountPo;
import com.java110.po.accountDetail.AccountDetailPo;
import com.java110.po.shopVipAccount.ShopVipAccountPo;
import com.java110.po.shopVipAccountDetail.ShopVipAccountDetailPo;
import com.java110.utils.lock.DistributedLock;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.List;
/**
@@ -25,6 +36,8 @@
@RestController
public class ShopVipAccountInnerServiceSMOImpl extends BaseServiceSMO implements IShopVipAccountInnerServiceSMO {
    @Autowired
    private IShopVipAccountDetailServiceDao shopVipAccountDetailServiceDaoImpl;
    @Autowired
    private IShopVipAccountServiceDao shopVipAccountServiceDaoImpl;
@@ -80,4 +93,122 @@
    public void setShopVipAccountServiceDaoImpl(IShopVipAccountServiceDao shopVipAccountServiceDaoImpl) {
        this.shopVipAccountServiceDaoImpl = shopVipAccountServiceDaoImpl;
    }
    /**
     * 预存接口
     *
     * @param accountDetailPo
     * @return
     */
    @Override
    @Java110Transactional
    public int prestoreAccount(@RequestBody ShopVipAccountDetailPo accountDetailPo) {
        if (StringUtil.isEmpty(accountDetailPo.getVipAcctId())) {
            throw new IllegalArgumentException("账户ID为空");
        }
        //开始枷锁
        String requestId = DistributedLock.getLockUUID();
        String key = accountDetailPo.getVipAcctId();
        ShopVipAccountDto accountDto = null;
        List<ShopVipAccountDto> accounts = null;
        int flag;
        try {
            DistributedLock.waitGetDistributedLock(key, requestId);
            accountDto = new ShopVipAccountDto();
            accountDto.setShopId(accountDetailPo.getShopId());
            accountDto.setVipAcctId(accountDetailPo.getVipAcctId());
            accounts = BeanConvertUtil.covertBeanList(shopVipAccountServiceDaoImpl.getShopVipAccountInfo(BeanConvertUtil.beanCovertMap(accountDto)), ShopVipAccountDto.class);
            if (accounts == null || accounts.size() < 1) {
                throw new IllegalArgumentException("账户不存在");
            }
            //在账户增加
            double amount = Double.parseDouble(accounts.get(0).getAmount());
            BigDecimal amountBig = new BigDecimal(amount);
            amount = amountBig.add(new BigDecimal(accountDetailPo.getAmount())).doubleValue();
            ShopVipAccountPo accountPo = new ShopVipAccountPo();
            accountPo.setShopId(accountDetailPo.getShopId());
            accountPo.setVipAcctId(accountDetailPo.getVipAcctId());
            accountPo.setAmount(amount + "");
            flag = shopVipAccountServiceDaoImpl.updateShopVipAccountInfo(BeanConvertUtil.beanCovertMap(accountPo));
            if (flag < 1) {
                throw new IllegalArgumentException("更新账户失败");
            }
        } finally {
            DistributedLock.releaseDistributedLock(requestId, key);
        }
        accountDetailPo.setDetailType(AccountDetailDto.DETAIL_TYPE_IN);
        if(StringUtil.isEmpty(accountDetailPo.getDetailId())) {
            accountDetailPo.setDetailId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_detailId));
        }
        if(StringUtil.isEmpty(accountDetailPo.getOrderId())){
            accountDetailPo.setOrderId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_orderId));
        }
        if(StringUtil.isEmpty(accountDetailPo.getRelAcctId())){
            accountDetailPo.setRelAcctId("-1");
        }
        //保存交易明细
        return shopVipAccountDetailServiceDaoImpl.saveShopVipAccountDetailInfo(BeanConvertUtil.beanCovertMap(accountDetailPo));
    }
    /**
     * 扣款接口
     *
     * @param accountDetailPo
     * @return
     */
    @Override
    @Java110Transactional
    public int withholdAccount(@RequestBody ShopVipAccountDetailPo accountDetailPo) {
        if (StringUtil.isEmpty(accountDetailPo.getVipAcctId())) {
            throw new IllegalArgumentException("账户ID为空");
        }
        //开始枷锁
        String requestId = DistributedLock.getLockUUID();
        String key = accountDetailPo.getVipAcctId();
        ShopVipAccountDto accountDto = null;
        List<ShopVipAccountDto> accounts = null;
        int flag;
        try {
            DistributedLock.waitGetDistributedLock(key, requestId);
            accountDto = new ShopVipAccountDto();
            accountDto.setShopId(accountDetailPo.getShopId());
            accountDto.setVipAcctId(accountDetailPo.getVipAcctId());
            accounts = BeanConvertUtil.covertBeanList(shopVipAccountServiceDaoImpl.getShopVipAccountInfo(BeanConvertUtil.beanCovertMap(accountDto)), ShopVipAccountDto.class);
            if (accounts == null || accounts.size() < 1) {
                throw new IllegalArgumentException("账户不存在");
            }
            //在账户增加
            double amount = Double.parseDouble(accounts.get(0).getAmount());
            BigDecimal amountBig = new BigDecimal(amount);
            amount = amountBig.subtract(new BigDecimal(accountDetailPo.getAmount())).doubleValue();
            if(amount < 0){
                throw new IllegalArgumentException("余额不足");
            }
            ShopVipAccountPo accountPo = new ShopVipAccountPo();
            accountPo.setShopId(accountDetailPo.getShopId());
            accountPo.setVipAcctId(accountDetailPo.getVipAcctId());
            accountPo.setAmount(amount + "");
            flag = shopVipAccountServiceDaoImpl.updateShopVipAccountInfo(BeanConvertUtil.beanCovertMap(accountPo));
            if (flag < 1) {
                throw new IllegalArgumentException("更新账户失败");
            }
        } finally {
            DistributedLock.releaseDistributedLock(requestId, key);
        }
        accountDetailPo.setDetailType(AccountDetailDto.DETAIL_TYPE_OUT);
        if(StringUtil.isEmpty(accountDetailPo.getDetailId())) {
            accountDetailPo.setDetailId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_detailId));
        }
        if(StringUtil.isEmpty(accountDetailPo.getOrderId())){
            accountDetailPo.setOrderId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_orderId));
        }
        if(StringUtil.isEmpty(accountDetailPo.getRelAcctId())){
            accountDetailPo.setRelAcctId("-1");
        }
        //保存交易明细
        return shopVipAccountDetailServiceDaoImpl.saveShopVipAccountDetailInfo(BeanConvertUtil.beanCovertMap(accountDetailPo));
    }
}