1 数据设计
/**
* 套餐
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Setmeal implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
//分类id
private Long categoryId;
//套餐名称
private String name;
//套餐价格
private BigDecimal price;
//状态 0:停用 1:启用
private Integer status;
//描述信息
private String description;
//图片
private String image;
private LocalDateTime createTime;
private LocalDateTime updateTime;
private Long createUser;
private Long updateUser;
}
/**
* 套餐菜品关系
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SetmealDish implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
//套餐id
private Long setmealId;
//菜品id
private Long dishId;
//菜品名称 (冗余字段)
private String name;
//菜品原价
private BigDecimal price;
//份数
private Integer copies;
}
2 新增套餐
业务规则:
-
套餐名称唯一
-
套餐必须属于某个分类
-
套餐必须包含菜品
-
名称、分类、价格、图片为必填项
-
添加菜品窗口需要根据分类类型来展示菜品
-
新增的套餐默认为停售状态
接口设计:
-
根据类型查询分类(已完成)
-
根据分类id查询菜品
-
图片上传(已完成)
-
新增套餐
根据类型查询分类
path:/admin/dish/list
method:Get
总结一下参数:
1、Query参数在地址栏以URL后加"?"跟着键值对传入,不需要加注解
2、POST方法传递JSON参数,加注解 @RequestBody
3、路径参数 /admin/employee/status/{status},注解 @PathVariable
新增套餐
path:/admin/setmeal
method:POST
请求参数
实现
Service
package com.sky.service.impl;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class SetmealServiceImpl implements SetmealService {
@Autowired
private SetmealMapper setmealMapper;
@Autowired
private SetmealDishMapper setmealDishMapper;
@Override
public void add(SetmealDTO setmealDTO) {
Setmeal setmeal = new Setmeal();
BeanUtils.copyProperties(setmealDTO,setmeal);
// 设置更新信息
setmeal.setCreateTime(LocalDateTime.now());
setmeal.setCreateUser(BaseContext.getCurrentId());
setmeal.setUpdateTime(LocalDateTime.now());
setmeal.setUpdateUser(BaseContext.getCurrentId());
//1.添加套餐
setmealMapper.add(setmeal);
//获取生成的套餐的id
Long setmealId = setmeal.getId();
List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
setmealDishes.forEach(setmealDish -> {
setmealDish.setSetmealId(setmealId);
});
//2.添加套餐和菜品的关系
setmealDishMapper.addSetWithDish(setmealDishes);
}
}
mapper
添加菜品
<insert id="add" parameterType="Setmeal" useGeneratedKeys="true" keyProperty="id">
insert into sky_take_out.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})
</insert>
添加套餐和菜品的关系
<insert id="addSetWithDish" parameterType="list">
insert into sky_take_out.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>
3 套餐分页查询
业务规则:
-
根据页码进行分页展示
-
每页展示10条数据
-
可以根据需要,按照套餐名称、分类、售卖状态进行查询
path:/admin/setmeal/page
method:GET
controller
controller
mapper
<select id="page" resultType="com.sky.vo.SetmealVO">
select s.*,c.name categoryName
from sky_take_out.setmeal s
left join sky_take_out.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>
测试