解析计算公式,然后生成自己想要的格式

想要实现的效果

package ys.util.jsgs;
update YS_NDYSMX T
   SET F_DNYS =
   
   
   
       (SELECT T1.F_DNYS * T2.F_DNYS
          FROM (SELECT *
                  FROM YS_NDYSMX
                 WHERE F_YSQJ = '2016'
                   AND F_YSLXBH = 'Y09'
                   AND F_XMBH = 'A07002002001') T1,
               (SELECT *
                  FROM YS_NDYSMX
                 WHERE F_YSQJ = '2016'
                   AND F_YSLXBH = 'Y09'
                   AND F_XMBH = 'A07002002002') T2
         WHERE T.F_DWBH = T1.F_DWBH
           AND T.F_DWBH = T2.F_DWBH)
           
           
           
 WHERE T.F_YSQJ = '2016'
   AND T.F_YSLXBH = 'Y09'
   AND T.F_XMBH = 'A07002002'

package ys.util.jsgs;

public class Test {
	
	public static void main(String[] args) {
		//String str = "A01+A02+A03+A04+A05@1";
		String str = "(A09003-A08-A07)/A04@Y04";
		int index = 0 ;
		//String[] ystype = new String []{"F_NDYS","F_SNWC"} ;
		String ystype =  "F_SNWC";
		str = str.trim().replaceAll(" ", "");
		System.out.println(":"+str);
		String[] nameArray = str.split("\\*|\\+|\\-|\\/");
		String [] bh = new String[nameArray.length];
		char [] fh = new char[nameArray.length-1];
		for(int i = 0 ; i <nameArray.length;i++){
			if(index == 0 ){
				index = index + nameArray[i].length();
			}else{
				index = index + nameArray[i].length()+1;
			}
			bh[i] = nameArray[i];
		}
		index = 0;
		for(int i = 0 ; i <nameArray.length-1;i++){
			if(index == 0 ){
				index = index + nameArray[i].length();
			}else{
				index = index + nameArray[i].length()+1;
			}
			fh[i] = str.charAt(index);
		}
		String strr = "";
		for(int i = 0 ;i < bh.length;i++){
			if(i == bh.length-1){
				if(bh[i].indexOf(")")!=-1){
					if(bh[i].startsWith("A")){
						strr = strr + "NVL(T"+(i+1)+"."+ystype+",0) )";
					}else{
						strr = strr + bh[i];
					}
				}else{
					//判断是不是出发
					if(strr!="" &&strr.substring(strr.length()-1).equals("/")){
						if(bh[i].startsWith("A")){
							strr = strr + "NVL(T"+(i+1)+"."+ystype+",0) )";
						}else{
							strr = strr + bh[i] +")";
						}
					}else{
						if(bh[i].startsWith("A")){
							strr = strr + "NVL(T"+(i+1)+"."+ystype+" , 0) ";
						}else{
							strr = strr  + bh[i] ;
						}
					}
				} 
			}else{
				if(bh[i].indexOf("(")!=-1){
					if(bh[i].replace("(", "").startsWith("A")){
						strr = strr +"("  + "NVL(T"+(i+1)+"."+ystype+",0) " + fh[i] ;
					}else{
						strr = strr + bh[i] + fh[i] ;
					}
					
				}else if(bh[i].indexOf(")")>0){
					if(bh[i].startsWith("A")){
						strr = strr  + "NVL(T"+(i+1)+"."+ystype+",0) )" + fh[i] ;
					}else{
						strr = strr  + bh[i] + fh[i] ;
					}
				}else{
					if(strr!="" &&strr.substring(strr.length()-1).equals("/")){
						if(bh[i].startsWith("A")){
							strr = strr + "NVL(T"+(i+1)+"."+ystype+",0) )"+ fh[i];
						}else{
							strr = strr + bh[i]+")"+ fh[i];
						}
					}else  
					if(fh[i]=='/'){
						if(bh[i].startsWith("A")){
							if(bh[i+1].startsWith("A")){
								strr = strr  + " decode( NVL (T"+(i+2)+"."+ystype+" ,0),0,0, NVL (T"+(i+1)+"."+ystype+" ,0) " + fh[i] ;
							}else{
								strr = strr  + " decode("+bh[i+1]+" ,0,0, NVL(T"+(i+1)+"."+ystype+" ,0)"  + fh[i] ;
							}
						}else{
							if(bh[i+1].startsWith("A")){
								strr = strr  + " decode(NVL(T"+(i+2)+"."+ystype+" ,0),0,0, "+bh[i]+ fh[i] ;
							}else{
								strr = strr  + " decode("+bh[i+1]+" ,0,0, "+bh[i] + fh[i] ;
							}
						}
					}else{
						if(bh[i].startsWith("A")){
							strr = strr  + "NVL(T"+(i+1)+"."+ystype+" ,0)" + fh[i] ;
						}else{
							strr = strr  + bh[i] + fh[i] ;
						}
					}
				}
			}
		}
		String sql = "";
		String where = "";
		String yslx = "";
		String xmbh = "";
		String year= "2016";
		for(int i = 0 ;i < bh.length;i++){
			if(bh[i].replace("(","").replace(")", "").indexOf("@")!=-1){
				xmbh =  bh[i].replace("(","").replace(")", "").split("@")[0];
				yslx = bh[i].replace("(","").replace(")", "").split("@")[1];
			}else if(bh[i].indexOf("decode")!=-1){
				xmbh = bh[i].split(",")[bh[i].split(",").length-1];
			}else{
				xmbh = bh[i].replace("(","").replace(")", "");
			}
			if(i==bh.length-1){
				if(sql == "" ||sql ==null){
					if(bh[i].startsWith("A")){
						sql = sql +"( SELECT * FROM YS_NDYSMX  WHERE F_YSQJ = '"+year+"'  AND F_YSLXBH = '"+yslx+"' AND F_XMBH = '"+xmbh+"' ) " +"T"+(i+1);
						if("Y08".equals(yslx)){
							where = where + " T.F_DWBH = "+"T"+(i+1)+".F_DWBH AND T"+(i+1)+".F_JH = T"+(i+1)+".F_DWBH";
						}else{
							where = where + " T.F_DWBH = "+"T"+(i+1)+".F_DWBH";
						}
						
					}else{}
				}else if(",".equals(sql.substring(sql.length()-1, sql.length()))){
					if(bh[i].startsWith("A")){
						sql = sql +"( SELECT * FROM YS_NDYSMX  WHERE F_YSQJ = '"+year+"'  AND F_YSLXBH = '"+yslx+"' AND F_XMBH = '"+xmbh+"' ) " +"T"+(i+1);
						if("Y08".equals(yslx)){
							where = where + " T.F_DWBH = "+"T"+(i+1)+".F_DWBH AND T"+(i+1)+".F_JH = T"+(i+1)+".F_DWBH";
						}else{
							where = where + " T.F_DWBH = "+"T"+(i+1)+".F_DWBH";
						}
					}else{
						sql =  sql.substring(0, sql.length()-1);
						where = where.substring(0, where.length()-3);
					}
				}
			}else{
				if(bh[i].replace("(", "").startsWith("A")){
					sql = sql +"( SELECT * FROM YS_NDYSMX  WHERE F_YSQJ = '"+year+"'  AND F_YSLXBH = '"+yslx+"' AND F_XMBH = '"+xmbh+"' ) " +"T"+(i+1) +",";
					where = where + " T.F_DWBH = "+"T"+(i+1)+".F_DWBH AND" ;
				}
			}
		}
		System.out.println(strr);
		System.out.println(sql);
		System.out.println(where);
	}
}





暂时不能解析  连除   即使 (A01/A02/A03)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值