苍穹外卖项目学习日记(5)

苍穹外卖项目学习日记(5) day03-2

删除菜品

  • controll层添加批量删除函数
  • DishController.java
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result delete(@RequestParam List<Long> ids){
        log.info("菜品批量删除:{}",ids);
        dishService.deleteBatch(ids);
        return Result.success();
    }
  • service层添加相应方法,并且在实现类中实现
  • DishServiceImpl.java
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //判断当前菜品是否能够删除-是否起售中
        for (Long id : ids) {
            Dish dish =  dishMapper.getById(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断当前菜品是否能够删除-是否被套餐关联
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIds != null && setmealIds.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品表中的菜品数据
        for (Long id : ids) {
            dishMapper.deleteById(id);
            //删除菜品关联的口味数据
            dishFlavorMapper.deleteByDishId(id);
        }
    }
  • DishMapper添加相应方法
  • DishMapper.java
    @Delete("delete from dish where id = #{id}")
    void deleteById(Long id);
  • 新建SetmealDishMapper接口,添加getSetmealIdsByDishIds方法,并且在xml中实现
  • SetmealDishMapper.xml
    <select id="getSetmealIdsByDishIds" resultType="java.lang.Long">
        select setmeal_id from setmeal_dish where dish_id in
        <foreach collection="dishIds" item="dishId" separator="," open="(" close=")">
            #{dishId}
        </foreach>
    </select>
  • dishFlavorMapper中添加删除口味的方法
  • dishFlavorMapper.java
    @Delete("delete from dish_flavor where dish_id = #{dishId}")
    void deleteByDishId(Long dishId);

代码优化

  • 删除菜品和口味是一个一个删除的,可能引发性能问题,需要sql语句批量删除数据
  • 修改DishServiceImpl.java
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //判断当前菜品是否能够删除-是否起售中
        for (Long id : ids) {
            Dish dish =  dishMapper.getById(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断当前菜品是否能够删除-是否被套餐关联
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIds != null && setmealIds.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品表中的菜品数据
//        for (Long id : ids) {
//            dishMapper.deleteById(id);
//            //删除菜品关联的口味数据
//            dishFlavorMapper.deleteByDishId(id);
//        }

        //批量删除
        dishMapper.deleteByIds(ids);
        dishFlavorMapper.deleteByDishIds(ids);
    }
  • DishMapper添加批量删除函数deleteByIds,并且在xml中实现
  • DishMapper.xml
    <delete id="deleteByIds">
        delete from dish where id in
        <foreach collection="ids" separator="," open="(" close=")" item="id">
            #{id}
        </foreach>
    </delete>
  • DishFlavorMapper添加批量删除函数deleteByDishIds,并且在xml中实现
  • DishFlavorMapper.xml
    <delete id="deleteByDishIds">
        delete from dish_flavor where dish_id in
        <foreach collection="dishIds" separator="," open="(" close=")" item="dishId">
            #{dishId}
        </foreach>
    </delete>

修改菜品(包括根据id查菜品+修改菜品)

根据id查菜品

  • controll层添加根据id查询菜品函数
  • DishController.java
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品:{}",id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }
  • service层添加相应方法,并且在实现类中实现
  • DishServiceImpl.java
    @Override
    public DishVO getByIdWithFlavor(Long id) {
        //根据id查菜品
        Dish dish = dishMapper.getById(id);
        //根据id查口味
        List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }
  • DishFlavorMapper层添加相应方法,并且在实现类中实现
  • DishFlavorMapper.java
    @Select("select * from dish_flavor where dish_id = #{dishId}")
    List<DishFlavor> getByDishId(Long dishId);

修改菜品

  • controll层添加update函数
  • DishController.java
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品:{}",dishDTO);
        dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }
  • service层添加相应方法,并且在实现类中实现
  • DishServiceImpl.java
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        //修改菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //删除口味
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        //添加口味
        //向口味表插入n条
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }
  • DishFlavorMapper层添加相应方法,并且在实现类中实现
  • DishFlavorMapper.java
    <update id="update">
        update dish
        <set>
            <if test="name != null">name = #{name},</if>
            <if test="categoryId != null">category_id = #{categoryId},</if>
            <if test="price != null">price = #{price},</if>
            <if test="image != null">image = #{image},</if>
            <if test="description != null">description = #{description},</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>

你可能感兴趣的:(苍穹外卖,spring,boot,java)