Bläddra i källkod

批量添加应用&业务

yonghuifan 1 år sedan
förälder
incheckning
8886a3dc91

BIN
lib/eventpoi-0.1.3.jar


+ 38 - 0
pom.xml

@@ -174,6 +174,41 @@
             <groupId>commons-codec</groupId>
             <artifactId>commons-codec</artifactId>
         </dependency>
+
+        <!-- https://mvnrepository.com/artifact/xerces/xercesImpl -->
+        <dependency>
+            <groupId>xerces</groupId>
+            <artifactId>xercesImpl</artifactId>
+            <version>2.12.2</version>
+        </dependency>
+
+        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi</artifactId>
+            <version>5.2.0</version>
+        </dependency>
+        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi-ooxml</artifactId>
+            <version>5.2.0</version>
+        </dependency>
+        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-lang3</artifactId>
+            <version>3.12.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId>eventpoi</groupId>
+            <artifactId>eventpoi</artifactId>
+            <version>0.1.3</version>
+            <scope>system</scope>
+            <systemPath>${project.basedir}/lib/eventpoi-0.1.3.jar</systemPath>
+        </dependency>
+
     </dependencies>
 
     <build>
@@ -181,6 +216,9 @@
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
+                <configuration>
+                    <includeSystemScope>true</includeSystemScope>
+                </configuration>
             </plugin>
 
             <plugin>

+ 9 - 0
src/main/java/com/dragon/tj/portal/controller/AppController.java

@@ -14,6 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.time.LocalDateTime;
 import java.util.List;
@@ -121,6 +122,14 @@ public class AppController {
         return R.ok(appService.search(appInfoPage));
     }
 
+    @SysLog(value = "批量添加应用", module = ModuleEnum.APP_ADD)
+    @PostMapping("batchAdd")
+    public R batchAdd(MultipartFile file) {
+
+        return R.ok(appService.batchAdd(file));
+    }
+
+
     /*
      *   点击量
      * */

+ 10 - 0
src/main/java/com/dragon/tj/portal/controller/BusinessController.java

@@ -14,6 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.time.LocalDateTime;
 import java.util.List;
@@ -93,6 +94,15 @@ public class BusinessController {
         return R.ok(b);
     }
 
+
+    @SysLog(value = "批量添加业务", module = ModuleEnum.BUSINESS_ADD)
+    @PostMapping("batchAdd")
+    public R batchAdd(MultipartFile file) {
+
+        return R.ok(businessService.batchAdd(file));
+    }
+
+
     //    @SysLog("删除业务")
     @SysLog(value = "删除业务", module = ModuleEnum.BUSINESS_DELETE)
     @GetMapping("delete/{id}")

+ 32 - 0
src/main/java/com/dragon/tj/portal/controller/FileManageController.java

@@ -7,9 +7,15 @@ import com.dragon.tj.portal.entity.FileInfo;
 import com.dragon.tj.portal.entity.PageParam;
 import com.dragon.tj.portal.service.FileManageService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.InputStreamResource;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.io.*;
 
 @RestController
 @RequestMapping("/fileMge")
@@ -47,4 +53,30 @@ public class FileManageController {
         return R.ok(fileManageService.top(type,num));
     }
 
+    /*
+     *  模板文件下载
+     * */
+    @SysLog(value = "模板文件下载", module = ModuleEnum.FILE_QUERY)
+    @GetMapping("temp/{type}")
+    public ResponseEntity<InputStreamResource> downloadTemp(@PathVariable("type")String type) throws IOException {
+//        String path = AppService.class.getClassLoader().getResource("").getPath();
+//        path = URLDecoder.decode(path, "UTF-8");
+
+//        String filePath = path + "file/" + type + ".xlsx";
+        String filename = type + ".xlsx";
+        ClassPathResource classPathResource = new ClassPathResource("file/" +filename);
+        InputStream inputStream = classPathResource.getInputStream();
+//        FileSystemResource file = new FileSystemResource(filePath);
+        HttpHeaders headers = new HttpHeaders();
+        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
+        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", filename));
+        headers.add("Pragma", "no-cache");
+        headers.add("Expires", "0");
+        return ResponseEntity
+                .ok()
+                .headers(headers)
+//                .contentLength(file.contentLength())
+                .contentType(MediaType.parseMediaType("application/octet-stream"))
+                .body(new InputStreamResource(inputStream));
+    }
 }

+ 13 - 0
src/main/java/com/dragon/tj/portal/entity/AppInfo.java

@@ -1,5 +1,6 @@
 package com.dragon.tj.portal.entity;
 
+import com.alibaba.fastjson.annotation.JSONField;
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
@@ -34,6 +35,10 @@ public class AppInfo implements Serializable {
      */
     private Integer appType;
 
+    @TableField(exist = false)
+    @JSONField(name = "typeName")
+    private String typeName;
+
     /**
      * 应用系统名称
      */
@@ -66,11 +71,19 @@ public class AppInfo implements Serializable {
      */
     private Integer activeFlag;
 
+    @TableField(exist = false)
+    @JSONField(name = "activeName")
+    private String activeName;
+
     /**
      * 图标path
      */
     private String icon;
 
+    @TableField(exist = false)
+    @JSONField(name = "image")
+    private byte[] image;
+
     /**
      * 警种分类
      */

+ 8 - 0
src/main/java/com/dragon/tj/portal/entity/BusinessInfo.java

@@ -1,5 +1,6 @@
 package com.dragon.tj.portal.entity;
 
+import com.alibaba.fastjson.annotation.JSONField;
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
@@ -32,6 +33,10 @@ public class BusinessInfo {
      */
     private Integer businessType;
 
+    @TableField(exist = false)
+    @JSONField(name = "typeName")
+    private String typeName;
+
     /**
      * 业务名称
      */
@@ -65,6 +70,9 @@ public class BusinessInfo {
      */
     private Integer activeFlag;
 
+    @JSONField(name = "activeName")
+    private String activeName;
+
     /**
      * 创建时间
      */

+ 3 - 0
src/main/java/com/dragon/tj/portal/service/AppService.java

@@ -6,6 +6,7 @@ import com.dragon.tj.portal.auth.model.LoginUser;
 import com.dragon.tj.portal.common.base.R;
 import com.dragon.tj.portal.entity.AppInfo;
 import com.dragon.tj.portal.entity.PageParam;
+import org.springframework.web.multipart.MultipartFile;
 
 
 public interface AppService {
@@ -51,4 +52,6 @@ public interface AppService {
     void updateAppStat();
 
     int clickIncr(Long id);
+
+    boolean batchAdd(MultipartFile file);
 }

+ 3 - 0
src/main/java/com/dragon/tj/portal/service/BusinessService.java

@@ -6,6 +6,7 @@ import com.dragon.tj.portal.auth.model.LoginUser;
 import com.dragon.tj.portal.common.base.R;
 import com.dragon.tj.portal.entity.BusinessInfo;
 import com.dragon.tj.portal.entity.PageParam;
+import org.springframework.web.multipart.MultipartFile;
 
 
 public interface BusinessService {
@@ -51,4 +52,6 @@ public interface BusinessService {
     void updateAppStat();
 
     int clickIncr(Long id);
+
+    boolean batchAdd(MultipartFile file);
 }

+ 2 - 0
src/main/java/com/dragon/tj/portal/service/FileManageService.java

@@ -10,6 +10,8 @@ import java.util.List;
 public interface FileManageService {
     FileInfo upload(MultipartFile file,Integer type);
 
+    String upload(byte[] fileByte,String fileName,Integer type);
+
     Page<FileInfo> search(PageParam<FileInfo> pageParam);
 
     List<FileInfo> top(String type, Integer num);

+ 72 - 0
src/main/java/com/dragon/tj/portal/service/impl/AppServiceImpl.java

@@ -6,17 +6,23 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.metadata.OrderItem;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.dc.eventpoi.ExcelHelper;
 import com.dragon.tj.portal.auth.model.LoginUser;
+import com.dragon.tj.portal.auth.util.SecurityUtils;
 import com.dragon.tj.portal.common.base.R;
 import com.dragon.tj.portal.entity.*;
 import com.dragon.tj.portal.mapper.app.AppInfoMapper;
 import com.dragon.tj.portal.mapper.app.InstallInfoMapper;
 import com.dragon.tj.portal.service.AppService;
+import com.dragon.tj.portal.service.FileManageService;
 import com.dragon.tj.portal.service.SysDictItemService;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.io.ClassPathResource;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
 
+import java.io.*;
 import java.time.LocalDateTime;
 import java.util.HashMap;
 import java.util.List;
@@ -35,6 +41,8 @@ public class AppServiceImpl implements AppService {
     @Autowired
     private SysDictItemService sysDictItemService;
 
+    @Autowired
+    private FileManageService fileManageService;
 
     private static List<SysDictItem> appTypeDict;
 
@@ -205,4 +213,68 @@ public class AppServiceImpl implements AppService {
         return appInfoMapper.updateById(appInfo);
     }
 
+    @Override
+    public boolean batchAdd(MultipartFile file) {
+        InputStream inputStream = null;
+        try {
+            inputStream = file.getInputStream();
+//            String path = AppService.class.getClassLoader().getResource("").getPath();
+//            path = URLDecoder.decode(path, "UTF-8");
+//            FileInputStream tempFis = new FileInputStream(new File(path + "file/appInfoTemplete.xlsx"));
+
+            ClassPathResource classPathResource = new ClassPathResource("file/appInfoTemplete.xlsx");
+            InputStream tempFis = classPathResource.getInputStream();
+
+            List<AppInfo> appInfos = ExcelHelper.parseExcelToObject(
+                    new BufferedInputStream(tempFis),
+                    inputStream,
+                    AppInfo.class,true);
+
+            appInfos.forEach(appInfo -> {
+                AppInfo appInfo1 = parseAppInfo(appInfo);
+                this.add(appInfo1, SecurityUtils.getLoginUser());
+            });
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (inputStream != null){
+                try {
+                    inputStream.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+
+        return true;
+    }
+
+    private AppInfo parseAppInfo(AppInfo appInfo) {
+        List<SysDictItem> appTypeDict = getAppTypeDict();
+
+        //类型表码转换
+        String appTypeName = appInfo.getTypeName();
+        SysDictItem sysDictItem = appTypeDict.stream()
+                .filter(t -> t.getLabel().equals(appTypeName))
+                .findFirst().get();
+        appInfo.setAppType(sysDictItem.getId());
+
+        //在用标识转换
+        appInfo.setActiveFlag("在用".equals(appInfo.getActiveName()) ? 1:0);
+
+        //图标处理
+        byte[] iconImageByte = appInfo.getImage();
+        if (iconImageByte.length > 0){
+
+            appInfo.setIcon(fileManageService.upload(
+                    iconImageByte,
+                    appInfo.getSystemName() + ".jpg",
+                    appInfo.getAppType()));
+        }
+
+        return appInfo;
+
+    }
+
 }

+ 80 - 6
src/main/java/com/dragon/tj/portal/service/impl/BusinessServiceImpl.java

@@ -4,22 +4,24 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.metadata.OrderItem;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.dc.eventpoi.ExcelHelper;
 import com.dragon.tj.portal.auth.model.LoginUser;
+import com.dragon.tj.portal.auth.util.SecurityUtils;
 import com.dragon.tj.portal.common.base.R;
-import com.dragon.tj.portal.entity.AppInfo;
-import com.dragon.tj.portal.entity.BusinessInfo;
-import com.dragon.tj.portal.entity.InstallInfo;
-import com.dragon.tj.portal.entity.PageParam;
+import com.dragon.tj.portal.entity.*;
 import com.dragon.tj.portal.mapper.BusinessMapper;
 import com.dragon.tj.portal.mapper.app.AppInfoMapper;
 import com.dragon.tj.portal.mapper.app.InstallInfoMapper;
-import com.dragon.tj.portal.service.BusinessService;
-import com.dragon.tj.portal.service.SysDictService;
+import com.dragon.tj.portal.service.*;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.io.ClassPathResource;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
 
+import java.io.*;
 import java.time.LocalDateTime;
 import java.util.HashMap;
 import java.util.List;
@@ -41,6 +43,12 @@ public class BusinessServiceImpl implements BusinessService {
     @Autowired
     private SysDictService sysDictService;
 
+    @Autowired
+    private SysDictItemService sysDictItemService;
+
+    private static List<SysDictItem> businessTypeDict;
+
+
     /*
      * CRUD
      * */
@@ -229,5 +237,71 @@ public class BusinessServiceImpl implements BusinessService {
         return businessMapper.updateById(businessInfo);
     }
 
+    @Override
+    public boolean batchAdd(MultipartFile file) {
+        InputStream inputStream = null;
+        try {
+            inputStream = file.getInputStream();
+//            String path = AppService.class.getClassLoader().getResource("").getPath();
+//            path = URLDecoder.decode(path, "UTF-8");
+//            FileInputStream tempFis = new FileInputStream(new File(path + "file/businessTemplete.xlsx"));
+            ClassPathResource classPathResource = new ClassPathResource("file/businessTemplete.xlsx");
+            InputStream tempFis = classPathResource.getInputStream();
+
+            List<BusinessInfo> businessInfos = ExcelHelper.parseExcelToObject(
+                    new BufferedInputStream(tempFis),
+                    inputStream,
+                    BusinessInfo.class,true);
+
+            businessInfos.forEach(businessInfo -> {
+                BusinessInfo businessInfo1 = parseBusinessInfo(businessInfo);
+                this.add(businessInfo1, SecurityUtils.getLoginUser());
+            });
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (inputStream != null){
+                try {
+                    inputStream.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+
+        return true;
+    }
+
+    private BusinessInfo parseBusinessInfo(BusinessInfo businessInfo) {
+        List<SysDictItem> businessTypeDict = getBusinessTypeDict();
+
+        //类型表码转换
+        String appTypeName = businessInfo.getTypeName();
+        SysDictItem sysDictItem = businessTypeDict.stream()
+                .filter(t -> t.getLabel().equals(appTypeName))
+                .findFirst().get();
+        businessInfo.setBusinessType(sysDictItem.getId());
+
+        //获取Appinfo
+        QueryWrapper<AppInfo> wrapper = new QueryWrapper<>();
+        wrapper.eq(businessInfo.getAppName() != null,
+                "system_name",businessInfo.getAppName());
+
+        AppInfo appInfo = appInfoMapper.selectOne(wrapper);
+        businessInfo.setAppId(appInfo.getId());
+
+        return businessInfo;
+
+    }
+
+
+    public List<SysDictItem> getBusinessTypeDict(){
+        if (businessTypeDict == null || businessTypeDict.size() == 0) {
+            businessTypeDict = sysDictItemService.list(Wrappers.<SysDictItem>query().lambda().eq(SysDictItem::getType, "business_type").orderByAsc(SysDictItem::getSort));
+        }
+
+        return businessTypeDict;
+    }
 
 }

+ 30 - 2
src/main/java/com/dragon/tj/portal/service/impl/FileManageServiceImpl.java

@@ -22,8 +22,8 @@ import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
 import java.io.IOException;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.*;
 
 @Service
@@ -77,6 +77,34 @@ public class FileManageServiceImpl implements FileManageService {
         return fileInfo;
     }
 
+    @Override
+    public String upload(byte[] fileByte,String fileName, Integer type) {
+
+        File dateDir = new File(localPath + type);
+        if (!dateDir.exists()) {
+            dateDir.mkdirs();
+        }
+
+        try {
+            Files.write(Paths.get(dateDir.getPath() + "/" + fileName), fileByte);
+        } catch (IOException e) {
+            e.printStackTrace();
+            return null;
+        }
+
+        FileInfo fileInfo = new FileInfo();
+        fileInfo.setFileName(fileName);
+        fileInfo.setType(type);
+        LoginUser loginUser = SecurityUtils.getLoginUser();
+        fileInfo.setCreateUser(loginUser.getUsername());
+        fileInfo.setUrl("/file/" + type + "/" + fileName);
+
+        //save to db
+        fileManageMapper.insert(fileInfo);
+
+        return fileInfo.getUrl();
+    }
+
     @Override
     public Page<FileInfo> search(PageParam<FileInfo> pageParam) {
         FileInfo fileParam = pageParam.getParams();

+ 6 - 3
src/main/resources/application-dev.properties

@@ -1,8 +1,11 @@
 ###########spring#############
-spring.datasource.url=jdbc:mysql://portal-tj.com:3306/portal?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
+#spring.datasource.url=jdbc:mysql://portal-tj.com:3306/portal?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
+spring.datasource.url=jdbc:mysql://127.0.0.1:3306/portal?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
 #spring.datasource.url=jdbc:mysql://portal-tj.com:3306/portal?characterEncoding=utf8
-spring.datasource.username=portal
-spring.datasource.password=portal01!
+#spring.datasource.username=portal
+#spring.datasource.password=portal01!
+spring.datasource.username=root
+spring.datasource.password=redhat
 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
 #log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl
 

BIN
src/main/resources/file/appInfoDemo.xlsx


BIN
src/main/resources/file/appInfoTemplete.xlsx


BIN
src/main/resources/file/businessDemo.xlsx


BIN
src/main/resources/file/businessTemplete.xlsx