Procházet zdrojové kódy

新增菜品业务代码开发

潘海瑞 před 10 měsíci
rodič
revize
7f2a9b9332

+ 23 - 0
sky-server/pom.xml

@@ -115,6 +115,29 @@
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-ooxml</artifactId>
         </dependency>
+
+        <!--oss-->
+        <dependency>
+            <groupId>com.aliyun.oss</groupId>
+            <artifactId>aliyun-sdk-oss</artifactId>
+            <version>3.15.1</version>
+        </dependency>
+        <dependency>
+            <groupId>javax.xml.bind</groupId>
+            <artifactId>jaxb-api</artifactId>
+            <version>2.3.1</version>
+        </dependency>
+        <dependency>
+            <groupId>javax.activation</groupId>
+            <artifactId>activation</artifactId>
+            <version>1.1.1</version>
+        </dependency>
+        <!-- no more than 2.3.3-->
+        <dependency>
+            <groupId>org.glassfish.jaxb</groupId>
+            <artifactId>jaxb-runtime</artifactId>
+            <version>2.3.3</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 23 - 0
sky-server/src/main/java/com/sky/config/OssConfiguration.java

@@ -0,0 +1,23 @@
+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());
+    }
+}

+ 50 - 0
sky-server/src/main/java/com/sky/controller/CommonController.java

@@ -0,0 +1,50 @@
+package com.sky.controller;
+
+import com.sky.result.Result;
+import com.sky.utils.AliOssUtil;
+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
+@Slf4j
+@RequestMapping("/admin/common")
+public class CommonController {
+    @Autowired
+    private AliOssUtil aliOssUtil;
+
+    /**
+     * 文件上传
+     * @param file
+     * @return
+     */
+    @PostMapping("/upload")
+    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.info("文件上传失败:{}",e);
+        }
+
+        return null;
+    }
+}

+ 34 - 0
sky-server/src/main/java/com/sky/controller/DishController.java

@@ -0,0 +1,34 @@
+package com.sky.controller;
+
+import com.sky.dto.DishDTO;
+import com.sky.result.Result;
+import com.sky.service.DishService;
+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")
+@Slf4j
+public class DishController {
+    @Autowired
+    private DishService dishService;
+    /**
+     * 添加菜品
+     * @param dishDTO
+     * @return
+     */
+    @PostMapping
+    public Result save(@RequestBody DishDTO dishDTO){
+        log.info("新增菜品:{}",dishDTO);
+        dishService.saveWithFlavor(dishDTO);
+        return Result.success();
+    }
+
+}

+ 16 - 0
sky-server/src/main/java/com/sky/mapper/DishFlavorsMapper.java

@@ -0,0 +1,16 @@
+package com.sky.mapper;
+
+import com.sky.entity.DishFlavor;
+import org.apache.ibatis.annotations.Mapper;
+
+import java.util.List;
+
+@Mapper
+public interface DishFlavorsMapper {
+
+    /**
+     * 添加菜品风味
+     * @param flavors
+     */
+    void insertBatch(List<DishFlavor> flavors);
+}

+ 10 - 0
sky-server/src/main/java/com/sky/mapper/DishMapper.java

@@ -1,5 +1,9 @@
 package com.sky.mapper;
 
+import com.sky.annotation.AutoFill;
+import com.sky.dto.DishDTO;
+import com.sky.entity.Dish;
+import com.sky.enumeration.OperationType;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Select;
 
@@ -14,4 +18,10 @@ 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);
 }

+ 12 - 0
sky-server/src/main/java/com/sky/service/DishService.java

@@ -0,0 +1,12 @@
+package com.sky.service;
+
+import com.sky.dto.DishDTO;
+
+public interface DishService {
+
+    /**
+     * 新增菜品
+     * @param dishDTO
+     */
+    void saveWithFlavor(DishDTO dishDTO);
+}

+ 51 - 0
sky-server/src/main/java/com/sky/service/impl/DishServiceImpl.java

@@ -0,0 +1,51 @@
+package com.sky.service.impl;
+
+import com.sky.dto.DishDTO;
+import com.sky.entity.Dish;
+import com.sky.entity.DishFlavor;
+import com.sky.mapper.DishFlavorsMapper;
+import com.sky.mapper.DishMapper;
+import com.sky.service.DishService;
+import lombok.extern.slf4j.Slf4j;
+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
+@Slf4j
+public class DishServiceImpl implements DishService {
+    @Autowired
+    private DishMapper dishMapper;
+    @Autowired
+    private DishFlavorsMapper dishFlavorsMapper;
+
+    /**
+     * 新增菜品
+     * @param dishDTO
+     */
+    @Transactional
+    @Override
+    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);
+            });
+            //向口味表插入n条数据
+            dishFlavorsMapper.insertBatch(flavors);
+        }
+
+    }
+}

+ 5 - 0
sky-server/src/main/resources/application-dev.yml

@@ -6,3 +6,8 @@ sky:
     database: sky_take_out
     username: root
     password: 1234
+  alioss:
+    endpoint: oss-cn-beijing.aliyuncs.com
+    access-key-id: LTAI5tCWqFgcLgYfxoCzNo1J
+    access-key-secret: 0tVFd0llLCyj6r3ubO6WZhpIDrmePS
+    bucket-name: sky-itcast-p

+ 6 - 0
sky-server/src/main/resources/application.yml

@@ -37,3 +37,9 @@ sky:
     admin-ttl: 43200000
     # 设置前端传递过来的令牌名称
     admin-token-name: token
+
+  alioss:
+    endpoint: ${sky.alioss.endpoint}
+    access-key-id: ${sky.alioss.access-key-id}
+    access-key-secret: ${sky.alioss.access-key-secret}
+    bucket-name: ${sky.alioss.bucket-name}

+ 12 - 0
sky-server/src/main/resources/mapper/DishFlavorsMapper.xml

@@ -0,0 +1,12 @@
+<?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.DishFlavorsMapper">
+
+    <insert id="insertBatch">
+        insert into dish_flavor (dish_id, name, value) VALUES
+        <foreach collection="flavors" item="df" separator=",">
+            (#{df.dishId}, #{df.name}, #{df.value})
+        </foreach>
+    </insert>
+</mapper>

+ 12 - 0
sky-server/src/main/resources/mapper/DishMapper.xml

@@ -0,0 +1,12 @@
+<?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.DishMapper">
+
+    <insert id="insert" useGeneratedKeys="true" keyProperty="id">/*获得插入数据的主键值*/
+        insert into dish (name, category_id, price, image, description, create_time, update_time, create_user, update_user, status)
+        VALUES
+            (#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser},#{status})
+
+    </insert>
+</mapper>