package com.prolog.cs.base.gsp;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SqlMain {
    private String preparing;//对应带?的jdbc sql语句
    private String parameters;//对应所有的参数字符串
    private Integer updates;//对应受影响的行数
    private List<String> params;//对应所有的参数集合
    private String resultSql;//对应输出的sql

    /**
     * 解析sql
     * @param content mybatis日志sql打印部分
     * @return 输出的sql
     */
    public String parseSql(String content){
        String preParingPn = "==>  Preparing: (.*?)\n";
        Pattern r = Pattern.compile(preParingPn);
        Matcher m = r.matcher(content);
        if(m.find()){
            preparing = m.group(1);
        }
        String parametersPn = "==> Parameters: (.*?)\n";
        r = Pattern.compile(parametersPn);
        m = r.matcher(content);
        if(m.find()){
            parameters = m.group(1);
        }
        String updatesPn = "<==    Updates: (\\d+)\n";
        r = Pattern.compile(updatesPn);
        m = r.matcher(content);
        if(m.find()){
            updates = Integer.valueOf(m.group(1));
        }
        parseParameters();
        return resultSql;
    }

    /**
     * 正则匹配所有的参数
     */
    private void parseParameters(){
        parameters = parameters+ ",";
        String pattern = "([^,]*?)\\((.*?)\\),|null,";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(parameters);
        params = new ArrayList<>();
        while(m.find()){
            String param = m.group(1);
            String type = m.group(2);
            if("String".equals(type)){
                params.add("\'"+param.trim()+"\'");
            }else if(param == null) {
                params.add("null");
            }else{
                params.add(param.trim());
            }
        }
        resultSql = preparing;
        for (String param : params) {
            resultSql = resultSql.replaceFirst("\\?", param);
        }
    }

    public static void main(String[] args) {
//        String content = "==>  Preparing: insert into DXI_GSP_MAINTENANCE_RECORD (ID,billid,billno,branchid,branchname,consignorid,consignorname,lineid,seqno,billdate,itemid,itemcode,itemname,lotno,locno,stockqty,itemtype,stockstate,status,sourcetype,upkeepoperator,processopinion,recheckoperator,recheckopinion,auditoperator,auditopinion,checkoperation,qualifiedqty,pendingqty,qualitystatus,thstatus,pendingreason,upkeepdate,recheckdate,remark,attr_str1,attr_str2,attr_num1,attr_num2,attr_dte1,attr_dte2,createby,createdate,lastmodifyby,lastmodifydate,p_enterpriseid,p_enterprisename) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)  \n" +
//                "==> Parameters: 609360218727518208(String), 609356179071700992(String), YHD71700992(String), 599581702716788736(String), null, E23A4BUUPPW(String), 曲靖市康桥医药有限责任公司(String), 609356179071700992(String), null, 2021-07-05 09:24:59.0(Timestamp), 0021162773(String), 101010200561(String), 复方蛤青胶囊(String), batch10002(String), null, 30080800(String), 1(String), 1(String), 1(String), WMS(String), null, null, null, null, null, null, null, 30080800(String), (String), 1(String), null, (String), 2021-07-05 09:24:59.0(Timestamp), null, null, null, null, null, null, null, null, kzywms-admin(String), 2021-07-05 09:41:03.151(Timestamp), 昆中药WMS管理员(String), 2021-07-05 09:40:55.118(Timestamp), null, null\n" +
//                "<==    Updates: 1";
//        SqlMain sqlLog = new SqlMain();
//        String resultSql = sqlLog.parseSql(content);
//        System.out.println(resultSql);
        //int pageNum=1419/50;
        int pageNum=100/50;
        System.out.println(pageNum);
        System.out.println(28*50);
        System.out.println(1419-1350);
    }

}

