新建分支
This commit is contained in:
@@ -16,6 +16,7 @@ import com.chaozhanggui.system.cashierservice.socket.AppWebSocketServer;
|
||||
import com.chaozhanggui.system.cashierservice.socket.WebSocketServer;
|
||||
import com.chaozhanggui.system.cashierservice.util.DateUtils;
|
||||
import com.chaozhanggui.system.cashierservice.util.JSONUtil;
|
||||
import com.chaozhanggui.system.cashierservice.util.N;
|
||||
import com.chaozhanggui.system.cashierservice.util.SnowFlakeUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -54,7 +55,10 @@ public class CartService {
|
||||
private TbOrderDetailMapper orderDetailMapper;
|
||||
@Autowired
|
||||
private TbShopTableMapper shopTableMapper;
|
||||
|
||||
@Autowired
|
||||
private TbUserCouponsMapper userCouponsMapper;
|
||||
@Autowired
|
||||
private TbSystemCouponsMapper systemCouponsMapper;
|
||||
|
||||
// @Transactional(rollbackFor = Exception.class)
|
||||
public void createCart(JSONObject jsonObject) throws Exception {
|
||||
@@ -202,23 +206,35 @@ public class CartService {
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void createOrder(JSONObject jsonObject) throws IOException {
|
||||
try {
|
||||
|
||||
String shopId = jsonObject.getString("shopId");
|
||||
JSONArray array = JSON.parseArray(redisUtil.getMessage(RedisCst.TABLE_CART.concat(jsonObject.getString("tableId").concat("-").concat(shopId))));
|
||||
//总金额
|
||||
List<Integer> ids = new ArrayList<>();
|
||||
BigDecimal totalAmount = BigDecimal.ZERO;
|
||||
BigDecimal packAMount = BigDecimal.ZERO;
|
||||
BigDecimal originAmount = BigDecimal.ZERO;
|
||||
BigDecimal saleAmount = BigDecimal.ZERO;
|
||||
String couponId = "";
|
||||
BigDecimal couponAmount = BigDecimal.ZERO;
|
||||
Map<Integer, TbProductSku> skuMap = new HashMap<>();
|
||||
List<TbOrderDetail> orderDetails = new ArrayList<>();
|
||||
Integer orderId = 0;
|
||||
TbMerchantAccount tbMerchantAccount = merchantAccountMapper.selectByShopId(jsonObject.getString("shopId"));
|
||||
if (tbMerchantAccount == null) {
|
||||
throw new MsgException("生成订单错误");
|
||||
}
|
||||
|
||||
String userId = jsonObject.getString("userId");
|
||||
TbUserInfo tbUserInfo = userInfoMapper.selectByPrimaryKey(Integer.valueOf(userId));
|
||||
if (tbUserInfo == null) {
|
||||
throw new MsgException("生成订单失败");
|
||||
}
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
JSONObject object = array.getJSONObject(i);
|
||||
TbCashierCart cashierCart = JSONUtil.parseJSONStr2T(object.toJSONString(), TbCashierCart.class);
|
||||
TbProductSkuWithBLOBs tbProduct = productSkuMapper.selectByPrimaryKey(Integer.valueOf(cashierCart.getSkuId()));
|
||||
totalAmount = totalAmount.add(cashierCart.getTotalAmount());
|
||||
packAMount = packAMount.add(cashierCart.getPackFee());
|
||||
originAmount = originAmount.add(cashierCart.getTotalAmount());
|
||||
if (Objects.nonNull(tbProduct)) {
|
||||
saleAmount = saleAmount.add(tbProduct.getSalePrice());
|
||||
}
|
||||
@@ -249,108 +265,186 @@ public class CartService {
|
||||
orderId = Integer.valueOf(cashierCart.getOrderId());
|
||||
}
|
||||
}
|
||||
|
||||
TbMerchantAccount tbMerchantAccount = merchantAccountMapper.selectByShopId(jsonObject.getString("shopId"));
|
||||
if (tbMerchantAccount == null) {
|
||||
throw new MsgException("生成订单错误");
|
||||
}
|
||||
|
||||
TbUserInfo tbUserInfo = userInfoMapper.selectByPrimaryKey(jsonObject.getInteger("userId"));
|
||||
if (tbUserInfo == null) {
|
||||
throw new MsgException("生成订单失败");
|
||||
}
|
||||
//总金额
|
||||
TbShopTable shopTable = shopTableMapper.selectQRcode(jsonObject.getString("tableId"));
|
||||
//生成订单
|
||||
TbOrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(orderId);
|
||||
if (Objects.nonNull(orderInfo)) {
|
||||
log.info("订单状态:" + orderInfo.getStatus());
|
||||
if (!"unpaid".equals(orderInfo.getStatus())) {
|
||||
log.info("开始处理订单");
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
jsonObject1.put("status", "fail");
|
||||
jsonObject1.put("msg", "订单正在支付中,请稍后再试");
|
||||
jsonObject1.put("type", jsonObject.getString("type"));
|
||||
jsonObject1.put("data", "");
|
||||
AppWebSocketServer.AppSendInfo(jsonObject1, jsonObject.getString("userId"), true);
|
||||
log.info("消息推送");
|
||||
return;
|
||||
String isBuyYhq = "false";
|
||||
if (jsonObject.containsKey("isYhq")){
|
||||
//1:购买优惠券,0:自己持有优惠券
|
||||
Integer couponsId = jsonObject.getInteger("couponsId");
|
||||
if (jsonObject.getString("isBuyYhq").equals("1")){
|
||||
TbSystemCoupons systemCoupons = systemCouponsMapper.selectByPrimaryKey(couponsId);
|
||||
if (Objects.isNull(systemCoupons) || !systemCoupons.getStatus().equals("0")){
|
||||
log.info("开始处理订单");
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
jsonObject1.put("status", "fail");
|
||||
jsonObject1.put("msg", "优惠券已售空");
|
||||
jsonObject1.put("type", jsonObject.getString("type"));
|
||||
jsonObject1.put("data", "");
|
||||
AppWebSocketServer.AppSendInfo(jsonObject1, jsonObject.getString("userId"), true);
|
||||
log.info("消息推送");
|
||||
return;
|
||||
}
|
||||
if (N.gt(systemCoupons.getCouponsAmount(),totalAmount)){
|
||||
log.info("开始处理订单");
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
jsonObject1.put("status", "fail");
|
||||
jsonObject1.put("msg", "订单金额小于优惠价金额");
|
||||
jsonObject1.put("type", jsonObject.getString("type"));
|
||||
jsonObject1.put("data", "");
|
||||
AppWebSocketServer.AppSendInfo(jsonObject1, jsonObject.getString("userId"), true);
|
||||
log.info("消息推送");
|
||||
return;
|
||||
}
|
||||
totalAmount = totalAmount.add(systemCoupons.getCouponsPrice()).subtract(systemCoupons.getCouponsAmount());
|
||||
originAmount = originAmount.add(systemCoupons.getCouponsPrice());
|
||||
systemCoupons.setStatus("1");
|
||||
systemCouponsMapper.updateByPrimaryKeySelective(systemCoupons);
|
||||
TbUserCoupons userCoupons = new TbUserCoupons();
|
||||
userCoupons.setEndTime(DateUtils.getNewDate(new Date(),3,systemCoupons.getDayNum()));
|
||||
userCoupons.setCouponsAmount(systemCoupons.getCouponsAmount());
|
||||
userCoupons.setCouponsPrice(systemCoupons.getCouponsPrice());
|
||||
userCoupons.setStatus("1");
|
||||
userCoupons.setUserId(userId);
|
||||
userCoupons.setCreateTime(new Date());
|
||||
userCouponsMapper.insert(userCoupons);
|
||||
couponId = userCoupons.getId()+"";
|
||||
couponAmount = userCoupons.getCouponsAmount();
|
||||
}else {
|
||||
TbUserCoupons userCoupons = userCouponsMapper.selectByPrimaryKey(couponsId);
|
||||
if (Objects.isNull(userCoupons) || !userCoupons.getStatus().equals("0")){
|
||||
log.info("开始处理订单");
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
jsonObject1.put("status", "fail");
|
||||
jsonObject1.put("msg", "优惠券已使用");
|
||||
jsonObject1.put("type", jsonObject.getString("type"));
|
||||
jsonObject1.put("data", "");
|
||||
AppWebSocketServer.AppSendInfo(jsonObject1, jsonObject.getString("userId"), true);
|
||||
log.info("消息推送");
|
||||
return;
|
||||
}
|
||||
if (N.gt(userCoupons.getCouponsAmount(),totalAmount)){
|
||||
log.info("开始处理订单");
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
jsonObject1.put("status", "fail");
|
||||
jsonObject1.put("msg", "订单金额小于优惠价金额");
|
||||
jsonObject1.put("type", jsonObject.getString("type"));
|
||||
jsonObject1.put("data", "");
|
||||
AppWebSocketServer.AppSendInfo(jsonObject1, jsonObject.getString("userId"), true);
|
||||
log.info("消息推送");
|
||||
return;
|
||||
}
|
||||
totalAmount = totalAmount.subtract(userCoupons.getCouponsAmount());
|
||||
userCoupons.setStatus("1");
|
||||
userCoupons.setEndTime(DateUtils.getNewDate(new Date(),5,30));
|
||||
userCouponsMapper.updateByPrimaryKeySelective(userCoupons);
|
||||
}
|
||||
}else {
|
||||
if (Objects.nonNull(orderInfo)) {
|
||||
log.info("订单状态:" + orderInfo.getStatus());
|
||||
if (!"unpaid".equals(orderInfo.getStatus())) {
|
||||
log.info("开始处理订单");
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
jsonObject1.put("status", "fail");
|
||||
jsonObject1.put("msg", "订单正在支付中,请稍后再试");
|
||||
jsonObject1.put("type", jsonObject.getString("type"));
|
||||
jsonObject1.put("data", "");
|
||||
AppWebSocketServer.AppSendInfo(jsonObject1, jsonObject.getString("userId"), true);
|
||||
log.info("消息推送");
|
||||
return;
|
||||
}
|
||||
|
||||
orderDetailMapper.deleteByOUrderId(orderId);
|
||||
orderInfo.setUpdatedAt(System.currentTimeMillis());
|
||||
orderInfo.setSettlementAmount(totalAmount);
|
||||
orderInfo.setAmount(totalAmount);
|
||||
orderInfo.setOriginAmount(totalAmount);
|
||||
orderInfo.setOrderAmount(totalAmount.add(packAMount));
|
||||
orderInfo.setFreightAmount(BigDecimal.ZERO);
|
||||
orderInfo.setProductAmount(saleAmount);
|
||||
orderInfoMapper.updateByPrimaryKeySelective(orderInfo);
|
||||
} else {
|
||||
|
||||
orderInfo = new TbOrderInfo();
|
||||
String orderNo = generateOrderNumber();
|
||||
orderInfo.setOrderNo(orderNo);
|
||||
orderInfo.setSettlementAmount(totalAmount);
|
||||
orderInfo.setPackFee(packAMount);
|
||||
orderInfo.setOriginAmount(totalAmount);
|
||||
orderInfo.setProductAmount(totalAmount);
|
||||
orderInfo.setAmount(totalAmount);
|
||||
orderInfo.setOrderAmount(totalAmount.add(packAMount));
|
||||
orderInfo.setPayAmount(BigDecimal.ZERO);
|
||||
orderInfo.setRefundAmount(new BigDecimal("0.00"));
|
||||
orderInfo.setTableId(jsonObject.getString("tableId"));
|
||||
orderInfo.setSendType("table");
|
||||
orderInfo.setOrderType("miniapp");
|
||||
orderInfo.setTradeDay(DateUtils.getDay());
|
||||
orderInfo.setStatus("unpaid");
|
||||
orderInfo.setShopId(jsonObject.getString("shopId"));
|
||||
orderInfo.setUserId(jsonObject.getString("userId"));
|
||||
orderInfo.setCreatedAt(Instant.now().toEpochMilli());
|
||||
orderInfo.setSystemTime(Instant.now().toEpochMilli());
|
||||
orderInfo.setUpdatedAt(Instant.now().toEpochMilli());
|
||||
orderInfo.setIsAccepted((byte) 1);
|
||||
if (Objects.nonNull(shopTable)) {
|
||||
orderInfo.setTableName(shopTable.getName());
|
||||
orderDetailMapper.deleteByOUrderId(orderId);
|
||||
orderInfo.setUpdatedAt(System.currentTimeMillis());
|
||||
orderInfo.setSettlementAmount(totalAmount);
|
||||
orderInfo.setUserCouponId(couponId);
|
||||
orderInfo.setUserCouponAmount(couponAmount);
|
||||
orderInfo.setAmount(totalAmount);
|
||||
orderInfo.setOriginAmount(originAmount);
|
||||
orderInfo.setOrderAmount(totalAmount.add(packAMount));
|
||||
orderInfo.setFreightAmount(BigDecimal.ZERO);
|
||||
orderInfo.setProductAmount(saleAmount);
|
||||
orderInfo.setUserCouponId(couponId);
|
||||
orderInfo.setIsBuyCoupon(isBuyYhq);
|
||||
orderInfo.setUserCouponAmount(couponAmount);
|
||||
orderInfoMapper.updateByPrimaryKeySelective(orderInfo);
|
||||
} else {
|
||||
orderInfo.setMerchantId(String.valueOf(tbMerchantAccount.getId()));
|
||||
orderInfo = getOrder(totalAmount,packAMount,shopTable,tbMerchantAccount.getId().toString(),jsonObject,originAmount);
|
||||
orderInfo.setUserCouponId(couponId);
|
||||
orderInfo.setOriginAmount(originAmount);
|
||||
orderInfo.setIsBuyCoupon(isBuyYhq);
|
||||
orderInfo.setUserCouponAmount(couponAmount);
|
||||
orderInfoMapper.insert(orderInfo);
|
||||
orderId = orderInfo.getId();
|
||||
}
|
||||
orderInfo.setMerchantId(String.valueOf(tbMerchantAccount.getId()));
|
||||
orderInfoMapper.insert(orderInfo);
|
||||
orderId = orderInfo.getId();
|
||||
}
|
||||
for (TbOrderDetail orderDetail : orderDetails) {
|
||||
orderDetail.setOrderId(orderId);
|
||||
orderDetailMapper.insert(orderDetail);
|
||||
}
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
JSONObject object = array.getJSONObject(i);
|
||||
TbCashierCart cashierCart = JSONUtil.parseJSONStr2T(object.toJSONString(), TbCashierCart.class);
|
||||
cashierCart.setUpdatedAt(System.currentTimeMillis());
|
||||
cashierCart.setOrderId(orderId + "");
|
||||
cashierCart.setStatus("closed");
|
||||
cashierCartMapper.updateByPrimaryKeySelective(cashierCart);
|
||||
object.put("updatedAt", System.currentTimeMillis());
|
||||
object.put("orderId", orderId + "");
|
||||
}
|
||||
redisUtil.saveMessage(RedisCst.TABLE_CART.concat(jsonObject.getString("tableId")).concat("-").concat(shopId), array.toJSONString());
|
||||
orderInfo.setDetailList(orderDetails);
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
jsonObject1.put("status", "success");
|
||||
jsonObject1.put("msg", "成功");
|
||||
jsonObject1.put("type", jsonObject.getString("type"));
|
||||
jsonObject1.put("data", orderInfo);
|
||||
redisUtil.deleteByKey(RedisCst.TABLE_CART.concat(jsonObject.getString("tableId")).concat("-").concat(shopId));
|
||||
AppWebSocketServer.AppSendInfo(jsonObject1, jsonObject.getString("userId"), true);
|
||||
JSONObject jsonObject12 = new JSONObject();
|
||||
jsonObject12.put("status", "success");
|
||||
jsonObject12.put("msg", "成功");
|
||||
jsonObject12.put("type", "order");
|
||||
jsonObject12.put("amount", BigDecimal.ZERO);
|
||||
for (TbOrderDetail orderDetail : orderDetails) {
|
||||
orderDetail.setOrderId(orderId);
|
||||
orderDetailMapper.insert(orderDetail);
|
||||
}
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
JSONObject object = array.getJSONObject(i);
|
||||
TbCashierCart cashierCart = JSONUtil.parseJSONStr2T(object.toJSONString(), TbCashierCart.class);
|
||||
cashierCart.setUpdatedAt(System.currentTimeMillis());
|
||||
cashierCart.setOrderId(orderId + "");
|
||||
cashierCart.setStatus("closed");
|
||||
cashierCartMapper.updateByPrimaryKeySelective(cashierCart);
|
||||
object.put("updatedAt", System.currentTimeMillis());
|
||||
object.put("orderId", orderId + "");
|
||||
}
|
||||
redisUtil.saveMessage(RedisCst.TABLE_CART.concat(jsonObject.getString("tableId")).concat("-").concat(shopId), array.toJSONString());
|
||||
orderInfo.setDetailList(orderDetails);
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
jsonObject1.put("status", "success");
|
||||
jsonObject1.put("msg", "成功");
|
||||
jsonObject1.put("type", jsonObject.getString("type"));
|
||||
jsonObject1.put("data", orderInfo);
|
||||
redisUtil.deleteByKey(RedisCst.TABLE_CART.concat(jsonObject.getString("tableId")).concat("-").concat(shopId));
|
||||
AppWebSocketServer.AppSendInfo(jsonObject1, jsonObject.getString("userId"), true);
|
||||
JSONObject jsonObject12 = new JSONObject();
|
||||
jsonObject12.put("status", "success");
|
||||
jsonObject12.put("msg", "成功");
|
||||
jsonObject12.put("type", "order");
|
||||
jsonObject12.put("amount", BigDecimal.ZERO);
|
||||
|
||||
jsonObject12.put("data", new JSONArray());
|
||||
AppWebSocketServer.AppSendInfo(jsonObject12, jsonObject.getString("tableId").concat("-").concat(shopId), false);
|
||||
jsonObject12.put("data", new JSONArray());
|
||||
AppWebSocketServer.AppSendInfo(jsonObject12, jsonObject.getString("tableId").concat("-").concat(shopId), false);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.getMessage();
|
||||
}
|
||||
}
|
||||
private TbOrderInfo getOrder(BigDecimal totalAmount, BigDecimal packAMount,
|
||||
TbShopTable shopTable, String merchantId, JSONObject jsonObject, BigDecimal originAmount){
|
||||
TbOrderInfo orderInfo = new TbOrderInfo();
|
||||
String orderNo = generateOrderNumber();
|
||||
orderInfo.setOrderNo(orderNo);
|
||||
orderInfo.setSettlementAmount(totalAmount);
|
||||
orderInfo.setPackFee(packAMount);
|
||||
orderInfo.setOriginAmount(originAmount);
|
||||
orderInfo.setProductAmount(totalAmount);
|
||||
orderInfo.setAmount(totalAmount);
|
||||
orderInfo.setOrderAmount(totalAmount.add(packAMount));
|
||||
orderInfo.setPayAmount(BigDecimal.ZERO);
|
||||
orderInfo.setRefundAmount(new BigDecimal("0.00"));
|
||||
orderInfo.setTableId(jsonObject.getString("tableId"));
|
||||
orderInfo.setSendType("table");
|
||||
orderInfo.setOrderType("miniapp");
|
||||
orderInfo.setTradeDay(DateUtils.getDay());
|
||||
orderInfo.setStatus("unpaid");
|
||||
orderInfo.setShopId(jsonObject.getString("shopId"));
|
||||
orderInfo.setUserId(jsonObject.getString("userId"));
|
||||
orderInfo.setCreatedAt(Instant.now().toEpochMilli());
|
||||
orderInfo.setSystemTime(Instant.now().toEpochMilli());
|
||||
orderInfo.setUpdatedAt(Instant.now().toEpochMilli());
|
||||
orderInfo.setIsAccepted((byte) 1);
|
||||
if (Objects.nonNull(shopTable)) {
|
||||
orderInfo.setTableName(shopTable.getName());
|
||||
}
|
||||
orderInfo.setMerchantId(merchantId);
|
||||
return orderInfo;
|
||||
}
|
||||
public String generateOrderNumber() {
|
||||
String date = DateUtils.getSdfTimes();
|
||||
Random random = new Random();
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.chaozhanggui.system.cashierservice.service;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.chaozhanggui.system.cashierservice.dao.*;
|
||||
import com.chaozhanggui.system.cashierservice.entity.*;
|
||||
import com.chaozhanggui.system.cashierservice.exception.MsgException;
|
||||
import com.chaozhanggui.system.cashierservice.redis.RedisUtil;
|
||||
import com.chaozhanggui.system.cashierservice.util.DateUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.ParseException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author lyf
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class IntegralService {
|
||||
@Autowired
|
||||
private RedisUtil redisUtil;
|
||||
@Autowired
|
||||
private TbOrderInfoMapper orderInfoMapper;
|
||||
@Autowired
|
||||
private TbCashierCartMapper cashierCartMapper;
|
||||
@Autowired
|
||||
private TbProductMapper productMapper;
|
||||
@Autowired
|
||||
private TbProductSkuMapper productSkuMapper;
|
||||
@Autowired
|
||||
private TbShopInfoMapper shopInfoMapper;
|
||||
@Autowired
|
||||
private TbShopUserMapper tbShopUserMapper;
|
||||
@Resource
|
||||
private TbReleaseFlowMapper integralFlowMapper;
|
||||
@Autowired
|
||||
private TbUserCouponsMapper userCouponsMapper;
|
||||
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void integralAdd(JSONObject jsonObject) throws ParseException {
|
||||
String type = jsonObject.getString("type");
|
||||
if (type.equals("trade")){
|
||||
//优惠券兑换积分
|
||||
Integer integralId = jsonObject.getInteger("integralId");
|
||||
TbUserCoupons userCoupons = userCouponsMapper.selectByPrimaryKey(integralId);
|
||||
if (Objects.isNull(userCoupons) || !"0".equals(userCoupons.getStatus())){
|
||||
throw new MsgException("优惠券已被使用");
|
||||
}
|
||||
userCoupons.setStatus("trade");
|
||||
userCoupons.setUpdateTime(new Date());
|
||||
userCouponsMapper.updateByPrimaryKeySelective(userCoupons);
|
||||
TbParams params = tbShopUserMapper.selectParams();
|
||||
TbReleaseFlow integralFlow = new TbReleaseFlow();
|
||||
integralFlow.setNum(userCoupons.getCouponsAmount().multiply(params.getTradeRatio()));
|
||||
integralFlow.setCreateTime(new Date());
|
||||
integralFlow.setFromSource(userCoupons.getId()+"");
|
||||
integralFlow.setType("TRADEADD");
|
||||
integralFlow.setUserId(userCoupons.getUserId());
|
||||
integralFlowMapper.insert(integralFlow);
|
||||
}else {
|
||||
Integer orderId = jsonObject.getInteger("orderId");
|
||||
TbOrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(orderId);
|
||||
if (Objects.isNull(orderInfo)) {
|
||||
throw new MsgException("该订单不存在");
|
||||
}
|
||||
TbShopInfo shopInfo = shopInfoMapper.selectByPrimaryKey(Integer.valueOf(orderInfo.getShopId()));
|
||||
if (Objects.isNull(shopInfo) || !"true".equals(shopInfo.getIsOpenYhq())){
|
||||
throw new MsgException("该店铺未开启优惠券功能");
|
||||
}
|
||||
TbParams params = tbShopUserMapper.selectParams();
|
||||
TbUserCoupons userCoupons = new TbUserCoupons();
|
||||
userCoupons.setUserId(orderInfo.getUserId());
|
||||
userCoupons.setCouponsAmount(orderInfo.getPayAmount().multiply(params.getIntegralRatio()));
|
||||
userCoupons.setStatus("0");
|
||||
userCoupons.setCouponsPrice(userCoupons.getCouponsAmount().multiply(new BigDecimal("0.5")));
|
||||
userCoupons.setCreateTime(new Date());
|
||||
userCoupons.setEndTime(DateUtils.getNewDate(new Date(),3,30));
|
||||
//执行插入方法
|
||||
userCouponsMapper.insert(userCoupons);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
package com.chaozhanggui.system.cashierservice.service;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.chaozhanggui.system.cashierservice.dao.*;
|
||||
import com.chaozhanggui.system.cashierservice.entity.*;
|
||||
import com.chaozhanggui.system.cashierservice.entity.vo.IntegralVo;
|
||||
import com.chaozhanggui.system.cashierservice.exception.MsgException;
|
||||
import com.chaozhanggui.system.cashierservice.redis.RedisCst;
|
||||
import com.chaozhanggui.system.cashierservice.redis.RedisUtil;
|
||||
import com.chaozhanggui.system.cashierservice.sign.CodeEnum;
|
||||
import com.chaozhanggui.system.cashierservice.sign.Result;
|
||||
import com.chaozhanggui.system.cashierservice.util.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
|
||||
@Autowired
|
||||
private TbShopUserMapper shopUserMapper;
|
||||
@Autowired
|
||||
private TbReleaseFlowMapper releaseFlowMapper;
|
||||
|
||||
@Autowired
|
||||
RedisUtils redisUtils;
|
||||
|
||||
public JSONObject modityIntegral(IntegralVo integralVo, String userSign) {
|
||||
JSONObject object = (JSONObject) JSONObject.toJSON(integralVo);
|
||||
if (N.gt(BigDecimal.ZERO, integralVo.getNum())) {
|
||||
JSONObject result = new JSONObject();
|
||||
result.put("status", "fail");
|
||||
result.put("msg", "积分数量不允许小于0");
|
||||
result.put("data", "");
|
||||
return result;
|
||||
}
|
||||
object.put("userSign", userSign);
|
||||
|
||||
JSONObject jsonObject = JSONUtil.sortJSONObject(object, CacheMap.map);
|
||||
System.out.println(jsonObject.toJSONString());
|
||||
String sign = MD5Util.encrypt(jsonObject.toJSONString());
|
||||
if (!sign.equals(integralVo.getSign())) {
|
||||
JSONObject result = new JSONObject();
|
||||
result.put("status", "fail");
|
||||
result.put("msg", "签名验证失败");
|
||||
result.put("data", "");
|
||||
return result;
|
||||
}
|
||||
TbShopUser shopUser = shopUserMapper.selectByOpenId(integralVo.getOpenId());
|
||||
if (Objects.isNull(shopUser)) {
|
||||
JSONObject result = new JSONObject();
|
||||
result.put("status", "fail");
|
||||
result.put("msg", "用户不存在");
|
||||
result.put("data", "");
|
||||
return result;
|
||||
}
|
||||
boolean falg = updateIntegral(shopUser.getId(), integralVo.getNum(), integralVo.getType());
|
||||
if (!falg) {
|
||||
JSONObject result = new JSONObject();
|
||||
result.put("status", "fail");
|
||||
result.put("msg", "余额不足");
|
||||
result.put("data", "");
|
||||
return result;
|
||||
}
|
||||
JSONObject result = new JSONObject();
|
||||
result.put("status", "success");
|
||||
result.put("msg", "操作成功");
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
IntegralVo integralVo = new IntegralVo();
|
||||
integralVo.setNum(new BigDecimal("5254"));
|
||||
integralVo.setOpenId("or1l864NBOoJZhC5x_yeziZ26j6c");
|
||||
integralVo.setType("sub");
|
||||
JSONObject object = (JSONObject) JSONObject.toJSON(integralVo);
|
||||
object.put("userSign", "02c03d79c36b4c01b217ffb1baef9009");
|
||||
JSONObject jsonObject = JSONUtil.sortJSONObject(object, CacheMap.map);
|
||||
System.out.println("加密前字符串:" + jsonObject.toJSONString());
|
||||
String sign = MD5Util.encrypt(jsonObject.toJSONString());
|
||||
System.out.println("加密后签名:" + sign);
|
||||
}
|
||||
|
||||
private Boolean updateIntegral(String userId, BigDecimal num, String type) {
|
||||
|
||||
boolean lock_coin = redisUtils.lock(RedisCst.INTEGRAL_COIN_KEY + userId, 5000, TimeUnit.MILLISECONDS);
|
||||
if (lock_coin) {
|
||||
TbShopUser tbShopUser = shopUserMapper.selectByPrimaryKey(userId);
|
||||
boolean flag = true;
|
||||
if (type.equals("sub")) {
|
||||
if (N.gt(num, tbShopUser.getLevelConsume())) {
|
||||
flag = false;
|
||||
} else {
|
||||
tbShopUser.setLevelConsume(tbShopUser.getLevelConsume().subtract(num));
|
||||
}
|
||||
} else if (type.equals("add")) {
|
||||
tbShopUser.setLevelConsume(tbShopUser.getLevelConsume().add(num));
|
||||
}
|
||||
if (flag) {
|
||||
if (type.equals("sub")) {
|
||||
TbReleaseFlow releaseFlow = new TbReleaseFlow();
|
||||
releaseFlow.setNum(num);
|
||||
releaseFlow.setCreateTime(new Date());
|
||||
releaseFlow.setFromSource("OWER");
|
||||
releaseFlow.setType("BUYSUB");
|
||||
releaseFlow.setUserId(userId);
|
||||
releaseFlowMapper.insert(releaseFlow);
|
||||
}else if (type.equals("sub")){
|
||||
TbReleaseFlow releaseFlow = new TbReleaseFlow();
|
||||
releaseFlow.setNum(num);
|
||||
releaseFlow.setCreateTime(new Date());
|
||||
releaseFlow.setFromSource("OWER");
|
||||
releaseFlow.setType("THREEADD");
|
||||
releaseFlow.setUserId(userId);
|
||||
releaseFlowMapper.insert(releaseFlow);
|
||||
}
|
||||
shopUserMapper.updateByPrimaryKeySelective(tbShopUser);
|
||||
}
|
||||
redisUtils.releaseLock(RedisCst.INTEGRAL_COIN_KEY + userId);
|
||||
return flag;
|
||||
} else {
|
||||
return updateIntegral(userId, num, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user