重构返回结果集
This commit is contained in:
parent
711b258f12
commit
4a388770b0
|
|
@ -1,6 +1,7 @@
|
|||
package com.ebts.common.utils;
|
||||
|
||||
import com.ebts.common.config.EBTSConfig;
|
||||
import com.ebts.common.core.entity.Result;
|
||||
import com.ebts.common.core.entity.ResultBackI;
|
||||
import com.ebts.common.exception.CustomException;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
|
|
@ -68,6 +69,17 @@ public class MapExcelUtil {
|
|||
this.init(list, sheetName);
|
||||
return exportExcel();
|
||||
}
|
||||
/**
|
||||
* 对list数据源将其里面的数据导入到excel表单
|
||||
*
|
||||
* @param list 导出数据集合
|
||||
* @param sheetName 工作表的名称
|
||||
* @return 结果
|
||||
*/
|
||||
public Result exportExcelResult(List<Map<String, Object>> list, String sheetName) {
|
||||
this.init(list, sheetName);
|
||||
return exportExcelResult();
|
||||
}
|
||||
|
||||
|
||||
public void init(List<Map<String, Object>> list, String sheetName) {
|
||||
|
|
@ -292,4 +304,53 @@ public class MapExcelUtil {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 对list数据源将其里面的数据导入到excel表单
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public Result exportExcelResult() {
|
||||
OutputStream out = null;
|
||||
try {
|
||||
// 取出一共有多少个sheet.
|
||||
double sheetNo = Math.ceil(list.size() / sheetSize);
|
||||
for (int index = 0; index <= sheetNo; index++) {
|
||||
createSheet(sheetNo, index);
|
||||
|
||||
// 产生一行
|
||||
Row row = sheet.createRow(0);
|
||||
int column = 0;
|
||||
// 写入各个字段的列头名称
|
||||
for (String herder : herders) {
|
||||
//设置列名称
|
||||
this.createCell(row, herder, column++);
|
||||
}
|
||||
fillExcelData(index, row);
|
||||
}
|
||||
String filename = encodingFilename(sheetName);
|
||||
out = new FileOutputStream(getAbsoluteFile(filename));
|
||||
wb.write(out);
|
||||
return Result.ok(filename);
|
||||
} catch (Exception e) {
|
||||
logger.error("导出Excel异常{}", e.getMessage());
|
||||
throw new CustomException("导出Excel失败,请联系网站管理员!");
|
||||
} finally {
|
||||
if (wb != null) {
|
||||
try {
|
||||
wb.close();
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (out != null) {
|
||||
try {
|
||||
out.close();
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
package com.ebts.generator.controller;
|
||||
|
||||
import com.ebts.common.core.controller.BaseController;
|
||||
import com.ebts.common.core.entity.ResultBackI;
|
||||
import com.ebts.common.core.entity.Result;
|
||||
import com.ebts.common.utils.MapExcelUtil;
|
||||
import com.ebts.common.utils.ServerResult;
|
||||
import com.ebts.common.utils.StringUtils;
|
||||
import com.ebts.generator.utils.sql.GenSqlUtil;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
|
|
@ -31,8 +30,6 @@ import java.util.Map;
|
|||
@RestController
|
||||
@RequestMapping("/query")
|
||||
public class QueryController extends BaseController {
|
||||
private Logger logger = LoggerFactory.getLogger(QueryController.class);
|
||||
|
||||
|
||||
@Autowired
|
||||
private QueryService queryService;
|
||||
|
|
@ -45,18 +42,18 @@ public class QueryController extends BaseController {
|
|||
*/
|
||||
@PreAuthorize("@ebts.hasPermi('query:list')")
|
||||
@GetMapping("/{id}")
|
||||
public ResultBackI Info(@PathVariable("id") Long id) {
|
||||
ServerResult<UniQuery> ServerResult = queryService.selectQueryById(id);
|
||||
if (ServerResult.isStart()) {
|
||||
UniQuery uniQuery = ServerResult.getData();
|
||||
public Result<Map<String, Object>> Info(@PathVariable("id") Long id) {
|
||||
Result<UniQuery> result = queryService.selectQueryById(id);
|
||||
if (result.isSuccess()) {
|
||||
UniQuery uniQuery = result.getData();
|
||||
Map<String, Object> modeMap = new HashMap<>();
|
||||
List<UniCon> uniCons = uniQuery.getUniCons();
|
||||
uniQuery.setUniCons(null);
|
||||
modeMap.put("info", uniQuery);
|
||||
modeMap.put("list", uniCons);
|
||||
return ResultBackI.success(modeMap);
|
||||
return Result.ok(modeMap);
|
||||
} else {
|
||||
return ResultBackI.error(ServerResult.getMsg());
|
||||
return Result.error(result.getMsg());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -68,13 +65,8 @@ public class QueryController extends BaseController {
|
|||
*/
|
||||
@PreAuthorize("@ebts.hasPermi('query:edit')")
|
||||
@PutMapping
|
||||
public ResultBackI edit(@Validated @RequestBody UniQuery uniQuery) {
|
||||
ServerResult ServerResult = queryService.updateQueryInfo(uniQuery);
|
||||
if (ServerResult.isStart()) {
|
||||
return ResultBackI.success();
|
||||
} else {
|
||||
return ResultBackI.error(ServerResult.getMsg());
|
||||
}
|
||||
public Result<Integer> edit(@Validated @RequestBody UniQuery uniQuery) {
|
||||
return queryService.updateQueryInfo(uniQuery);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -85,12 +77,12 @@ public class QueryController extends BaseController {
|
|||
*/
|
||||
@PreAuthorize("@ebts.hasAnyPermi('query:export')")
|
||||
@PutMapping("export")
|
||||
public ResultBackI export(@Validated @RequestBody UniQuery uniQuery) {
|
||||
ServerResult<List<Map<String, Object>>> ServerResult = queryService.previewQuery(uniQuery);
|
||||
if (ServerResult.isStart()) {
|
||||
return new MapExcelUtil().exportExcel(ServerResult.getData(), uniQuery.getUqName());
|
||||
public Serializable export(@Validated @RequestBody UniQuery uniQuery) {
|
||||
Result<List<Map<String, Object>>> result = queryService.previewQuery(uniQuery);
|
||||
if (result.isSuccess()) {
|
||||
return new MapExcelUtil().exportExcelResult(result.getData(), uniQuery.getUqName());
|
||||
} else {
|
||||
return ResultBackI.error(ServerResult.getMsg());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -104,11 +96,11 @@ public class QueryController extends BaseController {
|
|||
@PutMapping("preview")
|
||||
public Serializable Preview(@Validated @RequestBody UniQuery uniQuery) {
|
||||
startPage(uniQuery);
|
||||
ServerResult<List<Map<String, Object>>> ServerResult = queryService.previewQuery(uniQuery);
|
||||
if (ServerResult.isStart()) {
|
||||
return getDataTable(ServerResult.getData());
|
||||
Result<List<Map<String, Object>>> result = queryService.previewQuery(uniQuery);
|
||||
if (result.isSuccess()) {
|
||||
return getDataTable(result.getData());
|
||||
} else {
|
||||
return ResultBackI.error(ServerResult.getMsg());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -120,20 +112,14 @@ public class QueryController extends BaseController {
|
|||
*/
|
||||
@PreAuthorize("@ebts.hasPermi('query:release')")
|
||||
@PutMapping("/release")
|
||||
public ResultBackI Release(@RequestBody UniQuery uniQuery) {
|
||||
public Result<Integer> Release(@RequestBody UniQuery uniQuery) {
|
||||
if (uniQuery.getId() == null) {
|
||||
return ResultBackI.error("id不能为空!");
|
||||
return Result.error("id不能为空!");
|
||||
}
|
||||
if (uniQuery.getIsRelease() == 1 || uniQuery.getIsRelease() == 2) {
|
||||
return queryService.Release(uniQuery);
|
||||
} else {
|
||||
if (uniQuery.getIsRelease() == 1 || uniQuery.getIsRelease() == 2) {
|
||||
ServerResult<Integer> ServerResult = queryService.Release(uniQuery);
|
||||
if (ServerResult.isStart()) {
|
||||
return ResultBackI.success();
|
||||
} else {
|
||||
return ResultBackI.error(ServerResult.getMsg());
|
||||
}
|
||||
} else {
|
||||
return ResultBackI.error("状态输入错误!");
|
||||
}
|
||||
return Result.error("状态输入错误!");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package com.ebts.generator.service;
|
||||
|
||||
import com.ebts.common.utils.ServerResult;
|
||||
import com.ebts.common.core.entity.Result;
|
||||
import com.ebts.generator.entity.UniQuery;
|
||||
|
||||
import java.util.List;
|
||||
|
|
@ -18,7 +18,7 @@ public interface QueryService {
|
|||
* @param id
|
||||
* @return
|
||||
*/
|
||||
ServerResult<UniQuery> selectQueryById(Long id);
|
||||
Result<UniQuery> selectQueryById(Long id);
|
||||
|
||||
/**
|
||||
* 修改基础信息
|
||||
|
|
@ -26,7 +26,7 @@ public interface QueryService {
|
|||
* @param uniQuery
|
||||
* @return
|
||||
*/
|
||||
ServerResult<Integer> updateQueryInfo(UniQuery uniQuery);
|
||||
Result<Integer> updateQueryInfo(UniQuery uniQuery);
|
||||
|
||||
/**
|
||||
* 预览万能查询数据
|
||||
|
|
@ -34,11 +34,13 @@ public interface QueryService {
|
|||
* @param uniQuery
|
||||
* @return
|
||||
*/
|
||||
ServerResult<List<Map<String, Object>>> previewQuery(UniQuery uniQuery);
|
||||
Result<List<Map<String, Object>>> previewQuery(UniQuery uniQuery);
|
||||
|
||||
/**
|
||||
* 发布
|
||||
*
|
||||
* @param uniQuery
|
||||
* @return
|
||||
*/
|
||||
ServerResult<Integer> Release(UniQuery uniQuery);
|
||||
Result<Integer> Release(UniQuery uniQuery);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,10 +2,9 @@ package com.ebts.generator.service.impl;
|
|||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ebts.common.constant.ReturnConstants;
|
||||
import com.ebts.common.core.entity.Result;
|
||||
import com.ebts.common.core.entity.entity.Menu;
|
||||
import com.ebts.common.exception.EbtsException;
|
||||
import com.ebts.common.utils.SecurityUtils;
|
||||
import com.ebts.common.utils.ServerResult;
|
||||
import com.ebts.generator.dao.QueryDao;
|
||||
import com.ebts.generator.dao.UniQueryDao;
|
||||
import com.ebts.generator.entity.UniCon;
|
||||
|
|
@ -13,11 +12,12 @@ import com.ebts.generator.entity.UniQuery;
|
|||
import com.ebts.generator.service.QueryService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
|
@ -30,10 +30,10 @@ import java.util.Map;
|
|||
public class QueryServiceImpl implements QueryService {
|
||||
private final Logger logger = LoggerFactory.getLogger(QueryServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
@Resource
|
||||
private QueryDao queryDao;
|
||||
|
||||
@Autowired
|
||||
@Resource
|
||||
private UniQueryDao uniQueryDao;
|
||||
|
||||
|
||||
|
|
@ -41,54 +41,45 @@ public class QueryServiceImpl implements QueryService {
|
|||
* 查询万能查询的基本信息和条件信息
|
||||
*/
|
||||
@Override
|
||||
public ServerResult<UniQuery> selectQueryById(Long id) {
|
||||
try {
|
||||
UniQuery uniQuery = queryDao.selectQueryInfo(id);
|
||||
if (uniQuery != null) {
|
||||
return new ServerResult<>(true, uniQuery);
|
||||
} else {
|
||||
return new ServerResult<>(false, ReturnConstants.RESULT_EMPTY);
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
logger.error(e.getMessage());
|
||||
throw new EbtsException(ReturnConstants.OP_ERROR,e);
|
||||
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
|
||||
public Result<UniQuery> selectQueryById(Long id) {
|
||||
UniQuery uniQuery = queryDao.selectQueryInfo(id);
|
||||
if (uniQuery != null) {
|
||||
return Result.ok(uniQuery);
|
||||
} else {
|
||||
return Result.info(ReturnConstants.RESULT_EMPTY);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ServerResult<Integer> Release(UniQuery uniQuery) {
|
||||
try {
|
||||
Integer isRelease = queryDao.Release(uniQuery.getId());
|
||||
if ((isRelease == 1 && uniQuery.getIsRelease() == 2) || (isRelease == 2 && uniQuery.getIsRelease() == 1)) {
|
||||
if (uniQuery.getIsRelease() == 1) {
|
||||
UniQuery query = uniQueryDao.selectUniQueryById(uniQuery.getId());
|
||||
Menu Menu = new Menu(query.getId(), query.getUqName(), SecurityUtils.getUserId());
|
||||
Integer insermenu = queryDao.insertMenu(Menu);
|
||||
if (insermenu == 0) {
|
||||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||||
return new ServerResult<>(false, ReturnConstants.OP_ERROR);
|
||||
}
|
||||
} else {
|
||||
Integer deleteMenu = queryDao.deleteMenu("data/" + uniQuery.getId());
|
||||
if (deleteMenu == 0) {
|
||||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||||
return new ServerResult<>(false, ReturnConstants.OP_ERROR);
|
||||
}
|
||||
}
|
||||
Integer release = queryDao.changeRelease(uniQuery);
|
||||
if (release > 0) {
|
||||
return new ServerResult<>(true);
|
||||
} else {
|
||||
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
|
||||
public Result<Integer> Release(UniQuery uniQuery) {
|
||||
Integer isRelease = queryDao.Release(uniQuery.getId());
|
||||
if ((isRelease == 1 && uniQuery.getIsRelease() == 2) || (isRelease == 2 && uniQuery.getIsRelease() == 1)) {
|
||||
if (uniQuery.getIsRelease() == 1) {
|
||||
UniQuery query = uniQueryDao.selectUniQueryById(uniQuery.getId());
|
||||
Menu Menu = new Menu(query.getId(), query.getUqName(), SecurityUtils.getUserId());
|
||||
Integer insermenu = queryDao.insertMenu(Menu);
|
||||
if (insermenu == 0) {
|
||||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||||
return new ServerResult<>(false, ReturnConstants.OP_ERROR);
|
||||
return Result.error(ReturnConstants.OP_ERROR);
|
||||
}
|
||||
} else {
|
||||
return new ServerResult<>(false, ReturnConstants.STATE_ERROR);
|
||||
Integer deleteMenu = queryDao.deleteMenu("data/" + uniQuery.getId());
|
||||
if (deleteMenu == 0) {
|
||||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||||
return Result.error(ReturnConstants.OP_ERROR);
|
||||
}
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
logger.error(e.getMessage());
|
||||
throw new EbtsException(ReturnConstants.OP_ERROR,e);
|
||||
Integer release = queryDao.changeRelease(uniQuery);
|
||||
if (release > 0) {
|
||||
return Result.ok();
|
||||
} else {
|
||||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||||
return Result.error(ReturnConstants.OP_ERROR);
|
||||
}
|
||||
} else {
|
||||
return Result.error(ReturnConstants.STATE_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -96,51 +87,41 @@ public class QueryServiceImpl implements QueryService {
|
|||
* 更新万能查询基本信息和条件
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ServerResult<Integer> updateQueryInfo(UniQuery uniQuery) {
|
||||
try {
|
||||
int uqrenewal = uniQueryDao.updateUniQuery(uniQuery);
|
||||
if (uqrenewal > 0) {
|
||||
if (uniQuery.getUniCons() != null && uniQuery.getUniCons().size() > 0) {
|
||||
queryDao.deleteUniCon(uniQuery.getId());
|
||||
Integer ucrenewal = queryDao.insertUniCon(uniQuery.getUniCons());
|
||||
if (ucrenewal == 0) {
|
||||
return new ServerResult<>(false, ReturnConstants.OP_ERROR);
|
||||
}
|
||||
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
|
||||
public Result<Integer> updateQueryInfo(UniQuery uniQuery) {
|
||||
int uqrenewal = uniQueryDao.updateUniQuery(uniQuery);
|
||||
if (uqrenewal > 0) {
|
||||
if (uniQuery.getUniCons() != null && uniQuery.getUniCons().size() > 0) {
|
||||
queryDao.deleteUniCon(uniQuery.getId());
|
||||
Integer ucrenewal = queryDao.insertUniCon(uniQuery.getUniCons());
|
||||
if (ucrenewal == 0) {
|
||||
return Result.error(ReturnConstants.OP_ERROR);
|
||||
}
|
||||
return new ServerResult<>(true);
|
||||
}
|
||||
return new ServerResult<>(false, ReturnConstants.OP_ERROR);
|
||||
} catch (RuntimeException e) {
|
||||
logger.error(e.getMessage());
|
||||
throw new EbtsException(ReturnConstants.OP_ERROR,e);
|
||||
return Result.ok();
|
||||
}
|
||||
return Result.error(ReturnConstants.OP_ERROR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerResult<List<Map<String, Object>>> previewQuery(UniQuery uniQuery) {
|
||||
try {
|
||||
StringBuilder sql = new StringBuilder(uniQuery.getUqSql().toLowerCase());
|
||||
List<UniCon> uniConList = uniQuery.getUniCons();
|
||||
if (sql.toString().contains("insert") || sql.toString().contains("delete ") || sql.toString().contains("update ") ||
|
||||
sql.toString().contains("drop ") || sql.toString().contains("database ") || sql.toString().contains("create ") ||
|
||||
sql.toString().contains("view ") || sql.toString().contains("alter ") || sql.toString().contains("gen_")) {
|
||||
return new ServerResult<>(false, "sql语句含有insert,delete,update,drop,database,view,alter,gen_等特殊字符!");
|
||||
}
|
||||
if (uniConList.size() > 0) {
|
||||
sql.append(" where 1 = 1");
|
||||
for (UniCon uniCon : uniConList) {
|
||||
if (!uniCon.getUcMock().equals("") && uniCon.getUcMock() != null) {
|
||||
sql.append(conversionPreview(uniCon));
|
||||
}
|
||||
public Result<List<Map<String, Object>>> previewQuery(UniQuery uniQuery) {
|
||||
StringBuilder sql = new StringBuilder(uniQuery.getUqSql().toLowerCase());
|
||||
List<UniCon> uniConList = uniQuery.getUniCons();
|
||||
if (sql.toString().contains("insert") || sql.toString().contains("delete ") || sql.toString().contains("update ") ||
|
||||
sql.toString().contains("drop ") || sql.toString().contains("database ") || sql.toString().contains("create ") ||
|
||||
sql.toString().contains("view ") || sql.toString().contains("alter ") || sql.toString().contains("gen_")) {
|
||||
return Result.error("sql语句含有insert,delete,update,drop,database,view,alter,gen_等特殊字符!");
|
||||
}
|
||||
if (uniConList.size() > 0) {
|
||||
sql.append(" where 1 = 1");
|
||||
for (UniCon uniCon : uniConList) {
|
||||
if (!uniCon.getUcMock().equals("") && uniCon.getUcMock() != null) {
|
||||
sql.append(conversionPreview(uniCon));
|
||||
}
|
||||
}
|
||||
List<Map<String, Object>> dataMap = queryDao.UniQuery(sql.toString());
|
||||
return new ServerResult<>(true, dataMap);
|
||||
} catch (RuntimeException e) {
|
||||
logger.error(e.getMessage());
|
||||
return new ServerResult<>(false, e.getMessage());
|
||||
}
|
||||
List<Map<String, Object>> dataMap = queryDao.UniQuery(sql.toString());
|
||||
return Result.ok(dataMap);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -39,7 +39,6 @@ public class UniQueryServiceImpl implements UniQueryService {
|
|||
@Override
|
||||
public Result<UniQuery> selectUniQueryById(Long id) {
|
||||
UniQuery uniQuery = uniQueryDao.selectUniQueryById(id);
|
||||
System.out.println(1/0);
|
||||
if (uniQuery != null) {
|
||||
return Result.ok(uniQuery);
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Reference in New Issue