package com.product.org.admin.controller; import com.product.common.lang.StringUtils; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.product.core.permission.PermissionService; import com.product.module.sys.config.SystemErrorCode; 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.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.product.core.config.CoreConst; import com.product.core.controller.support.AbstractBaseController; import com.product.core.dao.BaseDao; import com.product.core.entity.FieldSetEntity; import com.product.core.entity.RequestParameterEntity; import com.product.core.exception.BaseException; import com.product.core.spring.context.SpringMVCContextHolder; import com.product.module.sys.entity.SystemUser; import com.product.module.sys.version.ApiVersion; import com.product.org.admin.config.CmnConst; import com.product.org.admin.config.SystemCode; import com.product.org.admin.service.ProcedureService; import com.product.org.admin.service.idel.IProcedureService; import com.product.util.BaseUtil; /** * Copyright © LX-BASE * * @Title: ProcedureController * @Project: LX-BASE-SERVER * @Date: 2020-05-29 11:42:06 * @Author: Xin.Luo * @Description: 存储过程管理 */ @RestController @RequestMapping("/api/procedure") public class ProcedureController extends AbstractBaseController { @Autowired public ProcedureService procedureService; @Autowired public BaseDao baseDao = null; /** * 数据权限验证 */ @Autowired PermissionService permissionService; public BaseDao getBaseDao() { return baseDao; } public void setBaseDao(BaseDao baseDao) { this.baseDao = baseDao; } /** * 解析存储过程 * @param response * @param request * @param session * @return */ @ResponseBody @RequestMapping(value = "/runSql/{version}", method = RequestMethod.POST) @ApiVersion(1) public String testSql(HttpServletResponse response, HttpServletRequest request, HttpSession session) { try { FieldSetEntity fse = null; Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if (bean != null) { RequestParameterEntity reqp = (RequestParameterEntity) bean; fse = reqp.getFormData(); } if (fse == null) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } if (fse.getTableName() == null || !CmnConst.PRODUCT_SYS_DATAMODEL_PROCEDURE.equals(fse.getTableName())) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); } IProcedureService service = (IProcedureService) getProxyInstance(procedureService); FieldSetEntity field = service.runProcedure(fse); // 返回field if (field != null) { return OK_List(field); } SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_SQL_START_FAIL.getValue(), SystemCode.SYSTEM_SQL_START_FAIL.getText()); return this.error(SystemCode.SYSTEM_SQL_START_FAIL.getValue(), SystemCode.SYSTEM_SQL_START_FAIL.getText()); } catch (BaseException e) { SpringMVCContextHolder.getSystemLogger().error(e); return this.error(e.getCode(), e.getMessageInfo()); } catch (Exception e) { e.printStackTrace(); SpringMVCContextHolder.getSystemLogger().error(e); return this.error(SystemCode.SYSTEM_RUN_SQL_PROCEDURE_FAIL.getValue(), SystemCode.SYSTEM_RUN_SQL_PROCEDURE_FAIL.getText()); } } /** * 保存存储过程 * @param response * @param request * @param session * @return */ @ResponseBody @RequestMapping(value = "/saveProcedure/{version}", method = RequestMethod.POST) @ApiVersion(1) public String saveProcedure(HttpServletResponse response, HttpServletRequest request, HttpSession session) { try { // 获取参数 FieldSetEntity fse = null; Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if (bean != null) { RequestParameterEntity reqp = (RequestParameterEntity) bean; fse = reqp.getFormData(); } // 判断参数是否为空 if (fse == null) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } String procedure_sql = fse.getString(CmnConst.PROCEDURE_SQL); String uuid = fse.getString(CmnConst.UUID); FieldSetEntity proField = new FieldSetEntity(); proField.setTableName(CmnConst.PRODUCT_SYS_DATAMODEL_PROCEDURE); procedure_sql = procedure_sql.replaceAll("\n", " ");// 替换为空格 procedure_sql = procedure_sql.replaceAll("\r", " ");// 替换为空格 proField.setValue(CmnConst.PROCEDURE_SQL, procedure_sql); if (StringUtils.isEmpty(uuid)) { fse.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());// 创建人 fse.setValue(CmnConst.CREATED_UTC_DATETIME, transitionDate(new Date()));// 创建时间 } else { // 超级管理员为1 SystemUser currentUser = SpringMVCContextHolder.getCurrentUser(); int type; if (currentUser.getUserType() == 1) { type = CoreConst.DATA_PERMISSION_VALID_TYPE_USER; } else { type = CoreConst.DATA_PERMISSION_VALID_TYPE_ORG; } // 数据操作的权限验证 if (!permissionService.validDataPermission(fse, type)) { SpringMVCContextHolder.getSystemLogger().error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(), SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText()); return this.error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(), SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText()); } fse.setValue(CmnConst.UUID, uuid); fse.setValue(CmnConst.UPDATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());// 修改人 fse.setValue(CmnConst.UPDATED_UTC_DATETIME, transitionDate(new Date()));// 修改时间 } IProcedureService service = (IProcedureService) getProxyInstance(procedureService); String pro_uuid = service.saveProcedure(fse); if (StringUtils.isEmpty(pro_uuid)) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_SAVE_PROCEDURE_FAIL.getValue(), SystemCode.SYSTEM_SAVE_PROCEDURE_FAIL.getText()); return this.error(SystemCode.SYSTEM_SAVE_PROCEDURE_FAIL.getValue(), SystemCode.SYSTEM_SAVE_PROCEDURE_FAIL.getText()); } return OK(); } catch (BaseException e) { SpringMVCContextHolder.getSystemLogger().error(e); return this.error(e.getCode(), e.getMessageInfo()); } catch (Exception e) { e.printStackTrace(); SpringMVCContextHolder.getSystemLogger().error(e); return this.error(SystemCode.SYSTEM_SAVE_PROCEDURE_FAIL.getValue(), SystemCode.SYSTEM_SAVE_PROCEDURE_FAIL.getText()); } } /** * 删除存储过程 * @param response * @param request * @param session * @return */ @ResponseBody @RequestMapping(value = "/delProcedure/{version}", method = RequestMethod.POST) @ApiVersion(1) public String delProcedure(HttpServletResponse response, HttpServletRequest request, HttpSession session) { try { // 获取参数 FieldSetEntity fse = null; Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if (bean != null) { RequestParameterEntity reqp = (RequestParameterEntity) bean; fse = reqp.getFormData(); } // 判断参数是否为空 if (fse == null) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } // 超级管理员为1 SystemUser currentUser = SpringMVCContextHolder.getCurrentUser(); int type; if (currentUser.getUserType() == 1) { type = CoreConst.DATA_PERMISSION_VALID_TYPE_USER; } else { type = CoreConst.DATA_PERMISSION_VALID_TYPE_ORG; } // 数据操作的权限验证 if (!permissionService.validDataPermission(fse, type)) { SpringMVCContextHolder.getSystemLogger().error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(), SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText()); return this.error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(), SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText()); } if (fse.getTableName() == null || !CmnConst.PRODUCT_SYS_DATAMODEL_PROCEDURE.equals(fse.getTableName())) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); } IProcedureService service = (IProcedureService) getProxyInstance(procedureService); boolean success = service.deleteProcedure(fse); if (success) { return OK(); } SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_PROCEDURE_FILTER_ERROR.getValue(), SystemCode.SYSTEM_PROCEDURE_FILTER_ERROR.getText()); return this.error(SystemCode.SYSTEM_PROCEDURE_FILTER_ERROR.getValue(), SystemCode.SYSTEM_PROCEDURE_FILTER_ERROR.getText()); } catch (BaseException e) { SpringMVCContextHolder.getSystemLogger().error(e); return this.error(e.getCode(), e.getMessageInfo()); } catch (Exception e) { e.printStackTrace(); SpringMVCContextHolder.getSystemLogger().error(e); return this.error(SystemCode.SYSTEM_PROCEDURE_FILTER_ERROR.getValue(), SystemCode.SYSTEM_PROCEDURE_FILTER_ERROR.getText()); } } /** * 获取存储过程详情 * @param response * @param request * @param session * @return */ @ResponseBody @RequestMapping(value = "/getProcedureInfo/{version}", method = RequestMethod.POST) @ApiVersion(1) public String getProcedureInfo(HttpServletResponse response, HttpServletRequest request, HttpSession session) { try { // 获取参数 FieldSetEntity fse = null; Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if (bean != null) { RequestParameterEntity reqp = (RequestParameterEntity) bean; fse = reqp.getFormData(); } // 判断参数是否为空 if (fse == null) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } String uuid = fse.getString(CmnConst.UUID); if (BaseUtil.strIsNull(uuid)) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } // 超级管理员为1 SystemUser currentUser = SpringMVCContextHolder.getCurrentUser(); int type; if (currentUser.getUserType() == 1) { type = CoreConst.DATA_PERMISSION_VALID_TYPE_USER; } else { type = CoreConst.DATA_PERMISSION_VALID_TYPE_ORG; } // 数据操作的权限验证 if (!permissionService.validDataPermission(fse, type)) { SpringMVCContextHolder.getSystemLogger().error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(), SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText()); return this.error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(), SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText()); } FieldSetEntity field = procedureService.getProcedureInfo(uuid); // 返回field if (field != null) { return OK_List(field); } SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_SQL_START_FAIL.getValue(), SystemCode.SYSTEM_SQL_START_FAIL.getText()); return this.error(SystemCode.SYSTEM_SQL_START_FAIL.getValue(), SystemCode.SYSTEM_SQL_START_FAIL.getText()); } catch (BaseException e) { SpringMVCContextHolder.getSystemLogger().error(e); return this.error(e.getCode(), e.getMessageInfo()); } catch (Exception e) { e.printStackTrace(); SpringMVCContextHolder.getSystemLogger().error(e); return this.error(SystemCode.SYSTEM_PROCEDUREINFO_FAIL.getValue(), SystemCode.SYSTEM_PROCEDUREINFO_FAIL.getText()); } } /** * 存储过程列表 * @param response * @param request * @param session * @return */ @ResponseBody @RequestMapping(value = "/getProcedureAll/{version}", method = RequestMethod.POST) @ApiVersion(1) public String getProcedureAll(HttpServletResponse response, HttpServletRequest request, HttpSession session) { try { // 获取参数 FieldSetEntity fse = null; Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if (bean != null) { RequestParameterEntity reqp = (RequestParameterEntity) bean; fse = reqp.getFormData(); } // 判断参数是否为空 if (fse == null) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } return OK_List(procedureService.getProcedureAll(fse)); } catch (BaseException e) { SpringMVCContextHolder.getSystemLogger().error(e); return this.error(e.getCode(), e.getMessageInfo()); } catch (Exception e) { e.printStackTrace(); SpringMVCContextHolder.getSystemLogger().error(e); return this.error(SystemCode.SYSTEM_PROCEDUREALL_FAIL.getValue(), SystemCode.SYSTEM_PROCEDUREALL_FAIL.getText()); } } // 时间处理为字符串 public String transitionDate(Date date) { // yyyy表示年数,MM表示月,dd表示日 // HH表示24小时制的小时(hh表示12小时的小时),mm表示分钟,ss表示秒,SSS表示毫秒 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(date); } }