当前位置: 首页 > news >正文

Day04_苍穹外卖——套餐管理(实战)

目录

  • 根据分类id查询菜品
    • DishController
    • DishService
    • DishServiceImpl
    • DishMapper
  • 新增套餐
    • 接口设计
    • 代码实现
      • SetmealController
      • SetmealService
      • SetmealServiceImpl
      • SetmealMapper
      • SetmealDishMapper
      • SetmealDishMapper.xml
  • 分页查询
    • 接口设计
    • 代码实现
      • SetmealController
      • SetmealService
      • SetmealServiceImpl
      • SetmealMapper
      • SetmealMapper.xml
  • 根据id查询套餐
    • 接口设计
    • 代码实现
      • SetmealController
      • SetmealService
      • SetmealServiceImpl
      • SetmealMapper
      • SetmealMapper.xml
  • 修改套餐
    • 接口设计
    • 代码实现
      • SetmealController
      • SetmealService
      • SetmealServiceImpl
      • SetmealMapper
      • SetmealMapper.xml
    • SetmealDishMapper
  • 套餐起售、停售
    • 接口设计
    • 代码实现
      • SetmealController
      • SetmealService
      • SetmealServiceImpl
      • DishMapper
  • 批量删除套餐
    • 接口设计
    • 代码实现
      • Controller
      • Service
      • ServiceImpl
      • Mapper

根据分类id查询菜品

在新增套餐的时候,会根据分类id去选择菜品

DishController

    @GetMapping("/list")@ApiOperation("根据分类id查询菜品")public Result<List<Dish>> list(Long categoryId){log.info("根据分类id 查询菜品:{}",categoryId);List<Dish> dishList=dishService.list(categoryId);return Result.success(dishList);}

DishService

    @GetMapping("/list")@ApiOperation("根据分类id查询菜品")public Result<List<Dish>> list(Long categoryId){log.info("根据分类id 查询菜品:{}",categoryId);List<Dish> dishList=dishService.list(categoryId);return Result.success(dishList);}

DishServiceImpl

@Overridepublic List<Dish> list(Long categoryId) {Dish dish = Dish.builder().categoryId(categoryId).status(1).build();return dishMapper.list(dish);}

DishMapper

@Select("select * from dish where status = 1 and category_id = #{categoryId}")List<Dish> list(Dish dish);

新增套餐

接口设计

在这里插入图片描述
在这里插入图片描述

代码实现

SetmealController

@PostMapping@ApiOperation("新增套餐")public Result save(@RequestBody SetmealDTO setmealDTO){log.info("新增套餐:{}", setmealDTO);setmealService.save(setmealDTO);return Result.success();}

SetmealService

void save(SetmealDTO setmealDTO);

SetmealServiceImpl

	@Override@Transactionalpublic void save(SetmealDTO setmealDTO) {//1.构造套餐基本信息,保存数据到套餐setmeal表中Setmeal setmeal = new Setmeal();BeanUtils.copyProperties(setmealDTO, setmeal);setmeal.setStatus(StatusConstant.DISABLE);setmealMapper.insert(setmeal);//2.构造套餐菜品信息,保存数据到套餐菜品关系表setmeal_dish中List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();setmealDishes.forEach(setmealDish -> {//添加当前菜品的套餐idsetmealDish.setSetmealId(setmeal.getId());});setmealDishMapper.insertBatch(setmealDishes);}

SetmealMapper

	@AutoFill(value = OperationType.INSERT)@Options(useGeneratedKeys = true, keyProperty = "id")   //获取主键id值@Insert("insert into setmeal values (null, #{categoryId}, #{name}, #{price},  #{status}, #{description}, #{image}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})")void insert(Setmeal setmeal);

SetmealDishMapper

void insertBatch(List<SetmealDish> setmealDishes);

SetmealDishMapper.xml

    <insert id="insertBatch">insert into setmeal_dish values<foreach collection="setmealDishes" item="sd" separator=",">(null,#{sd.setmealId},#{sd.dishId},#{sd.name},#{sd.price},#{sd.copies})</foreach></insert>

分页查询

接口设计

在这里插入图片描述

代码实现

SetmealController

@RequestMapping("/page")public Result page(SetmealPageQueryDTO setmealPageQueryDTO) {log.info("分页查询套餐信息", setmealPageQueryDTO);PageResult pageResult = setmealService.page(setmealPageQueryDTO);return Result.success(pageResult);}

SetmealService

PageResult page(SetmealPageQueryDTO setmealPageQueryDTO);

SetmealServiceImpl

@Overridepublic PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {//1.设置分页参数PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());//2.调用mapper方法,强转为 PagePage<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);//3.封装PageResult并返回return new PageResult(page.getTotal(), page.getResult());}

SetmealMapper

Page<SetmealVO> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO);

SetmealMapper.xml

<select id="pageQuery" resultType="com.sky.vo.SetmealVO">select s.*,c.name as categoryName from setmeal s left join category c on s.category_id = c.id<where><if test="name != null and name != ''">and d.name like concat('%',#{name},'%')</if><if test="categoryId != null">and d.category_id = #{categoryId}</if><if test="status != null">and d.status = #{status}</if></where>order by s.create_time desc</select>

根据id查询套餐

接口设计

在这里插入图片描述
在这里插入图片描述

代码实现

SetmealController

@GetMapping("/{id}")@ApiOperation("根据id查询套餐")public Result<SetmealVO> getById(@PathVariable Long id) {log.info("根据id查询套餐,id为{}", id);SetmealVO setmealVO = setmealService.getByIdWithDish(id);return Result.success(setmealVO);}

SetmealService

SetmealVO getByIdWithDish(Long id);

SetmealServiceImpl

@Overridepublic SetmealVO getByIdWithDish(Long id) {SetmealVO setmealVO = setmealMapper.getByIdWithDish(id);return setmealVO;}

SetmealMapper

SetmealVO getByIdWithDish(Long id);

SetmealMapper.xml

    <!--根据id查询套餐信息(回显)--><!--结果集映射 autoMapping设置为true,代表完成结果的自动映射,并自动忽略大小写,驼峰命名规则--><resultMap id="setmealAndDishMap" type="com.sky.vo.SetmealVO" autoMapping="true"><result column="id" property="id"/> <!--主键映射--><!-- 结果映射 --><!--collection:配置一对多规则,将查询出来的每条数据,封装到SetmealDish对象中,然后将对象添加到list集合setmealDishes中属性property:表示要将结果封装到对象的哪个属性中属性ofType:指定要将结果封装到什么对象中collection封装规则:result:配置表字段与属性字段映射关系,coloum表示表字段,property表示属性字段--><collection property="setmealDishes" ofType="com.sky.entity.SetmealDish"><result column="sd_id" property="id"/><result column="setmeal_id" property="setmealId"/><result column="dish_id" property="dishId"/><result column="sd_name" property="name"/><result column="sd_price" property="price"/><result column="copies" property="copies"/></collection></resultMap><!--查询套餐及套餐菜品关系数据--><select id="getByIdWithDish" parameterType="long" resultMap="setmealAndDishMap">select a.*,b.id    sd_id,b.setmeal_id,b.dish_id,b.name  sd_name,b.price sd_price,b.copiesfrom setmeal aleft joinsetmeal_dish bona.id = b.setmeal_idwhere a.id = #{id}</select>

修改套餐

接口设计

在这里插入图片描述
在这里插入图片描述

代码实现

SetmealController

@PutMapping@ApiOperation("修改套餐")public Result update(@RequestBody SetmealDTO setmealDTO) {log.info("修改套餐:{}", setmealDTO);setmealService.update(setmealDTO);return Result.success();}

SetmealService

void update(SetmealDTO setmealDTO);

SetmealServiceImpl

    @Transactional@Overridepublic void update(SetmealDTO setmealDTO) {Setmeal setmeal = new Setmeal();BeanUtils.copyProperties(setmealDTO,setmeal);//修改套餐表,执行updatesetmealMapper.update(setmeal);//套餐idLong setmealId = setmealDTO.getId();//删除套餐和菜品的关联关系,操作setmeal_dish表,执行deletesetmealDishMapper.deleteBySetmealId(setmealId);List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();setmealDishes.forEach(setmealDish -> {setmealDish.setSetmealId(setmealId);});//重新插入套餐和菜品的关联关系,操作setmeal_dish表,执行insertsetmealDishMapper.insertBatch(setmealDishes);}

SetmealMapper

    @AutoFill(OperationType.UPDATE)void update(Setmeal setmeal);

SetmealMapper.xml

<!--修改套餐--><update id="update">update setmeal<set><if test="categoryId != null">category_id = #{categoryId},</if><if test="description != null">description = #{description},</if><if test="image != null">image       = #{image},</if><if test="name != null">name        = #{name},</if><if test="price != null">price       = #{price},</if><if test="status != null">status      = #{status},</if><if test="updateTime != null">update_time      = #{updateTime},</if><if test="updateUser != null">update_user      = #{updateUser}</if></set>where id = #{id}</update>

SetmealDishMapper

	@Delete("delete from setmeal_dish where setmeal_id = #{setmealId}")void deleteBySetmealId(Long setmealId);

套餐起售、停售

接口设计

在这里插入图片描述

代码实现

SetmealController

@PostMapping("/status/{status}")@ApiOperation("起售、停售套餐")public Result startOrStop(@PathVariable Integer status, Long id) {log.info("起售、停售套餐:status={},id={}", status,id);setmealService.startOrStop(status, id);return Result.success();}

SetmealService

void startOrStop(Integer status, Long id);

SetmealServiceImpl

@Overridepublic void startOrStop(Integer status, Long id) {//起售套餐时,判断套餐内是否有停售菜品,有停售菜品提示"套餐内包含未启售菜品,无法启售"if (status.equals(StatusConstant.ENABLE)) {//select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = ?List<Dish> dishList = dishMapper.getBySetmealId(id);if (!dishList.isEmpty()) {dishList.forEach(dish -> {if (StatusConstant.DISABLE.equals(dish.getStatus())) {throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);}});}}Setmeal setmeal = Setmeal.builder().id(id).status(status).build();setmealMapper.update(setmeal);}

DishMapper

	@Select("select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = #{setmealId}")List<Dish> getBySetmealId(Long setmealId);

批量删除套餐

接口设计

在这里插入图片描述

代码实现

Controller

@DeleteMapping@ApiOperation("批量删除套餐")public Result delete(@RequestParam List<Long> ids) {log.info("批量删除套餐:{}", ids);setmealService.delete(ids);return Result.success();}

Service

void delete(List<Long> ids);

ServiceImpl

@Override@Transactionalpublic void delete(List<Long> ids) {//1.起售中的套餐不能删除ids.forEach(id -> {Setmeal setmeal = setmealMapper.getById(id);if (setmeal.getStatus().equals(StatusConstant.ENABLE)){throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);}});//2.删除套餐的同时,需要将套餐菜品关系数据删除ids.forEach(setmealId -> {//删除套餐表中的数据setmealMapper.deleteById(setmealId);//删除套餐菜品关系表中的数据setmealDishMapper.deleteBySetmealId(setmealId);});}

Mapper

/*** 根据id查询套餐* @param id*/@Select("select * from setmeal where id = #{id}")Setmeal getById(Long id);/*** 根据id删除套餐* @param setmealId*/@Delete("delete from setmeal where id = #{setmealId}")void deleteById(Long setmealId);


文章转载自:

http://SorUuv0S.hpxxq.cn
http://gmd0xj8i.hpxxq.cn
http://2EHGQqNL.hpxxq.cn
http://ScfhMMMt.hpxxq.cn
http://1cpVvf20.hpxxq.cn
http://3A7KOhZp.hpxxq.cn
http://vJbP1IUw.hpxxq.cn
http://SPdonemU.hpxxq.cn
http://wJlYOlZu.hpxxq.cn
http://Nby3W1ZQ.hpxxq.cn
http://8kI1Owuq.hpxxq.cn
http://VdJn6wM4.hpxxq.cn
http://cwkyWywb.hpxxq.cn
http://LnfKxE1M.hpxxq.cn
http://mOBHcHMh.hpxxq.cn
http://8X6wD66Y.hpxxq.cn
http://1vAq4OBE.hpxxq.cn
http://OT5iqeOq.hpxxq.cn
http://M377iT3u.hpxxq.cn
http://5j4W68XX.hpxxq.cn
http://Hn3NFfR4.hpxxq.cn
http://WSIHJd3O.hpxxq.cn
http://f76z7Eya.hpxxq.cn
http://G75tzPNn.hpxxq.cn
http://dySKNYdR.hpxxq.cn
http://UEI8q8iP.hpxxq.cn
http://AGS7QEIr.hpxxq.cn
http://MsK84AJf.hpxxq.cn
http://UjJbzerQ.hpxxq.cn
http://NcGpHXV9.hpxxq.cn
http://www.dtcms.com/a/374757.html

相关文章:

  • ElementUI 组件概览
  • fifo之读写指针
  • 【第三次全国土壤普查】一键制备土壤三普环境变量23项遥感植被指数神器
  • Java反射机制详解
  • PDF文件中的广告二维码图片该怎么批量删除
  • 记一次 .NET 某中医药附属医院门诊系统 崩溃分析
  • WPF/Prism 中计算属性的通知机制详解 —— SetProperty 与 RaisePropertyChanged
  • jmeter使用指南
  • 硬件(六)arm指令
  • 后端错误处理的艺术:BusinessException 与 ResultUtils 的完美分工
  • MCU、CPLD、DSP、FPGA 有什么区别,该如何选择?
  • 【React Native】点赞特效动画组件FlowLikeView
  • android studio gradle 访问不了
  • 【C++】C++11 篇二
  • Kubernetes 配置检查与发布安全清单
  • Perforce Klocwork 2025.2版本更新:默认启用现代分析引擎、支持 MISRA C:2025 新规、CI构建性能提升等
  • 工业总线协议转换核心:SG-DP_MOD-110 Profibus-DP 转 Modbus-RTU 网关,打通异构设备数据链路
  • Win系统下配置PCL库第三步之链接库的路径(超详细)
  • 【远程运维】Linux 远程连接 Windows 好用的软件:MobaXterm 实战指南
  • Java入门级教程13-多线程同步安全机制synchronized(内置锁)、JavaMail发送电子邮箱、爬取CSDN到邮箱、备份数据库
  • 玩转Docker | 使用Docker部署KissLists任务管理工具
  • STL库——map/set(类函数学习)
  • STM32 串口接收数据包(自定义帧头帧尾)
  • 正向代理,反向代理,负载均衡还有nginx
  • 用户态与内核态的深度解析:安全、效率与优化之道
  • 搭建本地gitea服务器
  • ArcGIS JSAPI 高级教程 - 倾斜摄影数据开启透明(修改源码)
  • 输电线路分布式故障监测装置技术解析
  • 概率论第四讲—随机变量的数字特征
  • 学习stm32 蓝牙