|
@@ -0,0 +1,106 @@
|
|
|
+package com.zhentao.controller;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.zhentao.config.SnowflakeUtil;
|
|
|
+import com.zhentao.config.token.TokenUtils;
|
|
|
+import com.zhentao.pojo.*;
|
|
|
+import com.zhentao.pojo.dto.DisDto;
|
|
|
+import com.zhentao.pojo.dto.OrdersDto;
|
|
|
+import com.zhentao.service.*;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Date 2025/5/9 12:02
|
|
|
+ * @Author gln
|
|
|
+ **/
|
|
|
+@RestController
|
|
|
+@RequestMapping("orders")
|
|
|
+public class OrdersController {
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+ @Autowired
|
|
|
+ private CouponService couponService;
|
|
|
+ @Autowired
|
|
|
+ private OrderInfoService orderInfoService;
|
|
|
+ @Autowired
|
|
|
+ private UserCouponService userCouponService;
|
|
|
+ @Autowired
|
|
|
+ private OrderDetailService orderDetailService;
|
|
|
+ @Autowired
|
|
|
+ private DishInfoService dishInfoService;
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate redisTemplate;
|
|
|
+ @RequestMapping("addOrders")
|
|
|
+ public String addOrders(@RequestBody OrdersDto ordersDto){
|
|
|
+ if (ordersDto.getDisDtos().size()==0){
|
|
|
+ return "请选择商品";
|
|
|
+ }
|
|
|
+ //解析token 获取用户ID
|
|
|
+ Object token = redisTemplate.opsForValue().get("token");
|
|
|
+ String id = TokenUtils.parseJWT((String) token).getId();
|
|
|
+ System.out.println(id);
|
|
|
+ //添加订单
|
|
|
+ OrderInfo orderInfo = new OrderInfo();
|
|
|
+ orderInfo.setOid(String.valueOf(SnowflakeUtil.nextId()));
|
|
|
+ orderInfo.setUid(id);
|
|
|
+ orderInfoService.save(orderInfo);
|
|
|
+ //根据商品数量 添加订单详情
|
|
|
+ List<BigDecimal> list = new ArrayList<>();
|
|
|
+ for (DisDto dto:ordersDto.getDisDtos()) {
|
|
|
+ OrderDetail orderDetail = new OrderDetail();
|
|
|
+ orderDetail.setMid(String.valueOf(SnowflakeUtil.nextId()));
|
|
|
+ orderDetail.setOid(orderInfo.getOid());
|
|
|
+ orderDetail.setDid(dto.getDid());
|
|
|
+ orderDetail.setMnum(dto.getNum());
|
|
|
+ DishInfo byId = dishInfoService.getById(dto.getDid());
|
|
|
+ orderDetail.setMprice(byId.getDprice().multiply(new BigDecimal(dto.getNum())));
|
|
|
+ orderDetailService.save(orderDetail);
|
|
|
+ list.add(orderDetail.getMprice());
|
|
|
+ }
|
|
|
+ OrderInfo orderInfo1 = orderInfoService.getById(orderInfo.getOid());
|
|
|
+ BigDecimal all = list.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ System.out.println(all);
|
|
|
+ orderInfo1.setTotal(all);
|
|
|
+ QueryWrapper<UserCoupon> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.lambda().eq(UserCoupon::getUid,id);
|
|
|
+ UserCoupon userCoupon = userCouponService.getOne(wrapper);
|
|
|
+ if (userCoupon!=null){
|
|
|
+ Coupon coupon = couponService.getById(userCoupon.getCid());
|
|
|
+ orderInfo1.setCid(coupon.getCid());
|
|
|
+ if (coupon.getLimitMinFlag()==1){
|
|
|
+ if (all.compareTo(coupon.getLimitMinDecimal())>0||all.compareTo(coupon.getLimitMinDecimal())==0){
|
|
|
+ orderInfo1.setPaymoney(all.subtract(coupon.getFaceValue()));
|
|
|
+ orderInfo1.setDismoney(coupon.getFaceValue());
|
|
|
+ orderInfoService.updateById(orderInfo1);
|
|
|
+ return "优惠券满足使用条件";
|
|
|
+ }else {
|
|
|
+ orderInfo1.setCid(null);
|
|
|
+ orderInfo1.setPaymoney(all);
|
|
|
+ orderInfo1.setDismoney(BigDecimal.valueOf(0));
|
|
|
+ orderInfoService.updateById(orderInfo1);
|
|
|
+ return "优惠券不满足使用条件";
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ orderInfo1.setPaymoney(all.subtract(coupon.getFaceValue()));
|
|
|
+ orderInfo1.setDismoney(coupon.getFaceValue());
|
|
|
+ orderInfoService.updateById(orderInfo1);
|
|
|
+ return "优惠券满足使用条件--无门槛";
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ orderInfo1.setCid(null);
|
|
|
+ orderInfo1.setPaymoney(all);
|
|
|
+ orderInfo1.setDismoney(BigDecimal.valueOf(0));
|
|
|
+ orderInfoService.updateById(orderInfo1);
|
|
|
+ return "订单添加成功--没有待使用优惠卷";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //推送1
|
|
|
+}
|