北京网站制作长沙,南联网站建设推广,呼伦贝尔做网站公司,辽宁工程咨询招投标交易平台1. 新增套餐
1 需求分析和设计 业务规则#xff1a; 套餐名称唯一 套餐必须属于某个分类 套餐必须包含菜品 名称、分类、价格、图片为必填项 添加菜品窗口需要根据分类类型来展示菜品 新增的套餐默认为停售状态 2 代码实现
1 根据分类id查询菜品
DishControllerGetMa…1. 新增套餐
1 需求分析和设计 业务规则 套餐名称唯一 套餐必须属于某个分类 套餐必须包含菜品 名称、分类、价格、图片为必填项 添加菜品窗口需要根据分类类型来展示菜品 新增的套餐默认为停售状态 2 代码实现
1 根据分类id查询菜品
DishControllerGetMapping(/list)
ApiOperation(根据分类id查询菜品列表)
public Result queryDishesByCategoryId(Long categoryId){return dishService.queryDishesByCategoryId(categoryId);
}----------------
DishService/*** 根据分类id查询菜品列表* param categoryId 分类id* return categoryId对应的菜品列表*/
Result queryDishesByCategoryId(Long categoryId);----------------
DishServiceImplOverride
public Result queryDishesByCategoryId(Long categoryId) {ListDish dishes dishMapper.selectByCategoryId(categoryId);return Result.success(dishes);
}----------------
DishMapperSelect(select * from dish where category_id #{categoryId})
ListDish selectByCategoryId(Long categoryId);
2 新增套餐 SetmealControllerpackage com.sky.controller.admin;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;RestController
Api(tags 套餐管理相关接口)
RequestMapping(/admin/setmeal)
public class SetmealController {Autowiredprivate SetmealService setmealService;PostMappingApiOperation(新增套餐)public Result addSetmeal(RequestBody SetmealDTO dto){return setmealService.addSetmeal(dto);}
}-----------------
SetmealServicepackage com.sky.service;
import com.sky.dto.SetmealDTO;
import com.sky.result.Result;
import java.util.List;public interface SetmealService {Result addSetmeal(SetmealDTO dto);}-----------------
SetmealServiceImplpackage com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.SetmeatlDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import java.util.List;
Service
public class SetmealServiceImpl implements SetmealService {Autowiredprivate SetmealMapper setmealMapper;Autowiredprivate SetmeatlDishMapper setmealDishMapper;OverrideTransactionalpublic Result addSetmeal(SetmealDTO dto) {//1. 保存套餐Setmeal setmeal new Setmeal();BeanUtils.copyProperties(dto, setmeal);setmealMapper.insert(setmeal);//2. 保存套餐里关联的菜品ListSetmealDish setmealDishes dto.getSetmealDishes();if (setmealDishes ! null setmealDishes.size() 0) {setmealDishes.forEach(setmealDish - setmealDish.setSetmealId(setmeal.getId()));setmealDishMapper.batchInsert(setmealDishes);}return Result.success();}
}-----------------
SetmealMapper/*** 新增套餐* param setmeal*/
AutoFill(OperationType.INSERT)
Options(useGeneratedKeys true, keyProperty id)
Insert(insert 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}))
void insert(Setmeal setmeal);-------------------
SetmeatlDishMappervoid batchInsert(ListSetmealDish setmealDishes);----------------
SetmeatlDishMapper.xmlinsert idbatchInsertinsert into setmeal_dish (setmeal_id, dish_id, name, price, copies) valuesforeach collectionsetmealDishes itemsd separator,(#{sd.setmealId}, #{sd.dishId}, #{sd.name}, #{sd.price}, #{sd.copies})/foreach
/insert
2. 套餐分页查询
1 需求分析和设计 业务规则 根据页码进行分页展示 每页展示10条数据 可以根据需要按照套餐名称、分类、售卖状态进行查询 2 代码实现
SetmealControllerGetMapping(/page)
ApiOperation(分页查询套餐)
public Result querySetmealsByPage(SetmealPageQueryDTO dto){return setmealService.querySetmealsByPage(dto);
}----------------
SetmealService/*** 分页查询套餐* param dto* return*/
Result querySetmealsByPage(SetmealPageQueryDTO dto);----------------
SetmealServiceImplOverride
public Result querySetmealsByPage(SetmealPageQueryDTO dto) {//1. 开启分页PageHelper.startPage(dto.getPage(), dto.getPageSize());//2. 查询列表PageSetmealVO page setmealMapper.selectByPage(dto);//3. 封装结果PageResult pageResult new PageResult(page.getTotal(), page.getResult());return Result.success(pageResult);
}--------------
SetmealMapper/*** 分页查询套餐列表* param dto* return*/
PageSetmealVO selectByPage(SetmealPageQueryDTO dto);--------------
SetmealMapper.xml?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.sky.mapper.SetmealMapperselect idselectByPage resultTypecom.sky.vo.SetmealVOselect s.*, c.name categoryName from setmeal s left join category c on s.category_id c.idwhereif testname!null and name.length()0and s.name like concat(%, #{name}, %)/ifif testcategoryId!nulland s.category_id #{categoryId}/ifif teststatus!nulland s.status #{status}/if/whereorder by s.create_time desc/select
/mapper
3. 删除套餐
1 需求分析和设计 业务规则 可以一次删除一个套餐也可以批量删除套餐 起售中的套餐不能删除 2 代码实现
SetmealControllerDeleteMapping
ApiOperation(删除套餐)
public Result batchDeleteSetmealsByIds(RequestParam ListLong ids){return setmealService.batchDeleteSetmealsByIds(ids);
}----------
SetmealService/*** 批量删除套餐* param ids
*/
Result batchDeleteSetmealsByIds(ListLong ids);----------
SetmealServiceImplOverride
Transactional
public Result batchDeleteSetmealsByIds(ListLong ids) {//1. 如果有某个套餐是“起售”状态则不允许删除int count setmealMapper.selectEnableSetmealsCount(ids);if (count 0) {throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);}//2. 删除套餐setmealMapper.batchDeleteByIds(ids);//3. 删除套餐关联的菜品setmealDishMapper.batchDeleteBySetmealIds(ids);return Result.success();
}-----------
SetmealMapper/*** 查询 ids对应的套餐中起售状态的套餐 数量* param ids* return*/
int selectEnableSetmealsCount(ListLong ids);/*** 根据ids批量删除套餐* param ids*/
void batchDeleteByIds(ListLong ids);------------
SetmealMapper.xmlselect idselectEnableSetmealsCount resultTypeintselect count(*) from setmeal where status 1 andforeach collectionids itemid separator, openid in( close)#{id}/foreach
/selectdelete idbatchDeleteByIdsdelete from setmeal where id inforeach collectionids itemid separator, open( close)#{id}/foreach
/delete---------------
SetmeatlDishMapper/*** 根据套餐ids批量删除这些套餐包含的菜品关联关系* param ids*/
void batchDeleteBySetmealIds(ListLong ids);-----------
SetmeatlDishMapper.xmldelete idbatchDeleteBySetmealIdsdelete from setmeal_dish where setmeal_id inforeach collectionids itemid separator, open( close)#{id}/foreach
/delete
4. 修改套餐
1 需求分析和设计 接口设计共涉及到5个接口 根据id查询套餐 根据类型查询分类已完成 根据分类id查询菜品已完成 图片上传已完成 修改套餐 2 代码实现
1 根据id查询套餐
SetmealControllerGetMapping(/{id})
ApiOperation(根据id查询套餐)
public Result querySetmealById(PathVariable(id) Long id) {return setmealService.querySetmealById(id);
}----------
SetmealService/*** 根据id查询套餐* param id* return*/
Result querySetmealById(Long id);-----------
SetmealServiceImplOverride
public Result querySetmealById(Long id) {//查询套餐信息Setmeal setmeal setmealMapper.selectById(id);//查询套餐关联的菜品列表ListSetmealDish setmealDishes setmealDishMapper.selectBySetmealId(id);//封装成VO对象SetmealVO vo new SetmealVO();BeanUtils.copyProperties(setmeal, vo);vo.setSetmealDishes(setmealDishes);return Result.success(vo);
}----------
SetmealMapper/*** 根据id查询套餐* param id* return*/
Select(select * from setmeal where id #{id})
Setmeal selectById(Long id);----------
SetmeatlDishMapper/*** 根据套餐id查询套餐内包含的菜品* param setmealId* return*/
Select(select * from setmeal_dish where setmeal_id #{setmealId})
ListSetmealDish selectBySetmealId(Long setmealId);
2 修改套餐
SetmealControllerPutMapping
ApiOperation(修改套餐)
public Result updateSetmeal(RequestBody SetmealDTO dto){return setmealService.updateSetmeal(dto);
}----------
SetmealService/*** 修改套餐* param dto* return*/
Result updateSetmeal(SetmealDTO dto);----------
SetmealServiceImplOverride
Transactional
public Result updateSetmeal(SetmealDTO dto) {//1. 修改套餐Setmeal setmeal new Setmeal();BeanUtils.copyProperties(dto, setmeal);setmealMapper.updateById(setmeal);//2. 修改套餐关联的菜品//2.1 删除套餐 之前关联的菜品setmealDishMapper.batchDeleteBySetmealIds(Collections.singletonList(dto.getId()));//2.2 重新添加 关联的菜品ListSetmealDish setmealDishes dto.getSetmealDishes();if (setmealDishes ! null setmealDishes.size() 0) {setmealDishes.forEach(setmealDish - setmealDish.setSetmealId(setmeal.getId()));setmealDishMapper.batchInsert(setmealDishes);}return Result.success();
}-----------
SetmealMapper/*** 根据id修改套餐* param setmeal*/
AutoFill(OperationType.UPDATE)
void updateById(Setmeal setmeal);-----------
SetmealMapper.xmlupdate idupdateByIdUPDATE setmealsetif testcategoryId!nullcategory_id #{categoryId},/ifif testname!null and name.length()0name #{name},/ifif testprice!nullprice #{price},/ifif teststatus!nullstatus #{status},/ifif testdescription!null and description.length()0description #{description},/ifif testimage!null and image.length()0image #{image},/ifif testupdateTime!nullupdate_time #{updateTime},/ifif testupdateUser!nullupdate_user #{updateUser}/if/setWHERE id #{id}
/update
5. 起售停售套餐
1 需求分析和设计 业务规则 可以对状态为起售的套餐进行停售操作可以对状态为停售的套餐进行起售操作 起售的套餐可以展示在用户端停售的套餐不能展示在用户端 起售套餐时如果套餐内包含停售的菜品则不能起售 2 代码实现
SetmealControllerPostMapping(/status/{status})
ApiOperation(启用禁用套餐)
public Result updateStatus(PathVariable(status) Integer status, Long id){return setmealService.updateStatus(id, status);
}--------------
SetmealService/*** 套餐起售、停售* param status* param id
*/
Result updateStatus(Long id, Integer status);-------------
SetmealServiceImplAutowired
private DishMapper dishMapper;Override
public Result updateStatus(Long id, Integer status) {//起售套餐时如果包含了“停售”状态的菜品则不允许起售if (StatusConstant.ENABLE.equals(status)) {//查询套餐关联的所有菜品ListDish dishes dishMapper.selectBySetmealId(id);//如果有任意一个菜品是“停售”状态就抛出异常long count dishes.stream().filter(dish - dish.getStatus().equals(StatusConstant.DISABLE)).count();if (count 0) {throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);}}//起售/停售套餐Setmeal setmeal Setmeal.builder().id(id).status(status).build();setmealMapper.updateById(setmeal);return Result.success();
}-----------
DishMapper/*** 根据套餐id查询菜品* param setmealId* return
*/
Select(select * from dish where id in(select dish_id from setmeal_dish where setmeal_id #{setmealId}))
ListDish selectBySetmealId(Long setmealId);