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 | 162 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 147 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/highdatas/mdm/controller/FileController.java b/src/main/java/com/highdatas/mdm/controller/FileController.java index f250ac9..5b109ff 100644 --- a/src/main/java/com/highdatas/mdm/controller/FileController.java +++ b/src/main/java/com/highdatas/mdm/controller/FileController.java @@ -1,30 +1,32 @@ package com.highdatas.mdm.controller; import com.alibaba.fastjson.JSONObject; -import com.highdatas.mdm.entity.SysAssemble; -import com.highdatas.mdm.entity.SysAssembleParams; +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.service.ISysAssembleDbService; -import com.highdatas.mdm.service.ISysAssembleParamsService; -import com.highdatas.mdm.service.ISysAssembleService; -import com.highdatas.mdm.util.Constant; -import com.highdatas.mdm.util.DbUtils; -import com.highdatas.mdm.util.RedisClient; +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.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; +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 */ @@ -39,7 +41,25 @@ 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) { @@ -47,17 +67,21 @@ 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); @@ -70,11 +94,19 @@ } } - + /** + * + * @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) { - // 鏆備笉鏍¢獙鏄笉鏄痵elect璇彞浜� 鍚庣画闇�瑕佸啀鏍¢獙 + + //閫氳繃id鑾峰彇姹囬泦浠诲姟淇℃伅 SysAssemble assemble = assembleService.selectById(id); if (assemble == null){ return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED); @@ -83,7 +115,9 @@ byte[] bytes = file.getBytes(); String sql = new String (bytes); + //鍘婚櫎绌烘牸闃叉不sql鍥犱负绌烘牸鎶ラ敊 sql = DbUtils.replaceEscape(sql); + // 鏆備笉鏍¢獙鏄笉鏄痵elect璇彞浜� 鍚庣画闇�瑕佸啀鏍¢獙 //TODO 鏍¢獙sql 鏆備笉杩涜 assemble.setPurgeSql(sql); boolean update = assemble.updateById(); @@ -99,7 +133,14 @@ } } - + /** + * + * @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) { @@ -112,6 +153,7 @@ byte[] bytes = file.getBytes(); String sql = new String (bytes); + //鍘婚櫎绌烘牸闃叉不sql鍥犱负绌烘牸鎶ラ敊 sql = DbUtils.replaceEscape(sql); //TODO 鏍¢獙sql 鏆備笉杩涜 sysAssembleParams.setUpdateSql(sql).setUpdateTime(new Date()).updateById(); @@ -123,4 +165,94 @@ } } + + /** + * + * @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