From 13363998643e4b31f992193a310d334588375ca3 Mon Sep 17 00:00:00 2001 From: muhua <1498117283@qq.com> Date: Fri, 12 Jul 2024 17:35:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=B4=AD=E7=89=A9=E8=BD=A6?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/OrderController.java | 16 +++ .../takeawaysystemserver/entity/Order.java | 2 +- .../entity/ShoppingCart.java | 131 ++++++++++++++++++ .../mapper/ShoppingCartMapper.java | 18 +++ .../service/OrderService.java | 8 +- .../service/ShoppingCartService.java | 38 +++++ .../service/impl/ShoppingCartServiceImpl.java | 85 ++++++++++++ .../resources/mapper/ShoppingCartMapper.xml | 23 +++ 8 files changed, 319 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/example/takeawaysystemserver/controller/OrderController.java create mode 100644 src/main/java/com/example/takeawaysystemserver/entity/ShoppingCart.java create mode 100644 src/main/java/com/example/takeawaysystemserver/mapper/ShoppingCartMapper.java create mode 100644 src/main/java/com/example/takeawaysystemserver/service/ShoppingCartService.java create mode 100644 src/main/java/com/example/takeawaysystemserver/service/impl/ShoppingCartServiceImpl.java create mode 100644 src/main/resources/mapper/ShoppingCartMapper.xml diff --git a/src/main/java/com/example/takeawaysystemserver/controller/OrderController.java b/src/main/java/com/example/takeawaysystemserver/controller/OrderController.java new file mode 100644 index 0000000..6265ba8 --- /dev/null +++ b/src/main/java/com/example/takeawaysystemserver/controller/OrderController.java @@ -0,0 +1,16 @@ +package com.example.takeawaysystemserver.controller; + +import io.swagger.annotations.Api; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author Ethereal + * @date 2024/7/12 + * @description + */ +@RestController +@RequestMapping("order") +@Api(tags = "订单模块") +public class OrderController { +} diff --git a/src/main/java/com/example/takeawaysystemserver/entity/Order.java b/src/main/java/com/example/takeawaysystemserver/entity/Order.java index 9bb3895..2823c68 100644 --- a/src/main/java/com/example/takeawaysystemserver/entity/Order.java +++ b/src/main/java/com/example/takeawaysystemserver/entity/Order.java @@ -24,7 +24,7 @@ public class Order implements Serializable { private Integer userId; /** - * 商品ID + * 商家ID */ private Integer shopId; diff --git a/src/main/java/com/example/takeawaysystemserver/entity/ShoppingCart.java b/src/main/java/com/example/takeawaysystemserver/entity/ShoppingCart.java new file mode 100644 index 0000000..f0afb49 --- /dev/null +++ b/src/main/java/com/example/takeawaysystemserver/entity/ShoppingCart.java @@ -0,0 +1,131 @@ +package com.example.takeawaysystemserver.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import jakarta.annotation.Nullable; +import lombok.Data; + +import java.io.Serializable; + +/** + * + * @TableName shopping_cart + */ +@TableName(value ="shopping_cart") +@Data +public class ShoppingCart implements Serializable { + /** + * 主键ID + */ + @TableId(value = "id",type = IdType.AUTO) + private Integer id; + + /** + * 名称 + */ + private String name; + + /** + * 图片 + */ + private String image; + + /** + * 用户ID + */ + @Nullable + private Integer userId; + + /** + * 菜品ID + */ + @Nullable + private Integer dishId; + + /** + * 口味ID + */ + @Nullable + private Integer dishFlavorId; + + /** + * 数量 + */ + private Integer number; + + /** + * 金额 + */ + private Integer amount; + + /** + * 商家ID + */ + private Integer shopId; + + /** + * 创建时间 + */ + private Integer createTime; + + @TableField(exist = false) + private static final long serialVersionUID = 1L; + + @Override + public boolean equals(Object that) { + if (this == that) { + return true; + } + if (that == null) { + return false; + } + if (getClass() != that.getClass()) { + return false; + } + ShoppingCart other = (ShoppingCart) that; + return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId())) + && (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName())) + && (this.getImage() == null ? other.getImage() == null : this.getImage().equals(other.getImage())) + && (this.getUserId() == null ? other.getUserId() == null : this.getUserId().equals(other.getUserId())) + && (this.getDishId() == null ? other.getDishId() == null : this.getDishId().equals(other.getDishId())) + && (this.getDishFlavorId() == null ? other.getDishFlavorId() == null : this.getDishFlavorId().equals(other.getDishFlavorId())) + && (this.getNumber() == null ? other.getNumber() == null : this.getNumber().equals(other.getNumber())) + && (this.getAmount() == null ? other.getAmount() == null : this.getAmount().equals(other.getAmount())); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((getId() == null) ? 0 : getId().hashCode()); + result = prime * result + ((getName() == null) ? 0 : getName().hashCode()); + result = prime * result + ((getImage() == null) ? 0 : getImage().hashCode()); + result = prime * result + ((getUserId() == null) ? 0 : getUserId().hashCode()); + result = prime * result + ((getDishId() == null) ? 0 : getDishId().hashCode()); + result = prime * result + ((getDishFlavorId() == null) ? 0 : getDishFlavorId().hashCode()); + result = prime * result + ((getNumber() == null) ? 0 : getNumber().hashCode()); + result = prime * result + ((getAmount() == null) ? 0 : getAmount().hashCode()); + return result; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName()); + sb.append(" ["); + sb.append("Hash = ").append(hashCode()); + sb.append(", id=").append(id); + sb.append(", name=").append(name); + sb.append(", image=").append(image); + sb.append(", userId=").append(userId); + sb.append(", dishId=").append(dishId); + sb.append(", dishFlavorId=").append(dishFlavorId); + sb.append(", number=").append(number); + sb.append(", amount=").append(amount); + sb.append(", serialVersionUID=").append(serialVersionUID); + sb.append("]"); + return sb.toString(); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/takeawaysystemserver/mapper/ShoppingCartMapper.java b/src/main/java/com/example/takeawaysystemserver/mapper/ShoppingCartMapper.java new file mode 100644 index 0000000..ff36431 --- /dev/null +++ b/src/main/java/com/example/takeawaysystemserver/mapper/ShoppingCartMapper.java @@ -0,0 +1,18 @@ +package com.example.takeawaysystemserver.mapper; + +import com.example.takeawaysystemserver.entity.ShoppingCart; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; + +/** +* @author qiushengyu +* @description 针对表【shopping_cart】的数据库操作Mapper +* @createDate 2024-07-12 16:13:47 +* @Entity com.example.takeawaysystemserver.entity.ShoppingCart +*/ +public interface ShoppingCartMapper extends BaseMapper { + +} + + + + diff --git a/src/main/java/com/example/takeawaysystemserver/service/OrderService.java b/src/main/java/com/example/takeawaysystemserver/service/OrderService.java index ebc83d4..d8e5dbc 100644 --- a/src/main/java/com/example/takeawaysystemserver/service/OrderService.java +++ b/src/main/java/com/example/takeawaysystemserver/service/OrderService.java @@ -2,6 +2,7 @@ package com.example.takeawaysystemserver.service; import com.example.takeawaysystemserver.entity.Order; import com.baomidou.mybatisplus.extension.service.IService; +import com.example.takeawaysystemserver.util.R; /** * @author qiushengyu @@ -9,5 +10,10 @@ import com.baomidou.mybatisplus.extension.service.IService; * @createDate 2024-07-11 17:13:46 */ public interface OrderService extends IService { - + /** + * 添加订单 + * @param order 订单类 + * @return 添加信息 + */ + R addOrders(Order order); } diff --git a/src/main/java/com/example/takeawaysystemserver/service/ShoppingCartService.java b/src/main/java/com/example/takeawaysystemserver/service/ShoppingCartService.java new file mode 100644 index 0000000..3fde87e --- /dev/null +++ b/src/main/java/com/example/takeawaysystemserver/service/ShoppingCartService.java @@ -0,0 +1,38 @@ +package com.example.takeawaysystemserver.service; + +import com.example.takeawaysystemserver.entity.ShoppingCart; +import com.baomidou.mybatisplus.extension.service.IService; +import com.example.takeawaysystemserver.util.R; + +import java.util.List; + +/** +* @author qiushengyu +* @description 针对表【shopping_cart】的数据库操作Service +* @createDate 2024-07-12 16:13:47 +*/ +public interface ShoppingCartService extends IService { + + /** + * 添加购物车 + * @param shoppingCart 购物车类 + * @param token 用户token + * @return 添加信息 + */ + R addShoppingCart(String token, ShoppingCart shoppingCart); + + /** + * 获取购物车信息 + * @param token 用户token + * @return 购物车信息 + */ + R> getShoppingCarts(String token); + + /** + * 删除购物车信息 + * @param shoppingCart 购物车类 + * @param token 用户token + * @return 删除信息 + */ + R deleteShoppingCart(String token, ShoppingCart shoppingCart); +} diff --git a/src/main/java/com/example/takeawaysystemserver/service/impl/ShoppingCartServiceImpl.java b/src/main/java/com/example/takeawaysystemserver/service/impl/ShoppingCartServiceImpl.java new file mode 100644 index 0000000..2bf126a --- /dev/null +++ b/src/main/java/com/example/takeawaysystemserver/service/impl/ShoppingCartServiceImpl.java @@ -0,0 +1,85 @@ +package com.example.takeawaysystemserver.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.example.takeawaysystemserver.entity.Shop; +import com.example.takeawaysystemserver.entity.ShoppingCart; +import com.example.takeawaysystemserver.service.ShoppingCartService; +import com.example.takeawaysystemserver.mapper.ShoppingCartMapper; +import com.example.takeawaysystemserver.service.UserService; +import com.example.takeawaysystemserver.util.R; +import com.example.takeawaysystemserver.util.TimeUtil; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** +* @author qiushengyu +* @description 针对表【shopping_cart】的数据库操作Service实现 +* @createDate 2024-07-12 16:13:47 +*/ +@Service +public class ShoppingCartServiceImpl extends ServiceImpl + implements ShoppingCartService{ + + @Resource + private UserServiceImpl userServiceImpl; + + + @Override + public R addShoppingCart(String token, ShoppingCart shoppingCart) { + Integer id = Integer.parseInt(userServiceImpl.getUserId(token)); + shoppingCart.setUserId(id); + LambdaQueryWrapper wrapper = new LambdaQueryWrapper(); + wrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId()); + wrapper.eq(shoppingCart.getShopId() != null, ShoppingCart::getShopId, shoppingCart.getShopId()); + + ShoppingCart one = getOne(wrapper); + //根据商家ID、菜品ID查询数据库是否已有 + if (one != null){ + //有就+1 + shoppingCart.setId(one.getId()); + shoppingCart.setNumber(one.getNumber() + 1); + updateById(shoppingCart); + }else { + //没有就添加购物车 + Integer createTime = TimeUtil.getCurTimestamp(); + shoppingCart.setCreateTime(createTime); + save(shoppingCart); + } + return R.ok("添加成功"); + } + + @Override + public R> getShoppingCarts(String token) { + Integer id = Integer.parseInt(userServiceImpl.getUserId(token)); + LambdaQueryWrapper wrapper = new LambdaQueryWrapper(); + wrapper.orderByDesc(ShoppingCart::getCreateTime); + wrapper.eq(ShoppingCart::getUserId, id); + return R.ok(list(wrapper)); + } + + @Override + public R deleteShoppingCart(String token, ShoppingCart shoppingCart) { + Integer id = Integer.parseInt(userServiceImpl.getUserId(token)); + shoppingCart.setUserId(id); + LambdaQueryWrapper wrapper = new LambdaQueryWrapper(); + wrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId()); + wrapper.eq(shoppingCart.getShopId() != null, ShoppingCart::getShopId, shoppingCart.getShopId()); + ShoppingCart one = getOne(wrapper); + + if (one.getNumber() == 1){ + removeById(one.getId()); + }else { + shoppingCart.setId(one.getId()); + shoppingCart.setNumber(one.getNumber() - 1); + updateById(shoppingCart); + } + return R.ok("删除成功"); + } +} + + + + diff --git a/src/main/resources/mapper/ShoppingCartMapper.xml b/src/main/resources/mapper/ShoppingCartMapper.xml new file mode 100644 index 0000000..c35b710 --- /dev/null +++ b/src/main/resources/mapper/ShoppingCartMapper.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + id,name,image, + user_id,dish_id,dish_flavor_id, + number,amount + +