haungxing
7 months ago
10 changed files with 199 additions and 16 deletions
@ -0,0 +1,27 @@
|
||||
package com.hnac.hzims.bigmodel.interactive.constants; |
||||
|
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Getter; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Optional; |
||||
|
||||
/** |
||||
* @Author: huangxing |
||||
* @Date: 2024/04/30 11:41 |
||||
*/ |
||||
@AllArgsConstructor |
||||
public enum FuncRouteEnum { |
||||
OPEN_SCADA("open_scada","打开实时画面"), |
||||
; |
||||
@Getter |
||||
private String funcCode; |
||||
@Getter |
||||
private String funcName; |
||||
|
||||
public static FuncRouteEnum getEnumByFuncCode(String funcCode) { |
||||
Optional<FuncRouteEnum> FuncRoute = Arrays.stream(FuncRouteEnum.class.getEnumConstants()).filter(e -> funcCode.equals(e.getFuncCode())).findFirst(); |
||||
return FuncRoute.get(); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,134 @@
|
||||
package com.hnac.hzims.bigmodel.interactive.service.impl; |
||||
|
||||
import com.alibaba.fastjson.JSON; |
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.hnac.hzims.bigmodel.entity.FunctionEntity; |
||||
import com.hnac.hzims.bigmodel.interactive.constants.FuncRouteEnum; |
||||
import com.hnac.hzims.bigmodel.interactive.constants.FunctionConstants; |
||||
import com.hnac.hzims.bigmodel.interactive.vo.ExtraVO; |
||||
import org.springblade.core.log.exception.ServiceException; |
||||
import org.springblade.core.tool.utils.Func; |
||||
import org.springblade.core.tool.utils.StringUtil; |
||||
import org.springframework.stereotype.Component; |
||||
import org.springframework.util.Assert; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
|
||||
/** |
||||
* @Author: huangxing |
||||
* @Date: 2024/04/30 11:38 |
||||
*/ |
||||
@Component |
||||
public class JumpRouteJoinStrategy { |
||||
|
||||
/** |
||||
* 解析大模型传参 |
||||
* @param args 大模型传参 |
||||
* @return 跳转path所需要的参数 |
||||
*/ |
||||
public ExtraVO resolve(FunctionEntity function,Map<String,String> args) { |
||||
FuncRouteEnum routeEnum = FuncRouteEnum.getEnumByFuncCode(function.getCode()); |
||||
if(Func.isNotEmpty(routeEnum)) { |
||||
switch(routeEnum) { |
||||
case OPEN_SCADA: |
||||
return this.getScadaExtra(args,function); |
||||
default: |
||||
break; |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
private ExtraVO getScadaExtra(Map<String,String> args,FunctionEntity function) { |
||||
// 跳转页面逻辑
|
||||
ExtraVO extraVO = new ExtraVO(); |
||||
Map<String, String> params = this.scadaResolve(args); |
||||
// 根据hz3000画面版本获取path
|
||||
Integer picResource = Integer.valueOf(params.get("picResource")); |
||||
JSONObject pathObject = JSONObject.parseObject(function.getPath()); |
||||
// 云组态
|
||||
if(picResource == 0) { |
||||
String scada = pathObject.getString("scada"); |
||||
extraVO.setRoute(this.replacePath(scada, params)); |
||||
extraVO.setRoute(scada); |
||||
} |
||||
// v3.0
|
||||
else if (picResource == 1) { |
||||
String v3 = pathObject.getString("v3"); |
||||
extraVO.setRoute(this.replacePath(v3, params)); |
||||
} |
||||
// v4.0
|
||||
else if (picResource == 2) { |
||||
String v4 = pathObject.getString("v4"); |
||||
// pic_name 去掉头部的/ 以及尾部的.js
|
||||
String context = params.get("context"); |
||||
context = this.removeHeadChars(context,"/"); |
||||
context = this.removeTailChars(context,".js"); |
||||
params.put("context",context); |
||||
extraVO.setRoute(this.replacePath(v4, params)); |
||||
} |
||||
else { |
||||
throw new ServiceException("解析出来的画面类型在云组态、v3、v4类型之外,无法解析路由"); |
||||
} |
||||
extraVO.setType(FunctionConstants.TypeEnum.JUMP.getType()); |
||||
return extraVO; |
||||
} |
||||
|
||||
/** |
||||
* 打开实时画面 |
||||
* @param args 大模型解析参数 |
||||
* @return 实时画面路径拼接所需参数 |
||||
*/ |
||||
private Map<String,String> scadaResolve(Map<String,String> args) { |
||||
Map<String,String> result = new HashMap<>(); |
||||
Assert.isTrue(args.containsKey("params"), () -> { |
||||
throw new ServiceException("大模型传参缺少params参数"); |
||||
}); |
||||
// 参数格式为:picResource^context^stationNum^projectId^taskId^name^id
|
||||
List<String> params = Func.toStrList("\\^", args.get("params")); |
||||
Assert.isTrue(params.size() == 7,() -> { |
||||
throw new ServiceException("大模型传参params长度错误,传参为:" + args.get("params")); |
||||
}); |
||||
result.put("picResource",params.get(0)); |
||||
result.put("context",params.get(1)); |
||||
result.put("stationNum",params.get(2)); |
||||
result.put("projectId",params.get(3)); |
||||
result.put("taskId",params.get(4)); |
||||
result.put("name",params.get(5)); |
||||
result.put("id",params.get(6)); |
||||
return result; |
||||
} |
||||
|
||||
private String replacePath(String path,Map<String,String> params) { |
||||
Set<Map.Entry<String, String>> entries = params.entrySet(); |
||||
// 替换path中变量
|
||||
for (Map.Entry<String, String> entry : entries) { |
||||
String replaceVariables = "{" + entry.getKey() + "}"; |
||||
path = StringUtil.replace(path,replaceVariables,entry.getValue()); |
||||
} |
||||
return path; |
||||
} |
||||
|
||||
public String removeHeadChars(String str, String charsToRemove) { |
||||
if (str == null || charsToRemove == null || charsToRemove.isEmpty()) { |
||||
return str; |
||||
} |
||||
while (str.startsWith(charsToRemove)) { |
||||
str = str.substring(charsToRemove.length()); |
||||
} |
||||
return str; |
||||
} |
||||
|
||||
public String removeTailChars(String str, String charsToRemove) { |
||||
if (str == null || charsToRemove == null || charsToRemove.isEmpty()) { |
||||
return str; |
||||
} |
||||
while (str.endsWith(charsToRemove)) { |
||||
str = str.substring(0, str.length() - charsToRemove.length()); |
||||
} |
||||
return str; |
||||
} |
||||
} |
Loading…
Reference in new issue