|
@@ -1,10 +1,13 @@
|
|
|
package com.fs.his.service.impl;
|
|
|
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
+import cn.hutool.core.lang.TypeReference;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.fs.common.constant.FsConstants;
|
|
|
import com.fs.common.core.domain.R;
|
|
|
import com.fs.common.core.redis.RedisCache;
|
|
@@ -23,6 +26,7 @@ import com.fs.his.enums.FsUserIntegralLogTypeEnum;
|
|
|
import com.fs.his.enums.PaymentMethodEnum;
|
|
|
import com.fs.his.mapper.*;
|
|
|
import com.fs.his.param.*;
|
|
|
+import com.fs.his.service.IFsIntegralCartService;
|
|
|
import com.fs.his.service.IFsIntegralOrderService;
|
|
|
import com.fs.his.service.IFsStorePaymentService;
|
|
|
import com.fs.his.service.IFsUserIntegralLogsService;
|
|
@@ -101,6 +105,8 @@ public class FsIntegralOrderServiceImpl implements IFsIntegralOrderService
|
|
|
private IPayService ybPayService;
|
|
|
@Autowired
|
|
|
private IFsStorePaymentService storePaymentService;
|
|
|
+ @Autowired
|
|
|
+ private IFsIntegralCartService cartService;
|
|
|
|
|
|
/**
|
|
|
* 查询积分商品订单
|
|
@@ -215,25 +221,51 @@ public class FsIntegralOrderServiceImpl implements IFsIntegralOrderService
|
|
|
FsUser user=fsUserMapper.selectFsUserByUserId(param.getUserId());
|
|
|
FsUserAddress address=fsUserAddressMapper.selectFsUserAddressByAddressId(param.getAddressId());
|
|
|
FsIntegralGoods integralGoods=fsIntegralGoodsMapper.selectFsIntegralGoodsByGoodsId(param.getGoodsId());
|
|
|
- if(integralGoods.getStock()<=0l){
|
|
|
+ if (Objects.isNull(user)) {
|
|
|
+ return R.error("用户不存在");
|
|
|
+ }
|
|
|
+ if (Objects.isNull(address)) {
|
|
|
+ return R.error("地址不存在");
|
|
|
+ }
|
|
|
+ if (Objects.isNull(integralGoods)) {
|
|
|
+ return R.error("商品不存在");
|
|
|
+ }
|
|
|
+ if(integralGoods.getStock()<=0L){
|
|
|
return R.error("库存不足");
|
|
|
}
|
|
|
if(user.getIntegral()<integralGoods.getIntegral()){
|
|
|
return R.error("积分不足");
|
|
|
}
|
|
|
- FsIntegralOrder order=new FsIntegralOrder();
|
|
|
- String orderSn = OrderCodeUtils.getOrderSn();
|
|
|
+
|
|
|
+ // 减库存
|
|
|
+ if (fsIntegralGoodsMapper.subStock(integralGoods.getGoodsId(), 1) <= 0) {
|
|
|
+ throw new CustomException("库存不足");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<FsIntegralGoods> goodsItem = new ArrayList<>();
|
|
|
+ goodsItem.add(integralGoods);
|
|
|
+
|
|
|
+ // 创建订单
|
|
|
+ return createOrder(user, address, integralGoods.getIntegral(),integralGoods.getCash(), goodsItem);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建订单
|
|
|
+ */
|
|
|
+ private R createOrder(FsUser user, FsUserAddress address, Long totalIntegral, BigDecimal totalCash, List<FsIntegralGoods> goodsItem) {
|
|
|
+ FsIntegralOrder order = new FsIntegralOrder();
|
|
|
+ String orderSn = OrderCodeUtils.getOrderSn();
|
|
|
if(StringUtils.isEmpty(orderSn)){
|
|
|
- return R.error("订单生成失败,请重试");
|
|
|
+ throw new CustomException("订单生成失败,请重试");
|
|
|
}
|
|
|
|
|
|
// 现金
|
|
|
- if (integralGoods.getCash().compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ if (totalCash.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
// 现金+积分
|
|
|
- order.setPayType(integralGoods.getIntegral() > 0 ? 3 : 2);
|
|
|
+ order.setPayType(totalIntegral > 0 ? 3 : 2);
|
|
|
order.setStatus(4);
|
|
|
order.setIsPay(0);
|
|
|
- order.setPayMoney(integralGoods.getCash());
|
|
|
+ order.setPayMoney(totalCash);
|
|
|
}
|
|
|
// 积分
|
|
|
else {
|
|
@@ -245,9 +277,9 @@ public class FsIntegralOrderServiceImpl implements IFsIntegralOrderService
|
|
|
|
|
|
order.setOrderCode(orderSn);
|
|
|
order.setUserId(user.getUserId());
|
|
|
- order.setBarCode(integralGoods.getBarCode());
|
|
|
- order.setIntegral(integralGoods.getIntegral().toString());
|
|
|
- order.setItemJson(JSONUtil.toJsonStr(integralGoods));
|
|
|
+ order.setBarCode(goodsItem.get(0).getBarCode());
|
|
|
+ order.setIntegral(totalIntegral.toString());
|
|
|
+ order.setItemJson(JSONUtil.toJsonStr(goodsItem));
|
|
|
order.setUserName(address.getRealName());
|
|
|
order.setUserAddress(address.getProvince()+address.getCity()+address.getDistrict()+address.getDetail());
|
|
|
order.setUserPhone(address.getPhone());
|
|
@@ -270,23 +302,18 @@ public class FsIntegralOrderServiceImpl implements IFsIntegralOrderService
|
|
|
//写入日志
|
|
|
FsUser userMap=new FsUser();
|
|
|
userMap.setUserId(user.getUserId());
|
|
|
- userMap.setIntegral(user.getIntegral()-integralGoods.getIntegral());
|
|
|
+ userMap.setIntegral(user.getIntegral()-totalIntegral);
|
|
|
fsUserMapper.updateFsUser(userMap);
|
|
|
FsUserIntegralLogs logs = new FsUserIntegralLogs();
|
|
|
- logs.setIntegral(-integralGoods.getIntegral());
|
|
|
+ logs.setIntegral(-totalIntegral);
|
|
|
logs.setUserId(order.getUserId());
|
|
|
- logs.setBalance(userMap.getIntegral().longValue());
|
|
|
+ logs.setBalance(userMap.getIntegral());
|
|
|
logs.setLogType(5);
|
|
|
logs.setBusinessId(order.getOrderId().toString());
|
|
|
logs.setCreateTime(new Date());
|
|
|
fsUserIntegralLogsMapper.insertFsUserIntegralLogs(logs);
|
|
|
}
|
|
|
|
|
|
- // 减库存
|
|
|
- if (fsIntegralGoodsMapper.subStock(integralGoods.getGoodsId(), 1) <= 0) {
|
|
|
- throw new CustomException("库存不足");
|
|
|
- }
|
|
|
-
|
|
|
// 积分支付
|
|
|
if (order.getPayType() == 1) {
|
|
|
// 首次完成积分商城下单
|
|
@@ -306,9 +333,61 @@ public class FsIntegralOrderServiceImpl implements IFsIntegralOrderService
|
|
|
}
|
|
|
}
|
|
|
else{
|
|
|
+ throw new CustomException("订单创建失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- return R.error("订单创建失败");
|
|
|
+ /**
|
|
|
+ * 创建购物车订单
|
|
|
+ * @param param 参数
|
|
|
+ * @return R
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public R createCartOrder(FsIntegralCartOrderCreateParam param) {
|
|
|
+ FsUser user=fsUserMapper.selectFsUserByUserId(param.getUserId());
|
|
|
+ FsUserAddress address=fsUserAddressMapper.selectFsUserAddressByAddressId(param.getAddressId());
|
|
|
+
|
|
|
+ if (Objects.isNull(user)) {
|
|
|
+ throw new CustomException("用户不存在");
|
|
|
+ }
|
|
|
+ if (Objects.isNull(address)) {
|
|
|
+ throw new CustomException("地址不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ Wrapper<FsIntegralCart> wrapper = Wrappers.<FsIntegralCart>lambdaQuery()
|
|
|
+ .eq(FsIntegralCart::getUserId, user.getUserId())
|
|
|
+ .in(FsIntegralCart::getId, param.getIds());
|
|
|
+ List<FsIntegralCart> carts = cartService.list(wrapper);
|
|
|
+ if (carts.isEmpty()) {
|
|
|
+ throw new CustomException("请选择商品");
|
|
|
+ }
|
|
|
+
|
|
|
+ long totalIntegral = 0L;
|
|
|
+ BigDecimal totalCash = BigDecimal.ZERO;
|
|
|
+ List<FsIntegralGoods> goodsItem = new ArrayList<>();
|
|
|
+ for (FsIntegralCart cart : carts) {
|
|
|
+ FsIntegralGoods integralGoods = fsIntegralGoodsMapper.selectFsIntegralGoodsByGoodsId(cart.getGoodsId());
|
|
|
+ if (Objects.isNull(integralGoods)) {
|
|
|
+ throw new CustomException("商品不存在");
|
|
|
+ }
|
|
|
+ if(integralGoods.getStock() < cart.getCartNum()){
|
|
|
+ throw new CustomException("库存不足");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 减库存
|
|
|
+ if (fsIntegralGoodsMapper.subStock(integralGoods.getGoodsId(), cart.getCartNum()) <= 0) {
|
|
|
+ throw new CustomException("库存不足");
|
|
|
+ }
|
|
|
+
|
|
|
+ totalIntegral += integralGoods.getIntegral() * cart.getCartNum();
|
|
|
+ totalCash = totalCash.add(integralGoods.getCash().multiply(BigDecimal.valueOf(cart.getCartNum())));
|
|
|
+
|
|
|
+ integralGoods.setNum(cart.getCartNum());
|
|
|
+ goodsItem.add(integralGoods);
|
|
|
}
|
|
|
+
|
|
|
+ return createOrder(user, address, totalIntegral, totalCash, goodsItem);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -400,19 +479,24 @@ public class FsIntegralOrderServiceImpl implements IFsIntegralOrderService
|
|
|
fsIntegralOrderMapper.updateFsIntegralOrder(order);
|
|
|
|
|
|
// 还原库存
|
|
|
- FsIntegralGoods integralGoods = JSONUtil.toBean(order.getItemJson(), FsIntegralGoods.class);
|
|
|
- fsIntegralGoodsMapper.addStock(integralGoods.getGoodsId(), 1);
|
|
|
+ if (order.getItemJson().startsWith("[") && order.getItemJson().endsWith("]")){
|
|
|
+ List<FsIntegralGoods> goodsItem = JSONUtil.toBean(order.getItemJson(), new TypeReference<List<FsIntegralGoods>>(){}, true);
|
|
|
+ goodsItem.forEach(goods -> fsIntegralGoodsMapper.addStock(goods.getGoodsId(), goods.getNum()));
|
|
|
+ } else {
|
|
|
+ FsIntegralGoods integralGoods = JSONUtil.toBean(order.getItemJson(), FsIntegralGoods.class);
|
|
|
+ fsIntegralGoodsMapper.addStock(integralGoods.getGoodsId(), 1);
|
|
|
+ }
|
|
|
|
|
|
// 还原积分
|
|
|
FsUser user=fsUserMapper.selectFsUserByUserId(order.getUserId());
|
|
|
FsUser userMap=new FsUser();
|
|
|
userMap.setUserId(user.getUserId());
|
|
|
- userMap.setIntegral(user.getIntegral()+integralGoods.getIntegral());
|
|
|
+ userMap.setIntegral(user.getIntegral() + Long.parseLong(order.getIntegral()));
|
|
|
fsUserMapper.updateFsUser(userMap);
|
|
|
FsUserIntegralLogs logs = new FsUserIntegralLogs();
|
|
|
logs.setIntegral(Long.parseLong(order.getIntegral()));
|
|
|
logs.setUserId(order.getUserId());
|
|
|
- logs.setBalance(userMap.getIntegral().longValue());
|
|
|
+ logs.setBalance(userMap.getIntegral());
|
|
|
logs.setLogType(5);
|
|
|
logs.setBusinessId(order.getOrderId().toString());
|
|
|
logs.setCreateTime(new Date());
|