菜品管理模块:
菜品分页查询:
业务规则:
根据页面展示菜品信息
每页展示10条数据
分页查询时可以根据需要输入菜品名称、菜品分类、菜品状态进行查询
controller层代码
/**
* 菜品分页查询
* @param dishPageQueryDTO
* @return
*/
@GetMapping("/page")
@ApiOperation("菜品分页查询")
public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
log.info("菜品分页查询:{}", dishPageQueryDTO);
PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
return Result.success(pageResult);
}
Service层代码:
/**
* 菜品分页查询
* @param dishPageQueryDTO
* @return
*/
PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO);
service实现类代码:
/**
* 菜品分页查询
* @param dishPageQueryDTO
* @return
*/
public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize()); // 开始分页
Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
return new PageResult(page.getTotal(), page.getResult());
}
mapper层:
/**
* 菜品分页查询
* @param dishPageQueryDTO
* @return
*/
Page<DishVO> pageQuery(DishPageQueryDTO dishPageQueryDTO);
mapper.xml
<select id="pageQuery" resultType="com.sky.vo.DishVO">
select d.* , c.name as categoryName from dish as d left outer join category as c on d.category_id = c.id
<where>
<if test="name != null">
and d.name like concat('%',#{name},'%')
</if>
<if test="categoryId != null">
and d.category_id = #{categoryId}
</if>
<if test="status != null">
and d.status = #{status}
</if>
</where>
order by d.create_time desc
</select>
菜品的删除:
业务规则:
可以一次删除一个菜品,也可以批量删除菜品
起售中的菜品不能删除
被套餐关联的菜品也不能删除
删除菜品后,关联的口味也需要删除掉
当我们删除菜品之前需要先判断当前这个菜品是不是被某个套餐关联了,所以要查套餐与菜品的关系表(setmeal_dish),通过查询这个关系表来判断当前要删除的这个菜品是否被某个套餐关联。假设我们这个菜品能够被删除,要从dish表里面删除一条数据,那它对应的口味数据也要删掉,所以我们还要操作(dish_flavor)。
删除菜品执行的是删除操作所以这里我们用DELETE请求方式(DELETE:用于请求服务器删除指定的资源)。
Controller
@DeleteMapping
@ApiOperation("菜品批量删除")
public Result delete(@RequestParam List<Long> ids){
log.info("菜品批量删除:{}",ids);
dishService.deleteBatch(ids);
return Result.success();
}
ServiceImpl
@Transactional //涉及多表操作,开启事务管理,确保方法的原子性
public void deleteBatch(List<Long> ids) {
//1、判断当前菜品是否能够删除--是否存在起售中的菜品 status
for (Long id : ids) {
//根据菜品id查询这个菜品所有信息,返回一个dish对象
Dish dish = dishMapper.getById(id);
if(dish.getStatus() == StatusConstant.ENABLE){
//当前菜品处于起售中,不能删除
throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
}
}
//2、判断当前菜品是否能够删除--根据菜品id查询是否被套餐关联了
List<Long> setmealIds = setmealDishMapper.getSetmealIdByDishIds(ids);
if(setmealIds != null && setmealIds.size() > 0){
//当前菜品被套餐关联,不能删除
throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
}
//3、删除菜品表中的菜品数据
/* for (Long id : ids) {
dishMapper.deleteById(id);
//删除菜品关联的口味数据
dishFlavorMapper.deleteByDishId(id);
} */
//批量删除
//delete from dish where id in {?,?,?}
//根据菜品id集合批量删除菜品数据
dishMapper.deleteByIds(ids);
//delete from dish_flavor where id in {?,?,?}
//根据菜品id集合批量删除关联的口味数据
dishFlavorMapper.deleteByDishIds(ids);
}
Mapper.xml
<select id="getSetmealIdByDishIds" resultType="java.lang.Long">
select setmeal_id from sky_take_out.setmeal_dish where dish_id in
<foreach collection="dishIds" item="dishId" separator="," open="(" close=")">
#{dishId}
</foreach>
</select>
查询菜品:
controller:
@GetMapping("/{id}")
@ApiOperation("根据id查询菜品")
public Result<DishVO> getById(@PathVariable Long id){
log.info("根据id查询菜品 {}",id);
DishVO dishVO = dishService.getByIdWithFlavor(id);
return Result.success(dishVO);
}
ServiceImpl:
public DishVO getByIdWithFlavor(Long id) {
//先根据id查询菜品数据
Dish dish = dishMapper.getById(id);
//根据菜品id查询关联的口味
List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);
//将查询到的数据封装到VO
DishVO dishVO = new DishVO();
BeanUtils.copyProperties(dish,dishVO);
dishVO.setFlavors(dishFlavors);
return dishVO;
}
mapper:
@Select("select * from sky_take_out.dish_flavor where dish_id = #{dishId}")
List<DishFlavor> getByDishId(Long dishId);
修改菜品:
controller:
@PutMapping
@ApiOperation("修改菜品")
public Result update(@RequestBody DishDTO dishDTO){
log.info("修改菜品 {}",dishDTO);
dishService.updateWithFlavor(dishDTO);
return Result.success();
}
ServiceImpl:
public void updateWithFlavor(DishDTO dishDTO) {
Dish dish = new Dish();
BeanUtils.copyProperties(dishDTO,dish);
//1 修改菜品表基本信息
dishMapper.update(dish);
//2 先删除原来的口味数据
dishFlavorMapper.deleteByDishId(dishDTO.getId());
//3 再插入口味
List<DishFlavor> flavors = dishDTO.getFlavors();
if(flavors != null && flavors.size() > 0){
flavors.forEach(dishFlavor -> {
dishFlavor.setDishId(dishDTO.getId());
});
//向口味表插入n条数据
dishFlavorMapper.insertBatch(flavors);
}
}
mapper:
@AutoFill(OperationType.UPDATE)
void update(Dish dish);
Mapper.xml:
<update id="update">
update sky_take_out.dish
<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>
菜品起售停售
核心:修改状态
Controller:
@PostMapping("/status/{status}")
@ApiOperation("菜品起售停售")
public Result startOrStop(@PathVariable Integer status,Long id){
log.info("菜品起售停售:{},{}",status,id);
dishService.startOrStop(status,id);
return Result.success();
}
ServiceImpl:
//update employee set status = ? where id = ?
public void startOrStop(Integer status, Long id) {
Dish dish = Dish.builder()
.status(status)
.id(id)
.build();
dishMapper.update(dish);
//如果是停售操作,那么菜品所关联的套餐也不能售卖
if(status == StatusConstant.DISABLE){
ArrayList<Long> dishIds = new ArrayList<>();
dishIds.add(id);
// select setmealId from setmeal_dish where dish_id in (?,?,?)
List<Long> setmealIds = setmealDishMapper.getSetmealIdByDishIds(dishIds);
if(setmealIds != null &&setmealIds.size()>0){
for (Long setmealId :setmealIds) {
Setmeal setmeal = Setmeal.builder()
.id(setmealId)
.status(StatusConstant.DISABLE)
.build();
setmealMapper.update(setmeal);
}
}
}
}
mapper:
@AutoFill(OperationType.UPDATE)
void update(Setmeal setmeal);
Mapper.xml
<update id="update">
update sky_take_out.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>