| | |
| | | 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.SysAssemble; |
| | | import com.highdatas.mdm.entity.SysAssembleParams; |
| | | 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 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.Date; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * @author kimi |
| | |
| | | @RestController |
| | | @RequestMapping("/file") |
| | | public class FileController { |
| | | @Autowired |
| | | ISysAssembleDbService dbService; |
| | | @Autowired |
| | | ISysAssembleService assembleService; |
| | | @Autowired |
| | | ISysAssembleParamsService paramsService; |
| | | @Autowired |
| | | RedisClient redisClient; |
| | | |
| | | @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 = DbUtils.replaceEscape(sql); |
| | | String[] split = sql.split(Constant.SEMICOLON); |
| | | 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保存失败")); |
| | | } |
| | | // 暂不校验是不是select语句了 后续需要再校验 |
| | | 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())); |
| | | } |
| | | |
| | | } |
| | | |
| | | @RequestMapping(value = "/loadPurgeSql", method = RequestMethod.POST) |
| | | @ResponseBody |
| | | public Result loadPurgeSql(@RequestParam("file")MultipartFile file,@RequestParam String id, HttpServletRequest request) { |
| | | // 暂不校验是不是select语句了 后续需要再校验 |
| | | 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 = DbUtils.replaceEscape(sql); |
| | | //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())); |
| | | } |
| | | |
| | | } |
| | | |
| | | @RequestMapping(value = "/loadParamsUpdateSql", method = RequestMethod.POST) |
| | | @ResponseBody |
| | | public Result loadParamsUpdateSql(@RequestParam("file")MultipartFile file,@RequestParam String id, HttpServletRequest request) { |
| | | // 暂不校验是不是select语句了 后续需要再校验 |
| | | 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 = 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())); |
| | | } |
| | | |
| | | } |
| | | } |