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 529d3e8 commit 0928642
Show file tree
Hide file tree
Showing 16 changed files with 435 additions and 2 deletions.
272 changes: 272 additions & 0 deletions SQL/database_reggie.sql

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,19 @@ public R<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO) {
return R.success(page);
}

/**
* 根据id删除分类
*
* @param id 要删除分类的id
* @return success
*/
@DeleteMapping
@ApiOperation("删除分类")
public R<String> deleteById(Long id) {
log.info("删除分类:{}", id);
categoryService.deleteById(id);
return R.success("删除成功");
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ public interface CategoryMapper {

//分类信息分页查询
Page<Category> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO);

//删除分类
void deleteById(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.reggie.mapper;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface DishFlavorMapper {

}
10 changes: 10 additions & 0 deletions reggie_server/src/main/java/com/reggie/mapper/DishMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.reggie.mapper;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface DishMapper {

//根据分类id查询对应的菜品数量
Long cuntByCategoryId(Long id);
}
10 changes: 10 additions & 0 deletions reggie_server/src/main/java/com/reggie/mapper/SetmealMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.reggie.mapper;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface SetmealMapper {

//根据分类id查询对应的套餐数量
Long cuntByCategoryId(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ public interface CategoryService {

//分类信息分页查询
PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO);

//根据id删除分类
void deleteById(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.reggie.service;

public interface DishService {

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

public interface SetmealService {

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.reggie.constant.MessageConstant;
import com.reggie.constant.StatusConstant;
import com.reggie.dto.CategoryDTO;
import com.reggie.dto.CategoryPageQueryDTO;
import com.reggie.entity.Category;
import com.reggie.exception.DeletionNotAllowedException;
import com.reggie.mapper.CategoryMapper;
import com.reggie.mapper.DishMapper;
import com.reggie.mapper.SetmealMapper;
import com.reggie.result.PageResult;
import com.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -20,7 +24,13 @@
public class CategoryServiceImpl implements CategoryService {

@Autowired
private CategoryMapper categoryMapper;
private CategoryMapper categoryMapper; //分类 DAO

@Autowired
private DishMapper dishMapper; //菜品DAO

@Autowired
private SetmealMapper setmealMapper; // 套餐DAO

/**
* 新增分类
Expand Down Expand Up @@ -53,6 +63,29 @@ public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {

Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);

return new PageResult(page.getTotal(),page.getResult());
return new PageResult(page.getTotal(), page.getResult());
}

/**
* 根据id删除分类
*
* @param id 要删除分类的id
*/
public void deleteById(Long id) {
//根据分类id查询对应的菜品数量
Long dishCount = dishMapper.cuntByCategoryId(id);
if (dishCount > 0) {
//当前关联了菜品不能删除
throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
}

//根据分类id查询对应的套餐数量
Long setmealCount = setmealMapper.cuntByCategoryId(id);
if (setmealCount > 0) {
//当前关联了套餐不能删除
throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
}
//删除分类
categoryMapper.deleteById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.reggie.service.impl;

import com.reggie.mapper.DishFlavorMapper;
import com.reggie.mapper.DishMapper;
import com.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.reggie.service.impl;

import com.reggie.mapper.SetmealMapper;
import com.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* 套餐业务实现
*/
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
@Autowired
private SetmealMapper setmealMapper;
}
7 changes: 7 additions & 0 deletions reggie_server/src/main/resources/mapper/CategoryMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@
</where>
order by sort
</select>

<!--删除分类-->
<delete id="deleteById">
delete
from category
where id = #{id}
</delete>
</mapper>
4 changes: 4 additions & 0 deletions reggie_server/src/main/resources/mapper/DishFlavorMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?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">
</mapper>
11 changes: 11 additions & 0 deletions reggie_server/src/main/resources/mapper/DishMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?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.DishMapper">

<!--根据分类id查询对应的菜品数量-->
<select id="cuntByCategoryId" resultType="java.lang.Long">
select count(id)
from dish
where category_id = #{id};
</select>
</mapper>
10 changes: 10 additions & 0 deletions reggie_server/src/main/resources/mapper/SetmealMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?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">

<select id="cuntByCategoryId" resultType="java.lang.Long">
select count(*)
from setmeal
where category_id = #{id}
</select>
</mapper>

0 comments on commit 0928642

Please sign in to comment.