您的位置:首页 > 文旅 > 美景 > 重庆宣传片2023_中国公司查询网站_百度推广获客方法_任务放单平台

重庆宣传片2023_中国公司查询网站_百度推广获客方法_任务放单平台

2024/12/22 23:18:26 来源:https://blog.csdn.net/qq_73340809/article/details/142741132  浏览:    关键词:重庆宣传片2023_中国公司查询网站_百度推广获客方法_任务放单平台
重庆宣传片2023_中国公司查询网站_百度推广获客方法_任务放单平台

文章目录

  • 二. 购物车功能
    • 添加购物车
      • ShoppingCartController
      • ShoppingCartService
      • ShoppingCartServiceImpl
      • ShoppingCartMapper
      • ShoppingCartMapper
      • application
    • 查看购物车
      • ShoppingCartController
      • ShoppingCartService
      • ShoppingCartServiceImpl
    • 清空购物车
      • ShoppingCartController
      • ShoppingCartService
      • ShoppingCartServiceImpl
    • 删除购物车中一个商品
      • ShoppingCartController
      • ShoppingCartService
      • ShoppingCartServiceImpl

二. 购物车功能

添加购物车

ShoppingCartController

package com.sky.controller.user;import com.sky.dto.ShoppingCartDTO;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author Jie.* @description: TODO* @date 2024/10/7* @version: 1.0*/
@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "购物车")
@Slf4j
public class ShoppingCartController {@Autowiredprivate ShoppingCartService shoppingCartService;/*** 添加购物车*/@PostMapping("add")@ApiOperation(value = "添加购物车")public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) {log.info("添加购物车:{}", shoppingCartDTO);shoppingCartService.addShoppingCart(shoppingCartDTO);return Result.success();}
}

ShoppingCartService

package com.sky.service;import com.sky.dto.ShoppingCartDTO;/*** @author Jie.* @description: TODO* @date 2024/10/7* @version: 1.0*/
public interface ShoppingCartService {/*** 添加购物车*/void addShoppingCart(ShoppingCartDTO shoppingCartDTO);
}

ShoppingCartServiceImpl

package com.sky.service.impl;import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.List;/*** @author Jie.* @description: TODO* @date 2024/10/7* @version: 1.0*/
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {@Autowiredprivate ShoppingCartMapper shoppingCartMapper;@Autowiredprivate DishMapper dishMapper;@Autowiredprivate SetmealMapper setmealMapper;/*** 添加购物车*/@Overridepublic void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {//判断加入购物车的商品是否已经存在ShoppingCart shoppingCart = new ShoppingCart();BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);Long currentId = BaseContext.getCurrentId();shoppingCart.setUserId(currentId);//查询购物车列表List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);//如果存在,修改商品数量if (list != null && !list.isEmpty()) {ShoppingCart cart = list.get(0);cart.setNumber(cart.getNumber() + 1);//数量加1shoppingCartMapper.updateById(cart);}//如果不存在,添加商品else {//判断是菜品还是套餐Long dishId = shoppingCartDTO.getDishId();if (dishId != null) {//菜品Dish dish = dishMapper.selectById(dishId);shoppingCart.setName(dish.getName());shoppingCart.setImage(dish.getImage());shoppingCart.setAmount(dish.getPrice());} else {//套餐Long setmealId = shoppingCartDTO.getSetmealId();Setmeal setmeal = setmealMapper.selectById(setmealId);shoppingCart.setName(setmeal.getName());shoppingCart.setImage(setmeal.getImage());shoppingCart.setAmount(setmeal.getPrice());}shoppingCart.setNumber(1);shoppingCart.setCreateTime(LocalDateTime.now());shoppingCartMapper.insert(shoppingCart);}}
}

ShoppingCartMapper

package com.sky.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sky.entity.ShoppingCart;
import org.apache.ibatis.annotations.Mapper;import java.util.List;/*** @author Jie.* @description: TODO* @date 2024/10/7* @version: 1.0*/
@Mapper
public interface ShoppingCartMapper extends BaseMapper<ShoppingCart> {/*** 查询购物车列表*/List<ShoppingCart> list(ShoppingCart shoppingCart);
}

ShoppingCartMapper

<?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.sky.mapper.ShoppingCartMapper"><select id="list" resultType="com.sky.entity.ShoppingCart">SELECT * FROM shopping_cart<where><if test="userId != null">AND user_id = #{userId}</if><if test="setmealId != null">AND setmeal_id = #{setmealId}</if><if test="dishId != null">AND dish_id = #{dishId}</if><if test="dishFlavor != null">AND dish_flavor = #{dishFlavor}</if></where></select>
</mapper>

application

server:port: 8080spring:profiles:active: devmain:allow-circular-references: truedatasource:druid:driver-class-name: ${sky.datasource.driver-class-name}url: jdbc:mysql://${sky.datasource.host}:${sky.datasource.port}/${sky.datasource.database}?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=trueusername: ${sky.datasource.username}password: ${sky.datasource.password}redis:host: ${sky.redis.host}post: ${sky.redis.post}database: ${sky.redis.database}# mybatis-plus配置
mybatis-plus:configuration:# 驼峰命名map-underscore-to-camel-case: true# 日志log-impl: org.apache.ibatis.logging.stdout.StdOutImplglobal-config:db-config:id-type: automapper-locations: classpath:mapper/*.xml# 日志配置
logging:level:com:sky:mapper: debugservice: infocontroller: infosky:jwt:# 设置jwt签名加密时使用的秘钥admin-secret-key: itcast# 设置jwt过期时间admin-ttl: 720000000# 设置前端传递过来的令牌名称admin-token-name: token# 设置jwt签名加密时使用的秘钥user-secret-key: itheima# 设置jwt过期时间user-ttl: 720000000# 设置前端传递过来的令牌名称user-token-name: authenticationalioss:endpoint: ${sky.alioss.endpoint}access-key-id: ${sky.alioss.access-key-id}access-key-secret: ${sky.alioss.access-key-secret}bucket-name: ${sky.alioss.bucket-name}wechat:appid: ${sky.wechat.appid}secret: ${sky.wechat.secret}

查看购物车

ShoppingCartController

 /*** 查看购物车*/@GetMapping("list")@ApiOperation(value = "查看购物车")public Result<List<ShoppingCart>> list() {List<ShoppingCart> list = shoppingCartService.showShoppingCart();return Result.success(list);}

ShoppingCartService

 /*** 查看购物车*/List<ShoppingCart> showShoppingCart();

ShoppingCartServiceImpl

  /*** 查看购物车*/@Overridepublic List<ShoppingCart> showShoppingCart() {Long currentId = BaseContext.getCurrentId();ShoppingCart shoppingCart = ShoppingCart.builder().userId(currentId).build();List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);return list;}

清空购物车

ShoppingCartController

    /*** 清空购物车*/@DeleteMapping("clean")@ApiOperation(value = "清空购物车")public Result clean() {shoppingCartService.cleanShoppingCart();return Result.success();}

ShoppingCartService

 /*** 清空购物车*/void cleanShoppingCart();

ShoppingCartServiceImpl

   /*** 清空购物车*/@Overridepublic void cleanShoppingCart() {Long currentId = BaseContext.getCurrentId();LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();wrapper.eq(ShoppingCart::getUserId, currentId);shoppingCartMapper.delete(wrapper);}

删除购物车中一个商品

ShoppingCartController

    /*** 删除购物车中一个商品*/@PostMapping("/sub")public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO) {log.info("删除购物车中一个商品:{}", shoppingCartDTO);shoppingCartService.subShoppingCart(shoppingCartDTO);return Result.success();}

ShoppingCartService

 /*** 删除购物车商品*/void subShoppingCart(ShoppingCartDTO shoppingCartDTO);

ShoppingCartServiceImpl

   /*** 删除购物车商品*/@Overridepublic void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {ShoppingCart shoppingCart = new ShoppingCart();BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);Long currentId = BaseContext.getCurrentId();shoppingCart.setUserId(currentId);List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);if (list != null && !list.isEmpty()) {ShoppingCart cart = list.get(0);if (cart.getNumber() > 1) {cart.setNumber(cart.getNumber() - 1);shoppingCartMapper.updateById(cart);} else {shoppingCartMapper.deleteById(cart.getId());}}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com