|
@@ -4,10 +4,11 @@ import com.alibaba.fastjson.JSONObject;
|
|
|
import com.judong.chuanyiserver.dao.DataSourceDao;
|
|
|
import com.judong.chuanyiserver.entity.DataSource;
|
|
|
import com.judong.chuanyiserver.entity.DataSourceType;
|
|
|
+import com.judong.chuanyiserver.enums.DataSourceTypeEnum;
|
|
|
+import com.judong.chuanyiserver.enums.ResultEnum;
|
|
|
+import com.judong.chuanyiserver.exception.CustomException;
|
|
|
import com.judong.chuanyiserver.service.DataSourceService;
|
|
|
-import com.judong.chuanyiserver.util.Blank;
|
|
|
-import com.judong.chuanyiserver.util.ConstantStr;
|
|
|
-import com.judong.chuanyiserver.util.Result;
|
|
|
+import com.judong.chuanyiserver.util.*;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
@@ -23,6 +24,9 @@ public class DataSourceServiceImpl implements DataSourceService {
|
|
|
@Resource
|
|
|
private DataSourceDao dataSourceDao;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private UserUtil userUtil;
|
|
|
+
|
|
|
@Override
|
|
|
public Result getDataSourceTree() {
|
|
|
List<DataSourceType> dataSourceTypeList = dataSourceDao.getAllDataSourceType();
|
|
@@ -33,8 +37,76 @@ public class DataSourceServiceImpl implements DataSourceService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Result addDataSource(DataSource dataSource) {
|
|
|
- return null;
|
|
|
+ public synchronized Result addDataSource(DataSource dataSource) {
|
|
|
+ dataSource = OpcUtil.convertPassword(dataSource);
|
|
|
+ String userId = userUtil.getCurrentUserId();
|
|
|
+ dataSource.setUserId(userId);
|
|
|
+ if (Blank.isEmpty(dataSource.getId())) {
|
|
|
+ DataSource isExistDataSource = dataSourceDao.getDataSourceByName(userId, dataSource.getDataSourceName());
|
|
|
+ if (Blank.isNotEmpty(isExistDataSource)) {
|
|
|
+ return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "已经存在此名称的数据源配置了,请更改名称");
|
|
|
+ }
|
|
|
+ if (dataSourceDao.addDataSource(dataSource) <= 0) {
|
|
|
+ return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "添加数据源配置失败");
|
|
|
+ }
|
|
|
+ return Result.ok("添加数据源配置成功");
|
|
|
+ } else {
|
|
|
+ DataSource isExistDataSource = dataSourceDao.getDataSourceByNameNoId(dataSource.getId(), userId, dataSource.getDataSourceName());
|
|
|
+ if (Blank.isNotEmpty(isExistDataSource)) {
|
|
|
+ return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "已经存在此名称的数据源配置了,请更改名称");
|
|
|
+ }
|
|
|
+ if (dataSourceDao.updateDataSource(dataSource) <= 0) {
|
|
|
+ return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "修改数据源配置失败");
|
|
|
+ }
|
|
|
+ return Result.ok("修改数据源配置成功");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result testConnect(DataSource dataSource) {
|
|
|
+ DataSourceType dataSourceType = dataSourceDao.getDataSourceTypeById(dataSource.getTypeId());
|
|
|
+ dataSource = OpcUtil.convertPassword(dataSource);
|
|
|
+ if (dataSourceType.getDataSourceTypeKey() == DataSourceTypeEnum.OPC_UA_REAL.getValue() ||
|
|
|
+ dataSourceType.getDataSourceTypeKey() == DataSourceTypeEnum.OPC_UA_HISTORY.getValue()) {
|
|
|
+ if (Blank.isEmpty(dataSource.getIpAddress(), dataSource.getIpPort(), dataSource.getIsAnonymous())) {
|
|
|
+ return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "数据源ip,端口号,匿名方式都不能为空");
|
|
|
+ }
|
|
|
+ if (dataSource.getIsAnonymous() == ConstantStr.NOT_ANONYMOUS) {
|
|
|
+ if (Blank.isEmpty(dataSource.getIpUserName(), dataSource.getIpPassword())) {
|
|
|
+ return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "选择不匿名方式,需要填写用户名和密码");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return OpcUaUtil.opcUaTestConnect(dataSource);
|
|
|
+ } else if (dataSourceType.getDataSourceTypeKey() == DataSourceTypeEnum.OPC_DA_REAL.getValue() ||
|
|
|
+ dataSourceType.getDataSourceTypeKey() == DataSourceTypeEnum.OPC_HDA_HISTORY.getValue() ||
|
|
|
+ dataSourceType.getDataSourceTypeKey() == DataSourceTypeEnum.OPC_AE_ALARMS.getValue()
|
|
|
+ ) {
|
|
|
+ if (Blank.isEmpty(dataSource.getIpAddress(), dataSource.getIpUserName(), dataSource.getIpPassword())) {
|
|
|
+ return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "数据源ip,用户名,密码都不能为空");
|
|
|
+ }
|
|
|
+ return OpcDaUtil.opcDaTestConnect(dataSource);
|
|
|
+ } else {
|
|
|
+ throw new CustomException(ResultEnum.SERVER_ERROR.getRespCode(), "目前还没有此种类型的连接方式");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result getAllDataSource() {
|
|
|
+ String userId = userUtil.getCurrentUserId();
|
|
|
+ return Result.ok(dataSourceDao.getAllDataSource(userId));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result getDataSourceById(Integer id) {
|
|
|
+ return Result.ok(dataSourceDao.getDataSourceById(id));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result delDataSourceById(Integer id) {
|
|
|
+ if (dataSourceDao.delDataSourceById(id) <= 0) {
|
|
|
+ return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "删除数据源配置失败");
|
|
|
+ }
|
|
|
+ return Result.ok("删除数据源配置成功");
|
|
|
}
|
|
|
|
|
|
public List<JSONObject> genDataSourceTree(List<DataSourceType> dataSourceTypeList, Integer parentId) {
|