新增菜品业务代码开发
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.sky;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.sky.config;
|
||||
|
||||
|
||||
import com.sky.properties.AliOssProperties;
|
||||
import com.sky.utils.AliOssUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 配置类 用于创建AliOssUtil对象
|
||||
*/
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class OssConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties) {
|
||||
log.info("开始创建阿里云文件上传工具类对象:{}", aliOssProperties);
|
||||
return new AliOssUtil(aliOssProperties.getEndpoint(),
|
||||
aliOssProperties.getAccessKeyId(),
|
||||
aliOssProperties.getAccessKeySecret(),
|
||||
aliOssProperties.getBucketName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.sky.controller.admin;
|
||||
|
||||
|
||||
import com.sky.constant.MessageConstant;
|
||||
import com.sky.result.Result;
|
||||
import com.sky.utils.AliOssUtil;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 通用接口
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/admin/common")
|
||||
@Api(tags = "通用接口")
|
||||
@Slf4j
|
||||
public class CommonController {
|
||||
|
||||
|
||||
@Autowired
|
||||
private AliOssUtil aliOssUtil;
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/upload")
|
||||
@ApiOperation("文件上传")
|
||||
public Result<String> upload(MultipartFile file) {
|
||||
log.info("文件上传:{}", file);
|
||||
|
||||
try {
|
||||
//原始文件名
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
//截取后缀
|
||||
String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
|
||||
String objectName = UUID.randomUUID().toString() + extension;
|
||||
|
||||
|
||||
//文件的请求路径
|
||||
String filePath = aliOssUtil.upload(file.getBytes(), objectName);
|
||||
return Result.success(filePath);
|
||||
} catch (IOException e) {
|
||||
log.error("文件上传失败:{}", e);
|
||||
}
|
||||
|
||||
return Result.error(MessageConstant.UPLOAD_FAILED);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.sky.controller.admin;
|
||||
|
||||
|
||||
import com.sky.constant.MessageConstant;
|
||||
import com.sky.dto.DishDTO;
|
||||
import com.sky.result.Result;
|
||||
import com.sky.service.DishService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
|
||||
/**
|
||||
* 菜品管理
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/admin/dish")
|
||||
@Api(tags = "菜品相关接口")
|
||||
@Slf4j
|
||||
public class DishController {
|
||||
|
||||
|
||||
|
||||
@Autowired
|
||||
private DishService dishService;
|
||||
|
||||
|
||||
/**
|
||||
* 新增菜品
|
||||
* @param dishDTO
|
||||
* @return
|
||||
*/
|
||||
@PostMapping
|
||||
@ApiOperation("新增菜品")
|
||||
public Result save(@RequestBody DishDTO dishDTO) {
|
||||
log.info("新增菜品:{}", dishDTO);
|
||||
dishService.saveWithFlavor(dishDTO);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
|
||||
import com.sky.entity.DishFlavor;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface DishFlavorMapper {
|
||||
|
||||
/**
|
||||
* 批量插入口味数据
|
||||
* @param flavors
|
||||
*/
|
||||
void insertBatch(List<DishFlavor> flavors);
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
import com.sky.annotation.AutoFill;
|
||||
import com.sky.entity.Dish;
|
||||
import com.sky.enumeration.OperationType;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
@@ -14,4 +17,11 @@ public interface DishMapper {
|
||||
@Select("select count(id) from dish where category_id = #{categoryId}")
|
||||
Integer countByCategoryId(Long categoryId);
|
||||
|
||||
|
||||
/**
|
||||
* 插入菜品数据
|
||||
* @param dish
|
||||
*/
|
||||
@AutoFill(value = OperationType.INSERT) //公共字段自动填充 不需要手动赋值
|
||||
void insert(Dish dish);
|
||||
}
|
||||
|
||||
13
sky-server/src/main/java/com/sky/service/DishService.java
Normal file
13
sky-server/src/main/java/com/sky/service/DishService.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.sky.service;
|
||||
|
||||
import com.sky.dto.DishDTO;
|
||||
|
||||
public interface DishService {
|
||||
|
||||
|
||||
/**
|
||||
* 新增菜品和对应的口味数据
|
||||
* @param dishDTO
|
||||
*/
|
||||
public void saveWithFlavor(DishDTO dishDTO);
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.sky.service.impl;
|
||||
|
||||
import com.sky.dto.DishDTO;
|
||||
import com.sky.entity.Dish;
|
||||
import com.sky.entity.DishFlavor;
|
||||
import com.sky.mapper.DishFlavorMapper;
|
||||
import com.sky.mapper.DishMapper;
|
||||
import com.sky.service.DishService;
|
||||
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 DishServiceImpl implements DishService {
|
||||
|
||||
|
||||
@Autowired
|
||||
private DishMapper dishMapper;
|
||||
|
||||
|
||||
@Autowired
|
||||
private DishFlavorMapper dishFlavorMapper;
|
||||
|
||||
/**
|
||||
* 新增菜品和对应的口味数据
|
||||
* @param dishDTO
|
||||
*/
|
||||
@Transactional
|
||||
public void saveWithFlavor(DishDTO dishDTO) {
|
||||
|
||||
Dish dish = new Dish();
|
||||
BeanUtils.copyProperties(dishDTO, dish);
|
||||
|
||||
|
||||
//向菜品表插入一条数据
|
||||
dishMapper.insert(dish);
|
||||
//获取insert语句生成的主键值
|
||||
Long dishId=dish.getId();
|
||||
|
||||
//向口味表插入多条数据
|
||||
List<DishFlavor> flavors = dishDTO.getFlavors();
|
||||
if (flavors != null && flavors.size() > 0) {
|
||||
|
||||
flavors.forEach(dishFlavor -> {
|
||||
dishFlavor.setDishId(dishId);
|
||||
});
|
||||
//批量插入 而不是遍历
|
||||
dishFlavorMapper.insertBatch(flavors);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user