commit 7323ba8817fe72c5db985ad8108b33b336571086 Author: xgl <2389009027@qq.com> Date: Wed Dec 4 11:09:34 2024 +0800 xgl diff --git a/Business.java b/Business.java new file mode 100644 index 00000000..479242be --- /dev/null +++ b/Business.java @@ -0,0 +1,122 @@ +package com.example.entity; + +import java.io.Serializable; + +/** + * 商家 +*/ +public class Business extends Account implements Serializable { + private static final long serialVersionUID = 1L; + + /** ID */ + private Integer id; + /** 用户名 */ + private String username; + /** 密码 */ + private String password; + /** 姓名 */ + private String name; + /** 电话 */ + private String phone; + /** 邮箱 */ + private String email; + /** 头像 */ + private String avatar; + /** 角色标识 */ + private String role; + + private String description; + + private String status; + @Override + public Integer getId() { + return id; + } + + @Override + public void setId(Integer id) { + this.id = id; + } + + @Override + public String getUsername() { + return username; + } + + @Override + public void setUsername(String username) { + this.username = username; + } + + @Override + public String getPassword() { + return password; + } + + @Override + public void setPassword(String password) { + this.password = password; + } + + @Override + public String getName() { + return name; + } + + @Override + public void setName(String name) { + this.name = name; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + @Override + public String getAvatar() { + return avatar; + } + + @Override + public void setAvatar(String avatar) { + this.avatar = avatar; + } + + @Override + public String getRole() { + return role; + } + + @Override + public void setRole(String role) { + this.role = role; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } +} \ No newline at end of file diff --git a/BusinessController.java b/BusinessController.java new file mode 100644 index 00000000..09d3731d --- /dev/null +++ b/BusinessController.java @@ -0,0 +1,89 @@ +package com.example.controller; + +import com.example.common.Result; +import com.example.entity.Admin; +import com.example.entity.Business; +import com.example.service.AdminService; +import com.example.service.BusinessService; +import com.github.pagehelper.PageInfo; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 商家前端操作接口 + **/ +@RestController +@RequestMapping("/business") +public class BusinessController { + + @Resource + private BusinessService businessService; + + /** + * 新增 + */ + @PostMapping("/add") + public Result add(@RequestBody Business business) { + businessService.add(business); + return Result.success(); + } + + /** + * 删除 + */ + @DeleteMapping("/delete/{id}") + public Result deleteById(@PathVariable Integer id) { + businessService.deleteById(id); + return Result.success(); + } + + /** + * 批量删除 + */ + @DeleteMapping("/delete/batch") + public Result deleteBatch(@RequestBody List ids) { + businessService.deleteBatch(ids); + return Result.success(); + } + + /** + * 修改 + */ + @PutMapping("/update") + public Result updateById(@RequestBody Business business) { + businessService.updateById(business); + return Result.success(); + } + + /** + * 根据ID查询 + */ + @GetMapping("/selectById/{id}") + public Result selectById(@PathVariable Integer id) { + Business business = businessService.selectById(id); + return Result.success(business); + } + + /** + * 查询所有 + */ + @GetMapping("/selectAll") + public Result selectAll(Business business ) { + List list = businessService.selectAll(business); + return Result.success(list); + } + + /** + * 分页查询 + */ + @GetMapping("/selectPage") + public Result selectPage(Business business, + @RequestParam(defaultValue = "1") Integer pageNum, + @RequestParam(defaultValue = "10") Integer pageSize) { + PageInfo page = businessService.selectPage(business, pageNum, pageSize); + return Result.success(page); + } + +} \ No newline at end of file diff --git a/BusinessMapper.java b/BusinessMapper.java new file mode 100644 index 00000000..4a5dc7a5 --- /dev/null +++ b/BusinessMapper.java @@ -0,0 +1,40 @@ +package com.example.mapper; + +import com.example.entity.Business; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * 操作business相关数据接口 +*/ +public interface BusinessMapper { + + /** + * 新增 + */ + int insert(Business business); + + /** + * 删除 + */ + int deleteById(Integer id); + + /** + * 修改 + */ + int updateById(Business business); + + /** + * 根据ID查询 + */ + Business selectById(Integer id); + + /** + * 查询所有 + */ + List selectAll(Business business); + + @Select("select * from business where username = #{username}") + Business selectByUsername(String username); +} \ No newline at end of file diff --git a/BusinessService.java b/BusinessService.java new file mode 100644 index 00000000..98d233d0 --- /dev/null +++ b/BusinessService.java @@ -0,0 +1,139 @@ +package com.example.service; + +import cn.hutool.core.util.ObjectUtil; +import com.example.common.Constants; +import com.example.common.enums.ResultCodeEnum; +import com.example.common.enums.RoleEnum; +import com.example.common.enums.StatusEnum; +import com.example.entity.Account; +import com.example.entity.Business; +import com.example.exception.CustomException; +import com.example.mapper.BusinessMapper; +import com.example.utils.TokenUtils; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import org.springframework.beans.BeanUtils; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 商家业务处理 + **/ +@Service +public class BusinessService { + + @Resource + private BusinessMapper businessMapper; + + /** + * 新增 + */ + public void add(Business business) { + Business dbBusiness = businessMapper.selectByUsername(business.getUsername()); + if (ObjectUtil.isNotNull(dbBusiness)) { + throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR); + } + if (ObjectUtil.isEmpty(business.getPassword())) { + business.setPassword(Constants.USER_DEFAULT_PASSWORD); + } + if (ObjectUtil.isEmpty(business.getName())) { + business.setName(business.getUsername()); + } + if (ObjectUtil.isEmpty(business.getStatus())) { + business.setStatus(StatusEnum.CHECKEING.status); + } + business.setRole(RoleEnum.BUSINESS.name()); + businessMapper.insert(business); + } + + /** + * 删除 + */ + public void deleteById(Integer id) { + businessMapper.deleteById(id); + } + + /** + * 批量删除 + */ + public void deleteBatch(List ids) { + for (Integer id : ids) { + businessMapper.deleteById(id); + } + } + + /** + * 修改 + */ + public void updateById(Business business) { + businessMapper.updateById(business); + } + + /** + * 根据ID查询 + */ + public Business selectById(Integer id) { + return businessMapper.selectById(id); + } + + /** + * 查询所有 + */ + public List selectAll(Business business) { + return businessMapper.selectAll(business); + } + + /** + * 分页查询 + */ + public PageInfo selectPage(Business business, Integer pageNum, Integer pageSize) { + PageHelper.startPage(pageNum, pageSize); + List list = businessMapper.selectAll(business); + return PageInfo.of(list); + } + + /** + * 登录 + */ + public Account login(Account account) { + Account dbBusiness = businessMapper.selectByUsername(account.getUsername()); + if (ObjectUtil.isNull(dbBusiness)) { + throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR); + } + if (!account.getPassword().equals(dbBusiness.getPassword())) { + throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR); + } + // 生成token + String tokenData = dbBusiness.getId() + "-" + RoleEnum.BUSINESS.name(); + String token = TokenUtils.createToken(tokenData, dbBusiness.getPassword()); + dbBusiness.setToken(token); + return dbBusiness; + } + + /** + * 注册 + */ + public void register(Account account) { + Business business = new Business(); + BeanUtils.copyProperties(account, business); + add(business); + } + + /** + * 修改密码 + */ + public void updatePassword(Account account) { + Business dbBusiness = businessMapper.selectByUsername(account.getUsername()); + if (ObjectUtil.isNull(dbBusiness)) { + throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR); + } + if (!account.getPassword().equals(dbBusiness.getPassword())) { + throw new CustomException(ResultCodeEnum.PARAM_PASSWORD_ERROR); + } + dbBusiness.setPassword(account.getNewPassword()); + businessMapper.updateById(dbBusiness); + } + +} \ No newline at end of file diff --git a/Cart.java b/Cart.java new file mode 100644 index 00000000..27e54ed1 --- /dev/null +++ b/Cart.java @@ -0,0 +1,105 @@ +package com.example.entity; + +import java.io.Serializable; + +/** + * 购物车信息表 +*/ +public class Cart implements Serializable { + private static final long serialVersionUID = 1L; + + private Integer id; + private Integer userId; + private Integer businessId; + private Integer goodsId; + private Integer num; + + private String businessName; + private String goodsName; + private String goodsImg; + private String goodUnit; + private Double goodsPrice; + + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public Integer getBusinessId() { + return businessId; + } + + public void setBusinessId(Integer businessId) { + this.businessId = businessId; + } + + public Integer getGoodsId() { + return goodsId; + } + + public void setGoodsId(Integer goodsId) { + this.goodsId = goodsId; + } + + public String getBusinessName() { + return businessName; + } + + public void setBusinessName(String businessName) { + this.businessName = businessName; + } + + public String getGoodsName() { + return goodsName; + } + + public void setGoodsName(String goodsName) { + this.goodsName = goodsName; + } + + public String getGoodsImg() { + return goodsImg; + } + + public void setGoodsImg(String goodsImg) { + this.goodsImg = goodsImg; + } + + public String getGoodUnit() { + return goodUnit; + } + + public void setGoodUnit(String goodUnit) { + this.goodUnit = goodUnit; + } + + public Double getGoodsPrice() { + return goodsPrice; + } + + public void setGoodsPrice(Double goodsPrice) { + this.goodsPrice = goodsPrice; + } + + public Integer getNum() { + return num; + } + + public void setNum(Integer num) { + this.num = num; + } +} + + diff --git a/CartController.java b/CartController.java new file mode 100644 index 00000000..bcb2445c --- /dev/null +++ b/CartController.java @@ -0,0 +1,87 @@ +package com.example.controller; + +import com.example.common.Result; +import com.example.entity.Cart; +import com.example.service.CartService; +import com.github.pagehelper.PageInfo; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 购物车前端操作接口 + **/ +@RestController +@RequestMapping("/cart") +public class CartController { + + @Resource + private CartService cartService; + + /** + * 新增 + */ + @PostMapping("/add") + public Result add(@RequestBody Cart cart) { + cartService.add(cart); + return Result.success(); + } + + /** + * 删除 + */ + @DeleteMapping("/delete/{id}") + public Result deleteById(@PathVariable Integer id) { + cartService.deleteById(id); + return Result.success(); + } + + /** + * 批量删除 + */ + @DeleteMapping("/delete/batch") + public Result deleteBatch(@RequestBody List ids) { + cartService.deleteBatch(ids); + return Result.success(); + } + + /** + * 修改 + */ + @PutMapping("/update") + public Result updateById(@RequestBody Cart cart) { + cartService.updateById(cart); + return Result.success(); + } + + /** + * 根据ID查询 + */ + @GetMapping("/selectById/{id}") + public Result selectById(@PathVariable Integer id) { + Cart cart = cartService.selectById(id); + return Result.success(cart); + } + + /** + * 查询所有 + */ + @GetMapping("/selectAll") + public Result selectAll(Cart cart ) { + List list = cartService.selectAll(cart); + return Result.success(list); + } + + /** + * 分页查询 + */ + @GetMapping("/selectPage") + public Result selectPage(Cart cart, + @RequestParam(defaultValue = "1") Integer pageNum, + @RequestParam(defaultValue = "10") Integer pageSize) { + PageInfo page = cartService.selectPage(cart, pageNum, pageSize); + return Result.success(page); + } + +} \ No newline at end of file diff --git a/CartMapper.java b/CartMapper.java new file mode 100644 index 00000000..6afb3ade --- /dev/null +++ b/CartMapper.java @@ -0,0 +1,41 @@ +package com.example.mapper; + +import com.example.entity.Cart; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * 操作cart相关数据接口 +*/ +public interface CartMapper { + + /** + * 新增 + */ + int insert(Cart cart); + + /** + * 删除 + */ + int deleteById(Integer id); + + /** + * 修改 + */ + int updateById(Cart cart); + + /** + * 根据ID查询 + */ + Cart selectById(Integer id); + + /** + * 查询所有 + */ + List selectAll(Cart cart); + + @Select("select * from cart where user_id = #{userId} and goods_id = #{goodsId}") + Cart selectByUserIdAndGoodsId(@Param("userId") Integer userId, @Param("goodsId") Integer goodsId); +} \ No newline at end of file diff --git a/CartService.java b/CartService.java new file mode 100644 index 00000000..28dbdc3c --- /dev/null +++ b/CartService.java @@ -0,0 +1,90 @@ +package com.example.service; + +import cn.hutool.core.util.ObjectUtil; +import com.example.common.enums.ResultCodeEnum; +import com.example.common.enums.RoleEnum; +import com.example.entity.Account; +import com.example.entity.Cart; +import com.example.exception.CustomException; +import com.example.mapper.CartMapper; +import com.example.utils.TokenUtils; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 购物车业务处理 + **/ +@Service +public class CartService { + + @Resource + private CartMapper cartMapper; + + /** + * 新增 + */ + public void add(Cart cart) { + // 判断该用户对该商品有没有加入过购物车,如果加入过,那么只要更新一下该条记录的num(+1) + Cart dbCart = cartMapper.selectByUserIdAndGoodsId(cart.getUserId(), cart.getGoodsId()); + if (ObjectUtil.isNotEmpty(dbCart)) { + dbCart.setNum(dbCart.getNum() + 1); + cartMapper.updateById(dbCart); + } else { + cartMapper.insert(cart); + } + } + + /** + * 删除 + */ + public void deleteById(Integer id) { + cartMapper.deleteById(id); + } + + /** + * 批量删除 + */ + public void deleteBatch(List ids) { + for (Integer id : ids) { + cartMapper.deleteById(id); + } + } + + /** + * 修改 + */ + public void updateById(Cart cart) { + cartMapper.updateById(cart); + } + + /** + * 根据ID查询 + */ + public Cart selectById(Integer id) { + return cartMapper.selectById(id); + } + + /** + * 查询所有 + */ + public List selectAll(Cart cart) { + return cartMapper.selectAll(cart); + } + + /** + * 分页查询 + */ + public PageInfo selectPage(Cart cart, Integer pageNum, Integer pageSize) { + Account currentUser = TokenUtils.getCurrentUser(); + if (RoleEnum.USER.name().equals(currentUser.getRole())) { + cart.setUserId(currentUser.getId()); + } + PageHelper.startPage(pageNum, pageSize); + List list = cartMapper.selectAll(cart); + return PageInfo.of(list); + } +} \ No newline at end of file diff --git a/Comment.java b/Comment.java new file mode 100644 index 00000000..05edb94a --- /dev/null +++ b/Comment.java @@ -0,0 +1,86 @@ +package com.example.entity; + +import java.io.Serializable; + +/** + * 评论信息表 +*/ +public class Comment implements Serializable { + private static final long serialVersionUID = 1L; + + /** ID */ + private Integer id; + private Integer userId; + private Integer businessId; + private Integer goodsId; + + private String businessName; + private String goodsName; + + private String time; + private String content; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public Integer getBusinessId() { + return businessId; + } + + public void setBusinessId(Integer businessId) { + this.businessId = businessId; + } + + public Integer getGoodsId() { + return goodsId; + } + + public void setGoodsId(Integer goodsId) { + this.goodsId = goodsId; + } + + public String getBusinessName() { + return businessName; + } + + public void setBusinessName(String businessName) { + this.businessName = businessName; + } + + public String getGoodsName() { + return goodsName; + } + + public void setGoodsName(String goodsName) { + this.goodsName = goodsName; + } + + public String getTime() { + return time; + } + + public void setTime(String time) { + this.time = time; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } +} \ No newline at end of file diff --git a/CommentController.java b/CommentController.java new file mode 100644 index 00000000..e5a6885f --- /dev/null +++ b/CommentController.java @@ -0,0 +1,93 @@ +package com.example.controller; + +import com.example.common.Result; +import com.example.entity.Comment; +import com.example.service.CommentService; +import com.github.pagehelper.PageInfo; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 评论前端操作接口 + **/ +@RestController +@RequestMapping("/comment") +public class CommentController { + + @Resource + private CommentService commentService; + + /** + * 新增 + */ + @PostMapping("/add") + public Result add(@RequestBody Comment comment) { + commentService.add(comment); + return Result.success(); + } + + /** + * 删除 + */ + @DeleteMapping("/delete/{id}") + public Result deleteById(@PathVariable Integer id) { + commentService.deleteById(id); + return Result.success(); + } + + /** + * 批量删除 + */ + @DeleteMapping("/delete/batch") + public Result deleteBatch(@RequestBody List ids) { + commentService.deleteBatch(ids); + return Result.success(); + } + + /** + * 修改 + */ + @PutMapping("/update") + public Result updateById(@RequestBody Comment comment) { + commentService.updateById(comment); + return Result.success(); + } + + /** + * 根据ID查询 + */ + @GetMapping("/selectById/{id}") + public Result selectById(@PathVariable Integer id) { + Comment comment = commentService.selectById(id); + return Result.success(comment); + } + + @GetMapping("/selectByGoodsId") + public Result selectByGoodsId(@RequestParam Integer id) { + List list = commentService.selectByGoodsId(id); + return Result.success(list); + } + + /** + * 查询所有 + */ + @GetMapping("/selectAll") + public Result selectAll(Comment comment ) { + List list = commentService.selectAll(comment); + return Result.success(list); + } + + /** + * 分页查询 + */ + @GetMapping("/selectPage") + public Result selectPage(Comment comment, + @RequestParam(defaultValue = "1") Integer pageNum, + @RequestParam(defaultValue = "10") Integer pageSize) { + PageInfo page = commentService.selectPage(comment, pageNum, pageSize); + return Result.success(page); + } + +} \ No newline at end of file diff --git a/CommentMapper.java b/CommentMapper.java new file mode 100644 index 00000000..b9a18192 --- /dev/null +++ b/CommentMapper.java @@ -0,0 +1,41 @@ +package com.example.mapper; + +import com.example.entity.Comment; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * 操作comment相关数据接口 +*/ +public interface CommentMapper { + + /** + * 新增 + */ + int insert(Comment comment); + + /** + * 删除 + */ + int deleteById(Integer id); + + /** + * 修改 + */ + int updateById(Comment comment); + + /** + * 根据ID查询 + */ + Comment selectById(Integer id); + + /** + * 查询所有 + */ + List selectAll(Comment comment); + + @Select("select comment.*, user.avatar as userAvatar, user.name as userName from comment left join user on comment.user_id = user.id where comment.goods_id = #{id}") + List selectByGoodsId(Integer id); +} \ No newline at end of file diff --git a/CommentService.java b/CommentService.java new file mode 100644 index 00000000..052fb9e1 --- /dev/null +++ b/CommentService.java @@ -0,0 +1,92 @@ +package com.example.service; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ObjectUtil; +import com.example.common.enums.ResultCodeEnum; +import com.example.common.enums.RoleEnum; +import com.example.entity.Account; +import com.example.entity.Comment; +import com.example.exception.CustomException; +import com.example.mapper.CommentMapper; +import com.example.utils.TokenUtils; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 评论业务处理 + **/ +@Service +public class CommentService { + + @Resource + private CommentMapper commentMapper; + + /** + * 新增 + */ + public void add(Comment comment) { + comment.setTime(DateUtil.now()); + commentMapper.insert(comment); + } + + /** + * 删除 + */ + public void deleteById(Integer id) { + commentMapper.deleteById(id); + } + + /** + * 批量删除 + */ + public void deleteBatch(List ids) { + for (Integer id : ids) { + commentMapper.deleteById(id); + } + } + + /** + * 修改 + */ + public void updateById(Comment comment) { + commentMapper.updateById(comment); + } + + /** + * 根据ID查询 + */ + public Comment selectById(Integer id) { + return commentMapper.selectById(id); + } + + /** + * 查询所有 + */ + public List selectAll(Comment comment) { + return commentMapper.selectAll(comment); + } + + /** + * 分页查询 + */ + public PageInfo selectPage(Comment comment, Integer pageNum, Integer pageSize) { + Account currentUser = TokenUtils.getCurrentUser(); + if (RoleEnum.USER.name().equals(currentUser.getRole())) { + comment.setUserId(currentUser.getId()); + } + if (RoleEnum.BUSINESS.name().equals(currentUser.getRole())) { + comment.setBusinessId(currentUser.getId()); + } + PageHelper.startPage(pageNum, pageSize); + List list = commentMapper.selectAll(comment); + return PageInfo.of(list); + } + + public List selectByGoodsId(Integer id) { + return commentMapper.selectByGoodsId(id); + } +} \ No newline at end of file diff --git a/Goods.java b/Goods.java new file mode 100644 index 00000000..d2e96844 --- /dev/null +++ b/Goods.java @@ -0,0 +1,114 @@ +package com.example.entity; + +import java.io.Serializable; + +/** + * 商品信息表 +*/ +public class Goods implements Serializable { + private static final long serialVersionUID = 1L; + + /** ID */ + private Integer id; + /** 分类名称 */ + private String name; + /** 分类描述 */ + private String description; + /** 分类图标 */ + private String img; + private Double price; + private String unit; + private Integer count; + private Integer typeId; + private Integer businessId; + private String typeName; + private String businessName; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getImg() { + return img; + } + + public void setImg(String img) { + this.img = img; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + public String getUnit() { + return unit; + } + + public void setUnit(String unit) { + this.unit = unit; + } + + public Integer getCount() { + return count; + } + + public void setCount(Integer count) { + this.count = count; + } + + public Integer getTypeId() { + return typeId; + } + + public void setTypeId(Integer typeId) { + this.typeId = typeId; + } + + public Integer getBusinessId() { + return businessId; + } + + public void setBusinessId(Integer businessId) { + this.businessId = businessId; + } + + public String getTypeName() { + return typeName; + } + + public void setTypeName(String typeName) { + this.typeName = typeName; + } + + public String getBusinessName() { + return businessName; + } + + public void setBusinessName(String businessName) { + this.businessName = businessName; + } +} diff --git a/GoodsController.java b/GoodsController.java new file mode 100644 index 00000000..9a503b94 --- /dev/null +++ b/GoodsController.java @@ -0,0 +1,110 @@ +package com.example.controller; + +import com.example.common.Result; +import com.example.entity.Goods; +import com.example.service.GoodsService; +import com.github.pagehelper.PageInfo; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 商品信息表前端操作接口 + **/ +@RestController +@RequestMapping("/goods") +public class GoodsController { + + @Resource + private GoodsService goodsService; + + /** + * 新增 + */ + @PostMapping("/add") + public Result add(@RequestBody Goods goods) { + goodsService.add(goods); + return Result.success(); + } + + /** + * 删除 + */ + @DeleteMapping("/delete/{id}") + public Result deleteById(@PathVariable Integer id) { + goodsService.deleteById(id); + return Result.success(); + } + + /** + * 批量删除 + */ + @DeleteMapping("/delete/batch") + public Result deleteBatch(@RequestBody List ids) { + goodsService.deleteBatch(ids); + return Result.success(); + } + + /** + * 修改 + */ + @PutMapping("/update") + public Result updateById(@RequestBody Goods goods) { + goodsService.updateById(goods); + return Result.success(); + } + + /** + * 根据ID查询 + */ + @GetMapping("/selectById") + public Result selectById(@RequestParam Integer id) { + Goods goods = goodsService.selectById(id); + return Result.success(goods); + } + @GetMapping("/selectTop15") + public Result selectTop15() { + List list = goodsService.selectTop15(); + return Result.success(list); + } + + /** + * 查询所有 + */ + @GetMapping("/selectAll") + public Result selectAll(Goods goods ) { + List list = goodsService.selectAll(goods); + return Result.success(list); + } + + @GetMapping("/selectByTypeId") + public Result selectByTypeId(@RequestParam Integer id) { + List list = goodsService.selectByTypeId(id); + return Result.success(list); + } + + @GetMapping("/selectByName") + public Result selectByName(@RequestParam String name) { + List list = goodsService.selectByName(name); + return Result.success(list); + } + + @GetMapping("/selectByBusinessId") + public Result selectByBusinessId(@RequestParam Integer id) { + List list = goodsService.selectByBusinessId(id); + return Result.success(list); + } + + /** + * 分页查询 + */ + @GetMapping("/selectPage") + public Result selectPage(Goods goods, + @RequestParam(defaultValue = "1") Integer pageNum, + @RequestParam(defaultValue = "10") Integer pageSize) { + PageInfo page = goodsService.selectPage(goods, pageNum, pageSize); + return Result.success(page); + } + +} \ No newline at end of file diff --git a/GoodsMapper.java b/GoodsMapper.java new file mode 100644 index 00000000..3a543471 --- /dev/null +++ b/GoodsMapper.java @@ -0,0 +1,50 @@ +package com.example.mapper; + +import com.example.entity.Goods; +import com.example.entity.Type; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * 操作goods相关数据接口 +*/ +public interface GoodsMapper { + + /** + * 新增 + */ + int insert(Goods goods); + + /** + * 删除 + */ + int deleteById(Integer id); + + /** + * 修改 + */ + int updateById(Goods goods); + + /** + * 根据ID查询 + */ + Goods selectById(Integer id); + + /** + * 查询所有 + */ + List selectAll(Goods goods); + + @Select("select * from goods order by count desc limit 15") + List selectTop15(); + + @Select("select * from goods where type_id = #{id}") + List selectByTypeId(Integer id); + + @Select("select * from goods where business_id = #{id}") + List selectByBusinessId(Integer id); + + @Select("select * from goods where name like concat('%', #{name}, '%')") + List selectByName(String name); +} \ No newline at end of file diff --git a/GoodsService.java b/GoodsService.java new file mode 100644 index 00000000..c688cbcc --- /dev/null +++ b/GoodsService.java @@ -0,0 +1,101 @@ +package com.example.service; + + +import com.example.common.enums.RoleEnum; +import com.example.entity.Account; +import com.example.entity.Goods; +import com.example.mapper.GoodsMapper; +import com.example.utils.TokenUtils; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 商品信息表业务处理 + **/ +@Service +public class GoodsService { + + @Resource + private GoodsMapper goodsMapper; + + /** + * 新增 + */ + public void add(Goods goods) { + Account currentUser = TokenUtils.getCurrentUser(); + if (RoleEnum.BUSINESS.name().equals(currentUser.getRole())) { + goods.setBusinessId(currentUser.getId()); + } + goodsMapper.insert(goods); + } + + /** + * 删除 + */ + public void deleteById(Integer id) { + goodsMapper.deleteById(id); + } + + /** + * 批量删除 + */ + public void deleteBatch(List ids) { + for (Integer id : ids) { + goodsMapper.deleteById(id); + } + } + + /** + * 修改 + */ + public void updateById(Goods goods) { + goodsMapper.updateById(goods); + } + + /** + * 根据ID查询 + */ + public Goods selectById(Integer id) { + return goodsMapper.selectById(id); + } + + /** + * 查询所有 + */ + public List selectAll(Goods goods) { + return goodsMapper.selectAll(goods); + } + + /** + * 分页查询 + */ + public PageInfo selectPage(Goods goods, Integer pageNum, Integer pageSize) { + Account currentUser = TokenUtils.getCurrentUser(); + if (RoleEnum.BUSINESS.name().equals(currentUser.getRole())) { + goods.setBusinessId(currentUser.getId()); + } + PageHelper.startPage(pageNum, pageSize); + List list = goodsMapper.selectAll(goods); + return PageInfo.of(list); + } + + public List selectTop15() { + return goodsMapper.selectTop15(); + } + + public List selectByTypeId(Integer id) { + return goodsMapper.selectByTypeId(id); + } + + public List selectByBusinessId(Integer id) { + return goodsMapper.selectByBusinessId(id); + } + + public List selectByName(String name) { + return goodsMapper.selectByName(name); + } +} \ No newline at end of file diff --git a/Orders.java b/Orders.java new file mode 100644 index 00000000..e459d31d --- /dev/null +++ b/Orders.java @@ -0,0 +1,180 @@ +package com.example.entity; + +import java.io.Serializable; +import java.util.List; + +/** + * 购物车信息表 +*/ +public class Orders implements Serializable { + private static final long serialVersionUID = 1L; + + /** ID */ + private Integer id; + private Integer userId; + private Integer businessId; + private Integer goodsId; + private String orderId; + private Integer addressId; + private Integer num; + private Double price; + private String status; + + private List cartData; + + + private String businessName; + private String goodsName; + private String goodsImg; + private String goodsUnit; + private Double goodsPrice; + private String username; + private String useraddress; + private String phone; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public Integer getBusinessId() { + return businessId; + } + + public void setBusinessId(Integer businessId) { + this.businessId = businessId; + } + + public Integer getGoodsId() { + return goodsId; + } + + public void setGoodsId(Integer goodsId) { + this.goodsId = goodsId; + } + + public String getOrderId() { + return orderId; + } + + public void setOrderId(String orderId) { + this.orderId = orderId; + } + + public Integer getAddressId() { + return addressId; + } + + public void setAddressId(Integer addressId) { + this.addressId = addressId; + } + + public Integer getNum() { + return num; + } + + public void setNum(Integer num) { + this.num = num; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public List getCartData() { + return cartData; + } + + public void setCartData(List cartData) { + this.cartData = cartData; + } + + public String getBusinessName() { + return businessName; + } + + public void setBusinessName(String businessName) { + this.businessName = businessName; + } + + public String getGoodsName() { + return goodsName; + } + + public void setGoodsName(String goodsName) { + this.goodsName = goodsName; + } + + public String getGoodsImg() { + return goodsImg; + } + + public void setGoodsImg(String goodsImg) { + this.goodsImg = goodsImg; + } + + public String getGoodsUnit() { + return goodsUnit; + } + + public void setGoodsUnit(String goodsUnit) { + this.goodsUnit = goodsUnit; + } + + public Double getGoodsPrice() { + return goodsPrice; + } + + public void setGoodsPrice(Double goodsPrice) { + this.goodsPrice = goodsPrice; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getUseraddress() { + return useraddress; + } + + public void setUseraddress(String useraddress) { + this.useraddress = useraddress; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } +} + + diff --git a/OrdersController.java b/OrdersController.java new file mode 100644 index 00000000..1a2af359 --- /dev/null +++ b/OrdersController.java @@ -0,0 +1,87 @@ +package com.example.controller; + +import com.example.common.Result; +import com.example.entity.Orders; +import com.example.service.OrdersService; +import com.github.pagehelper.PageInfo; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 订单前端操作接口 + **/ +@RestController +@RequestMapping("/orders") +public class OrdersController { + + @Resource + private OrdersService ordersService; + + /** + * 新增 + */ + @PostMapping("/add") + public Result add(@RequestBody Orders orders) { + ordersService.add(orders); + return Result.success(); + } + + /** + * 删除 + */ + @DeleteMapping("/delete/{id}") + public Result deleteById(@PathVariable Integer id) { + ordersService.deleteById(id); + return Result.success(); + } + + /** + * 批量删除 + */ + @DeleteMapping("/delete/batch") + public Result deleteBatch(@RequestBody List ids) { + ordersService.deleteBatch(ids); + return Result.success(); + } + + /** + * 修改 + */ + @PutMapping("/update") + public Result updateById(@RequestBody Orders orders) { + ordersService.updateById(orders); + return Result.success(); + } + + /** + * 根据ID查询 + */ + @GetMapping("/selectById/{id}") + public Result selectById(@PathVariable Integer id) { + Orders orders = ordersService.selectById(id); + return Result.success(orders); + } + + /** + * 查询所有 + */ + @GetMapping("/selectAll") + public Result selectAll(Orders orders ) { + List list = ordersService.selectAll(orders); + return Result.success(list); + } + + /** + * 分页查询 + */ + @GetMapping("/selectPage") + public Result selectPage(Orders orders, + @RequestParam(defaultValue = "1") Integer pageNum, + @RequestParam(defaultValue = "10") Integer pageSize) { + PageInfo page = ordersService.selectPage(orders, pageNum, pageSize); + return Result.success(page); + } + +} \ No newline at end of file diff --git a/OrdersMapper.java b/OrdersMapper.java new file mode 100644 index 00000000..b011e9f2 --- /dev/null +++ b/OrdersMapper.java @@ -0,0 +1,39 @@ +package com.example.mapper; + +import com.example.entity.Orders; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * 操作orders相关数据接口 +*/ +public interface OrdersMapper { + + /** + * 新增 + */ + int insert(Orders orders); + + /** + * 删除 + */ + int deleteById(Integer id); + + /** + * 修改 + */ + int updateById(Orders orders); + + /** + * 根据ID查询 + */ + Orders selectById(Integer id); + + /** + * 查询所有 + */ + List selectAll(Orders orders); + +} \ No newline at end of file diff --git a/OrdersService.java b/OrdersService.java new file mode 100644 index 00000000..136fc922 --- /dev/null +++ b/OrdersService.java @@ -0,0 +1,103 @@ +package com.example.service; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ObjectUtil; +import com.example.common.enums.RoleEnum; +import com.example.entity.Account; +import com.example.entity.Cart; +import com.example.entity.Orders; +import com.example.mapper.CartMapper; +import com.example.mapper.OrdersMapper; +import com.example.utils.TokenUtils; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import org.springframework.beans.BeanUtils; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.Date; +import java.util.List; + +/** + * 订单业务处理 + **/ +@Service +public class OrdersService { + + @Resource + private OrdersMapper ordersMapper; + @Resource + private CartMapper cartMapper; + + /** + * 新增 + */ + public void add(Orders orders) { + orders.setOrderId(DateUtil.format(new Date(), "yyyyMMddHHmmss")); + for (Cart cart : orders.getCartData()) { + Orders dbOrders = new Orders(); + BeanUtils.copyProperties(orders, dbOrders); + dbOrders.setGoodsId(cart.getGoodsId()); + dbOrders.setBusinessId(cart.getBusinessId()); + dbOrders.setNum(cart.getNum()); + dbOrders.setPrice(cart.getNum() * cart.getGoodsPrice()); + ordersMapper.insert(dbOrders); + + // 把购物车里对应的商品删掉 + cartMapper.deleteById(cart.getId()); + } + } + + /** + * 删除 + */ + public void deleteById(Integer id) { + ordersMapper.deleteById(id); + } + + /** + * 批量删除 + */ + public void deleteBatch(List ids) { + for (Integer id : ids) { + ordersMapper.deleteById(id); + } + } + + /** + * 修改 + */ + public void updateById(Orders orders) { + ordersMapper.updateById(orders); + } + + /** + * 根据ID查询 + */ + public Orders selectById(Integer id) { + return ordersMapper.selectById(id); + } + + /** + * 查询所有 + */ + public List selectAll(Orders orders) { + return ordersMapper.selectAll(orders); + } + + /** + * 分页查询 + */ + public PageInfo selectPage(Orders orders, Integer pageNum, Integer pageSize) { + Account currentUser = TokenUtils.getCurrentUser(); + if (RoleEnum.USER.name().equals(currentUser.getRole())) { + orders.setUserId(currentUser.getId()); + } + if (RoleEnum.BUSINESS.name().equals(currentUser.getRole())) { + orders.setBusinessId(currentUser.getId()); + } + PageHelper.startPage(pageNum, pageSize); + List list = ordersMapper.selectAll(orders); + return PageInfo.of(list); + } +} \ No newline at end of file diff --git a/Type.java b/Type.java new file mode 100644 index 00000000..c68b9766 --- /dev/null +++ b/Type.java @@ -0,0 +1,51 @@ +package com.example.entity; + +import java.io.Serializable; + +/** + * 商品分类信息表 +*/ +public class Type implements Serializable { + private static final long serialVersionUID = 1L; + + /** ID */ + private Integer id; + /** 分类名称 */ + private String name; + /** 分类描述 */ + private String description; + /** 分类图标 */ + private String img; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getImg() { + return img; + } + + public void setImg(String img) { + this.img = img; + } +} diff --git a/TypeController.java b/TypeController.java new file mode 100644 index 00000000..62ee9395 --- /dev/null +++ b/TypeController.java @@ -0,0 +1,87 @@ +package com.example.controller; + +import com.example.common.Result; +import com.example.entity.Type; +import com.example.service.TypeService; +import com.github.pagehelper.PageInfo; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 公告信息表前端操作接口 + **/ +@RestController +@RequestMapping("/type") +public class TypeController { + + @Resource + private TypeService typeService; + + /** + * 新增 + */ + @PostMapping("/add") + public Result add(@RequestBody Type type) { + typeService.add(type); + return Result.success(); + } + + /** + * 删除 + */ + @DeleteMapping("/delete/{id}") + public Result deleteById(@PathVariable Integer id) { + typeService.deleteById(id); + return Result.success(); + } + + /** + * 批量删除 + */ + @DeleteMapping("/delete/batch") + public Result deleteBatch(@RequestBody List ids) { + typeService.deleteBatch(ids); + return Result.success(); + } + + /** + * 修改 + */ + @PutMapping("/update") + public Result updateById(@RequestBody Type type) { + typeService.updateById(type); + return Result.success(); + } + + /** + * 根据ID查询 + */ + @GetMapping("/selectById/{id}") + public Result selectById(@PathVariable Integer id) { + Type type = typeService.selectById(id); + return Result.success(type); + } + + /** + * 查询所有 + */ + @GetMapping("/selectAll") + public Result selectAll(Type type ) { + List list = typeService.selectAll(type); + return Result.success(list); + } + + /** + * 分页查询 + */ + @GetMapping("/selectPage") + public Result selectPage(Type type, + @RequestParam(defaultValue = "1") Integer pageNum, + @RequestParam(defaultValue = "10") Integer pageSize) { + PageInfo page = typeService.selectPage(type, pageNum, pageSize); + return Result.success(page); + } + +} \ No newline at end of file diff --git a/TypeMapper.java b/TypeMapper.java new file mode 100644 index 00000000..9a6b8bd0 --- /dev/null +++ b/TypeMapper.java @@ -0,0 +1,37 @@ +package com.example.mapper; + +import com.example.entity.Type; + +import java.util.List; + +/** + * 操作type相关数据接口 +*/ +public interface TypeMapper { + + /** + * 新增 + */ + int insert(Type type); + + /** + * 删除 + */ + int deleteById(Integer id); + + /** + * 修改 + */ + int updateById(Type type); + + /** + * 根据ID查询 + */ + Type selectById(Integer id); + + /** + * 查询所有 + */ + List selectAll(Type type); + +} \ No newline at end of file diff --git a/TypeService.java b/TypeService.java new file mode 100644 index 00000000..81c930c4 --- /dev/null +++ b/TypeService.java @@ -0,0 +1,75 @@ +package com.example.service; + + +import com.example.entity.Type; +import com.example.mapper.TypeMapper; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 分类信息表业务处理 + **/ +@Service +public class TypeService { + + @Resource + private TypeMapper typeMapper; + + /** + * 新增 + */ + public void add(Type type) { + typeMapper.insert(type); + } + + /** + * 删除 + */ + public void deleteById(Integer id) { + typeMapper.deleteById(id); + } + + /** + * 批量删除 + */ + public void deleteBatch(List ids) { + for (Integer id : ids) { + typeMapper.deleteById(id); + } + } + + /** + * 修改 + */ + public void updateById(Type type) { + typeMapper.updateById(type); + } + + /** + * 根据ID查询 + */ + public Type selectById(Integer id) { + return typeMapper.selectById(id); + } + + /** + * 查询所有 + */ + public List selectAll(Type type) { + return typeMapper.selectAll(type); + } + + /** + * 分页查询 + */ + public PageInfo selectPage(Type type, Integer pageNum, Integer pageSize) { + PageHelper.startPage(pageNum, pageSize); + List list = typeMapper.selectAll(type); + return PageInfo.of(list); + } + +} \ No newline at end of file