Skip to content

Commit

Permalink
套餐修改
Browse files Browse the repository at this point in the history
  • Loading branch information
Q-1515 committed Jul 22, 2022
1 parent ece5ee2 commit e02e8f1
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.reggie.result.PageResult;
import com.reggie.result.R;
import com.reggie.service.SetmealService;
import com.reggie.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -63,5 +64,33 @@ public R<String> delete(@RequestParam List<Long> ids) {
return R.success("删除成功");
}

/**
* id查询套餐
*
* @param id 套餐id
* @return setmealVO 回显修改套餐的数据
*/
@GetMapping("/{id}")
@ApiOperation("id查询套餐接口")
public R<SetmealVO> getById(@PathVariable Long id) {
log.info("id查询套餐接口:{}", id);
SetmealVO setmealVO = setmealService.getById(id);
return R.success(setmealVO);
}

/**
* 修改套餐
*
* @param setmealDTO 修改的套餐信息
* @return success
*/
@PutMapping
@ApiOperation("修改套餐")
public R update(@RequestBody SetmealDTO setmealDTO) {
log.info("修改套餐:{}", setmealDTO);
setmealService.update(setmealDTO);
return R.success();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ public interface SetmealMapper {

//套餐ids删除套餐表中的数据
void deleteById(List<Long> ids);

//id查询套餐查询所有套餐关联数据
SetmealVO getByIdWithDish(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.reggie.dto.SetmealDTO;
import com.reggie.dto.SetmealPageQueryDTO;
import com.reggie.result.PageResult;
import com.reggie.vo.SetmealVO;

import java.util.List;

Expand All @@ -16,4 +17,10 @@ public interface SetmealService {

//批量删除的套餐
void deleteBatch(List<Long> ids);

//id查询套餐
SetmealVO getById(Long id);

//修改套餐
void update(SetmealDTO setmealDTO);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
Expand Down Expand Up @@ -98,6 +99,42 @@ public void deleteBatch(List<Long> ids) {
setmealDishMapper.deleteBySetmealId(ids);
}

/**
* id查询套餐
*
* @param id 套餐id
* @return setmealVO 回显修改套餐的数据
*/
public SetmealVO getById(Long id) {
return setmealMapper.getByIdWithDish(id);
}

/**
* 修改套餐
*
* @param setmealDTO 修改的套餐信息
*/
@Override
public void update(SetmealDTO setmealDTO) {
//拷贝套餐信息
Setmeal setmeal = new Setmeal();
BeanUtils.copyProperties(setmealDTO, setmeal);
//更新套餐数据
setmealMapper.updatesByIds(setmeal);

//套餐id
Long setmealId = setmealDTO.getId();

//删除套餐和菜品的关联关系,操作setmeal_dish表,执行delete
List<Long> ids = new ArrayList<>();
ids.add(setmealId);
setmealDishMapper.deleteBySetmealId(ids);

List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
setmealDishes.forEach(setmealDish -> {
setmealDish.setSetmealId(setmealId);
});
//3、重新插入套餐和菜品的关联关系,操作setmeal_dish表,执行insert
setmealDishMapper.insertBatch(setmealDishes);
}
}
18 changes: 18 additions & 0 deletions reggie_server/src/main/resources/mapper/SetmealMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,23 @@
</foreach>
</delete>

<!--封装套餐关联数据-->
<resultMap id="SetmealVOMap" type="com.reggie.vo.SetmealVO" autoMapping="true">
<id column="id" property="id"/>
<collection property="setmealDishes" ofType="setmealDish" autoMapping="true">
<id column="sd_id" property="id"/>
<result column="sd_name" property="name"/>
<result column="sd_price" property="price"/>
</collection>
</resultMap>
<!--id查询套餐查询所有套餐关联数据-->
<select id="getByIdWithDish" resultMap="SetmealVOMap">
select setmeal.*, sd.id sd_id, sd.setmeal_id, sd.dish_id, sd.name sd_name, sd.price sd_price, sd.copies
from setmeal
left join setmeal_dish sd
on setmeal.id = sd.setmeal_id
where setmeal.id = #{id}
</select>


</mapper>

0 comments on commit e02e8f1

Please sign in to comment.