Compare commits
5 Commits
2ffc04318f
...
4cda885a01
Author | SHA1 | Date |
---|---|---|
|
4cda885a01 | |
|
ce01726571 | |
|
5514212ad7 | |
|
f84dca1f1c | |
|
f46e752a40 |
|
@ -1,19 +1,19 @@
|
||||||
package com.sky.controller.user;
|
package com.sky.controller.user;
|
||||||
|
|
||||||
import com.sky.dto.ShoppingCartDTO;
|
import com.sky.dto.ShoppingCartDTO;
|
||||||
|
import com.sky.entity.ShoppingCart;
|
||||||
import com.sky.result.Result;
|
import com.sky.result.Result;
|
||||||
import com.sky.service.ShoppingCartService;
|
import com.sky.service.ShoppingCartService;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/user/shopping")
|
@RequestMapping("/user/shoppingCart")
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Api(tags = "C端购物车相关接口")
|
@Api(tags = "C端购物车相关接口")
|
||||||
public class ShoppingCartController {
|
public class ShoppingCartController {
|
||||||
|
@ -28,10 +28,50 @@ public class ShoppingCartController {
|
||||||
* @return Result
|
* @return Result
|
||||||
*/
|
*/
|
||||||
@ApiOperation("添加购物车")
|
@ApiOperation("添加购物车")
|
||||||
@PostMapping("add")
|
@PostMapping("/add")
|
||||||
public Result<String> add(ShoppingCartDTO shoppingCartDTO) {
|
public Result<String> add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
|
||||||
log.info("添加购物车:{}", shoppingCartDTO);
|
log.info("添加购物车:{}", shoppingCartDTO);
|
||||||
shoppingCartService.addShoppingCart(shoppingCartDTO);
|
shoppingCartService.addShoppingCart(shoppingCartDTO);
|
||||||
return Result.success();
|
return Result.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除购物车一个商品
|
||||||
|
*
|
||||||
|
* @param shoppingCartDTO ShoppingCartDTO
|
||||||
|
* @return Result<String>
|
||||||
|
*/
|
||||||
|
@ApiOperation("删除购物车一个商品")
|
||||||
|
@PostMapping("/sub")
|
||||||
|
public Result<String> sub(@RequestBody ShoppingCartDTO shoppingCartDTO) {
|
||||||
|
log.info("删除购物车一个商品:{}", shoppingCartDTO);
|
||||||
|
shoppingCartService.subShoppingCart(shoppingCartDTO);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查看购物车
|
||||||
|
*
|
||||||
|
* @return 购物车集合
|
||||||
|
*/
|
||||||
|
@ApiOperation("查看购物车")
|
||||||
|
@GetMapping("/list")
|
||||||
|
public Result<List<ShoppingCart>> list() {
|
||||||
|
log.info("查看购物车");
|
||||||
|
List<ShoppingCart> list = shoppingCartService.showShoppingCart();
|
||||||
|
return Result.success(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空购物车
|
||||||
|
*
|
||||||
|
* @return Result<String>
|
||||||
|
*/
|
||||||
|
@ApiOperation("清空购物车")
|
||||||
|
@DeleteMapping("/clean")
|
||||||
|
public Result<String> clean() {
|
||||||
|
log.info("清空购物车");
|
||||||
|
shoppingCartService.clean();
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ import java.util.List;
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface ShoppingCartMapper {
|
public interface ShoppingCartMapper {
|
||||||
/**
|
/**
|
||||||
* 添加购物车
|
* 查看购物车商品
|
||||||
*
|
*
|
||||||
* @param shoppingCart ShoppingCart
|
* @param shoppingCart ShoppingCart
|
||||||
* @return List<ShoppingCart>
|
* @return List<ShoppingCart>
|
||||||
|
@ -28,4 +28,18 @@ public interface ShoppingCartMapper {
|
||||||
* @param shoppingCart ShoppingCart
|
* @param shoppingCart ShoppingCart
|
||||||
*/
|
*/
|
||||||
void insert(ShoppingCart shoppingCart);
|
void insert(ShoppingCart shoppingCart);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空购物车
|
||||||
|
*
|
||||||
|
* @param userId 用户id
|
||||||
|
*/
|
||||||
|
void deleteByUserId(Long userId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id删除购物车内容
|
||||||
|
*
|
||||||
|
* @param id 购物车中id
|
||||||
|
*/
|
||||||
|
void deleteById(Long id);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package com.sky.service;
|
package com.sky.service;
|
||||||
|
|
||||||
import com.sky.dto.ShoppingCartDTO;
|
import com.sky.dto.ShoppingCartDTO;
|
||||||
|
import com.sky.entity.ShoppingCart;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface ShoppingCartService {
|
public interface ShoppingCartService {
|
||||||
/**
|
/**
|
||||||
|
@ -9,4 +12,24 @@ public interface ShoppingCartService {
|
||||||
* @param shoppingCartDTO 购物车参数
|
* @param shoppingCartDTO 购物车参数
|
||||||
*/
|
*/
|
||||||
void addShoppingCart(ShoppingCartDTO shoppingCartDTO);
|
void addShoppingCart(ShoppingCartDTO shoppingCartDTO);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查看购物车
|
||||||
|
*
|
||||||
|
* @return 购物车集合
|
||||||
|
*/
|
||||||
|
List<ShoppingCart> showShoppingCart();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空购物车
|
||||||
|
*/
|
||||||
|
void clean();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除购物车一个商品
|
||||||
|
*
|
||||||
|
* @param shoppingCartDTO 当前购物车信息
|
||||||
|
*/
|
||||||
|
void subShoppingCart(ShoppingCartDTO shoppingCartDTO);
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,9 +64,61 @@ public class ShoppingCartServiceImplImpl implements ShoppingCartService {
|
||||||
shoppingCart.setImage(setmeal.getImage());
|
shoppingCart.setImage(setmeal.getImage());
|
||||||
shoppingCart.setAmount(setmeal.getPrice());
|
shoppingCart.setAmount(setmeal.getPrice());
|
||||||
}
|
}
|
||||||
|
// 因为都是新增的,所以无论是菜品还是套餐都设为1
|
||||||
shoppingCart.setNumber(1);
|
shoppingCart.setNumber(1);
|
||||||
shoppingCart.setCreateTime(LocalDateTime.now());
|
shoppingCart.setCreateTime(LocalDateTime.now());
|
||||||
shoppingCartMapper.insert(shoppingCart);
|
shoppingCartMapper.insert(shoppingCart);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查看购物车
|
||||||
|
*
|
||||||
|
* @return 购物车列表
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<ShoppingCart> showShoppingCart() {
|
||||||
|
// 当前微信用户id
|
||||||
|
Long currentId = BaseContext.getCurrentId();
|
||||||
|
ShoppingCart shoppingCart = ShoppingCart.builder().userId(currentId).build();
|
||||||
|
return shoppingCartMapper.list(shoppingCart);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空购物车
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void clean() {
|
||||||
|
Long currentId = BaseContext.getCurrentId();
|
||||||
|
shoppingCartMapper.deleteByUserId(currentId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除购物车一个商品
|
||||||
|
*
|
||||||
|
* @param shoppingCartDTO 当前购物车信息
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
|
||||||
|
ShoppingCart shoppingCart = new ShoppingCart();
|
||||||
|
BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
|
||||||
|
// 设置当前用户id
|
||||||
|
shoppingCart.setUserId(BaseContext.getCurrentId());
|
||||||
|
List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
|
||||||
|
|
||||||
|
if (shoppingCartList != null && !shoppingCartList.isEmpty()) {
|
||||||
|
// // 当前购物车
|
||||||
|
shoppingCart = shoppingCartList.get(0);
|
||||||
|
Integer number = shoppingCart.getNumber();
|
||||||
|
Long id = shoppingCart.getId();
|
||||||
|
// 如果当前值为1直接删除
|
||||||
|
if (number == 1) {
|
||||||
|
shoppingCartMapper.deleteById(id);
|
||||||
|
} else {
|
||||||
|
// 如果当前值不为1需要将数量减一
|
||||||
|
shoppingCart.setNumber(shoppingCart.getNumber() - 1);
|
||||||
|
shoppingCartMapper.updateNumberById(shoppingCart);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,13 +16,27 @@
|
||||||
where id = #{id};
|
where id = #{id};
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
<!-- 添加购物车 -->
|
<!-- 清空购物车 -->
|
||||||
|
<delete id="deleteByUserId">
|
||||||
|
delete
|
||||||
|
from shopping_cart
|
||||||
|
where user_id = #{userId};
|
||||||
|
</delete>
|
||||||
|
|
||||||
|
<!-- 根据id删除购物车内容 -->
|
||||||
|
<delete id="deleteById">
|
||||||
|
delete
|
||||||
|
from shopping_cart
|
||||||
|
where id = #{id};
|
||||||
|
</delete>
|
||||||
|
|
||||||
|
<!-- 查看购物车商品 -->
|
||||||
<select id="list" resultType="com.sky.entity.ShoppingCart">
|
<select id="list" resultType="com.sky.entity.ShoppingCart">
|
||||||
select *
|
select *
|
||||||
from shopping_cart
|
from shopping_cart
|
||||||
<where>
|
<where>
|
||||||
<if test="userId != null">
|
<if test="userId != null">
|
||||||
and userId = #{userId}
|
and user_id = #{userId}
|
||||||
</if>
|
</if>
|
||||||
<if test="setmealId != null">
|
<if test="setmealId != null">
|
||||||
and setmeal_id = #{setmealId}
|
and setmeal_id = #{setmealId}
|
||||||
|
|
Loading…
Reference in New Issue