Pārlūkot izejas kodu

套餐相关业务开发

潘海瑞 9 mēneši atpakaļ
vecāks
revīzija
8190977435

+ 3 - 1
sky-server/src/main/java/com/sky/controller/admin/DishController.java

@@ -103,6 +103,8 @@ public class DishController {
      */
     @GetMapping("/list")
     public Result<List<Dish>> list(Long categoryId){
-        return Result.success();
+        List<Dish> list = dishService.list(categoryId);
+
+        return Result.success(list);
     }
 }

+ 60 - 4
sky-server/src/main/java/com/sky/controller/admin/SetmealController.java

@@ -5,12 +5,13 @@ import com.sky.dto.SetmealPageQueryDTO;
 import com.sky.result.PageResult;
 import com.sky.result.Result;
 import com.sky.service.SetmealService;
+import com.sky.vo.SetmealVO;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.ibatis.annotations.Mapper;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
 
 /**
  * 套餐相关
@@ -22,7 +23,17 @@ public class SetmealController {
     @Autowired
     private SetmealService setmealService;
 
-    
+
+    /**
+     * 新增套餐
+     * @param setmealDTO
+     * @return
+     */
+    @PostMapping
+    public Result save(@RequestBody SetmealDTO setmealDTO){
+        setmealService.saveWithDish(setmealDTO);
+        return Result.success();
+    }
     /**
      * 分页查询
      * @param setmealPageQueryDTO
@@ -34,4 +45,49 @@ public class SetmealController {
         PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
         return Result.success(pageResult);
     }
+
+    /**
+     * 批量删除套餐
+     * @param ids
+     * @return
+     */
+    @DeleteMapping
+    public Result deleteById(@RequestParam List<Long> ids){
+        setmealService.deleteById(ids);
+        return Result.success();
+    }
+
+    /**
+     * 根据id查询套餐
+     * @param id
+     * @return
+     */
+    @GetMapping("/{id}")
+    public Result<SetmealVO> getById(@PathVariable Long id){
+        SetmealVO setmealVO =  setmealService.getById(id);
+        return Result.success(setmealVO);
+    }
+
+    /**
+     * 修改套餐
+     * @param setmealDTO
+     * @return
+     */
+    @PutMapping
+    public Result update(@RequestBody SetmealDTO setmealDTO){
+        setmealService.update(setmealDTO);
+        return Result.success();
+    }
+
+    /**
+     * 起售停售状态修改
+     * @param status
+     * @param id
+     * @return
+     */
+    @PostMapping("/status/{status}")
+    public Result startOrStop(@PathVariable Integer status, Long id){
+        setmealService.startOrStop(status,id);
+        return Result.success();
+    }
 }

+ 17 - 0
sky-server/src/main/java/com/sky/mapper/DishMapper.java

@@ -10,6 +10,8 @@ import org.apache.ibatis.annotations.Delete;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Select;
 
+import java.util.List;
+
 @Mapper
 public interface DishMapper {
 
@@ -49,4 +51,19 @@ public interface DishMapper {
 
     @AutoFill(value = OperationType.UPDATE)
     void update(Dish dish);
+
+    /**
+     * 根据id查询菜品
+     * @param dish
+     * @return
+     */
+    List<Dish> list(Dish dish);
+
+    /**
+     * 根据id查询菜品
+     * @param setmealId
+     * @return
+     */
+    @Select("select * from dish a left join setmeal_dish sd on a.id = sd.dish_id where sd.setmeal_id = #{setmealId}")
+    List<Dish> getBySetmealId(Long setmealId);
 }

+ 24 - 0
sky-server/src/main/java/com/sky/mapper/SetmealDishMapper.java

@@ -1,6 +1,9 @@
 package com.sky.mapper;
 
+import com.sky.entity.SetmealDish;
+import org.apache.ibatis.annotations.Delete;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Select;
 
 import java.util.List;
 
@@ -12,4 +15,25 @@ public interface SetmealDishMapper {
      * @return
      */
     List<Long> getSetmealIdsByDishIds(List<Long> dishIds);
+
+    /**
+     * 批量保存菜品和套餐的关联关系
+     * @param setmealDishes
+     */
+    void insertBatch(List<SetmealDish> setmealDishes);
+
+    /**
+     * 根据setmealId删除套餐和菜品的关联关系
+     * @param setmealId
+     */
+    @Delete("delete from setmeal_dish where setmeal_id = #{setmealId}")
+    void deleteBySetmealId(Long setmealId);
+
+    /**
+     * 根据套餐id查询套餐和菜品之间的关系
+     * @param setmealId
+     * @return
+     */
+    @Select("select * from setmeal_dish where setmeal_id = #{setmealId}")
+    List<SetmealDish> getBySetmealId(Long setmealId);
 }

+ 37 - 1
sky-server/src/main/java/com/sky/mapper/SetmealMapper.java

@@ -1,8 +1,14 @@
 package com.sky.mapper;
 
 import com.github.pagehelper.Page;
+import com.sky.annotation.AutoFill;
 import com.sky.dto.SetmealPageQueryDTO;
+import com.sky.entity.Setmeal;
+import com.sky.enumeration.OperationType;
+import com.sky.result.Result;
 import com.sky.vo.SetmealVO;
+import org.apache.ibatis.annotations.Delete;
+import org.apache.ibatis.annotations.Insert;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Select;
 
@@ -22,5 +28,35 @@ public interface SetmealMapper {
      * @param setmealPageQueryDTO
      * @return
      */
-    Page<SetmealVO> pagequery(SetmealPageQueryDTO setmealPageQueryDTO);
+    Page<SetmealVO> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO);
+
+    /**
+     * 增加套餐
+     * @param setmeal
+     */
+    @AutoFill(value = OperationType.INSERT)
+    void insert(Setmeal setmeal);
+
+    /**
+     * 回显套餐id
+     * @param id
+     * @return
+     */
+    @Select("select * from setmeal where id = #{id}")
+    Setmeal getById(Long id);
+
+    /**
+     *根据id删除套餐
+     * @param setmealId
+     */
+    @Delete("delete from setmeal where id = #{id}")
+    void deleteById(Long setmealId);
+
+    /**
+     * 修改套餐
+     * @param setmeal
+     */
+    @AutoFill(value = OperationType.UPDATE)
+    void update(Setmeal setmeal);
+
 }

+ 8 - 0
sky-server/src/main/java/com/sky/service/DishService.java

@@ -2,6 +2,7 @@ package com.sky.service;
 
 import com.sky.dto.DishDTO;
 import com.sky.dto.DishPageQueryDTO;
+import com.sky.entity.Dish;
 import com.sky.result.PageResult;
 import com.sky.vo.DishVO;
 
@@ -48,4 +49,11 @@ public interface DishService {
      * @param id
      */
     void isSole(Integer status, Long id);
+
+    /**
+     * 根据分类ID查询菜品
+     * @param categoryId
+     * @return
+     */
+    List<Dish> list(Long categoryId);
 }

+ 33 - 0
sky-server/src/main/java/com/sky/service/SetmealService.java

@@ -1,7 +1,11 @@
 package com.sky.service;
 
+import com.sky.dto.SetmealDTO;
 import com.sky.dto.SetmealPageQueryDTO;
 import com.sky.result.PageResult;
+import com.sky.vo.SetmealVO;
+
+import java.util.List;
 
 public interface SetmealService {
     /**
@@ -11,6 +15,35 @@ public interface SetmealService {
     PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO);
 
 
+    /**
+     * 新增套餐
+     * @param setmealDTO
+     */
+    void saveWithDish(SetmealDTO setmealDTO);
 
+    /**
+     * 批量删除套餐
+     * @param ids
+     */
+    void deleteById(List<Long> ids);
 
+    /**
+     * 根据id查询套餐
+     * @param id
+     * @return
+     */
+    SetmealVO getById(Long id);
+
+    /**
+     * 修改套餐
+     * @param setmealDTO
+     */
+    void update(SetmealDTO setmealDTO);
+
+    /**
+     * 起售停售状态修改
+     * @param status
+     * @param id
+     */
+    void startOrStop(Integer status, Long id);
 }

+ 14 - 0
sky-server/src/main/java/com/sky/service/impl/DishServiceImpl.java

@@ -155,5 +155,19 @@ public class DishServiceImpl implements DishService {
         dishMapper.update(dish);
     }
 
+    /**
+     * 根据分类id查询菜品
+     * @param categoryId
+     * @return
+     */
+    @Override
+    public List<Dish> list(Long categoryId) {
+        Dish dish = Dish.builder()
+                .categoryId(categoryId)
+                .status(StatusConstant.ENABLE)
+                .build();
+        return dishMapper.list(dish);
+    }
+
 
 }

+ 144 - 1
sky-server/src/main/java/com/sky/service/impl/SetmealServiceImpl.java

@@ -2,14 +2,28 @@ package com.sky.service.impl;
 
 import com.github.pagehelper.Page;
 import com.github.pagehelper.PageHelper;
+import com.sky.constant.MessageConstant;
+import com.sky.constant.StatusConstant;
+import com.sky.dto.SetmealDTO;
 import com.sky.dto.SetmealPageQueryDTO;
+import com.sky.entity.Dish;
+import com.sky.entity.Setmeal;
+import com.sky.entity.SetmealDish;
+import com.sky.exception.DeletionNotAllowedException;
+import com.sky.exception.SetmealEnableFailedException;
+import com.sky.mapper.DishMapper;
+import com.sky.mapper.SetmealDishMapper;
 import com.sky.mapper.SetmealMapper;
 import com.sky.result.PageResult;
 import com.sky.service.SetmealService;
 import com.sky.vo.SetmealVO;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.List;
 
 @Service
 @Slf4j
@@ -17,6 +31,12 @@ public class SetmealServiceImpl implements SetmealService {
     @Autowired
     private SetmealMapper setmealMapper;
 
+    @Autowired
+    private SetmealDishMapper setmealDishMapper;
+
+    @Autowired
+    private DishMapper dishMapper;
+
     /**
      * 分页查询
      * @param setmealPageQueryDTO
@@ -25,8 +45,131 @@ public class SetmealServiceImpl implements SetmealService {
     public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
         PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
 
-        Page<SetmealVO> page = setmealMapper.pagequery(setmealPageQueryDTO);
+        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
 
         return new PageResult(page.getTotal(), page.getResult());
     }
+
+    /**
+     * 新增套餐,同时需要保存套餐和菜品的关联关系
+     * @param setmealDTO
+     */
+    @Override
+    public void saveWithDish(SetmealDTO setmealDTO) {
+        Setmeal setmeal = new Setmeal();
+        BeanUtils.copyProperties(setmealDTO, setmeal);//将setmealDTO属性赋值给对应的setmeal
+        setmealMapper.insert(setmeal);//向套餐表插入数据
+        Long setmealId = setmeal.getId();
+
+        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
+        for(SetmealDish setmealDish : setmealDishes ){
+            setmealDish.setSetmealId(setmealId);
+            System.out.println(setmealDish);
+        }
+//        setmealDishes.forEach(setmealDish -> {
+//            setmealDish.setSetmealId(setmealId);
+//        });
+        setmealDishMapper.insertBatch(setmealDishes);//保存套餐和菜品的关联关系
+
+    }
+
+    /**
+     * 批量删除套餐
+     * @param ids
+     */
+    @Override
+    @Transactional
+    public void deleteById(List<Long> ids) {
+        /*for(Long id : ids){
+           Setmeal setmeal =  setmealMapper.getById(id);
+        }*/
+        ids.forEach(id -> {
+            Setmeal setmeal = setmealMapper.getById(id);
+            if(setmeal.getStatus() == StatusConstant.ENABLE){
+                // 起售中的套餐不能删除
+                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
+            }
+        });
+
+        ids.forEach(setmealId -> {
+            //删除套餐表中的数据
+            setmealMapper.deleteById(setmealId);
+
+            //删除套餐菜品关系表中的数据
+            setmealDishMapper.deleteBySetmealId(setmealId);
+        });
+
+    }
+
+    /**
+     * 根据id查询套餐
+     * @param id
+     * @return
+     */
+    @Override
+    public SetmealVO getById(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;
+    }
+
+    /**
+     * 修改套餐
+     * @param setmealDTO
+     */
+    @Override
+    @Transactional
+    public void update(SetmealDTO setmealDTO) {
+        Setmeal setmeal = new Setmeal();
+        BeanUtils.copyProperties(setmealDTO,setmeal);
+
+        //修改套餐表
+        setmealMapper.update(setmeal);
+
+        //套餐id
+        Long setmealId = setmealDTO.getId();
+
+        //删除套餐和菜品的关联关系,先删除添加
+        setmealDishMapper.deleteBySetmealId(setmealId);
+
+        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
+        setmealDishes.forEach(setmealDish -> {
+            setmealDish.setSetmealId(setmealId);
+        });
+
+        //重新插入套餐和菜品的关系
+        setmealDishMapper.insertBatch(setmealDishes);
+    }
+
+    /**
+     * 起售停售
+     * @param status
+     * @param id
+     */
+    @Override
+    public void startOrStop(Integer status, Long id) {
+        //起售套餐时,判断套餐内菜品是否在停售状态,有则提示
+        if(status == StatusConstant.ENABLE){
+            List<Dish> dishList = dishMapper.getBySetmealId(id);
+            if(dishList != null && dishList.size() > 0){
+                dishList.forEach(dish -> {
+                    if(StatusConstant.DISABLE == dish.getStatus()){
+                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
+                    }
+                });
+            }
+
+        }
+        Setmeal setmeal = Setmeal.builder()
+                .id(id)
+                .status(status)
+                .build();
+        setmealMapper.update(setmeal);
+    }
 }

+ 16 - 0
sky-server/src/main/resources/mapper/DishMapper.xml

@@ -46,4 +46,20 @@
         </where>
         order by d.create_time desc
     </select>
+    <!--根据分类id查询菜品-->
+    <select id="list" resultType="com.sky.entity.Dish">
+        select * from dish
+        <where>
+            <if test="name != null">
+                and name like concat('%',#{name},'%')
+            </if>
+            <if test="categoryId != null">
+                and category_id = #{categoryId}
+            </if>
+            <if test="status != null">
+                and status = #{status}
+            </if>
+        </where>
+        order by create_time desc
+    </select>
 </mapper>

+ 9 - 0
sky-server/src/main/resources/mapper/SetmealDishMapper.xml

@@ -2,6 +2,15 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 <mapper namespace="com.sky.mapper.SetmealDishMapper">
+    <insert id="insertBatch">
+        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>
 
     <select id="getSetmealIdsByDishIds" resultType="java.lang.Long">
         select setmeal_id from setmeal_dish where dish_id in

+ 49 - 1
sky-server/src/main/resources/mapper/SetmealMapper.xml

@@ -2,8 +2,56 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 <mapper namespace="com.sky.mapper.SetmealMapper">
+    <insert id="insert">
+        insert into setmeal
+        (category_id, name, price, description, image, create_time, update_time, create_user, update_user)
+        VALUES
+            (#{categoryId},#{name},#{price},#{description},#{image},#{createTime},#{updateTime},#{createUser},#{updateUser})
 
-    <select id="pagequery" resultType="com.sky.vo.SetmealVO">
+    </insert>
+    <!--修改套餐-->
+    <update id="update">
+        update setmeal
+        <set>
+            <if test="categoryId != null">category_id = #{categoryId},
+            </if>
+            <if test="name != null">name = #{name},
+            </if>
+            <if test="price != null">price = #{price},
+            </if>
+            <if test="status != null">status = #{status},
+            </if>
+            <if test="description != null">description = #{description},
+            </if>
+            <if test="image != null">image = #{image},
+            </if>
+            <if test="updateTime != null">update_time = #{updateTime},
+            </if>
+            <if test="updateUser != null">update_user = #{updateUser},
+            </if>
+        </set>
+            where id = #{id}
+    </update>
 
+
+    <!--分页查询-->
+    <select id="pageQuery" 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>
 </mapper>