网站界面设计需要首先做市场研究对吗,简单的广告设计图片,邢台专业做网站推广,南昌门户网站继续上一节的内容#xff0c;本节是作业课程#xff0c;要求独立完成套餐管理模块所有业务功能#xff0c;包括#xff1a;新增套餐、套餐分页查询、删除套餐、修改套餐、起售停售套餐。 目录 新增套餐根据分类id查询菜品功能新增套餐功能 套餐分页查询删除套餐根据id查询套…继续上一节的内容本节是作业课程要求独立完成套餐管理模块所有业务功能包括新增套餐、套餐分页查询、删除套餐、修改套餐、起售停售套餐。 目录 新增套餐根据分类id查询菜品功能新增套餐功能 套餐分页查询删除套餐根据id查询套餐功能删除套餐功能 修改套餐起售停售套餐起售停售菜品起售停售套餐写法一写法二 新增套餐
页面原型 其中的文件上传、和套餐分类查询已经完成。套餐分类查询的接口地址为/admin/category/list根据请求参数的type值区分查询的分类类型1为菜品分类2为套餐分类这里前端查询的是套餐分类。这些功能前面已经完成不再赘述。 在新增套餐时需要选择套餐里包含的菜品所以需要先完成根据分类id查询菜品功能。
根据分类id查询菜品功能
根据分类id查询菜品接口 先来完成根据分类id查询菜品
Controller层
DishController.java
/*** 根据分类id查询菜品** param categoryId* return*/
GetMapping(/list)
ApiOperation(根据分类id查询菜品)
public ResultListDish getByCategory(Long categoryId){log.info(根据分类id查询菜品{}, categoryId);ListDish list dishService.getByCategoryID(categoryId);return Result.success(list);
}Service层实现类
DishServiceImpl.java
/*** 根据分类id查询菜品信息** param categoryId*/
public ListDish getByCategoryID(Long categoryId) {return dishMapper.getByCategoryID(categoryId);
}Mapper层
/*** 根据分类id查询菜品** param categoryId*/
Select(select * from dish where status 1 and category_id #{categoryId} order by create_time desc)
ListDish getByCategoryID(Long categoryId);完成之后测试新增套餐时往套餐中加入菜品 按道理来说这里前端写了输入菜品名称进行搜索的后端应该开发出动态条件查询菜品的功能输入数据是分类id还有菜品名等但是这里因为前后端的接口文档里这个接口只传入了一个分类id所以楼主就没有写动态查询的功能因此前端的添加菜品里的按名称进行搜索也是无法使用的有兴趣的可以自己修改前端源码修改接口。 新增套餐功能
新增套餐接口 下面来完成新增套餐
Controller层
SetmealController
/*** 套餐管理*/
RestController
RequestMapping(/admin/setmeal)
Slf4j
Api(tags 套餐相关接口) //描述类的作用
public class SetmealController {Autowiredprivate SetmealService setmealServices;/*** 新增套餐** param setmealDTO* return*/PostMappingApiOperation(value 新增套餐)public Result save(RequestBody SetmealDTO setmealDTO){log.info(新增套餐:{},setmealDTO);setmealServices.save(setmealDTO);return Result.success();}
}Service层实现类
SetmealServiceImpl
Service
public class SetmealServiceImpl implements SetmealService {Autowiredprivate SetmealMapper setmealMapper;Autowiredprivate SetmealDishMapper setmealDishMapper;/*** 新增套餐同时需要保存套餐和菜品的关联关系** param setmealDTO* return*/Transactional // 事务public void save(SetmealDTO setmealDTO) {Setmeal setmeal new Setmeal();BeanUtils.copyProperties(setmealDTO, setmeal); //套餐ListSetmealDish setmealDishlist setmealDTO.getSetmealDishes(); // 套餐菜品关系setmealMapper.insert(setmeal);long setmealId setmeal.getId(); //返回自动生成的套餐菜品表主键idif (setmealDishlist ! null setmealDishlist.size() 0) {setmealDishlist.forEach(setmealDish - {setmealDish.setSetmealId(setmealId);});//向套餐菜品关系表插入n条数据setmealDishMapper.insertBatch(setmealDishlist);}}
}Mapper层
SetmealMapper
/*** 新增套餐数据** param setmeal*/
AutoFill(OperationType.INSERT) // 公共字段填充
void insert(Setmeal setmeal);SetmealMapper.xml
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.sky.mapper.SetmealMapperinsert idinsert useGeneratedKeystrue keyPropertyidinsert 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
/mapperSetmealDishMapper
/*** 向套餐菜品关系表批量插入数据** param setmealDishlist* return*/
void insertBatch(ListSetmealDish setmealDishlist);SetmealDishMapper.xml
insert idinsertBatchinsert into setmeal_dish (setmeal_id, dish_id, name, price, copies) VALUESforeach collectionsetmealDishlist itemsd separator,(#{sd.setmealId},#{sd.dishId},#{sd.name},#{sd.price},#{sd.copies})/foreach
/insert前后端联调测试新增套餐功能由于未开发套餐分页查询功能固在数据库中查看数据是否插入成功 测试通过提交代码。 套餐分页查询
接口信息 Controller层
SetmealController
/*** 套餐分页查询* param setmealPageQueryDTO* return*/
GetMapping(/page)
ApiOperation(套餐分页查询)
public ResultPageResult page(SetmealPageQueryDTO setmealPageQueryDTO){log.info(套餐分页查询参数为{}, setmealPageQueryDTO);PageResult pageResult setmealServices.pageQuery(setmealPageQueryDTO);return Result.success(pageResult);
}Service层实现类
SetmealServiceImpl
/*** 套餐分页查询** param setmealPageQueryDTO* return*/
public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO){// select * from setmeal limit 0,10//开始分页查询PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());PageSetmealVO page setmealMapper.pageQuery(setmealPageQueryDTO);long total page.getTotal();ListSetmealVO records page.getResult();return new PageResult(total, records);
}Mapper层
SetmealMapper /*** 套餐分页查询* param setmealPageQueryDTO* return*/PageSetmealVO pageQuery(SetmealPageQueryDTO setmealPageQueryDTO);SetmealMapper.xml
select idpageQuery resultTypecom.sky.vo.SetmealVOselects.*,c.name categoryNamefromsetmeal sleft joincategory cons.category_id c.idwhereif testname ! nulland s.name like concat(%,#{name},%)/ifif teststatus ! nulland s.status #{status}/ifif testcategoryId ! nulland s.category_id #{categoryId}/if/whereorder by s.create_time desc
/select测试 测试通过提交代码。 删除套餐
接口传入的套餐ids包含多个id所以需要开发批量删除功能。
删除套餐的时候需要判断当前套餐是否在起售状态否则不能删除需要使用到根据id查询套餐功能里的查询套餐功能根据id查询套餐功能里还包括了根据套餐id查询对应菜品的功能所以干脆先把根据id查询套餐的功能开发了。
根据id查询套餐功能
接口如下 Controller层
SetmealController
GetMapping(/{id})
ApiOperation(根据id查询套餐)
public ResultSetmealVO getById(PathVariable Long id){log.info(根据id查询套餐{}, id);SetmealVO setmealVO setmealServices.getByIdWithDishes(id);return Result.success(setmealVO);
}Service层实现类
SetmealServiceImpl
/*** 根据套餐id查询套餐和其对应菜品* param id* return*/
Override
public SetmealVO getByIdWithDishes(Long id) {//根据id查询套餐数据Setmeal setmeal setmealMapper.getById(id);//根据套餐id查询菜品数据ListSetmealDish setmealDishes setmealDishMapper.getBySetmealId(id);//将查询到的数据封装到VOSetmealVO setmealVO new SetmealVO();BeanUtils.copyProperties(setmeal, setmealVO);setmealVO.setSetmealDishes(setmealDishes);return setmealVO;
}Mapper层
SetmealMapper
/*** 根据套餐id查询套餐数据* param id* return*/
Select(select * from setmeal where id#{id})
Setmeal getById(Long id);SetmealDishMapper
/*** 根据套餐id查询菜品数据* param setmealId* return*/
Select(select * from setmeal_dish where setmeal_id#{setmealId})
ListSetmealDish getBySetmealId(Long setmealId);删除套餐功能
接口如下 Controller层
SetmealController
/*** 套餐批量删除** param ids* return*/
DeleteMapping
ApiOperation(套餐批量删除)
public Result delete(RequestParam ListLong ids) {log.info(套餐批量删除{}, ids);setmealServices.deleteBatch(ids);return Result.success();
}Service层实现类
SetmealServiceImpl
/*** 套餐批量删除* param ids*/
Transactional //事务
public void deleteBatch(ListLong ids) {//判断当前套餐是否能够删除---是否存在起售中的套餐for (Long id : ids) {Setmeal setmeal setmealMapper.getById(id); //在这里需要使用到根据id查询套餐功能的查询套餐if (setmeal.getStatus() StatusConstant.ENABLE) {//当前菜品处于起售中不能删除throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);}}for (Long id : ids) {setmealMapper.deleteById(id);//删除套餐数据setmealDishMapper.deleteBySetmealId(id);//删除套餐菜品关系表中套餐关联的菜品数据}
}Mapper层
SetmealMapper
/*** 根据套餐id删除套餐数据* param id*/
Delete(delete from setmeal where id #{id})
void deleteById(Long id);SetmealDishMapper
/*** 根据套餐id删除套餐菜品关系表中的菜品数据* param setmealId*/
Delete(delete from setmeal_dish where setmeal_id #{setmealId})
void deleteBySetmealId(Long setmealId);测试略由于还未开发套餐起售状态修改功能固可以去数据库直接修改套餐的销售状态然后去swagger或者前端测试删除起售中的套餐同时测试批量删除功能。 测试通过后提交代码到git。 修改套餐
共涉及到5个接口根据id查询套餐(在删除套餐里已完成)、根据类型查询分类已完成、根据分类id查询菜品已完成、图片上传已完成、修改套餐
修改套餐接口信息如下传入的参数和新增套餐是一致的只不过从insert改成了update。 Controller层
SetmealController
/*** 修改套餐** param setmealVO* return*/
PutMapping
ApiOperation(修改菜品)
public Result update(RequestBody SetmealVO setmealVO) {log.info(修改菜品{}, setmealVO);setmealServices.updateWithDishes(setmealVO);return Result.success();
}Service层实现类
SetmealServiceImpl
/*** 根据id修改套餐基本信息和对应的菜品信息* param setmealVO*/
Transactional //事务
public void updateWithDishes(SetmealVO setmealVO) {Setmeal setmeal new Setmeal();BeanUtils.copyProperties(setmealVO, setmeal);//修改套餐表基本信息setmealMapper.update(setmeal);//删除原有的套餐菜品关系表数据setmealDishMapper.deleteBySetmealId(setmealVO.getId());//重新插入套餐菜品关系数据ListSetmealDish setmealDishes setmealVO.getSetmealDishes();if (setmealDishes ! null setmealDishes.size() 0) {setmealDishes.forEach(setmealDish - {setmealDish.setSetmealId(setmealVO.getId());});//向套餐菜品关系表插入n条数据setmealDishMapper.insertBatch(setmealDishes);}
}Mapper层
SetmealMapper
/*** 根据id动态修改套餐数据* param setmeal*/
AutoFill(OperationType.UPDATE) // 公共字段填充
void update(Setmeal setmeal);SetmealMapper.xml
update idupdateupdate setmealsetif testcategoryId ! nullcategory_id #{categoryId},/ifif testname ! nullname #{name},/ifif testprice ! nullprice #{price},/ifif teststatus ! nullstatus #{status},/ifif testdescription ! nulldescription #{description},/ifif testimage ! nullimage #{image},/ifif testupdateTime ! nullupdate_time #{updateTime},/ifif testupdateUser ! nullupdate_user #{updateUser},/if/setwhere id #{id}
/update测试 数据库 测试通过提交代码到github。 起售停售套餐
写这一块的时候发现菜品管理里面的起售停售菜品好像老师也没有带我们写所以干脆一块写了吧。
起售停售菜品 Controller层
DishController
PostMapping(/status/{status})
ApiOperation(修改菜品销售状态)
public Result updateStatus(PathVariable Integer status,Long id){log.info(根据分类id修改菜品销售状态{}, status);dishService.updateStatusById(status,id);return Result.success();
}Service层实现类
DishServiceImpl
/*** 根据菜品id修改菜品销售状态* param status* param id*/
public void updateStatusById(Integer status, Long id) {Dish dish Dish.builder().id(id).status(status).build();dishMapper.update(dish);//直接调用以前的根据id动态修改菜品数据接口就行
}Mapper层
这里直接调用以前的菜品修改的Mapper层接口。
DishMapper
/*** 根据id动态修改菜品数据** param dish*/
AutoFill(value OperationType.UPDATE) // 公共字段填充
void update(Dish dish);DishMapper.xml
update idupdateupdate dishsetif testname ! nullname #{name},/ifif testcategoryId ! nullcategory_id #{categoryId},/ifif testprice ! nullprice #{price},/ifif testimage ! nullimage #{image},/ifif testdescription ! nulldescription #{description},/ifif teststatus ! nullstatus #{status},/ifif testupdateTime ! nullupdate_time #{updateTime},/ifif testupdateUser ! nullupdate_user #{updateUser},/if/setwhere id #{id}
/update起售停售套餐
接口 写法一
Controller层
SetmealController
PostMapping(/status/{status})
ApiOperation(修改套餐销售状态)
public Result updateStatus(PathVariable Integer status,Long id){log.info(根据套餐id修改套餐销售状态{}, status);setmealServices.updateStatusById(status,id);return Result.success();
}Service层实现类
SetmealServiceImpl
Autowired
private DishMapper dishMapper;/*** 根据套餐id修改套餐销售状态** param status* param id*/
public void updateStatusById(Integer status, Long id) {//如果是将套餐的销售状态修改为起售,则需要保证套餐里所有的菜品的销售状态也是起售 否则抛出异常if(statusStatusConstant.ENABLE){ListSetmealDish setmealDishes setmealDishMapper.getBySetmealId(id);//根据套餐id去套餐菜品关系表里找到所有的菜品if(setmealDishes null || setmealDishes.size()0) return; //这里其实可以抛出一个异常给前端 套餐里没有菜品setmealDishes.forEach(setmealDish - {//再根据菜品id去菜品表里找到菜品的销售状态if(dishMapper.getById(setmealDish.getDishId()).getStatus()StatusConstant.DISABLE){throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);}});}Setmeal setmealSetmeal.builder().id(id).status(status).build();setmealMapper.update(setmeal);//直接调用之前写好的根据id动态修改套餐数据接口
}Mapper层
这里直接调用之前写好的根据id动态修改套餐数据接口就行
SetmealMapper
/*** 根据id动态修改套餐数据* param setmeal*/
AutoFill(OperationType.UPDATE) // 公共字段填充
void update(Setmeal setmeal);update idupdateupdate setmealsetif testcategoryId ! nullcategory_id #{categoryId},/ifif testname ! nullname #{name},/ifif testprice ! nullprice #{price},/ifif teststatus ! nullstatus #{status},/ifif testdescription ! nulldescription #{description},/ifif testimage ! nullimage #{image},/ifif testupdateTime ! nullupdate_time #{updateTime},/ifif testupdateUser ! nullupdate_user #{updateUser},/if/setwhere id #{id}
/update测试 测试通过提交代码。 写法二
这里再分享另外一种写法上面这种写法其实比较偷懒因为调用的都是现成的mapper接口得先通过套餐id去套餐菜品关系表里找到套餐菜品关联数据然后拿到所有的菜品id去菜品表里拿到菜品的销售状态。
其实可以直接通过套餐id、套餐表联合菜品表直接拿到菜品数据把逻辑写在sql语句就行
SetmealServiceImpl
Autowired
private DishMapper dishMapper;/*** 套餐起售、停售* param status* param id
*/
public void startOrStop(Integer status, Long id) {//起售套餐时判断套餐内是否有停售菜品有停售菜品提示套餐内包含未启售菜品无法启售if(status StatusConstant.ENABLE){//select a.* from dish a left join setmeal_dish b on a.id b.dish_id where b.setmeal_id ?ListDish dishList dishMapper.getBySetmealId(id);if(dishList ! null dishList.size() 0){dishList.forEach(dish - {if(StatusConstant.DISABLE dish.getStatus()){throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);}});}}Setmeal setmeal Setmeal.builder().id(id).status(status).build();setmealMapper.update(setmeal);
}DishMapper
/*** 根据套餐id查询菜品* param setmealId* return
*/
Select(select a.* from dish a left join setmeal_dish b on a.id b.dish_id where b.setmeal_id #{setmealId})
ListDish getBySetmealId(Long setmealId);