|
@@ -0,0 +1,79 @@
|
|
|
+package com.zhentao.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.zhentao.controller.cart.dto.CartDto;
|
|
|
+import com.zhentao.controller.cart.dto.CartDtoId;
|
|
|
+import com.zhentao.domain.CourseDatails;
|
|
|
+import com.zhentao.domain.Courses;
|
|
|
+import com.zhentao.domain.ShoppingCart;
|
|
|
+import com.zhentao.dto.Result;
|
|
|
+import com.zhentao.service.CoursesService;
|
|
|
+import com.zhentao.service.ShoppingCartService;
|
|
|
+import com.zhentao.mapper.ShoppingCartMapper;
|
|
|
+import org.checkerframework.checker.units.qual.A;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+* @author 86183
|
|
|
+* @description 针对表【shopping_cart(购物车表)】的数据库操作Service实现
|
|
|
+* @createDate 2025-05-13 17:11:39
|
|
|
+*/
|
|
|
+@Service
|
|
|
+public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart>
|
|
|
+ implements ShoppingCartService{
|
|
|
+ @Autowired
|
|
|
+ private CoursesService service;
|
|
|
+ @Autowired
|
|
|
+ private ShoppingCartMapper shoppingCartMapper;
|
|
|
+// 查询所有的购物车物品
|
|
|
+ @Override
|
|
|
+ public Result findAll(CartDtoId cartDtoId) {
|
|
|
+ QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("user_id",cartDtoId.getUserId());
|
|
|
+ List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(queryWrapper);
|
|
|
+ List<Courses> list = service.list();
|
|
|
+ for (ShoppingCart s:shoppingCarts) {
|
|
|
+ for (Courses c:list) {
|
|
|
+ if (s.getCourseDatailsId().equals(c.getCourseId())){
|
|
|
+ s.setCourses(c);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Result.OK("查询成功",shoppingCarts);
|
|
|
+ }
|
|
|
+// 添加物品
|
|
|
+ @Override
|
|
|
+ public Result add(CartDto cartDto) {
|
|
|
+ QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("course_datails_id",cartDto.getCourseId());
|
|
|
+ queryWrapper.eq("user_id",cartDto.getUserId());
|
|
|
+ ShoppingCart one = this.getOne(queryWrapper);
|
|
|
+ if (one==null){
|
|
|
+ // 先写一个对象
|
|
|
+ ShoppingCart shoppingCart = new ShoppingCart();
|
|
|
+ long l = IdUtil.getSnowflake(1, 1).nextId();
|
|
|
+ shoppingCart.setCartId(l);
|
|
|
+ shoppingCart.setUserId(cartDto.getUserId());
|
|
|
+ shoppingCart.setCourseDatailsId(cartDto.getCourseId());
|
|
|
+ shoppingCart.setAddTime(new Date());
|
|
|
+ shoppingCart.setUpdateTime(new Date());
|
|
|
+ boolean save = this.save(shoppingCart);
|
|
|
+ if (save){
|
|
|
+ return Result.OK("添加成功",save);
|
|
|
+ }
|
|
|
+ return Result.ERR("添加失败",null);
|
|
|
+ }else {
|
|
|
+ return Result.ERR("该商品已添加",null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|