同菜品管理,练习使用
SetmealController
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealController {
@Autowired
SetmealService setmealService;
@PostMapping
@ApiOperation("新增套餐")
public Result save(@RequestBody SetmealDTO setmealDTO){
log.info("新增套餐:{}",setmealDTO);
setmealService.saveWithDish(setmealDTO);
return Result.success();
}
//套餐分页查询
@GetMapping("/page")
@ApiOperation("套餐分页查询套餐")
public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
PageResult pageResult=setmealService.pageResult(setmealPageQueryDTO);
return Result.success(pageResult);
}
/**
* 删除套餐
* @param
* @param
* @return
*/
@DeleteMapping
@ApiOperation("删除套餐")
public Result delete(@RequestParam List<Long> ids){
setmealService.deleteBatch(ids);
return Result.success();
}
/**
* 根据id查询套餐
* @param
* @param
* @return
*/
@GetMapping("/{id}")
@ApiOperation("根据id查询套餐")
public Result<SetmealVO> getById(@PathVariable Long id){
SetmealVO setmealVO= setmealService.getByIdWithDish(id);
return Result.success(setmealVO);
}
@PutMapping
@ApiOperation("修改套餐")
public Result update(@RequestBody SetmealDTO setmealDTO){
setmealService.updateWithDish(setmealDTO);
return Result.success();
}
/**
* 套餐起售停售
* @param status
* @param id
* @return
*/
@PostMapping("/status/{status}")
@ApiOperation("菜品起售停售")
public Result<String> startOrStop(@PathVariable Integer status, Long id){
setmealService.startOrStop(status,id);
return Result.success();
}
}
SetmealService
public interface SetmealService {
void saveWithDish(SetmealDTO setmealDTO);
PageResult pageResult(SetmealPageQueryDTO setmealPageQueryDTO);
void deleteBatch(List<Long> ids);
SetmealVO getByIdWithDish(Long id);
void updateWithDish(SetmealDTO setmealDTO);
void startOrStop(Integer status, Long id);
}
SetmealServiceImpl
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
@Autowired
private SetmealMapper setmealMapper;
@Autowired
private SetmealDishMapper setmealDishMapper;
@Transactional
//新增套餐
@Override
public void saveWithDish(SetmealDTO setmealDTO) {
Setmeal setmeal = new Setmeal();
BeanUtils.copyProperties(setmealDTO, setmeal);
setmealMapper.insert(setmeal);
Long setmealId = setmeal.getId();
List<SetmealDish> dishes = setmealDTO.getSetmealDishes();
if (dishes != null && dishes.size() > 0) {
dishes.forEach(setmealDish -> {
setmealDish.setSetmealId(setmealId);
});
//插入n条数据
setmealDishMapper.insertBatch(dishes);
}
}
@Override
public PageResult pageResult(SetmealPageQueryDTO setmealPageQueryDTO) {
PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
return new PageResult(page.getTotal(), page.getResult());
}
@Override
public void deleteBatch(List<Long> ids) {
for (Long id : ids) {
setmealMapper.deleteByid(id);
setmealDishMapper.deleteBySetmealId(id);
}
}
@Override
public SetmealVO getByIdWithDish(Long id) {
Setmeal setmeal = setmealMapper.getById(id);
List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
SetmealVO setmealVO = new SetmealVO();
BeanUtils.copyProperties(setmeal, setmealVO);
setmealVO.setSetmealDishes(setmealDishes);
return setmealVO;
}
@Override
public void updateWithDish(SetmealDTO setmealDTO) {
Setmeal setmeal = new Setmeal();
BeanUtils.copyProperties(setmealDTO, setmeal);
setmealMapper.update(setmeal);
setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
List<SetmealDish> dishes = setmealDTO.getSetmealDishes();
if (dishes != null && dishes.size() > 0) {
dishes.forEach(setmealDish -> {
setmealDish.setSetmealId(setmealDTO.getId());
});
setmealDishMapper.insertBatch(dishes);
}
}
@Transactional
@Override
public void startOrStop(Integer status, Long id) {
Setmeal setmeal = Setmeal.builder()
.id(id)
.status(status)
.build();
setmealMapper.update(setmeal);
}
}
SetmealMapper
@Mapper
public interface SetmealMapper {
/**
* 根据分类id查询套餐的数量
* @param id
* @return
*/
@Select("select count(id) from setmeal where category_id = #{categoryId}")
Integer countByCategoryId(Long id);
/**
* 根据id修改套餐
*
* @param setmeal
*/
@AutoFill(OperationType.UPDATE)
void update(Setmeal setmeal);
@AutoFill(value = OperationType.INSERT)
void insert(Setmeal setmeal);
Page<SetmealVO> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO);
@Delete("delete from setmeal where id=#{id}")
void deleteByid(Long id);
@Select("select*from setmeal where id =#{id}")
Setmeal getById(Long id);
}
SetmealDishMapper
import java.util.List;
//根据菜品id查询套餐id
@Mapper
public interface SetmealDishMapper {
List<Long> getSetmealDishIds(List<Long> dishIds);
List<Long> getSetmealIdsByDishIds(List<Long> dishIds);
void insertBatch(List<SetmealDish> dishes);
@Delete("delete from setmeal_dish where setmeal_id =#{id}")
void deleteBySetmealId(Long id);
@Select("select*from setmeal_dish where setmeal_id =#{id}")
List<SetmealDish> getBySetmealId(Long id);
}
SetmealMapper.xml
<?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.sky.mapper.SetmealMapper">
<update id="update" parameterType="Setmeal">
update 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="status != null">
status = #{status},
</if>
<if test="description != null">
description = #{description},
</if>
<if test="image != null">
image = #{image},
</if>
<if test="updateTime != null">
update_time = #{updateTime},
</if>
<if test="updateUser != null">
update_user = #{updateUser}
</if>
</set>
where id = #{id}
</update>
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into 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>
<select id="pageQuery" resultType="com.sky.vo.SetmealVO">
select d.*,c.name categoryName from setmeal d left join category 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>
</mapper>
SetmealDishMapper.xml
<?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.sky.mapper.SetmealDishMapper">
<select id="getSetmealDishIds" resultType="java.lang.Long">
select setmeal_id from setmeal_dish where dish_id in
<foreach collection="dishIds" item="dishId" separator="," open="(" close=")">
#{dishId}
</foreach>
</select>
<select id="getSetmealIdsByDishIds" resultType="java.lang.Long">
select setmeal_id from setmeal_dish where dish_id in
<foreach collection="dishIds" item="dishId" separator="," open="(" close=")">
#{dishId}
</foreach>
</select>
<insert id="insertBatch">
insert into setmeal_dish(setmeal_id, dish_id, name, price, copies) VALUES
<foreach collection="dishes" item="aa" separator=",">
(#{aa.setmealId},#{aa.dishId},#{aa.name},#{aa.price},#{aa.copies})
</foreach>
</insert>
</mapper>