|
@@ -1,9 +1,20 @@
|
|
|
package cn.cslg.pas.service.query;
|
|
|
|
|
|
+import cn.cslg.pas.common.model.cronModel.OrderConfig;
|
|
|
+import cn.cslg.pas.common.model.cronModel.SqlObject;
|
|
|
+import cn.cslg.pas.common.model.request.OrderDTO;
|
|
|
+import cn.cslg.pas.common.utils.StringUtils;
|
|
|
import cn.cslg.pas.common.utils.parseQueryToTree.*;
|
|
|
+import cn.cslg.pas.factorys.getOrderFactory.GetOrderFactory;
|
|
|
+import cn.cslg.pas.factorys.getOrderFactory.GetOrderObject;
|
|
|
+import cn.cslg.pas.factorys.getSqlFactorys.GetSqlFactory;
|
|
|
+import cn.cslg.pas.factorys.getSqlFactorys.GetSqlObject;
|
|
|
+import cn.cslg.pas.service.business.CommonService;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.google.gson.Gson;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
@@ -17,115 +28,101 @@ import java.util.*;
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
public class FormatQueryService {
|
|
|
+ @Autowired
|
|
|
+ private GetSqlFactory getSqlFactory;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private GetOrderFactory getOrderFactory;
|
|
|
public String getText(String text) throws Exception {
|
|
|
treeNode tree = expressManager.getInstance().Parse(text, false);
|
|
|
- String a = this.get(tree);
|
|
|
System.out.print(this.ToString((operateNode) tree));
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- public String get(treeNode node) {
|
|
|
- String a = "(";
|
|
|
- treeNode leftNode = node.getLeft();
|
|
|
- treeNode rightNode = node.getRight();
|
|
|
- if (leftNode instanceof valueNode) {
|
|
|
- a += ((valueNode) leftNode).getvalue() +" "+ ((operateNode)node).getoperate().getShowName()+" ";
|
|
|
- } else {
|
|
|
- a += this.get(leftNode)+" "+ ((operateNode)node).getoperate().getShowName()+" ";
|
|
|
- }
|
|
|
-
|
|
|
- if (rightNode instanceof valueNode) {
|
|
|
- a += ((valueNode) rightNode).ToString();
|
|
|
- } else {
|
|
|
- a += this.get(rightNode);
|
|
|
- }
|
|
|
- a+=")";
|
|
|
- return a;
|
|
|
- }
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 二叉树转检索式
|
|
|
+ * @param node
|
|
|
+ * @return
|
|
|
+ */
|
|
|
public String ToString(operateNode node){
|
|
|
operate operate1 =node.getoperate();
|
|
|
treeNode Left =node.getLeft();
|
|
|
treeNode Right =node.getRight();
|
|
|
String strCode = "";
|
|
|
+ String optionName="";
|
|
|
if ((operate1.getShowName()!=null))
|
|
|
{
|
|
|
strCode = operate1.getShowName();
|
|
|
+ optionName=operate1.getShowName();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
strCode = operate1.getCode();
|
|
|
+ optionName=operate1.getCode();
|
|
|
}
|
|
|
-
|
|
|
+ GetSqlObject getSqlObject =null;
|
|
|
if(Left != null)
|
|
|
{
|
|
|
- if((operate1.gettype() == enuType.Logic || operate1.gettype() == enuType.Assignment ) && (Left.getLeft() != null || Left.getRight() != null))
|
|
|
+ if((operate1.gettype() == enuType.Logic || operate1.gettype() == enuType.Assignment ) && (Left instanceof operateNode))
|
|
|
{
|
|
|
strCode = "(" + this.ToString((operateNode) Left) +") " + strCode;
|
|
|
}
|
|
|
else
|
|
|
- {
|
|
|
+ { //获得栏位名称
|
|
|
+ String field= Left.ToString();
|
|
|
strCode = Left.ToString() +" " + strCode;
|
|
|
//判断 参数类型
|
|
|
+ String json = CommonService.readJsonFile("event_sql.json");
|
|
|
+ List<SqlObject> sqlObjects = JSON.parseArray(json,SqlObject.class);
|
|
|
+ SqlObject sqlObject = sqlObjects.stream().filter(item->item.getField().equals(field)).findFirst().orElse(null);
|
|
|
+ if(sqlObject!=null){
|
|
|
+ getSqlObject= getSqlFactory.getClass(sqlObject.getSqlClass());
|
|
|
+ sqlObject.setOption(optionName);
|
|
|
+ getSqlObject.setSqlObject(sqlObject);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|
|
|
if(Right!= null)
|
|
|
- {
|
|
|
- if ((operate1.gettype() == enuType.Logic || operate1.gettype() == enuType.Assignment ) && (Right.getLeft() != null || Right.getRight() != null))
|
|
|
- {
|
|
|
- strCode = strCode +" (" + this.ToString((operateNode) Right)+ ") ";
|
|
|
+ { String rightValue="";
|
|
|
+ if ((operate1.gettype() == enuType.Logic || operate1.gettype() == enuType.Assignment ) && (Right instanceof operateNode))
|
|
|
+ { rightValue =this.ToString((operateNode) Right);
|
|
|
+ strCode = strCode +" (" + rightValue+ ") ";
|
|
|
}
|
|
|
else
|
|
|
- {
|
|
|
+ { rightValue =Right.ToString();
|
|
|
strCode = strCode + " " + Right.ToString();
|
|
|
}
|
|
|
-
|
|
|
+ if(getSqlObject!=null){
|
|
|
+ if(optionName.equals(" LIKE ")){
|
|
|
+ rightValue="'%"+rightValue+"%'";
|
|
|
+ }
|
|
|
+ strCode = getSqlObject.getSql(rightValue);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return strCode;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- public void write() {
|
|
|
- // 创建一个Map对象并添加一些数据
|
|
|
- Map<String, String> map = new HashMap<>();
|
|
|
- map.put("key1", "value1");
|
|
|
- map.put("key2", "value2");
|
|
|
- map.put("key3", "value3");
|
|
|
-
|
|
|
- // 将Map存储到文件中
|
|
|
- try {
|
|
|
- FileOutputStream fileOut = new FileOutputStream("map.ser");
|
|
|
- ObjectOutputStream out = new ObjectOutputStream(fileOut);
|
|
|
- out.writeObject(map);
|
|
|
- out.close();
|
|
|
- fileOut.close();
|
|
|
- System.out.println("Map已经成功存储到文件中");
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 排序转sql排序语句
|
|
|
+ * @param dtos
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String orderToString(List<OrderDTO> dtos){
|
|
|
+ //读取排序配置类
|
|
|
+ String json = CommonService.readJsonFile("event_order.json");
|
|
|
+ List<OrderConfig> configs = JSON.parseArray(json,OrderConfig.class);
|
|
|
+ String re ="order by ";
|
|
|
+ List<String> orders= new ArrayList<>();
|
|
|
+ dtos.forEach(item->{
|
|
|
+ OrderConfig orderConfig =configs.stream().filter(tem->tem.getField().equals(item.getOrderBy())).findFirst().orElse(null);
|
|
|
+ GetOrderObject getOrderObject = getOrderFactory.getClass(orderConfig.getOrderClass());
|
|
|
+ String order= getOrderObject.getOrderString(orderConfig.getSqlField(),item.getOrderType());
|
|
|
+ orders.add(order);
|
|
|
+ });
|
|
|
+ String orderStr = StringUtils.join(orders,",");
|
|
|
+ re += orderStr;
|
|
|
+return re;
|
|
|
}
|
|
|
-
|
|
|
- public static Map<String, Object> readJsonFile(String fileName) {
|
|
|
- Gson gson = new Gson();
|
|
|
- String json = "";
|
|
|
- try {
|
|
|
- File file = new File("target/file/test.json");
|
|
|
- Reader reader = new InputStreamReader(new FileInputStream(file), "utf-8");
|
|
|
- int ch = 0;
|
|
|
- StringBuffer buffer = new StringBuffer();
|
|
|
- while ((ch = reader.read()) != -1) {
|
|
|
- buffer.append((char) ch);
|
|
|
- }
|
|
|
- reader.close();
|
|
|
- json = buffer.toString();
|
|
|
- return gson.fromJson(json, Map.class);
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
}
|