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

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

新增套餐(分为根据分类id查询菜品,完善修改菜品销售状态,新增套餐)

根据分类id查询菜品(因为添加套餐时,需要选择菜品)

  • controll层添加根据分类id查询菜品函数
  • DishController.java
    @GetMapping("/list")
    @ApiOperation("根据分类ID查询菜品")
    public Result<List<Dish>> getByCategoryId(Long categoryId){
        log.info("根据分类ID查询菜品:{}",categoryId);
        List<Dish> dishes = dishService.getByCategoryId(categoryId);
        return Result.success(dishes);
    }
  • service层添加根据分类id查询菜品函数,并在实现类中实现
  • DishServiceImpl.java
    @Override
    public List<Dish> getByCategoryId(Long categoryId) {
        List<Dish> dishes = dishMapper.getByCategoryId(categoryId,StatusConstant.ENABLE);
        return dishes;
    }
  • mapper层实现
  • DishMapper.java
    @Select("select * from dish where category_id = #{categoryId} and status = #{enable}")
    List<Dish> getByCategoryId(Long categoryId, Integer enable);

完善修改菜品销售状态

  • controll层添加startOrStop函数
  • DishController.java
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品销售状态")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("修改菜品销售状态:{},{}",status,id);
        dishService.startOrStop(status,id);
        return Result.success();
    }
  • service层添加函数,并在实现类中实现
  • DishServiceImpl.java
    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.update(dish);
    }

新增套餐

  • 新建SetMealController类,并且添加save方法
package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.result.Result;
import com.sky.service.SetMealService;
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;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetMealController {

    @Autowired
    SetMealService setMealService;

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

  • 新建SetMealService类,添加save方法,并且新建实现类,在实现类中实现
    @Override
    @Transactional
    public void saveWithDishs(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });

        //保存套餐和菜品的关联关系
        setmealDishMapper.insertBatch(setmealDishes);
    }
  • mapper层实现
  • SetmealMapper.xml
    <insert id="insert" parameterType="Setmeal" useGeneratedKeys="true" keyProperty="id">
        insert into setmeal
        (category_id, name, price, status, description, image, create_time, update_time, create_user, update_user)
        values (#{categoryId}, #{name}, #{price}, #{status}, #{description}, #{image}, #{createTime}, #{updateTime},
                #{createUser}, #{updateUser})
    </insert>
  • SetmealDishMapper.xml
    <insert id="insertBatch" parameterType="list">
        insert into setmeal_dish
        (setmeal_id,dish_id,name,price,copies)
        values
        <foreach collection="setmealDishes" item="sd" separator=",">
            (#{sd.setmealId},#{sd.dishId},#{sd.name},#{sd.price},#{sd.copies})
        </foreach>
    </insert>

套餐分页查询

  • controll层添加分页管理函数
  • SetMealController.java
    @GetMapping("/page")
    @ApiOperation("套餐分类查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐分类查询:{}",setmealPageQueryDTO);
        PageResult pageResult = setMealService.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }
  • service层添加函数,并在实现类中实现
  • SetmealServiceImpl.java
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }
  • mapper层实现
  • SetmealMapper.xml
    <select id="page" resultType="com.sky.vo.SetmealVO">
        select
        s.*,c.name categoryName
        from
        setmeal s
        left join
        category c
        on
        s.category_id = c.id
        <where>
            <if test="name != null">
                and s.name like concat('%',#{name},'%')
            </if>
            <if test="status != null">
                and s.status = #{status}
            </if>
            <if test="categoryId != null">
                and s.category_id = #{categoryId}
            </if>
        </where>
        order by s.create_time desc
    </select>

删除套餐

  • controll层添加批量删除函数
  • SetMealController.java
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    public Result delete(@RequestParam List<Long> ids){
        log.info("批量删除套餐:{}",ids);
        setMealService.deleteBatch(ids);
        return Result.success();
    }
  • service层添加函数,并在实现类中实现
  • SetmealServiceImpl.java
    @Override
    public void deleteBatch(List<Long> ids) {
        //判断是否起售中
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if(setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        setmealMapper.deleteBatch(ids);
        setmealDishMapper.deleteBatch(ids);
    }
  • mapper层实现
  • SetmealMapper.java
    @Select("select * from setmeal where id = #{id}")
    Setmeal getById(Long id);
  • SetmealMapper.xml
    <delete id="deleteBatch">
        delete from setmeal where id in
        <foreach collection="ids" separator="," open="(" close=")" item="id">
            #{id}
        </foreach>
    </delete>
  • SetmealDishMapper.xml
    <delete id="deleteBatch">
        delete from setmeal_dish where setmeal_id in
        <foreach collection="setMealIds" separator="," open="(" close=")" item="setMealId">
            #{setMealId}
        </foreach>
    </delete>

修改套餐(分为根据id查询套餐+修改套餐)

根据id查询套餐

  • controll层添加根据id查询套餐函数
  • SetMealController.java
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        SetmealVO setmealVO = setMealService.getByIdWithDish(id);
        return Result.success(setmealVO);
    }
  • service层添加函数,并在实现类中实现
  • SetmealServiceImpl.java
    @Override
    public SetmealVO getByIdWithDish(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }
  • mapper层实现
  • SetmealDishMapper.java
    @Select("select * from setmeal_dish where setmeal_id = #{setmealId}")
    List<SetmealDish> getBySetmealId(Long setmealId);

修改套餐

  • controll层添加修改套餐函数
  • SetMealController.java
    @PutMapping
    @ApiOperation("修改套餐")
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐:{}",setmealDTO);
        setMealService.update(setmealDTO);
        return Result.success();
    }
  • service层添加函数,并在实现类中实现
  • SetmealServiceImpl.java
    @Override
    public void update(SetmealDTO setmealDTO) {
        //修改套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //删除菜品关联信息
        setmealDishMapper.deleteBySetMealId(setmealDTO.getId());
        //插入菜品关联信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDTO.getId());
            });
        }
        setmealDishMapper.insertBatch(setmealDishes);
    }
  • mapper层实现
  • SetmealMapper.xml
<update id="update">
        update setmeal
        <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>

起售停售套餐

  • controll层添加startOrStop函数
  • SetMealController.java
    @PostMapping("/status/#{status}")
    @ApiOperation("起售停售套餐")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("起售停售套餐:{},{}",status,id);
        setMealService.startOrStop(status,id);
        return Result.success();
    }
  • service层添加函数,并在实现类中实现
  • SetmealServiceImpl.java
    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

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