苍穹外卖--菜品管理-修改功能实现
This commit is contained in:
@@ -5,6 +5,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;
|
||||||
@@ -52,4 +53,20 @@ public class DishController {
|
|||||||
dishService.deleteBath(ids);
|
dishService.deleteBath(ids);
|
||||||
return Result.success();
|
return Result.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
@ApiOperation("根据id查询菜品")
|
||||||
|
public Result<DishVO> getById(@PathVariable Long id){ //@RequestParam:能够使用springmvc框架,对1,2,3 参数进行切割
|
||||||
|
log.info("根据id查询菜品:{}",id);
|
||||||
|
DishVO dishVO = dishService.getByIdWithFlavor(id);
|
||||||
|
return Result.success(dishVO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping
|
||||||
|
@ApiOperation("修改菜品")
|
||||||
|
public Result getById(@RequestBody DishDTO dishDTO){ //@RequestParam:能够使用springmvc框架,对1,2,3 参数进行切割
|
||||||
|
log.info("修改菜品:{}",dishDTO);
|
||||||
|
dishService.updateWithFlavor(dishDTO);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.sky.mapper;
|
|||||||
|
|
||||||
import com.sky.entity.DishFlavor;
|
import com.sky.entity.DishFlavor;
|
||||||
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;
|
||||||
|
|
||||||
@@ -18,4 +19,12 @@ public interface DishFlavorMapper {
|
|||||||
* @param ids
|
* @param ids
|
||||||
*/
|
*/
|
||||||
void deleteBath(List<Long> ids);
|
void deleteBath(List<Long> ids);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据菜品id,查询对应口味数据
|
||||||
|
* @param dishId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Select("select * from dish_flavor where dish_id = #{dishId}")
|
||||||
|
List<DishFlavor> getByDishId(Long dishId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,4 +50,11 @@ public interface DishMapper {
|
|||||||
* @param ids
|
* @param ids
|
||||||
*/
|
*/
|
||||||
void deleteBath(List<Long> ids);
|
void deleteBath(List<Long> ids);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新菜品表
|
||||||
|
* @param dish
|
||||||
|
*/
|
||||||
|
@AutoFill(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;
|
||||||
|
|
||||||
@@ -24,4 +25,17 @@ public interface DishService {
|
|||||||
* @param ids
|
* @param ids
|
||||||
*/
|
*/
|
||||||
void deleteBath(List<Long> ids);
|
void deleteBath(List<Long> ids);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查询菜品
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
DishVO getByIdWithFlavor(Long id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改菜品
|
||||||
|
* @param dishDTO
|
||||||
|
*/
|
||||||
|
void updateWithFlavor(DishDTO dishDTO);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@@ -97,4 +98,44 @@ public class DishServiceImpl implements DishService {
|
|||||||
dishMapper.deleteBath(ids);
|
dishMapper.deleteBath(ids);
|
||||||
dishFlavorMapper.deleteBath(ids);
|
dishFlavorMapper.deleteBath(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查询菜品
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public DishVO getByIdWithFlavor(Long id) {
|
||||||
|
//1.查询2张表:菜品表和口味表
|
||||||
|
Dish dish = dishMapper.getById(id);
|
||||||
|
|
||||||
|
List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);
|
||||||
|
//对象属性拷贝
|
||||||
|
DishVO dishVO = new DishVO();
|
||||||
|
BeanUtils.copyProperties(dish,dishVO);
|
||||||
|
dishVO.setFlavors(flavors);
|
||||||
|
return dishVO;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改菜品
|
||||||
|
* @param dishDTO
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void updateWithFlavor(DishDTO dishDTO) {
|
||||||
|
//1.修改的表:Dish Dish_flavor
|
||||||
|
Dish dish = new Dish();
|
||||||
|
BeanUtils.copyProperties(dishDTO,dish);
|
||||||
|
//2.修改菜品表
|
||||||
|
dishMapper.update(dish);
|
||||||
|
//3.修改口味表:先执行删除对应菜品的全部口味,再重新插入
|
||||||
|
dishFlavorMapper.deleteBath(Arrays.asList(dishDTO.getId()));
|
||||||
|
List<DishFlavor> flavors = dishDTO.getFlavors();
|
||||||
|
//确保口味的菜品id必须有
|
||||||
|
flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDTO.getId()));
|
||||||
|
if (flavors != null && flavors.size() > 0) {
|
||||||
|
dishFlavorMapper.insertBath(flavors);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ logging:
|
|||||||
mapper: debug
|
mapper: debug
|
||||||
service: info
|
service: info
|
||||||
controller: info
|
controller: info
|
||||||
|
org.springframework.jdbc.support.JdbcTransactionManager: debug #spring事务管理日志
|
||||||
|
|
||||||
sky:
|
sky:
|
||||||
jwt:
|
jwt:
|
||||||
@@ -41,3 +42,4 @@ sky:
|
|||||||
endpoint: ${sky.alioss.endpoint}
|
endpoint: ${sky.alioss.endpoint}
|
||||||
bucket-name: ${sky.alioss.bucket-name}
|
bucket-name: ${sky.alioss.bucket-name}
|
||||||
region: ${sky.alioss.region}
|
region: ${sky.alioss.region}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
values (#{name},#{categoryId},#{price},#{image},#{description},#{createTime},#{updateTime},#{createUser},#{updateUser},#{status})
|
values (#{name},#{categoryId},#{price},#{image},#{description},#{createTime},#{updateTime},#{createUser},#{updateUser},#{status})
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
|
|
||||||
<select id="pageQuery" resultType="com.sky.vo.DishVO">
|
<select id="pageQuery" resultType="com.sky.vo.DishVO">
|
||||||
SELECT d.*,c.name categoryName FROM dish d LEFT JOIN category c ON d.category_id = c.id
|
SELECT d.*,c.name categoryName FROM dish d LEFT JOIN category c ON d.category_id = c.id
|
||||||
<where>
|
<where>
|
||||||
@@ -32,4 +33,19 @@
|
|||||||
#{id}
|
#{id}
|
||||||
</foreach>
|
</foreach>
|
||||||
</delete>
|
</delete>
|
||||||
|
|
||||||
|
<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