From c007f0ca1785db093d48f4846cda82fe8e955765 Mon Sep 17 00:00:00 2001
From: kimi <kimi42345@gmail.com>
Date: 星期三, 27 五月 2020 09:59:29 +0800
Subject: [PATCH] merage

---
 src/main/java/com/highdatas/mdm/controller/FileController.java |  249 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 246 insertions(+), 3 deletions(-)

diff --git a/src/main/java/com/highdatas/mdm/controller/FileController.java b/src/main/java/com/highdatas/mdm/controller/FileController.java
index ce4ccc2..5b109ff 100644
--- a/src/main/java/com/highdatas/mdm/controller/FileController.java
+++ b/src/main/java/com/highdatas/mdm/controller/FileController.java
@@ -1,15 +1,258 @@
 package com.highdatas.mdm.controller;
 
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import com.alibaba.fastjson.JSONObject;
+import com.highdatas.mdm.entity.*;
+import com.highdatas.mdm.mapper.SysViewMapper;
+import com.highdatas.mdm.mapper.TableInfoMapper;
+import com.highdatas.mdm.pojo.CodeMsg;
+import com.highdatas.mdm.pojo.Result;
+import com.highdatas.mdm.pojo.Segment;
+import com.highdatas.mdm.pojo.kettle.UnBigDataDataSourceInfo;
+import com.highdatas.mdm.service.*;
+import com.highdatas.mdm.util.*;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.ibatis.session.ExecutorType;
+import org.apache.ibatis.session.SqlSession;
+import org.mybatis.spring.SqlSessionTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
 
 /**
  * @author kimi
- * @description
+ * @description  绠�鍗曚笂浼犳枃浠舵帴鍙�
  * @date 2019-12-18 10:22
  */
 
 @RestController
 @RequestMapping("/file")
 public class FileController {
+    @Autowired
+    ISysAssembleDbService dbService;
+    @Autowired
+    ISysAssembleService assembleService;
+    @Autowired
+    ISysAssembleParamsService paramsService;
+    @Autowired
+    RedisClient redisClient;
+    @Autowired
+    ISysViewService viewService;
+    @Autowired
+    IMenuMappingService menuMappingService;
+    @Autowired
+    TableInfoMapper tableInfoMapper;
+    @Autowired
+    private SqlSessionTemplate sqlSessionTemplate;
+    @Autowired
+    UnBigDataDataSourceInfo unBigDataDataSourceInfo;
+
+    /**
+     *
+     * @description:  姹囬泦  涓婁紶鐢辨簮琛ㄧ粍鍚堢殑sql
+     * @param file 鏂囦欢
+     * @param dbId 鏁版嵁婧�
+     * @return: 鏄惁娣诲姞鎴愬姛
+     *
+     */
+    @RequestMapping(value = "/loadSqlTable", method = RequestMethod.POST)
+    @ResponseBody
+    public Result loadSqlTable(@RequestParam("file")MultipartFile file,@RequestParam String dbId, HttpServletRequest request) {
+        try {
+            byte[] bytes = file.getBytes();
+
+            String sql = new String (bytes);
+            //鍘婚櫎绌烘牸闃叉不sql鍥犱负绌烘牸鎶ラ敊
+            sql = DbUtils.replaceEscape(sql);
+            String[] split = sql.split(Constant.SEMICOLON);
+            //鏍¢獙sql璇彞鏉℃暟
+            if (split.length > 1) {
+                return Result.error(new CodeMsg(6001 , "sql鑴氭湰涓婁紶閿欒,浠呰兘鏀寔涓�鏉¤鍙�"));
+            }
+            String sqlId = DbUtils.getUUID();
+            //娣诲姞杩涚紦瀛樺唴锛屽悗缁娇鐢�
+            boolean b = redisClient.putRedisVal(sqlId, sql);
+            if (!b){
+                return Result.error(new CodeMsg(6002, "sql淇濆瓨澶辫触"));
+            }
+            // 鏆備笉鏍¢獙鏄笉鏄痵elect璇彞浜�  鍚庣画闇�瑕佸啀鏍¢獙
+            //閫氳繃sql鏌ヨ瀛楁
+            List<String> fieldsBySql = dbService.getFieldsBySql(dbId, sql);
+            JSONObject object = new JSONObject();
+            object.fluentPut("fields", fieldsBySql);
+            object.fluentPut("sqlId",sqlId);
+
+            return Result.success(object);
+        }catch (Exception e) {
+            e.printStackTrace();
+            return Result.error(new CodeMsg(6002, e.getMessage()));
+        }
+
+    }
+    /**
+     *
+     * @description:  姹囬泦 涓婁紶娓呮礂sql
+     * @param file 娓呮礂鏂囦欢
+     * @param id 姹囬泦id
+     * @return: 鏄惁娣诲姞鎴愬姛
+     *
+     */
+    @RequestMapping(value = "/loadPurgeSql", method = RequestMethod.POST)
+    @ResponseBody
+    public Result loadPurgeSql(@RequestParam("file")MultipartFile file,@RequestParam String id, HttpServletRequest request) {
+
+        //閫氳繃id鑾峰彇姹囬泦浠诲姟淇℃伅
+        SysAssemble assemble = assembleService.selectById(id);
+        if (assemble == null){
+            return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
+        }
+        try {
+            byte[] bytes = file.getBytes();
+
+            String sql = new String (bytes);
+            //鍘婚櫎绌烘牸闃叉不sql鍥犱负绌烘牸鎶ラ敊
+            sql = DbUtils.replaceEscape(sql);
+            // 鏆備笉鏍¢獙鏄笉鏄痵elect璇彞浜�  鍚庣画闇�瑕佸啀鏍¢獙
+            //TODO 鏍¢獙sql 鏆備笉杩涜
+            assemble.setPurgeSql(sql);
+            boolean update = assemble.updateById();
+            if (update) {
+                return Result.success(assemble);
+            }else {
+                return Result.error(CodeMsg.UPDATE_ERROR);
+            }
+
+        }catch (Exception e) {
+            e.printStackTrace();
+            return Result.error(new CodeMsg(6002, e.getMessage()));
+        }
+
+    }
+    /**
+     *
+     * @description:  姹囬泦 涓婁紶鍙傛暟鏇存柊sql
+     * @param file 娓呮礂鏂囦欢
+     * @param id 姹囬泦id
+     * @return: 鏄惁娣诲姞鎴愬姛
+     *
+     */
+    @RequestMapping(value = "/loadParamsUpdateSql", method = RequestMethod.POST)
+    @ResponseBody
+    public Result loadParamsUpdateSql(@RequestParam("file")MultipartFile file,@RequestParam String id, HttpServletRequest request) {
+        // 鏆備笉鏍¢獙鏄笉鏄痵elect璇彞浜�  鍚庣画闇�瑕佸啀鏍¢獙
+        SysAssembleParams sysAssembleParams = paramsService.selectById(id);
+        if (sysAssembleParams == null){
+            return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
+        }
+        try {
+            byte[] bytes = file.getBytes();
+
+            String sql = new String (bytes);
+            //鍘婚櫎绌烘牸闃叉不sql鍥犱负绌烘牸鎶ラ敊
+            sql = DbUtils.replaceEscape(sql);
+            //TODO 鏍¢獙sql 鏆備笉杩涜
+            sysAssembleParams.setUpdateSql(sql).setUpdateTime(new Date()).updateById();
+
+            return Result.success(sysAssembleParams);
+        }catch (Exception e) {
+            e.printStackTrace();
+            return Result.error(new CodeMsg(6002, e.getMessage()));
+        }
+
+    }
+
+    /**
+     *
+     * @description:  瑙嗗浘锛屽�煎鍊艰浆鎹�
+     * @param file 娓呮礂鏂囦欢
+     * @param id 瑙嗗浘id
+     * @param menuId 瑙嗗浘瀵瑰簲鏁版嵁鏉ユ簮鐨勪富棰榠d
+     * @return: 鏄惁娣诲姞鎴愬姛
+     *
+     */
+    @RequestMapping(value = "/loadMappingFile", method = RequestMethod.POST)
+    @ResponseBody
+    public Result loadMappingFile(@RequestParam("file")MultipartFile file,@RequestParam String id, @RequestParam String menuId, @RequestParam String field, HttpServletRequest request) {
+        //鑾峰彇瑙嗗浘淇℃伅
+        SysView sysView = viewService.selectById(id);
+        if (sysView == null){
+            return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
+        }
+        //鑾峰彇瑙嗗浘瀵瑰簲鐨刴apping琛�
+        String mappingTable = sysView.getMappingTable();
+        if (StringUtils.isEmpty(mappingTable)){
+            return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
+        }
+        //妫�楠屾槸鍚﹀瓨鍦�
+        boolean exists = unBigDataDataSourceInfo.checkTableExists(mappingTable);
+        if (!exists) {
+            //涓嶅瓨鍦ㄥ垯鍒涘缓mapping琛�
+            ArrayList<String> fields = new ArrayList<>();
+            fields.add(Constant.ID);
+            fields.add(Constant.Code);
+            fields.add("pre");
+            fields.add("fix");
+            boolean created = unBigDataDataSourceInfo.createTable(mappingTable, fields);
+            if (!created) {
+                return Result.error(CodeMsg.CREATE_ERROR);
+            }
+        }
+
+        SqlSession session = null;
+        try {
+            String name = file.getOriginalFilename();
+            //閫氳繃鏂囦欢鑾峰彇杞崲mapping鍒楄〃
+            List<ViewMappingItem> viewMappingByExcel = ExcelUtil.getViewMappingByExcel(file.getInputStream(), name);
+            if (viewMappingByExcel.isEmpty()) {
+                return Result.error(CodeMsg.EMPTY_ERROR);
+            }
+            //鑾峰彇瑙嗗浘瀵瑰簲涓婚鐨勭増鏈俊鎭�
+            Maintain maintainByMenu = viewService.getMaintainByMenu(sysView, menuId);
+            if (maintainByMenu == null) {
+                return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
+            }
+            //鑾峰彇杞崲鍚庣殑瑙嗗浘瀹為檯浣跨敤鐨勫瓧娈�
+            String changeFieldName = viewService.changeFieldName(maintainByMenu.getTableName(), field);
+            Segment segment = new Segment(Constant.Code, changeFieldName);
+            //By 瀛楁鍒犻櫎鍘熸潵鐨刴apping
+            tableInfoMapper.delete(mappingTable, segment.toString());
+            session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);//鍏抽棴session鐨勮嚜鍔ㄦ彁浜�
+
+            SysViewMapper mapper = session.getMapper(SysViewMapper.class);
+            AtomicInteger idx = new AtomicInteger(0);
+            ContentBuilder builder = new ContentBuilder(Constant.COMMA);
+            //鎵归噺鎵ц鎻掑叆璇彞
+            for (ViewMappingItem viewMappingItem : viewMappingByExcel) {
+                builder.clear();
+                builder.append(DbUtils.quotedStr(changeFieldName));
+                builder.append(DbUtils.quotedStr(viewMappingItem.getPre())).append(DbUtils.quotedStr(viewMappingItem.getFix()));
+                mapper.insertViewMapping(mappingTable, builder.toString());
+                int i = idx.get();
+                if (i % 1000 == 0 || i == viewMappingByExcel.size()-1) {
+                    //鎵嬪姩姣�1000涓竴鎻愪氦锛屾彁浜ゅ悗鏃犳硶鍥炴粴
+                    session.commit();
+                    session.clearCache();//娉ㄦ剰锛屽鏋滄病鏈夎繖涓姩浣滐紝鍙兘浼氬鑷村唴瀛樺穿婧冦��
+                }
+                idx.getAndIncrement();
+            }
+
+        }catch (Exception e) {
+            e.printStackTrace();
+            if (session != null) {
+                session.rollback();
+            }
+
+            return Result.error(new CodeMsg(6002, e.getMessage()));
+        }finally {
+            if (session != null) {
+                session.close();
+            }
+        }
+        return Result.success(null);
+    }
 }

--
Gitblit v1.8.0