feat:充值模板配置,可配置充值多少送多少(基于目前的开发)
This commit is contained in:
parent
2b6eee2ada
commit
efdf3092e5
4 changed files with 254 additions and 8 deletions
|
|
@ -3,12 +3,24 @@ package com.sqx.modules.common.service;
|
||||||
import com.sqx.common.utils.Result;
|
import com.sqx.common.utils.Result;
|
||||||
import com.sqx.modules.common.entity.CommonInfo;
|
import com.sqx.modules.common.entity.CommonInfo;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author fang
|
* @author fang
|
||||||
* @date 2020/7/8
|
* @date 2020/7/8
|
||||||
*/
|
*/
|
||||||
public interface CommonInfoService {
|
public interface CommonInfoService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据充值金额获取赠送金额
|
||||||
|
* @param rechargeAmount 充值金额
|
||||||
|
* @return 赠送金额
|
||||||
|
*/
|
||||||
|
BigDecimal getRechargeBonus(BigDecimal rechargeAmount);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存对象
|
* 保存对象
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -7,18 +7,24 @@ import com.sqx.modules.common.entity.CommonInfo;
|
||||||
import com.sqx.modules.common.service.CommonInfoService;
|
import com.sqx.modules.common.service.CommonInfoService;
|
||||||
import com.sqx.modules.tbCoupon.entity.TbCoupon;
|
import com.sqx.modules.tbCoupon.entity.TbCoupon;
|
||||||
import com.sqx.modules.tbCoupon.service.TbCouponService;
|
import com.sqx.modules.tbCoupon.service.TbCouponService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author fang
|
* @author fang
|
||||||
* @date 2020/7/8
|
* @date 2020/7/8
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class CommonInfoServiceImpl extends ServiceImpl<CommonInfoDao, CommonInfo> implements CommonInfoService {
|
public class CommonInfoServiceImpl extends ServiceImpl<CommonInfoDao, CommonInfo> implements CommonInfoService {
|
||||||
|
|
||||||
|
|
@ -33,8 +39,93 @@ public class CommonInfoServiceImpl extends ServiceImpl<CommonInfoDao, CommonInfo
|
||||||
this.commonInfoDao = commonInfoDao;
|
this.commonInfoDao = commonInfoDao;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证充值金额配置格式
|
||||||
|
* @param configValue 配置值,格式为:充值金额:赠送金额,充值金额:赠送金额,... 或 充值金额,充值金额,...
|
||||||
|
* @return 验证结果,成功返回null,失败返回错误信息
|
||||||
|
*/
|
||||||
|
private Result validateRechargeBonusConfig(String configValue) {
|
||||||
|
if (StringUtils.isBlank(configValue)) {
|
||||||
|
return Result.error("充值金额配置不能为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 按逗号分割每个配置项
|
||||||
|
String[] configItems = configValue.split(",");
|
||||||
|
|
||||||
|
// 检查是否所有项都不包含冒号(纯充值金额格式)
|
||||||
|
boolean allWithoutColon = true;
|
||||||
|
boolean allWithColon = true;
|
||||||
|
|
||||||
|
for (String item : configItems) {
|
||||||
|
if (item.contains(":")) {
|
||||||
|
allWithoutColon = false;
|
||||||
|
} else {
|
||||||
|
allWithColon = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 不允许混合格式(有些有冒号,有些没有)
|
||||||
|
if (!allWithoutColon && !allWithColon) {
|
||||||
|
return Result.error("充值金额配置格式错误,不能混合使用两种格式。请使用统一格式:充值金额:赠送金额,... 或 充值金额,...");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查每个配置项的格式
|
||||||
|
for (String item : configItems) {
|
||||||
|
if (item.contains(":")) {
|
||||||
|
// 带赠送金额的格式
|
||||||
|
String[] parts = item.split(":");
|
||||||
|
if (parts.length != 2) {
|
||||||
|
return Result.error("充值金额配置格式错误,正确格式为:充值金额:赠送金额,充值金额:赠送金额,...");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 验证金额是否为有效的数字
|
||||||
|
BigDecimal rechargeAmount = new BigDecimal(parts[0].trim());
|
||||||
|
BigDecimal bonusAmount = new BigDecimal(parts[1].trim());
|
||||||
|
|
||||||
|
// 验证金额是否为正数
|
||||||
|
if (rechargeAmount.compareTo(BigDecimal.ZERO) <= 0 || bonusAmount.compareTo(BigDecimal.ZERO) < 0) {
|
||||||
|
return Result.error("充值金额必须大于0,赠送金额必须大于等于0");
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return Result.error("充值金额和赠送金额必须为有效的数字");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 纯充值金额格式
|
||||||
|
try {
|
||||||
|
// 验证金额是否为有效的数字
|
||||||
|
BigDecimal rechargeAmount = new BigDecimal(item.trim());
|
||||||
|
|
||||||
|
// 验证金额是否为正数
|
||||||
|
if (rechargeAmount.compareTo(BigDecimal.ZERO) <= 0) {
|
||||||
|
return Result.error("充值金额必须大于0");
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return Result.error("充值金额必须为有效的数字");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证通过
|
||||||
|
return null;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return Result.error("充值金额配置格式错误,正确格式为:充值金额:赠送金额,... 或 充值金额,...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Result update(CommonInfo commonInfo) {
|
public Result update(CommonInfo commonInfo) {
|
||||||
|
// 检查是否是充值金额配置(type=279)
|
||||||
|
if (commonInfo != null && commonInfo.getType() != null && commonInfo.getType() == 279) {
|
||||||
|
// 验证充值金额配置格式
|
||||||
|
Result validationResult = validateRechargeBonusConfig(commonInfo.getValue());
|
||||||
|
if (validationResult != null) {
|
||||||
|
return validationResult;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 通过验证,更新数据库
|
||||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
Date now = new Date();
|
Date now = new Date();
|
||||||
commonInfo.setCreateAt(sdf.format(now));
|
commonInfo.setCreateAt(sdf.format(now));
|
||||||
|
|
@ -80,6 +171,73 @@ public class CommonInfoServiceImpl extends ServiceImpl<CommonInfoDao, CommonInfo
|
||||||
}
|
}
|
||||||
return Result.success().put("data", commonInfoList);
|
return Result.success().put("data", commonInfoList);
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
|
public BigDecimal getRechargeBonus(BigDecimal rechargeAmount) {
|
||||||
|
// 获取充值赠送配置(type=279)
|
||||||
|
CommonInfo rechargeConfig = commonInfoDao.findOne(279);
|
||||||
|
if (rechargeConfig == null || StringUtils.isBlank(rechargeConfig.getValue())) {
|
||||||
|
return BigDecimal.ZERO;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析充值赠送配置
|
||||||
|
// 格式为:充值金额:赠送金额,充值金额:赠送金额,...
|
||||||
|
// 例如:100:10,200:20,500:50,...
|
||||||
|
String configValue = rechargeConfig.getValue();
|
||||||
|
Map<BigDecimal, BigDecimal> bonusMap = parseRechargeBonusConfig(configValue);
|
||||||
|
|
||||||
|
// 根据充值金额获取赠送金额
|
||||||
|
BigDecimal bonus = bonusMap.getOrDefault(rechargeAmount, BigDecimal.ZERO);
|
||||||
|
return bonus;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 获取支付方式描述
|
||||||
|
*/
|
||||||
|
private String getPayTypeDesc(Integer payType) {
|
||||||
|
switch (payType) {
|
||||||
|
case 1:
|
||||||
|
return "微信";
|
||||||
|
case 2:
|
||||||
|
return "支付宝";
|
||||||
|
case 3:
|
||||||
|
return "工商银行";
|
||||||
|
default:
|
||||||
|
return "未知";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析充值赠送配置
|
||||||
|
* @param configValue 配置值,格式为:充值金额:赠送金额,充值金额:赠送金额,...
|
||||||
|
* @return 充值金额与赠送金额的映射
|
||||||
|
*/
|
||||||
|
private Map<BigDecimal, BigDecimal> parseRechargeBonusConfig(String configValue) {
|
||||||
|
if (StringUtils.isBlank(configValue)) {
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 按逗号分割每个配置项
|
||||||
|
String[] configItems = configValue.split(",");
|
||||||
|
Map<BigDecimal, BigDecimal> bonusMap = new java.util.HashMap<>();
|
||||||
|
|
||||||
|
for (String item : configItems) {
|
||||||
|
// 检查配置项是否包含冒号
|
||||||
|
if (item.contains(":")) {
|
||||||
|
String[] parts = item.split(":");
|
||||||
|
if (parts.length == 2) {
|
||||||
|
BigDecimal rechargeAmount = new BigDecimal(parts[0].trim());
|
||||||
|
BigDecimal bonusAmount = new BigDecimal(parts[1].trim());
|
||||||
|
bonusMap.put(rechargeAmount, bonusAmount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return bonusMap;
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 解析异常时返回空映射
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -170,8 +170,18 @@ public class AliPayController {
|
||||||
//设置订单更新时间
|
//设置订单更新时间
|
||||||
orders.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
orders.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
||||||
payOrderDao.updateById(orders);
|
payOrderDao.updateById(orders);
|
||||||
//调用处理接口
|
// 处理充值赠送金额
|
||||||
userMoneyDao.updateMayMoney(1, orders.getUserId(), orders.getMoney());
|
BigDecimal bonusAmount = commonInfoService.getRechargeBonus(orders.getPayMoney());
|
||||||
|
// 计算总金额(充值金额 + 赠送金额)
|
||||||
|
BigDecimal totalAmount = orders.getMoney().add(bonusAmount);
|
||||||
|
|
||||||
|
// 一次性更新用户余额(充值金额 + 赠送金额)
|
||||||
|
userMoneyDao.updateMayMoney(1, orders.getUserId(), totalAmount);
|
||||||
|
|
||||||
|
// 创建交易记录列表
|
||||||
|
List<UserMoneyDetails> detailsList = new ArrayList<>();
|
||||||
|
|
||||||
|
// 添加充值记录
|
||||||
UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
|
UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
|
||||||
userMoneyDetails.setUserId(orders.getUserId());
|
userMoneyDetails.setUserId(orders.getUserId());
|
||||||
userMoneyDetails.setTitle("支付宝充值");
|
userMoneyDetails.setTitle("支付宝充值");
|
||||||
|
|
@ -180,7 +190,22 @@ public class AliPayController {
|
||||||
userMoneyDetails.setMoney(orders.getMoney());
|
userMoneyDetails.setMoney(orders.getMoney());
|
||||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
userMoneyDetails.setCreateTime(sdf.format(new Date()));
|
userMoneyDetails.setCreateTime(sdf.format(new Date()));
|
||||||
userMoneyDetailsService.save(userMoneyDetails);
|
detailsList.add(userMoneyDetails);
|
||||||
|
|
||||||
|
// 如果有赠送金额,添加赠送记录
|
||||||
|
if (bonusAmount.compareTo(BigDecimal.ZERO) > 0) {
|
||||||
|
UserMoneyDetails bonusDetails = new UserMoneyDetails();
|
||||||
|
bonusDetails.setUserId(orders.getUserId());
|
||||||
|
bonusDetails.setTitle("充值赠送");
|
||||||
|
bonusDetails.setContent("充值" + orders.getPayMoney() + "元,赠送" + bonusAmount + "元");
|
||||||
|
bonusDetails.setType(1);
|
||||||
|
bonusDetails.setMoney(bonusAmount);
|
||||||
|
bonusDetails.setCreateTime(sdf.format(new Date()));
|
||||||
|
detailsList.add(bonusDetails);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 一次性保存所有交易记录
|
||||||
|
userMoneyDetailsService.saveBatch(detailsList);
|
||||||
} else if (payDetails.getType() == 2) {
|
} else if (payDetails.getType() == 2) {
|
||||||
Orders orders = ordersDao.selectOne(new QueryWrapper<Orders>().eq("orders_no", payDetails.getOrderId()));
|
Orders orders = ordersDao.selectOne(new QueryWrapper<Orders>().eq("orders_no", payDetails.getOrderId()));
|
||||||
GoodsSku goodsSku = goodsSkuService.getById(orders.getSkuId());
|
GoodsSku goodsSku = goodsSkuService.getById(orders.getSkuId());
|
||||||
|
|
|
||||||
|
|
@ -512,8 +512,18 @@ public class WxServiceImpl implements WxService {
|
||||||
//设置订单更新时间
|
//设置订单更新时间
|
||||||
orders.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
orders.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
||||||
payOrderDao.updateById(orders);
|
payOrderDao.updateById(orders);
|
||||||
//调用处理接口
|
// 处理充值赠送金额
|
||||||
userMoneyDao.updateMayMoney(1, orders.getUserId(), orders.getMoney());
|
BigDecimal bonusAmount = commonInfoService.getRechargeBonus(orders.getPayMoney());
|
||||||
|
// 计算总金额(充值金额 + 赠送金额)
|
||||||
|
BigDecimal totalAmount = orders.getMoney().add(bonusAmount);
|
||||||
|
|
||||||
|
// 一次性更新用户余额(充值金额 + 赠送金额)
|
||||||
|
userMoneyDao.updateMayMoney(1, orders.getUserId(), totalAmount);
|
||||||
|
|
||||||
|
// 创建交易记录列表
|
||||||
|
List<UserMoneyDetails> detailsList = new ArrayList<>();
|
||||||
|
|
||||||
|
// 添加充值记录
|
||||||
UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
|
UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
|
||||||
userMoneyDetails.setUserId(orders.getUserId());
|
userMoneyDetails.setUserId(orders.getUserId());
|
||||||
userMoneyDetails.setTitle("微信充值");
|
userMoneyDetails.setTitle("微信充值");
|
||||||
|
|
@ -522,7 +532,22 @@ public class WxServiceImpl implements WxService {
|
||||||
userMoneyDetails.setMoney(orders.getMoney());
|
userMoneyDetails.setMoney(orders.getMoney());
|
||||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
userMoneyDetails.setCreateTime(sdf.format(new Date()));
|
userMoneyDetails.setCreateTime(sdf.format(new Date()));
|
||||||
userMoneyDetailsService.save(userMoneyDetails);
|
detailsList.add(userMoneyDetails);
|
||||||
|
|
||||||
|
// 如果有赠送金额,添加赠送记录
|
||||||
|
if (bonusAmount.compareTo(BigDecimal.ZERO) > 0) {
|
||||||
|
UserMoneyDetails bonusDetails = new UserMoneyDetails();
|
||||||
|
bonusDetails.setUserId(orders.getUserId());
|
||||||
|
bonusDetails.setTitle("充值赠送");
|
||||||
|
bonusDetails.setContent("充值" + orders.getPayMoney() + "元,赠送" + bonusAmount + "元");
|
||||||
|
bonusDetails.setType(1);
|
||||||
|
bonusDetails.setMoney(bonusAmount);
|
||||||
|
bonusDetails.setCreateTime(sdf.format(new Date()));
|
||||||
|
detailsList.add(bonusDetails);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 一次性保存所有交易记录
|
||||||
|
userMoneyDetailsService.saveBatch(detailsList);
|
||||||
} else if (payDetails.getType() == 2) {
|
} else if (payDetails.getType() == 2) {
|
||||||
Orders orders = ordersDao.selectOne(new QueryWrapper<Orders>().eq("orders_no", payDetails.getOrderId()));
|
Orders orders = ordersDao.selectOne(new QueryWrapper<Orders>().eq("orders_no", payDetails.getOrderId()));
|
||||||
GoodsSku goodsSku = goodsSkuService.getById(orders.getSkuId());
|
GoodsSku goodsSku = goodsSkuService.getById(orders.getSkuId());
|
||||||
|
|
@ -837,7 +862,18 @@ public class WxServiceImpl implements WxService {
|
||||||
orders.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
orders.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
||||||
payOrderDao.updateById(orders);
|
payOrderDao.updateById(orders);
|
||||||
//调用处理接口
|
//调用处理接口
|
||||||
userMoneyDao.updateMayMoney(1, orders.getUserId(), orders.getMoney());
|
// 处理充值赠送金额
|
||||||
|
BigDecimal bonusAmount = commonInfoService.getRechargeBonus(orders.getPayMoney());
|
||||||
|
// 计算总金额(充值金额 + 赠送金额)
|
||||||
|
BigDecimal totalAmount = orders.getMoney().add(bonusAmount);
|
||||||
|
|
||||||
|
// 一次性更新用户余额(充值金额 + 赠送金额)
|
||||||
|
userMoneyDao.updateMayMoney(1, orders.getUserId(), totalAmount);
|
||||||
|
|
||||||
|
// 创建交易记录列表
|
||||||
|
List<UserMoneyDetails> detailsList = new ArrayList<>();
|
||||||
|
|
||||||
|
// 添加充值记录
|
||||||
UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
|
UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
|
||||||
userMoneyDetails.setUserId(orders.getUserId());
|
userMoneyDetails.setUserId(orders.getUserId());
|
||||||
userMoneyDetails.setTitle("微信充值");
|
userMoneyDetails.setTitle("微信充值");
|
||||||
|
|
@ -846,7 +882,22 @@ public class WxServiceImpl implements WxService {
|
||||||
userMoneyDetails.setMoney(orders.getMoney());
|
userMoneyDetails.setMoney(orders.getMoney());
|
||||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
userMoneyDetails.setCreateTime(sdf.format(new Date()));
|
userMoneyDetails.setCreateTime(sdf.format(new Date()));
|
||||||
userMoneyDetailsService.save(userMoneyDetails);
|
detailsList.add(userMoneyDetails);
|
||||||
|
|
||||||
|
// 如果有赠送金额,添加赠送记录
|
||||||
|
if (bonusAmount.compareTo(BigDecimal.ZERO) > 0) {
|
||||||
|
UserMoneyDetails bonusDetails = new UserMoneyDetails();
|
||||||
|
bonusDetails.setUserId(orders.getUserId());
|
||||||
|
bonusDetails.setTitle("充值赠送");
|
||||||
|
bonusDetails.setContent("充值" + orders.getPayMoney() + "元,赠送" + bonusAmount + "元");
|
||||||
|
bonusDetails.setType(1);
|
||||||
|
bonusDetails.setMoney(bonusAmount);
|
||||||
|
bonusDetails.setCreateTime(sdf.format(new Date()));
|
||||||
|
detailsList.add(bonusDetails);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 一次性保存所有交易记录
|
||||||
|
userMoneyDetailsService.saveBatch(detailsList);
|
||||||
} else if (payDetails.getType() == 2) {
|
} else if (payDetails.getType() == 2) {
|
||||||
Orders orders = ordersDao.selectOne(new QueryWrapper<Orders>().eq("orders_no", payDetails.getOrderId()));
|
Orders orders = ordersDao.selectOne(new QueryWrapper<Orders>().eq("orders_no", payDetails.getOrderId()));
|
||||||
GoodsSku goodsSku = goodsSkuService.getById(orders.getSkuId());
|
GoodsSku goodsSku = goodsSkuService.getById(orders.getSkuId());
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue