Skip to content

Commit

Permalink
新增菜品
Browse files Browse the repository at this point in the history
  • Loading branch information
Q-1515 committed Jul 21, 2022
1 parent a0bc92a commit 595760b
Show file tree
Hide file tree
Showing 7 changed files with 116 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.DishDTO;
import com.reggie.result.R;
import com.reggie.service.DishService;
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/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

@Autowired
private DishService dishService;


/**
* 添加菜品
* @param dishDTO 分类id,菜品描述,口味,菜品id,菜品图片路径,菜品名称,菜品价格,菜品状态
* @return success
*/
@PostMapping
@ApiOperation("新增菜品")
public R<String> save(@RequestBody DishDTO dishDTO) {
log.info("添加菜品:{}",dishDTO);
dishService.save(dishDTO);
return R.success("菜品添加成功");
}

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

import com.reggie.entity.DishFlavor;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface DishFlavorMapper {

//插入菜品口味
void insert(@Param("flavors") List<DishFlavor> flavors);
}
8 changes: 8 additions & 0 deletions reggie_server/src/main/java/com/reggie/mapper/DishMapper.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package com.reggie.mapper;

import com.reggie.annotation.AutoFill;
import com.reggie.constant.AutoFillConstant;
import com.reggie.entity.Dish;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface DishMapper {

//根据分类id查询对应的菜品数量
Long cuntByCategoryId(Long id);


@AutoFill(type = AutoFillConstant.INSERT)
//插入数据到菜品表
void insert(Dish dish);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.reggie.service;

import com.reggie.dto.DishDTO;

public interface DishService {

//添加菜品
void save(DishDTO dishDTO);
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,59 @@
package com.reggie.service.impl;

import com.reggie.dto.DishDTO;
import com.reggie.entity.Dish;
import com.reggie.entity.DishFlavor;
import com.reggie.mapper.DishFlavorMapper;
import com.reggie.mapper.DishMapper;
import com.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* 菜品业务实现
*/
@Service
@Slf4j
public class DishServiceImpl implements DishService {
@Autowired
private DishMapper dishMapper;
private DishMapper dishMapper; //菜品Dao
@Autowired
private DishFlavorMapper dishFlavorMapper;
private DishFlavorMapper dishFlavorMapper; //菜品味道Dao

/**
* 添加菜品
*
* @param dishDTO 分类id,菜品描述,口味,菜品id,菜品图片路径,菜品名称,菜品价格,菜品状态
*/
public void save(DishDTO dishDTO) {
//菜品实体类
Dish dish = new Dish();
BeanUtils.copyProperties(dishDTO, dish);
//插入数据到菜品表
dishMapper.insert(dish);

//获取菜品主键值,需要保存到口味表中
Long dishId = dish.getId();

//菜品口味
List<DishFlavor> flavors = dishDTO.getFlavors();
if (flavors == null || flavors.size() == 0) {
return;
}

//循环添加id
flavors.forEach( item -> {
//设置口味关联的菜品id
item.setDishId(dishId);
});

//插入菜品口味
dishFlavorMapper.insert(flavors);


}
}
8 changes: 8 additions & 0 deletions reggie_server/src/main/resources/mapper/DishFlavorMapper.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?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.DishFlavorMapper">

<!--插入菜品口味-->
<insert id="insert">
insert into dish_flavor(dish_id, name, value) values
<foreach collection="flavors" item="flavor" separator=",">
(#{flavor.dishId},#{flavor.name},#{flavor.value})
</foreach>
</insert>
</mapper>
12 changes: 12 additions & 0 deletions reggie_server/src/main/resources/mapper/DishMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,16 @@
from dish
where category_id = #{id};
</select>

<!--
插入数据到菜品表 id回传给口味表
useGeneratedKeys设置为true:在执行插入记录之后可以获取到数据库自动生成的主键值
keyProperty:指定Java对象的属性名
-->
<insert id="insert" parameterType="Dish" useGeneratedKeys="true" keyProperty="id">
insert into dish (name, category_id, price, image, description, status, create_time, update_time,
create_user, update_user)
values (#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{status}, #{createTime}, #{updateTime},
#{createUser}, #{updateUser})
</insert>
</mapper>

0 comments on commit 595760b

Please sign in to comment.