فهرست منبع

批量导入实现

zhonghui 3 سال پیش
والد
کامیت
e671cd7083

+ 5 - 0
boman-report/pom.xml

@@ -97,6 +97,11 @@
             <artifactId>boman-api-gen</artifactId>
             <version>2.5.0-SNAPSHOT</version>
         </dependency>
+        <dependency>
+            <groupId>com.boman</groupId>
+            <artifactId>boman-api-web-core</artifactId>
+            <version>2.5.0-SNAPSHOT</version>
+        </dependency>
 
 <!--        <dependency>-->
 <!--            <groupId>commons-beanutils</groupId>-->

+ 39 - 0
boman-report/src/main/java/com/boman/report/controller/ImportController.java

@@ -0,0 +1,39 @@
+package com.boman.report.controller;
+
+import com.boman.domain.dto.AjaxResult;
+import com.boman.report.service.IImportServcie;
+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.util.List;
+
+/**
+ * 报表导入
+ */
+@RestController
+@RequestMapping("/cs/file")
+public class ImportController {
+
+    @Autowired
+    private IImportServcie importServcie;
+
+    /**
+     * 功能描述: 导入接口
+     *
+     * @param multipartFiles MultipartFile
+     * @param tableName     tableName
+     * @return void
+     */
+    @PostMapping("/import")
+    public AjaxResult importData(List<MultipartFile> multipartFiles, String tableName) {
+        try {
+            return importServcie.importData(multipartFiles, tableName);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return AjaxResult.error("失败");
+    }
+}

+ 52 - 0
boman-report/src/main/java/com/boman/report/mapper/ImportMapper.java

@@ -0,0 +1,52 @@
+package com.boman.report.mapper;
+
+import com.alibaba.fastjson.JSONObject;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.SelectProvider;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 导入mapper
+ */
+public interface ImportMapper {
+
+
+    /**
+     * 功能描述: 根据表名称获取表对应的设计器数据
+     * {@link SqlProvider#reportDesignData(Map)}
+     *
+     * @param tableName tableName
+     * @return com.alibaba.fastjson.JSONObject
+     */
+    @SelectProvider(type = SqlProvider.class, method = "reportDesignData")
+    JSONObject getReportDesignData(@Param("tableName") String tableName);
+
+    /**
+     * 功能描述: 根据sql获取数据
+     *
+     * {@link SqlProvider#getDataByDynamicSql(Map)}
+     *
+     * @param sql
+     * @return List<com.alibaba.fastjson.JSONObject>
+     */
+    @SelectProvider(type = SqlProvider.class, method = "getDataByDynamicSql")
+    List<JSONObject> getDataByDynamicSql(@Param("sql") String sql);
+
+    class SqlProvider {
+        public String reportDesignData(Map<String, Object> params) {
+            String tableName = (String) params.get("tableName");
+            StringBuilder sql = new StringBuilder();
+            sql.append("SELECT rd.db_ch_name as dbChName, r.json_str as jsonStr")
+                    .append(" FROM ").append(" jimu_report r ")
+                    .append(" JOIN jimu_report_db rd ON rd.jimu_report_id = r.id ")
+                    .append(" where rd.db_code = '").append(tableName).append("'");
+            return sql.toString();
+        }
+
+        public String getDataByDynamicSql(Map<String, Object> params) {
+            return (String) params.get("sql");
+        }
+    }
+}

+ 12 - 0
boman-report/src/main/java/com/boman/report/service/IImportServcie.java

@@ -0,0 +1,12 @@
+package com.boman.report.service;
+
+import com.boman.domain.dto.AjaxResult;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.util.List;
+
+public interface IImportServcie {
+
+    AjaxResult importData(List<MultipartFile> multipartFiles, String tableName) throws Exception;
+
+}

+ 167 - 0
boman-report/src/main/java/com/boman/report/service/impl/ImportServiceImpl.java

@@ -0,0 +1,167 @@
+package com.boman.report.service.impl;
+
+import com.alibaba.fastjson.JSONObject;
+import com.boman.common.core.utils.StringUtils;
+import com.boman.common.core.utils.obj.ObjectUtils;
+import com.boman.common.core.utils.poi.ExcelUtil;
+import com.boman.common.redis.RedisKey;
+import com.boman.common.redis.service.RedisService;
+import com.boman.domain.GenTable;
+import com.boman.domain.GenTableColumn;
+import com.boman.domain.dto.AjaxResult;
+import com.boman.domain.dto.ImportExcelDto;
+import com.boman.report.mapper.ImportMapper;
+import com.boman.report.service.IImportServcie;
+import com.boman.web.core.api.RemoteObjService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.util.*;
+
+/**
+ * 导入服务类
+ */
+@Service
+public class ImportServiceImpl implements IImportServcie {
+
+    private static final Logger logger = LoggerFactory.getLogger(ImportServiceImpl.class);
+
+    private static final String CREATE_BY = "create_by";
+    private static final String CREATE_TIME = "create_time";
+    private static final String UPDATE_BY = "update_by";
+    private static final String UPDATE_TIME = "update_time";
+
+    @Autowired
+    private ImportMapper importMapper;
+    @Autowired
+    private RedisService redisService;
+    @Autowired
+    private RemoteObjService remoteObjService;
+
+    @Override
+    public AjaxResult importData(List<MultipartFile> multipartFiles, String tableName) throws Exception {
+        Objects.requireNonNull(multipartFiles, "未上传文件!");
+        ObjectUtils.requireNonNull(tableName, "表名为空!");
+        GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName);
+        Map<String, GenTableColumn> columnMap = this.genImportColumn(genTable.getColumns());
+        List<GenTableColumn> columns = genColumData(tableName, columnMap);
+        this.genBaseData(columns,  columnMap);
+        ExcelUtil<JSONObject> util = new ExcelUtil<>(JSONObject.class);
+        List<JSONObject> list = new ArrayList<>();
+        for(MultipartFile multipartFile : multipartFiles) {
+            list.addAll(util.importCommonExcel("", multipartFile.getInputStream(), columns));
+        }
+        ImportExcelDto dto = new ImportExcelDto();
+        dto.setDataList(list);
+        dto.setTableName(tableName);
+        remoteObjService.importCommonData(dto);
+        return AjaxResult.success();
+    }
+
+    /**
+     * 将列组装成map,方便判断,无需多次循环
+     *
+     * @param allColumns
+     * @return
+     */
+    private Map<String, GenTableColumn> genImportColumn(List<GenTableColumn> allColumns) {
+        Map<String, GenTableColumn> columnMap = new HashMap<>();
+        for (GenTableColumn column : allColumns) {
+            String columnName = column.getColumnName();
+            columnMap.put(columnName, column);
+        }
+        return columnMap;
+    }
+
+    /**
+     * 根据报表设计中的表格字段获取gentable中的数据,用于导出
+     *
+     * 数据格式:"rows":{
+     * 	"0":{
+     * 		"cells":{
+         * 		"0":{
+         * 			"text":"#{urge_read_message.message_title}",
+         * 			"style":2
+         *                },
+         * 		"1":{
+         * 			"text":"#{urge_read_message.send_user_id}",
+         * 			"style":2
+         *        },
+         * 		"2":{
+         * 			"text":"#{urge_read_message.receive_user_name}",
+         * 			"style":2
+         *        },
+         * 		"3":{
+         * 			"style":2,
+         * 			"text":"#{urge_read_message.is_del}"
+         *        }* 	},
+         * 	"len":100
+     * },
+     * @param tableName
+     * @param columnMap 列map集合
+     * @return
+     */
+    private List<GenTableColumn> genColumData(String tableName, Map<String, GenTableColumn> columnMap) {
+        List<GenTableColumn> columns = new ArrayList<>();
+        // 根据名获取报表设计相关数据
+        JSONObject designObject = importMapper.getReportDesignData(tableName);
+        if(designObject == null) {
+            AjaxResult.error("数据表没有设计报表模板!请先设计报表模板!");
+        }
+        String jsonStr = designObject.getString("jsonStr");
+        if(StringUtils.isEmpty(jsonStr)) {
+            AjaxResult.error("报表模板没有相关设计数据,请确认是否设计好报表模板!");
+        }
+        JSONObject rowDatas = JSONObject.parseObject(jsonStr).getJSONObject("rows");
+        int maxKey = rowDatas.size() - 1;
+        Iterator<String> rowIterator = rowDatas.keySet().iterator();
+        int count  = 0;
+        while (rowIterator.hasNext()) {
+            count ++;
+            String key = rowIterator.next();
+            if(count == maxKey) {
+                JSONObject cells = JSONObject.parseObject(rowDatas.getString(key));
+                Iterator<String> cellIterator = cells.keySet().iterator();
+                while (cellIterator.hasNext()) {
+                    String cellKey = cellIterator.next();
+                    JSONObject feildJson = cells.getJSONObject(cellKey);
+                    Iterator<String> feildIterator = feildJson.keySet().iterator();
+                    while (feildIterator.hasNext()) {
+                        String feildName = "";
+                        String textStr = feildJson.getString(feildIterator.next());
+                        if(StringUtils.isNotEmpty(textStr)) {
+                            JSONObject textJson = JSONObject.parseObject(textStr);
+                            String text = textJson.getString("text");
+                            // 获取数据
+                            if(StringUtils.isNotEmpty(text)) {
+                                String realText = text.replace("#{", "").replace("}", "");
+                                feildName = realText.substring(tableName.length() + 1, realText.length());
+                                if(columnMap.containsKey(feildName)) {
+                                    columns.add(columnMap.get(feildName));
+                                }
+                            }
+                        }
+                    }
+                }
+                break;
+            }
+        }
+        return columns;
+    }
+
+    /**
+     * 基础字段填充
+     *
+     * @param columns
+     * @param columnMap
+     */
+    private void genBaseData(List<GenTableColumn> columns, Map<String, GenTableColumn> columnMap) {
+        columns.add(columnMap.get(CREATE_BY));
+        columns.add(columnMap.get(CREATE_TIME));
+        columns.add(columnMap.get(UPDATE_BY));
+        columns.add(columnMap.get(UPDATE_TIME));
+    }
+}