package com.sinata.shop.modular.mall.service.impl;
|
|
import cn.hutool.core.util.NumberUtil;
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
|
import com.sinata.common.enums.EnumUserBankDetailDoneType;
|
import com.sinata.common.enums.EnumUserBankDetailType;
|
import com.sinata.core.exception.GunsException;
|
import com.sinata.core.support.HttpKit;
|
import com.sinata.core.support.StrKit;
|
import com.sinata.shop.core.common.exception.BizExceptionEnum;
|
import com.sinata.shop.modular.member.model.MemUserBankDetail;
|
import com.sinata.shop.modular.mall.dao.MemMerchantBankMapper;
|
import com.sinata.shop.modular.mall.dao.MemMerchantMapper;
|
import com.sinata.shop.modular.mall.model.MemMerchant;
|
import com.sinata.shop.modular.mall.model.MemMerchantBank;
|
import com.sinata.shop.modular.mall.service.IMemMerchantBankService;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.Resource;
|
import java.math.BigDecimal;
|
|
/**
|
* <p>
|
* 门店账户信息 服务实现类
|
* </p>
|
*
|
* @author frankevil
|
* @since 2023-03-23
|
*/
|
@Service
|
@Transactional
|
public class MemMerchantBankServiceImpl extends ServiceImpl<MemMerchantBankMapper, MemMerchantBank> implements IMemMerchantBankService {
|
|
@Resource
|
public MemMerchantMapper merchantMapper;
|
|
@Override
|
public void handleMerchantBalance(Integer merchantId, BigDecimal amount, EnumUserBankDetailDoneType doneType) {
|
// 获取门店信息
|
MemMerchant merchant = merchantMapper.selectById(merchantId);
|
|
MemMerchantBank merchantBank = this.selectOne(new EntityWrapper<MemMerchantBank>()
|
.eq("id", merchantId)
|
.last("FOR UPDATE"));
|
|
BigDecimal oldAmount = merchantBank.getBalance();
|
if (amount.doubleValue() < 0 && merchantBank.getBalance().doubleValue() < amount.abs().doubleValue()) {
|
throw new GunsException(BizExceptionEnum.REQUEST_NULL.getCode(), "门店余额不足");
|
}
|
merchantBank.setBalance(NumberUtil.add(merchantBank.getBalance(), amount));
|
this.updateById(merchantBank);
|
|
if (doneType != null) {
|
// 所有门店账户记录,用MallUserBankDetail,记录值UserId、MerchantId必填
|
MemUserBankDetail bankDetail = new MemUserBankDetail()
|
.setUserId(merchant.getUserId())
|
.setAmount(amount)
|
.setType(EnumUserBankDetailType.BALANCE.getIndex())
|
.setDoneType(doneType.getIndex())
|
.setMark(EnumUserBankDetailDoneType.getMarkByIndex(doneType.getIndex()))
|
.setAdminId(merchantId)
|
.setOldAmount(oldAmount);
|
String ip = HttpKit.getIp();
|
if (StrKit.isBlank(ip)) {
|
ip = "127.0.0.1";
|
}
|
bankDetail.setDoneIp(ip);
|
|
//保存明细
|
bankDetail.insert();
|
}
|
}
|
|
@Override
|
public void settlement(Integer merchantId, BigDecimal amount, EnumUserBankDetailType type, EnumUserBankDetailDoneType doneType, String riceOrderNo) {
|
// 获取门店信息
|
MemMerchant merchant = merchantMapper.selectById(merchantId);
|
|
MemMerchantBank merchantBank = this.selectOne(new EntityWrapper<MemMerchantBank>()
|
.eq("id", merchantId)
|
.last("FOR UPDATE"));
|
if(merchantBank == null) {
|
merchantBank = new MemMerchantBank();
|
merchantBank.setId(merchantId);
|
merchantBank.setCreditScore(BigDecimal.ZERO);
|
merchantBank.insert();
|
}
|
|
// 记录原值
|
BigDecimal oldAmount = BigDecimal.ZERO;
|
switch (type) {
|
case CREDIT_SCORE:
|
// 记录原值
|
oldAmount = merchantBank.getCreditScore();
|
|
merchantBank.setCreditScore(NumberUtil.add(merchantBank.getCreditScore(), amount));
|
break;
|
case BALANCE:
|
// 记录原值
|
oldAmount = merchantBank.getBalance();
|
break;
|
default:
|
throw new GunsException(BizExceptionEnum.REQUEST_NULL.getCode(), "暂不支持支付类型");
|
}
|
this.updateById(merchantBank);
|
|
if (doneType != null) {
|
// 所有门店账户记录,用MallUserBankDetail,记录值UserId、MerchantId必填
|
MemUserBankDetail bankDetail = new MemUserBankDetail()
|
.setUserId(merchant.getUserId())
|
.setAmount(amount)
|
.setType(type.getIndex())
|
.setDoneType(doneType.getIndex())
|
.setMark(EnumUserBankDetailDoneType.getMarkByIndex(doneType.getIndex()))
|
.setAdminId(merchantId)
|
.setOldAmount(oldAmount)
|
.setDoneIp(HttpKit.getIp());
|
|
//保存明细
|
bankDetail.insert();
|
}
|
}
|
}
|