From 4bdecfd7626d294fead2b1b39dc2562e31804877 Mon Sep 17 00:00:00 2001 From: nagocoler Date: Sat, 9 Nov 2024 22:32:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=B4=AD=E7=89=A9=E8=BD=A6?= =?UTF-8?q?=E5=95=86=E5=93=81=E6=B7=BB=E5=8A=A0=E5=92=8C=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/controller/CartItemController.java | 136 ++++++++++++++++++ .../system/controller/CourseController.java | 1 - .../com/ruoyi/system/domain/CartItem.java | 108 ++++++++++++++ .../ruoyi/system/mapper/CartItemMapper.java | 64 +++++++++ .../system/service/ICartItemService.java | 64 +++++++++ .../service/impl/CartItemServiceImpl.java | 100 +++++++++++++ .../mapper/system/CartItemMapper.xml | 81 +++++++++++ 7 files changed, 553 insertions(+), 1 deletion(-) create mode 100644 ruoyi-core/src/main/java/com/ruoyi/system/controller/CartItemController.java create mode 100644 ruoyi-core/src/main/java/com/ruoyi/system/domain/CartItem.java create mode 100644 ruoyi-core/src/main/java/com/ruoyi/system/mapper/CartItemMapper.java create mode 100644 ruoyi-core/src/main/java/com/ruoyi/system/service/ICartItemService.java create mode 100644 ruoyi-core/src/main/java/com/ruoyi/system/service/impl/CartItemServiceImpl.java create mode 100644 ruoyi-core/src/main/resources/mapper/system/CartItemMapper.xml diff --git a/ruoyi-core/src/main/java/com/ruoyi/system/controller/CartItemController.java b/ruoyi-core/src/main/java/com/ruoyi/system/controller/CartItemController.java new file mode 100644 index 0000000..81eeebb --- /dev/null +++ b/ruoyi-core/src/main/java/com/ruoyi/system/controller/CartItemController.java @@ -0,0 +1,136 @@ +package com.ruoyi.system.controller; + +import java.math.BigDecimal; +import java.util.List; + +import com.ruoyi.common.utils.SecurityUtils; +import com.ruoyi.system.domain.Course; +import com.ruoyi.system.service.ICourseService; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import com.ruoyi.common.annotation.Log; +import com.ruoyi.common.core.controller.BaseController; +import com.ruoyi.common.core.domain.AjaxResult; +import com.ruoyi.common.enums.BusinessType; +import com.ruoyi.system.domain.CartItem; +import com.ruoyi.system.service.ICartItemService; +import com.ruoyi.common.utils.poi.ExcelUtil; +import com.ruoyi.common.core.page.TableDataInfo; + +/** + * 购物车商品Controller + * + * @author ruoyi + * @date 2024-11-09 + */ +@RestController +@RequestMapping("/system/item") +public class CartItemController extends BaseController +{ + @Autowired + private ICartItemService cartItemService; + + @Autowired + private ICourseService courseService; + + /** + * 查询购物车商品列表 + */ + @PreAuthorize("@ss.hasPermi('system:item:list')") + @GetMapping("/list") + public TableDataInfo list(CartItem cartItem) + { + startPage(); + List list = cartItemService.selectCartItemList(cartItem); + return getDataTable(list); + } + + /** + * 导出购物车商品列表 + */ + @PreAuthorize("@ss.hasPermi('system:item:export')") + @Log(title = "购物车商品", businessType = BusinessType.EXPORT) + @PostMapping("/export") + public void export(HttpServletResponse response, CartItem cartItem) + { + List list = cartItemService.selectCartItemList(cartItem); + ExcelUtil util = new ExcelUtil(CartItem.class); + util.exportExcel(response, list, "购物车商品数据"); + } + + /** + * 获取购物车商品详细信息 + */ + @PreAuthorize("@ss.hasPermi('system:item:query')") + @GetMapping(value = "/{id}") + public AjaxResult getInfo(@PathVariable("id") Long id) + { + return success(cartItemService.selectCartItemById(id)); + } + + /** + * 新增购物车商品 + */ + @PreAuthorize("@ss.hasPermi('system:item:add')") + @Log(title = "购物车商品", businessType = BusinessType.INSERT) + @PostMapping + public AjaxResult add(@RequestBody CartItem cartItem) + { + return toAjax(cartItemService.insertCartItem(cartItem)); + } + + /** + * 修改购物车商品 + */ + @PreAuthorize("@ss.hasPermi('system:item:edit')") + @Log(title = "购物车商品", businessType = BusinessType.UPDATE) + @PutMapping + public AjaxResult edit(@RequestBody CartItem cartItem) + { + return toAjax(cartItemService.updateCartItem(cartItem)); + } + + /** + * 删除购物车商品 + */ + @PreAuthorize("@ss.hasPermi('system:item:remove')") + @Log(title = "购物车商品", businessType = BusinessType.DELETE) + @DeleteMapping("/{ids}") + public AjaxResult remove(@PathVariable Long[] ids) + { + return toAjax(cartItemService.deleteCartItemByIds(ids)); + } + + /** + * 普通用户添加购物车商品 + */ + @PostMapping("/add/{courseId}") + public AjaxResult addByUser(@PathVariable Integer courseId) { + Course course = courseService.selectCourseById(courseId); + if(course == null) return toAjax(false); + Long userId = SecurityUtils.getUserId(); + CartItem item = cartItemService.selectCartItem(courseId, userId); + if(item == null) { + item = new CartItem(); + item.setUid(SecurityUtils.getUserId()); + item.setPrice(new BigDecimal(course.getPrice())); + item.setTitle(course.getTitle()); + item.setCourseId(courseId.longValue()); + item.setCount(1L); + cartItemService.insertCartItem(item); + } else { + item.setCount(item.getCount() + 1L); + cartItemService.updateCartItem(item); + } + return toAjax(true); + } +} diff --git a/ruoyi-core/src/main/java/com/ruoyi/system/controller/CourseController.java b/ruoyi-core/src/main/java/com/ruoyi/system/controller/CourseController.java index 5aea9e5..d429557 100644 --- a/ruoyi-core/src/main/java/com/ruoyi/system/controller/CourseController.java +++ b/ruoyi-core/src/main/java/com/ruoyi/system/controller/CourseController.java @@ -61,7 +61,6 @@ public class CourseController extends BaseController /** * 获取课程详细信息 */ - @PreAuthorize("@ss.hasPermi('system:course:query')") @GetMapping(value = "/{id}") public AjaxResult getInfo(@PathVariable("id") Integer id) { diff --git a/ruoyi-core/src/main/java/com/ruoyi/system/domain/CartItem.java b/ruoyi-core/src/main/java/com/ruoyi/system/domain/CartItem.java new file mode 100644 index 0000000..c48456f --- /dev/null +++ b/ruoyi-core/src/main/java/com/ruoyi/system/domain/CartItem.java @@ -0,0 +1,108 @@ +package com.ruoyi.system.domain; + +import java.math.BigDecimal; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import com.ruoyi.common.annotation.Excel; +import com.ruoyi.common.core.domain.BaseEntity; + +/** + * 购物车商品对象 db_cart_item + * + * @author ruoyi + * @date 2024-11-09 + */ +public class CartItem extends BaseEntity +{ + private static final long serialVersionUID = 1L; + + /** 商品ID */ + private Long id; + + /** 用户ID */ + @Excel(name = "用户ID") + private Long uid; + + /** 课程ID */ + @Excel(name = "课程ID") + private Long courseId; + + /** 课程标题 */ + @Excel(name = "课程标题") + private String title; + + /** 价格 */ + @Excel(name = "价格") + private BigDecimal price; + + /** 数量 */ + @Excel(name = "数量") + private Long count; + + public void setId(Long id) + { + this.id = id; + } + + public Long getId() + { + return id; + } + public void setUid(Long uid) + { + this.uid = uid; + } + + public Long getUid() + { + return uid; + } + public void setCourseId(Long courseId) + { + this.courseId = courseId; + } + + public Long getCourseId() + { + return courseId; + } + public void setTitle(String title) + { + this.title = title; + } + + public String getTitle() + { + return title; + } + public void setPrice(BigDecimal price) + { + this.price = price; + } + + public BigDecimal getPrice() + { + return price; + } + public void setCount(Long count) + { + this.count = count; + } + + public Long getCount() + { + return count; + } + + @Override + public String toString() { + return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) + .append("id", getId()) + .append("uid", getUid()) + .append("courseId", getCourseId()) + .append("title", getTitle()) + .append("price", getPrice()) + .append("count", getCount()) + .toString(); + } +} diff --git a/ruoyi-core/src/main/java/com/ruoyi/system/mapper/CartItemMapper.java b/ruoyi-core/src/main/java/com/ruoyi/system/mapper/CartItemMapper.java new file mode 100644 index 0000000..8d6f559 --- /dev/null +++ b/ruoyi-core/src/main/java/com/ruoyi/system/mapper/CartItemMapper.java @@ -0,0 +1,64 @@ +package com.ruoyi.system.mapper; + +import java.util.List; +import com.ruoyi.system.domain.CartItem; +import com.ruoyi.system.domain.Course; + +/** + * 购物车商品Mapper接口 + * + * @author ruoyi + * @date 2024-11-09 + */ +public interface CartItemMapper +{ + /** + * 查询购物车商品 + * + * @param id 购物车商品主键 + * @return 购物车商品 + */ + public CartItem selectCartItemById(Long id); + + CartItem selectCartItem(Integer courseId, Long userId); + + /** + * 查询购物车商品列表 + * + * @param cartItem 购物车商品 + * @return 购物车商品集合 + */ + public List selectCartItemList(CartItem cartItem); + + /** + * 新增购物车商品 + * + * @param cartItem 购物车商品 + * @return 结果 + */ + public int insertCartItem(CartItem cartItem); + + /** + * 修改购物车商品 + * + * @param cartItem 购物车商品 + * @return 结果 + */ + public int updateCartItem(CartItem cartItem); + + /** + * 删除购物车商品 + * + * @param id 购物车商品主键 + * @return 结果 + */ + public int deleteCartItemById(Long id); + + /** + * 批量删除购物车商品 + * + * @param ids 需要删除的数据主键集合 + * @return 结果 + */ + public int deleteCartItemByIds(Long[] ids); +} diff --git a/ruoyi-core/src/main/java/com/ruoyi/system/service/ICartItemService.java b/ruoyi-core/src/main/java/com/ruoyi/system/service/ICartItemService.java new file mode 100644 index 0000000..328b648 --- /dev/null +++ b/ruoyi-core/src/main/java/com/ruoyi/system/service/ICartItemService.java @@ -0,0 +1,64 @@ +package com.ruoyi.system.service; + +import java.util.List; +import com.ruoyi.system.domain.CartItem; +import com.ruoyi.system.domain.Course; + +/** + * 购物车商品Service接口 + * + * @author ruoyi + * @date 2024-11-09 + */ +public interface ICartItemService +{ + /** + * 查询购物车商品 + * + * @param id 购物车商品主键 + * @return 购物车商品 + */ + public CartItem selectCartItemById(Long id); + + /** + * 查询购物车商品列表 + * + * @param cartItem 购物车商品 + * @return 购物车商品集合 + */ + public List selectCartItemList(CartItem cartItem); + + /** + * 新增购物车商品 + * + * @param cartItem 购物车商品 + * @return 结果 + */ + public int insertCartItem(CartItem cartItem); + + public CartItem selectCartItem(Integer courseId, Long userId); + + /** + * 修改购物车商品 + * + * @param cartItem 购物车商品 + * @return 结果 + */ + public int updateCartItem(CartItem cartItem); + + /** + * 批量删除购物车商品 + * + * @param ids 需要删除的购物车商品主键集合 + * @return 结果 + */ + public int deleteCartItemByIds(Long[] ids); + + /** + * 删除购物车商品信息 + * + * @param id 购物车商品主键 + * @return 结果 + */ + public int deleteCartItemById(Long id); +} diff --git a/ruoyi-core/src/main/java/com/ruoyi/system/service/impl/CartItemServiceImpl.java b/ruoyi-core/src/main/java/com/ruoyi/system/service/impl/CartItemServiceImpl.java new file mode 100644 index 0000000..57ffb02 --- /dev/null +++ b/ruoyi-core/src/main/java/com/ruoyi/system/service/impl/CartItemServiceImpl.java @@ -0,0 +1,100 @@ +package com.ruoyi.system.service.impl; + +import java.util.List; + +import com.ruoyi.system.domain.Course; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import com.ruoyi.system.mapper.CartItemMapper; +import com.ruoyi.system.domain.CartItem; +import com.ruoyi.system.service.ICartItemService; + +/** + * 购物车商品Service业务层处理 + * + * @author ruoyi + * @date 2024-11-09 + */ +@Service +public class CartItemServiceImpl implements ICartItemService +{ + @Autowired + private CartItemMapper cartItemMapper; + + /** + * 查询购物车商品 + * + * @param id 购物车商品主键 + * @return 购物车商品 + */ + @Override + public CartItem selectCartItemById(Long id) + { + return cartItemMapper.selectCartItemById(id); + } + + @Override + public CartItem selectCartItem(Integer courseId, Long userId) { + return cartItemMapper.selectCartItem(courseId, userId); + } + + /** + * 查询购物车商品列表 + * + * @param cartItem 购物车商品 + * @return 购物车商品 + */ + @Override + public List selectCartItemList(CartItem cartItem) + { + return cartItemMapper.selectCartItemList(cartItem); + } + + /** + * 新增购物车商品 + * + * @param cartItem 购物车商品 + * @return 结果 + */ + @Override + public int insertCartItem(CartItem cartItem) + { + return cartItemMapper.insertCartItem(cartItem); + } + + /** + * 修改购物车商品 + * + * @param cartItem 购物车商品 + * @return 结果 + */ + @Override + public int updateCartItem(CartItem cartItem) + { + return cartItemMapper.updateCartItem(cartItem); + } + + /** + * 批量删除购物车商品 + * + * @param ids 需要删除的购物车商品主键 + * @return 结果 + */ + @Override + public int deleteCartItemByIds(Long[] ids) + { + return cartItemMapper.deleteCartItemByIds(ids); + } + + /** + * 删除购物车商品信息 + * + * @param id 购物车商品主键 + * @return 结果 + */ + @Override + public int deleteCartItemById(Long id) + { + return cartItemMapper.deleteCartItemById(id); + } +} diff --git a/ruoyi-core/src/main/resources/mapper/system/CartItemMapper.xml b/ruoyi-core/src/main/resources/mapper/system/CartItemMapper.xml new file mode 100644 index 0000000..9e446cc --- /dev/null +++ b/ruoyi-core/src/main/resources/mapper/system/CartItemMapper.xml @@ -0,0 +1,81 @@ + + + + + + + + + + + + + + + select id, uid, course_id, title, price, count from db_cart_item + + + + + + + + + + insert into db_cart_item + + uid, + course_id, + title, + price, + count, + + + #{uid}, + #{courseId}, + #{title}, + #{price}, + #{count}, + + + + + update db_cart_item + + uid = #{uid}, + course_id = #{courseId}, + title = #{title}, + price = #{price}, + count = #{count}, + + where id = #{id} + + + + delete from db_cart_item where id = #{id} + + + + delete from db_cart_item where id in + + #{id} + + + \ No newline at end of file