添加购物车模块接口

This commit is contained in:
muhua 2024-07-12 17:35:54 +08:00
parent f680ec4ec8
commit 1336399864
8 changed files with 319 additions and 2 deletions

View File

@ -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 {
}

View File

@ -24,7 +24,7 @@ public class Order implements Serializable {
private Integer userId;
/**
* ID
* ID
*/
private Integer shopId;

View File

@ -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();
}
}

View File

@ -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<ShoppingCart> {
}

View File

@ -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<Order> {
/**
* 添加订单
* @param order 订单类
* @return 添加信息
*/
R<String> addOrders(Order order);
}

View File

@ -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<ShoppingCart> {
/**
* 添加购物车
* @param shoppingCart 购物车类
* @param token 用户token
* @return 添加信息
*/
R<String> addShoppingCart(String token, ShoppingCart shoppingCart);
/**
* 获取购物车信息
* @param token 用户token
* @return 购物车信息
*/
R<List<ShoppingCart>> getShoppingCarts(String token);
/**
* 删除购物车信息
* @param shoppingCart 购物车类
* @param token 用户token
* @return 删除信息
*/
R<String> deleteShoppingCart(String token, ShoppingCart shoppingCart);
}

View File

@ -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<ShoppingCartMapper, ShoppingCart>
implements ShoppingCartService{
@Resource
private UserServiceImpl userServiceImpl;
@Override
public R<String> addShoppingCart(String token, ShoppingCart shoppingCart) {
Integer id = Integer.parseInt(userServiceImpl.getUserId(token));
shoppingCart.setUserId(id);
LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<ShoppingCart>();
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<List<ShoppingCart>> getShoppingCarts(String token) {
Integer id = Integer.parseInt(userServiceImpl.getUserId(token));
LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<ShoppingCart>();
wrapper.orderByDesc(ShoppingCart::getCreateTime);
wrapper.eq(ShoppingCart::getUserId, id);
return R.ok(list(wrapper));
}
@Override
public R<String> deleteShoppingCart(String token, ShoppingCart shoppingCart) {
Integer id = Integer.parseInt(userServiceImpl.getUserId(token));
shoppingCart.setUserId(id);
LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<ShoppingCart>();
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("删除成功");
}
}

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.takeawaysystemserver.mapper.ShoppingCartMapper">
<resultMap id="BaseResultMap" type="com.example.takeawaysystemserver.entity.ShoppingCart">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="image" column="image" jdbcType="VARCHAR"/>
<result property="userId" column="user_id" jdbcType="INTEGER"/>
<result property="dishId" column="dish_id" jdbcType="INTEGER"/>
<result property="dishFlavorId" column="dish_flavor_id" jdbcType="INTEGER"/>
<result property="number" column="number" jdbcType="INTEGER"/>
<result property="amount" column="amount" jdbcType="INTEGER"/>
</resultMap>
<sql id="Base_Column_List">
id,name,image,
user_id,dish_id,dish_flavor_id,
number,amount
</sql>
</mapper>