package com.product.data.sync.controller;
import com.alibaba.fastjson.JSONArray;
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.core.spring.context.SpringMVCContextHolder;
import com.product.data.sync.config.CmnConst;
import com.product.data.sync.config.SystemCode;
import com.product.data.sync.service.ConnectionConfigurationService;
import com.product.data.sync.service.ide.IConnectionConfigurationService;
import com.product.data.sync.util.ResultInfo;
import com.product.data.sync.util.ScheduledTaskExecution;
import com.product.module.sys.version.ApiVersion;
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 javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
/**
* Copyright LX-BASE
*
* @Title: ConnectionConfigurationController
* @Project: LX-BASE-SERVER
* @Date: 2021-08-10 11:30:48
* @Author: luoxin
* @Description: 鏁版嵁搴撹繛鎺ラ厤缃瓹onstoller
*
*/
@RequestMapping("/api/connectionConfiguration")
@RestController
public class ConnectionConfigurationController extends AbstractBaseController {
@Autowired
public ConnectionConfigurationService connectionConfigurationService;
@Autowired
public ScheduledTaskExecution scheduledTaskExecution;
/**
* 淇濆瓨鏁版嵁搴撹繛鎺ラ厤缃�
* @param request
* @return
*/
@RequestMapping(value = "/save-connectConfigure/{version}", method = RequestMethod.POST)
public String saveConnectionConfiguration(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) {
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());
}
//鍒ゆ柇琛ㄥ悕鏄惁姝e父
if (!CmnConst.PRODUCT_SYS_DATABASE_CONNECTION_CONFIG.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());
}
IConnectionConfigurationService service = (IConnectionConfigurationService) getProxyInstance(connectionConfigurationService);
String uuid = service.saveConnectionConfiguration(fse);
if (!BaseUtil.strIsNull(uuid)) {
return OK_Add(uuid);
}
SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_SAVE_CONNECT_CONFIGURE_FAIL.getValue(), SystemCode.SYSTEM_SAVE_CONNECT_CONFIGURE_FAIL.getText());
return error(SystemCode.SYSTEM_SAVE_CONNECT_CONFIGURE_FAIL.getValue(), SystemCode.SYSTEM_SAVE_CONNECT_CONFIGURE_FAIL.getText());
}catch(BaseException e) {
SpringMVCContextHolder.getSystemLogger().error(e);
return this.error(e);
}catch (Exception e) {
SpringMVCContextHolder.getSystemLogger().error(e);
return this.error(SystemCode.SYSTEM_SAVE_CONNECT_CONFIGURE_FAIL.getValue(), SystemCode.SYSTEM_SAVE_CONNECT_CONFIGURE_FAIL.getText());
}
}
/**
* 鍒犻櫎鏁版嵁搴撹繛鎺ラ厤缃�
* @param request
* @return
*/
@RequestMapping(value = "/delete-connectConfigure/{version}", method = RequestMethod.POST)
@ApiVersion(1)
public String delConnectionConfiguration(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) {
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());
}
//鍒ゆ柇琛ㄥ悕鏄惁姝e父
if (!CmnConst.PRODUCT_SYS_DATABASE_CONNECTION_CONFIG.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());
}
//鍒ゆ柇uuid鏄惁涓虹┖
if (StringUtils.isEmpty(fse.getString("uuid"))) {
SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_COUNT.getValue(), SystemCode.SYSTEM_FORM_COUNT.getText());
return this.error(SystemCode.SYSTEM_FORM_COUNT.getValue(), SystemCode.SYSTEM_FORM_COUNT.getText());
}
IConnectionConfigurationService service = (IConnectionConfigurationService) getProxyInstance(connectionConfigurationService);
boolean success=service.delConnectionConfiguration(fse);
if(success) {
return OK();
}
SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_DELETE_CONNECT_CONFIGURE_FAIL.getValue(),SystemCode.SYSTEM_DELETE_CONNECT_CONFIGURE_FAIL.getText());
return error(SystemCode.SYSTEM_DELETE_CONNECT_CONFIGURE_FAIL.getValue(),SystemCode.SYSTEM_DELETE_CONNECT_CONFIGURE_FAIL.getText());
}catch(BaseException e) {
SpringMVCContextHolder.getSystemLogger().error(e);
return this.error(e);
}catch (Exception e) {
SpringMVCContextHolder.getSystemLogger().error(e);
return this.error(SystemCode.SYSTEM_DELETE_CONNECT_CONFIGURE_FAIL.getValue(),SystemCode.SYSTEM_DELETE_CONNECT_CONFIGURE_FAIL.getText());
}
}
/**
* 鑾峰彇鏁版嵁搴撹繛鎺ラ厤缃垪琛�
* @param request
* @return
*/
@RequestMapping(value = "/list-connectConfigure/{version}", method = RequestMethod.POST)
public String listConnectionConfiguration(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) {
SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_TABLE_NODATA.getValue(),
SystemCode.SYSTEM_TABLE_NODATA.getText());
return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
}
// 鍒ゆ柇琛ㄥ悕鏄惁姝e父
if (!CmnConst.PRODUCT_SYS_DATABASE_CONNECTION_CONFIG.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());
}
// 鍒ゆ柇鍒嗛〉鍙傛暟
if (fse.getString(CmnConst.PAGESIZE) == null || fse.getString(CmnConst.CPAGE) == null) {
return this.error(SystemCode.SYSTEM_CPAGES_NOT_NULL.getValue(),
SystemCode.SYSTEM_CPAGES_NOT_NULL.getText());
}
DataTableEntity dt = connectionConfigurationService.listConnectionConfiguration(fse);
return OK_List(dt);
} catch (BaseException e) {
e.printStackTrace();
SpringMVCContextHolder.getSystemLogger().error(e);
return this.error(e);
} catch (Exception e) {
e.printStackTrace();
SpringMVCContextHolder.getSystemLogger().error(e);
return this.error(SystemCode.SYSTEM_LIST_CONNECT_CONFIGURE_FAIL.getValue(),
SystemCode.SYSTEM_LIST_CONNECT_CONFIGURE_FAIL.getText());
}
}
/**
* 鏌ヨ鏁版嵁搴撹繛鎺ラ厤缃鎯�
* @param request
* @return
*/
@RequestMapping(value = "/get-connectConfigure/{version}", method = RequestMethod.POST)
@ApiVersion(1)
public String getConnectionConfiguration(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) {
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());
}
// 鍒ゆ柇琛ㄥ悕鏄惁姝e父
if (!CmnConst.PRODUCT_SYS_DATABASE_CONNECTION_CONFIG.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());
}
// 鍒ゆ柇uuid鏄惁涓虹┖
if (StringUtils.isEmpty(fse.getString("uuid"))) {
SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_TABLE_NODATA.getValue(),
SystemCode.SYSTEM_TABLE_NODATA.getText());
return this.error(SystemCode.SYSTEM_FORM_COUNT.getValue(), SystemCode.SYSTEM_FORM_COUNT.getText());
}
FieldSetEntity fs = connectionConfigurationService.getConnectionConfiguration(fse);
return OK_List(fs);
} catch (BaseException e) {
e.printStackTrace();
SpringMVCContextHolder.getSystemLogger().error(e);
return this.error(e);
} catch (Exception e) {
e.printStackTrace();
SpringMVCContextHolder.getSystemLogger().error(e);
return this.error(SystemCode.SYSTEM_GET_CONNECT_CONFIGURE_FAIL.getValue(),
SystemCode.SYSTEM_GET_CONNECT_CONFIGURE_FAIL.getText());
}
}
/**
* 杩愯鏁版嵁搴撹繛鎺ラ厤缃�
* @param request
* @return
*/
@RequestMapping(value = "/run-connectConfigure/{version}", method = RequestMethod.POST)
@ApiVersion(1)
public String runConnectionConfiguration(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) {
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());
}
// 鍒ゆ柇琛ㄥ悕鏄惁姝e父
if (!CmnConst.PRODUCT_SYS_DATABASE_CONNECTION_CONFIG.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());
}
Connection conn = connectionConfigurationService.runConnectionConfiguration(fse);
if(conn != null){
conn.close();
return OK();
}
return this.error(SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getValue(),
SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getText());
} catch (BaseException e) {
e.printStackTrace();
SpringMVCContextHolder.getSystemLogger().error(e);
return this.error(e);
} catch (Exception e) {
e.printStackTrace();
SpringMVCContextHolder.getSystemLogger().error(e);
return this.error(SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getValue(),
SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getText());
}
}
/**
* 鑾峰彇骞朵繚瀛樺瓧娈�
* @param request
* @return
*/
@RequestMapping(value = "/saveField/{version}", method = RequestMethod.POST)
@ApiVersion(1)
public String saveField(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) {
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());
}
// 鍒ゆ柇琛ㄥ悕鏄惁姝e父
if (!CmnConst.PRODUCT_SYS_DATABASE_CONNECTION_CONFIG.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());
}
if(connectionConfigurationService.saveField(fse)){
return OK();
}
return this.error(SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getValue(),
SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getText());
} catch (BaseException e) {
e.printStackTrace();
SpringMVCContextHolder.getSystemLogger().error(e);
return this.error(e);
} catch (Exception e) {
e.printStackTrace();
SpringMVCContextHolder.getSystemLogger().error(e);
return this.error(SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getValue(),
SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getText());
}
}
/**
* 鑾峰彇鏁版嵁鍚屾閰嶇疆鏍�
* @param request
* @return
*/
@RequestMapping(value = "/tree/{version}", method = RequestMethod.POST)
@ApiVersion(1)
public String getTree(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) {
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());
}
// 鍒ゆ柇琛ㄥ悕鏄惁姝e父
if (!CmnConst.PRODUCT_SYS_DATABASE_CONNECTION_CONFIG.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());
}
JSONArray array = connectionConfigurationService.getSyncTree();
return ResultInfo.success(array);
} catch (BaseException e) {
e.printStackTrace();
SpringMVCContextHolder.getSystemLogger().error(e);
return this.error(e);
} catch (Exception e) {
e.printStackTrace();
SpringMVCContextHolder.getSystemLogger().error(e);
return this.error(SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getValue(),
SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getText());
}
}
}