修改菜品业务代码开发
This commit is contained in:
@@ -7,6 +7,7 @@ import com.sky.dto.DishPageQueryDTO;
|
|||||||
import com.sky.result.PageResult;
|
import com.sky.result.PageResult;
|
||||||
import com.sky.result.Result;
|
import com.sky.result.Result;
|
||||||
import com.sky.service.DishService;
|
import com.sky.service.DishService;
|
||||||
|
import com.sky.vo.DishVO;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -75,4 +76,37 @@ public class DishController {
|
|||||||
|
|
||||||
return Result.success();
|
return Result.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查询菜品
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
@ApiOperation("根据id查询菜品")
|
||||||
|
public Result<DishVO> getById(@PathVariable Long id){
|
||||||
|
log.info("根据id查询菜品:{}", id);
|
||||||
|
|
||||||
|
DishVO dishVO = dishService.getByIdWithFlavor(id);
|
||||||
|
|
||||||
|
return Result.success(dishVO);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改菜品
|
||||||
|
* @param dishDTO
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PutMapping
|
||||||
|
@ApiOperation("修改菜品")
|
||||||
|
public Result update(@RequestBody DishDTO dishDTO){
|
||||||
|
|
||||||
|
log.info("修改菜品:{}", dishDTO);
|
||||||
|
|
||||||
|
dishService.updateWithFlavor(dishDTO);
|
||||||
|
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ package com.sky.mapper;
|
|||||||
import com.sky.entity.DishFlavor;
|
import com.sky.entity.DishFlavor;
|
||||||
import org.apache.ibatis.annotations.Delete;
|
import org.apache.ibatis.annotations.Delete;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -30,4 +31,13 @@ public interface DishFlavorMapper {
|
|||||||
* @param dishIds
|
* @param dishIds
|
||||||
*/
|
*/
|
||||||
void deleteByDishIds(List<Long> dishIds);
|
void deleteByDishIds(List<Long> dishIds);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据菜品id查询对应的口味数据
|
||||||
|
* @param dishId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Select("select * from dish_flavor where dish_id = #{dishId}")
|
||||||
|
List<DishFlavor> getByDishId(Long dishId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,4 +62,12 @@ public interface DishMapper {
|
|||||||
* @param ids
|
* @param ids
|
||||||
*/
|
*/
|
||||||
void deleteByIds(List<Long> ids);
|
void deleteByIds(List<Long> ids);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id动态修改菜品数据
|
||||||
|
* @param dish
|
||||||
|
*/
|
||||||
|
@AutoFill(value = OperationType.UPDATE)
|
||||||
|
void update(Dish dish);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.sky.service;
|
|||||||
import com.sky.dto.DishDTO;
|
import com.sky.dto.DishDTO;
|
||||||
import com.sky.dto.DishPageQueryDTO;
|
import com.sky.dto.DishPageQueryDTO;
|
||||||
import com.sky.result.PageResult;
|
import com.sky.result.PageResult;
|
||||||
|
import com.sky.vo.DishVO;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -29,4 +30,19 @@ public interface DishService {
|
|||||||
* @param ids
|
* @param ids
|
||||||
*/
|
*/
|
||||||
void deleteBatch(List<Long> ids);
|
void deleteBatch(List<Long> ids);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查询菜品和对应的口味数据
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
DishVO getByIdWithFlavor(Long id);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id修改菜品信息及其关联的口味信息
|
||||||
|
* @param dishDTO
|
||||||
|
*/
|
||||||
|
void updateWithFlavor(DishDTO dishDTO);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -114,4 +114,52 @@ public class DishServiceImpl implements DishService {
|
|||||||
dishFlavorMapper.deleteByDishIds(ids);
|
dishFlavorMapper.deleteByDishIds(ids);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查询菜品和对应的口味数据
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public DishVO getByIdWithFlavor(Long id) {
|
||||||
|
|
||||||
|
//根据id查询菜品数据
|
||||||
|
Dish dish = dishMapper.getById(id);
|
||||||
|
|
||||||
|
//根据id查询菜品关联的口味
|
||||||
|
List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);
|
||||||
|
|
||||||
|
// 再封装到一起 DishVO
|
||||||
|
DishVO dishVO = new DishVO();
|
||||||
|
BeanUtils.copyProperties(dish, dishVO);
|
||||||
|
dishVO.setFlavors(dishFlavors);
|
||||||
|
return dishVO;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id修改菜品信息及其关联的口味信息
|
||||||
|
* @param dishDTO
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void updateWithFlavor(DishDTO dishDTO) {
|
||||||
|
Dish dish = new Dish();
|
||||||
|
BeanUtils.copyProperties(dishDTO, dish);
|
||||||
|
|
||||||
|
//修改菜品表基本信息
|
||||||
|
dishMapper.update(dish);
|
||||||
|
|
||||||
|
//先删除原有的口味数据
|
||||||
|
dishFlavorMapper.deleteByDishId(dishDTO.getId());
|
||||||
|
|
||||||
|
//再插入新的口味数据
|
||||||
|
List<DishFlavor> flavors = dishDTO.getFlavors();
|
||||||
|
if (flavors != null && flavors.size() > 0) {
|
||||||
|
flavors.forEach(dishFlavor -> {
|
||||||
|
dishFlavor.setDishId(dishDTO.getId());
|
||||||
|
});
|
||||||
|
dishFlavorMapper.insertBatch(flavors);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@
|
|||||||
(#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser}, #{status})
|
(#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser}, #{status})
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
|
|
||||||
<delete id="deleteByIds">
|
<delete id="deleteByIds">
|
||||||
delete from dish where id in
|
delete from dish where id in
|
||||||
<foreach collection="ids" item="id" separator="," open="(" close=")">
|
<foreach collection="ids" item="id" separator="," open="(" close=")">
|
||||||
@@ -18,7 +17,6 @@
|
|||||||
</foreach>
|
</foreach>
|
||||||
</delete>
|
</delete>
|
||||||
|
|
||||||
|
|
||||||
<select id="pageQuery" resultType="com.sky.vo.DishVO">
|
<select id="pageQuery" resultType="com.sky.vo.DishVO">
|
||||||
select d.*, c.name as categoryName from dish d left outer join category c on d.category_id = c.id
|
select d.*, c.name as categoryName from dish d left outer join category c on d.category_id = c.id
|
||||||
<where>
|
<where>
|
||||||
@@ -34,4 +32,20 @@
|
|||||||
</where>
|
</where>
|
||||||
order by d.create_time desc
|
order by d.create_time desc
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<update id="update">
|
||||||
|
update 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>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
Reference in New Issue
Block a user