实现购物车商品添加和管理
This commit is contained in:
parent
d1f4c3539f
commit
4bdecfd762
@ -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<CartItem> 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<CartItem> list = cartItemService.selectCartItemList(cartItem);
|
||||
ExcelUtil<CartItem> util = new ExcelUtil<CartItem>(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);
|
||||
}
|
||||
}
|
@ -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)
|
||||
{
|
||||
|
108
ruoyi-core/src/main/java/com/ruoyi/system/domain/CartItem.java
Normal file
108
ruoyi-core/src/main/java/com/ruoyi/system/domain/CartItem.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
@ -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<CartItem> 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);
|
||||
}
|
@ -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<CartItem> 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);
|
||||
}
|
@ -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<CartItem> 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);
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
<?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.ruoyi.system.mapper.CartItemMapper">
|
||||
|
||||
<resultMap type="CartItem" id="CartItemResult">
|
||||
<result property="id" column="id" />
|
||||
<result property="uid" column="uid" />
|
||||
<result property="courseId" column="course_id" />
|
||||
<result property="title" column="title" />
|
||||
<result property="price" column="price" />
|
||||
<result property="count" column="count" />
|
||||
</resultMap>
|
||||
|
||||
<sql id="selectCartItemVo">
|
||||
select id, uid, course_id, title, price, count from db_cart_item
|
||||
</sql>
|
||||
|
||||
<select id="selectCartItemList" parameterType="CartItem" resultMap="CartItemResult">
|
||||
<include refid="selectCartItemVo"/>
|
||||
<where>
|
||||
<if test="uid != null "> and uid = #{uid}</if>
|
||||
<if test="courseId != null "> and course_id = #{courseId}</if>
|
||||
<if test="title != null and title != ''"> and title = #{title}</if>
|
||||
<if test="price != null "> and price = #{price}</if>
|
||||
<if test="count != null "> and count = #{count}</if>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<select id="selectCartItemById" parameterType="Long" resultMap="CartItemResult">
|
||||
<include refid="selectCartItemVo"/>
|
||||
where id = #{id}
|
||||
</select>
|
||||
|
||||
<select id="selectCartItem" resultMap="CartItemResult">
|
||||
<include refid="selectCartItemVo"/>
|
||||
where course_id = #{courseId} and uid = #{userId}
|
||||
</select>
|
||||
|
||||
<insert id="insertCartItem" parameterType="CartItem" useGeneratedKeys="true" keyProperty="id">
|
||||
insert into db_cart_item
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="uid != null">uid,</if>
|
||||
<if test="courseId != null">course_id,</if>
|
||||
<if test="title != null">title,</if>
|
||||
<if test="price != null">price,</if>
|
||||
<if test="count != null">count,</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
<if test="uid != null">#{uid},</if>
|
||||
<if test="courseId != null">#{courseId},</if>
|
||||
<if test="title != null">#{title},</if>
|
||||
<if test="price != null">#{price},</if>
|
||||
<if test="count != null">#{count},</if>
|
||||
</trim>
|
||||
</insert>
|
||||
|
||||
<update id="updateCartItem" parameterType="CartItem">
|
||||
update db_cart_item
|
||||
<trim prefix="SET" suffixOverrides=",">
|
||||
<if test="uid != null">uid = #{uid},</if>
|
||||
<if test="courseId != null">course_id = #{courseId},</if>
|
||||
<if test="title != null">title = #{title},</if>
|
||||
<if test="price != null">price = #{price},</if>
|
||||
<if test="count != null">count = #{count},</if>
|
||||
</trim>
|
||||
where id = #{id}
|
||||
</update>
|
||||
|
||||
<delete id="deleteCartItemById" parameterType="Long">
|
||||
delete from db_cart_item where id = #{id}
|
||||
</delete>
|
||||
|
||||
<delete id="deleteCartItemByIds" parameterType="String">
|
||||
delete from db_cart_item where id in
|
||||
<foreach item="id" collection="array" open="(" separator="," close=")">
|
||||
#{id}
|
||||
</foreach>
|
||||
</delete>
|
||||
</mapper>
|
Loading…
x
Reference in New Issue
Block a user