package com.product.server.report.controller; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.product.server.report.config.ReportCode; import com.product.util.BaseUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.product.common.lang.StringUtils; import com.product.core.config.CoreConst; import com.product.core.controller.support.AbstractBaseController; import com.product.core.entity.DataTableEntity; import com.product.core.entity.FieldSetEntity; import com.product.core.entity.RequestParameterEntity; import com.product.core.exception.BaseException; import com.product.module.sys.version.ApiVersion; import com.product.server.report.config.CmnConst; import com.product.server.report.config.SystemCode; import com.product.server.report.service.ReportDatasourceService; import com.product.server.report.service.idel.IReportDatasourceService; import com.product.util.config.SystemParamSet; @RestController @RequestMapping("/api/report-datasource") public class ReportDatasourceController extends AbstractBaseController{ @Autowired ReportDatasourceService reportDatasourceService; /** * 报表数据源列表 * @param response * @param request * @return */ @RequestMapping(value = "/list-datasource/{version}", method = RequestMethod.POST) @ApiVersion(1) public String listDatasource(HttpServletResponse response,HttpServletRequest request) { try { //获取参数 FieldSetEntity fse=null; Object bean=request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if(bean != null){ RequestParameterEntity reqp=(RequestParameterEntity)bean; fse = reqp.getFormData(); } //判断参数是否为空 if(bean == null || fse == null) { return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } //判断表名是否正常 if (!CmnConst.PRODUCT_SYS_REPORT_DATASOURCE_CONFIG.equals(fse.getTableName())) { return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); } DataTableEntity dt=reportDatasourceService.listDatasource(fse); return OK_List(dt); } catch (BaseException e) { e.printStackTrace(); return this.error(e.getCode(),e.getMessage()); } catch (Exception e) { e.printStackTrace(); return this.error(ReportCode.REPORT_DATASOURCE_ERROR.getValue(), ReportCode.REPORT_DATASOURCE_ERROR.getText()+e.getMessage()); } } /** * 报表数据源详情 * @param response * @param request * @return */ @RequestMapping(value = "/find-datasource/{version}", method = RequestMethod.POST) @ApiVersion(1) public String findDatasource(HttpServletResponse response,HttpServletRequest request) { try { //获取参数 FieldSetEntity fse=null; Object bean=request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if(bean != null){ RequestParameterEntity reqp=(RequestParameterEntity)bean; fse = reqp.getFormData(); } //判断参数是否为空 if(bean == null || fse == null) { return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } //判断表名是否正常 if (!CmnConst.PRODUCT_SYS_REPORT_DATASOURCE_CONFIG.equals(fse.getTableName())) { return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); } //判断uuid是否为空 if (StringUtils.isEmpty(fse.getUUID())) { return this.error(SystemCode.SYSTEM_FORM_COUNT.getValue(), SystemCode.SYSTEM_FORM_COUNT.getText()); } FieldSetEntity dt=reportDatasourceService.findDatasource(fse.getUUID()); return OK_List(dt); } catch (BaseException e) { e.printStackTrace(); return this.error(e.getCode(),e.getMessage()); } catch (Exception e) { e.printStackTrace(); return this.error(ReportCode.REPORT_DATASOURCE_ERROR.getValue(), ReportCode.REPORT_DATASOURCE_ERROR.getText()+e.getMessage()); } } /** * 数据源-保存 * @param response * @param request * @return */ @RequestMapping(value = "/save-datasource/{version}", method = RequestMethod.POST) @ApiVersion(1) public String updateDatasource(HttpServletRequest request) { try { //获取参数 FieldSetEntity fse=null; Object bean=request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if(bean != null){ RequestParameterEntity reqp=(RequestParameterEntity)bean; fse = reqp.getFormData(); } //判断参数是否为空 if(bean == null || fse == null) { return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } //判断表名是否正常 if (!CmnConst.PRODUCT_SYS_REPORT_DATASOURCE_CONFIG.equals(fse.getTableName())) { return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); } IReportDatasourceService service=(IReportDatasourceService)getProxyInstance(reportDatasourceService); String uuid = service.saveDatasource(fse); return OK_Add(uuid); } catch (BaseException e) { e.printStackTrace(); return this.error(e.getCode(),e.getMessage()); } catch (Exception e) { e.printStackTrace(); return this.error(ReportCode.REPORT_DATASOURCE_SAVE_FAIL.getValue(), ReportCode.REPORT_DATASOURCE_SAVE_FAIL.getText()+e.getMessage()); } } /** * 报表数据源新增 * * @param request * @return */ @RequestMapping(value = "/copy-datasource/{version}", method = RequestMethod.POST) @ApiVersion(1) public String copyDatasource(HttpServletRequest request) { try { //获取参数 FieldSetEntity fse = null; Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if (bean != null) { RequestParameterEntity reqp = (RequestParameterEntity) bean; fse = reqp.getFormData(); } //判断参数是否为空 if (bean == null || fse == null) { return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } //判断表名是否正常 if (!CmnConst.PRODUCT_SYS_REPORT_DATASOURCE_CONFIG.equals(fse.getTableName())) { return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); } return OK_List(reportDatasourceService.copyDatasource(fse)); } catch (BaseException e) { e.printStackTrace(); return this.error(e.getCode(), e.getMessage()); } catch (Exception e) { e.printStackTrace(); return this.error(ReportCode.REPORT_DATASOURCE_COPY_FAIL.getValue(), ReportCode.REPORT_DATASOURCE_COPY_FAIL.getText() + e.getMessage()); } } /** * 报表数据源删除 * @param response * @param request * @return */ @RequestMapping(value = "/delete-datasource/{version}", method = RequestMethod.POST) @ApiVersion(1) public String deleteDatasource(HttpServletResponse response,HttpServletRequest request) { try { //获取参数 FieldSetEntity fse=null; Object bean=request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if(bean != null){ RequestParameterEntity reqp=(RequestParameterEntity)bean; fse = reqp.getFormData(); } //判断参数是否为空 if(bean == null || fse == null) { return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } //判断表名是否正常 if (!CmnConst.PRODUCT_SYS_REPORT_DATASOURCE_CONFIG.equals(fse.getTableName())) { return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); } //判断uuid是否为空 if (StringUtils.isEmpty(fse.getUUID())) { return this.error(SystemCode.SYSTEM_FORM_COUNT.getValue(), SystemCode.SYSTEM_FORM_COUNT.getText()); } IReportDatasourceService service=(IReportDatasourceService)getProxyInstance(reportDatasourceService); boolean succ=service.deleteDatasource(fse.getUUID()); if (succ) { return OK(); }else { return this.error(SystemCode.SYTEM_REPORT_DATASOURCE_DELETE_ERROR.getValue(), SystemCode.SYTEM_REPORT_DATASOURCE_DELETE_ERROR.getText()); } } catch (BaseException e) { e.printStackTrace(); return this.error(e.getCode(),e.getMessage()); } catch (Exception e) { e.printStackTrace(); return this.error(ReportCode.REPORT_DATASOURCE_ERROR.getValue(), ReportCode.REPORT_DATASOURCE_ERROR.getText()+e.getMessage()); } } /** * 报表sql解析与验证 * @param response * @param request * @return */ @RequestMapping(value = "/verify-sql/{version}", method = RequestMethod.POST) @ApiVersion(1) public String sqlVerify(HttpServletResponse response,HttpServletRequest request) { try { //获取参数 FieldSetEntity fse=null; Object bean=request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if(bean != null){ RequestParameterEntity reqp=(RequestParameterEntity)bean; fse = reqp.getFormData(); } //判断参数是否为空 if(bean == null || fse == null) { return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } //判断uuid是否为空 if (StringUtils.isEmpty(fse.getString(CmnConst.SQL_TEXT))) { return this.error(SystemCode.SYSTEM_FORM_COUNT.getValue(), SystemCode.SYSTEM_FORM_COUNT.getText()); } DataTableEntity dt=reportDatasourceService.sqlVerify(fse); return OK_List(dt); } catch (BaseException e) { e.printStackTrace(); return this.error(e); } catch (Exception e) { e.printStackTrace(); return this.error("", ReportCode.REPORT_DATASOURCE_ERROR.getText()+e.getMessage()); } } /** * 获取系统参数 * @param response * @param request * @return */ @RequestMapping(value = "/get-system-param/{version}", method = RequestMethod.POST) @ApiVersion(1) public String getSysParam(HttpServletResponse response,HttpServletRequest request) { try { //获取参数 FieldSetEntity fse=null; Object bean=request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if(bean != null){ RequestParameterEntity reqp=(RequestParameterEntity)bean; fse = reqp.getFormData(); } //判断参数是否为空 if(bean == null || fse == null) { return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } return BaseUtil.success(SystemParamSet.SYSTEM_PARAMS); } catch (Exception e) { e.printStackTrace(); return this.error(ReportCode.REPORT_DATASOURCE_ERROR.getValue(), ReportCode.REPORT_DATASOURCE_ERROR.getText()+e.getMessage()); } } /** * 获取字典参数名 * @param response * @param request * @return */ @RequestMapping(value = "/get-dict-names/{version}", method = RequestMethod.POST) @ApiVersion(1) public String getDictNames(HttpServletResponse response,HttpServletRequest request) { try { //获取参数 FieldSetEntity fse=null; Object bean=request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if(bean != null){ RequestParameterEntity reqp=(RequestParameterEntity)bean; fse = reqp.getFormData(); } //判断参数是否为空 if(bean == null || fse == null) { return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } DataTableEntity dt=reportDatasourceService.getDictNames(); return OK_List(dt); } catch (BaseException e) { e.printStackTrace(); return this.error(e.getCode()); } catch (Exception e) { e.printStackTrace(); return this.error("", e.getMessage()); } } /** * 报表数据源详情 * @param response * @param request * @return */ @RequestMapping(value = "/get-datasource-field/{version}", method = RequestMethod.POST) @ApiVersion(1) public String getDataReouceOfField(HttpServletResponse response,HttpServletRequest request) { try { //获取参数 FieldSetEntity fse=null; Object bean=request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if(bean != null){ RequestParameterEntity reqp=(RequestParameterEntity)bean; fse = reqp.getFormData(); } //判断参数是否为空 if(bean == null || fse == null) { return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } //判断表名是否正常 if (!CmnConst.PRODUCT_SYS_REPORT_DATASOURCE_CONFIG_FIELD.equals(fse.getTableName())) { return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); } //判断uuid是否为空 if (StringUtils.isEmpty(fse.getUUID())) { return this.error(SystemCode.SYSTEM_FORM_COUNT.getValue(), SystemCode.SYSTEM_FORM_COUNT.getText()); } DataTableEntity dt=reportDatasourceService.getDataReouceOfField(fse.getUUID()); return OK_List(dt); } catch (BaseException e) { e.printStackTrace(); return this.error(e.getCode(),e.getMessage()); } catch (Exception e) { e.printStackTrace(); return this.error(ReportCode.REPORT_DATASOURCE_ERROR.getValue(), ReportCode.REPORT_DATASOURCE_ERROR.getText()+e.getMessage()); } } /** * 报表数据源详情 * @param response * @param request * @return */ @RequestMapping(value = "/get-dict-and-pormpt/{version}", method = RequestMethod.POST) @ApiVersion(1) public String getDictAndPrompt(HttpServletResponse response,HttpServletRequest request) { try { //获取参数 FieldSetEntity fse=null; Object bean=request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if(bean != null){ RequestParameterEntity reqp=(RequestParameterEntity)bean; fse = reqp.getFormData(); } //判断参数是否为空 if(bean == null || fse == null) { return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } List dt=reportDatasourceService.getDictAndPrompt(); return BaseUtil.success(dt); } catch (BaseException e) { e.printStackTrace(); return this.error(e.getCode(),e.getMessage()); } catch (Exception e) { e.printStackTrace(); return this.error(ReportCode.REPORT_DATASOURCE_ERROR.getValue(), ReportCode.REPORT_DATASOURCE_ERROR.getText()+e.getMessage()); } } }