Skip to content

Commit

Permalink
新增套餐
Browse files Browse the repository at this point in the history
  • Loading branch information
Q-1515 committed Jul 22, 2022
1 parent 6dc19a1 commit 86eb45e
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.reggie.controller.admin;

import com.reggie.dto.SetmealDTO;
import com.reggie.result.R;
import com.reggie.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
private SetmealService setmealService;

/**
* 新增套餐
*
* @param setmealDTO 套餐数据
* @return success
*/
@PostMapping
@ApiOperation("新增套餐")
public R<String> save(@RequestBody SetmealDTO setmealDTO) {
log.info("新增套餐:{}", setmealDTO);
setmealService.saveWithDish(setmealDTO);
return R.success("套餐添加成功");
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.reggie.mapper;

import com.reggie.entity.SetmealDish;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
Expand All @@ -9,4 +10,7 @@ public interface SetmealDishMapper {

//根据菜品ids找套餐
List<Long> getSetmealIdsByDishIds(List<Long> ids);

//保存套餐和菜品的关联关系
void insertBatch(List<SetmealDish> setmealDishes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ public interface SetmealMapper {
//根据套餐id更新套餐数据
@AutoFill(type = AutoFillConstant.UPDATE)
void updatesByIds(Setmeal setmeal);

//套餐表插入数据
@AutoFill(type = AutoFillConstant.INSERT)
void insert(Setmeal setmeal);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.reggie.service;

import com.reggie.dto.SetmealDTO;

public interface SetmealService {

//新增套餐
void saveWithDish(SetmealDTO setmealDTO);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package com.reggie.service.impl;

import com.reggie.constant.StatusConstant;
import com.reggie.dto.SetmealDTO;
import com.reggie.entity.Setmeal;
import com.reggie.entity.SetmealDish;
import com.reggie.mapper.SetmealDishMapper;
import com.reggie.mapper.SetmealMapper;
import com.reggie.service.SetmealService;
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;

/**
* 套餐业务实现
Expand All @@ -13,5 +22,38 @@
@Slf4j
public class SetmealServiceImpl implements SetmealService {
@Autowired
private SetmealMapper setmealMapper;
private SetmealMapper setmealMapper; //套餐DAO

@Autowired
private SetmealDishMapper setmealDishMapper; //套餐菜品关系 DAO

/**
* 新增套餐
*
* @param setmealDTO 套餐数据
*/
@Transactional
public void saveWithDish(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);


}
}
11 changes: 11 additions & 0 deletions reggie_server/src/main/resources/mapper/SetmealDishMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,15 @@
</foreach>
</select>

<!--保存套餐和菜品的关联关系-->
<insert id="insertBatch">
insert into setmeal_dish(setmeal_id, dish_id, name, price, copies)
VALUES
<foreach collection="setmealDishes" item="setmealDishe" separator=",">
(#{setmealDishe.setmealId}, #{setmealDishe.dishId}, #{setmealDishe.name}, #{setmealDishe.price},
#{setmealDishe.copies})
</foreach>

</insert>

</mapper>
10 changes: 9 additions & 1 deletion reggie_server/src/main/resources/mapper/SetmealMapper.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?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.reggie.mapper.SetmealMapper">

<!--根据分类id查询对应的套餐数量-->
<select id="cuntByCategoryId" resultType="java.lang.Long">
select count(*)
Expand Down Expand Up @@ -41,4 +40,13 @@
where id = #{id}
</update>

<!--套餐表插入数据-->
<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>


</mapper>

0 comments on commit 86eb45e

Please sign in to comment.