JRT业务层共用虚拟支持

之前虚拟M只是给打印和导出调用实现打印和导出。对导出而言不可能为导出查数据再拷贝一份查询逻辑,所以查询业务要能共用虚拟M的逻辑。

提供虚拟M的调用
JRT业务层共用虚拟支持_第1张图片
代码

package JRTBLLBase;

import JRT.Core.Dto.OutParam;
import JRT.Core.Dto.OutValue;
import JRT.Core.Util.JsonUtil;
import JRT.Model.Bussiness.Parameters;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.net.ssl.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.cert.X509Certificate;
import java.util.HashMap;

/**
 * 调用虚拟M得到数据
 */
public class VMGetData {

    /**
     * 调用本地的虚拟M得到数据
     *
     * @param ClassName 类名 vm.his.printbarcode
     * @param FuncName  方法名 GetData
     * @param Param     P0-P14的参数json串
     * @param Session   会话
     * @param Output    带出参数
     * @return
     */
    public static String GetVMData(String ClassName, String FuncName, Parameters Param, OutValue Session, OutValue Output) throws Exception {
        try {
            //转换M的类名为java的类名称
            ClassName = ClassName.replace(".", "/");
            //反射得到类型
            Object objDeal = JRTBLLBase.GetBllObjService.GetObjectByConfString(ClassName, null, "", "");
            //没有实现类
            if (objDeal == null) {
                String errStr = "类" + ClassName + "不存在";
                throw new Exception(errStr);
            }
            //获得类型
            Class type = objDeal.getClass();
            //得到方法
            Method method = GetMethod(type, FuncName);
            //没有实现方法
            if (method == null) {
                throw new Exception(ClassName + "里面不存在:" + FuncName + "方法");
            }
            //执行返回数据
            Object retObj = method.invoke(objDeal, Param.P0, Param.P1, Param.P2, Param.P3, Param.P4, Param.P5, Param.P6, Param.P7, Param.P8, Param.P9, Param.P10, Param.P11, Param.P12, Param.P13, Session, Output);
            if (retObj != null) {
                return retObj.toString();
            }
            return "";
        } catch (Exception ex) {
            throw new Exception("调用:" + ClassName + ",方法:" + FuncName + ",参数:" + Param + ",会话:" + Session + "异常:" + ex.getMessage());
        }
    }


    /**
     * 按指定地址调用虚拟M方法得到数据
     *
     * @param Address   地址
     * @param ClassName 类名
     * @param FuncName  方法名
     * @param Param     参数
     * @param Session   会话
     * @return
     * @throws Exception
     */
    public static String GetVMData(String Address, String ClassName, String FuncName, Parameters Param, OutValue Session, OutValue Output) throws Exception {
        String result = "";
        String SessionStr = "";
        if (Session != null) {
            SessionStr = Session.GetString();
        }
        DealNullPara(Param);
        String paraStr = "Method=GetData&ClassName=" + ClassName + "&FuncName=" + FuncName + "&Param=" + URLEncoder.encode(JsonUtil.Object2Json(Param), "UTF-8") + "&Session=" + URLEncoder.encode(SessionStr, "UTF-8");

        try {
            //返回接受的数据
            result = GetHttpStr(Address, paraStr);
            //调用报错了
            if (!result.contains("")) {
                result = "-1" + result + "0";
            }
        } catch (Exception ex) {
            result += ",异常信息:" + ex.getMessage() + ",调用:" + ClassName + "," + FuncName + "," + JsonUtil.Object2Json(Param) + "," + SessionStr;
        }
        return DealXmlToJson(result, Session,Output);
    }

    /**
     * 通过类型和名称得到方法
     *
     * @param c
     * @param name
     * @return
     */
    private static Method GetMethod(Class c, String name) {
        Method[] methods = c.getMethods();
        for (Method method : methods) {
            if (method.getName().intern().equals(name)) {
                return method;
            }
        }
        return null;
    }

    /**
     * 把xml处理成json串
     * @param xmlStr xml串
     * @param Session 会话
     * @param Output 带出参数
     * @return
     * @throws Exception
     */
    private static String DealXmlToJson(String xmlStr, OutValue Session,OutValue Output) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new InputSource(new StringReader(xmlStr)));
        // 获得根节点
        Element rootElement = document.getDocumentElement();
        // 获得根节点下的所有子节点
        NodeList childs = rootElement.getChildNodes();
        HashMap dataMap = new HashMap();
        String ResultName = "";
        for (int i = 0; i < childs.getLength(); i++) {
            // 由于节点多种类型,而一般我们需要处理的是元素节点
            Node childNode = childs.item(i);
            // 元素节点就是非空的子节点,也就是还有孩子的子节点
            if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                Element childElement = (Element) childNode;
                dataMap.put(childElement.getNodeName(), childElement.getTextContent());
                //不是对象配置元素就忽略
                if (childElement.getNodeName().equals("Node")) {
                    ResultName = childElement.getTextContent() + "Result";
                }
            }
        }
        String Error = dataMap.get("Error").toString();
        if (!Error.isEmpty()) {
            System.out.println("异常:" + Error);
            throw new Exception(Error);
        }
        String ResType = dataMap.get("ResType").toString();
        if (Session != null) {
            Session.Value = dataMap.get("RetSession").toString();
        }
        if(Output!=null)
        {
            Output.Value = dataMap.get("RowCount").toString();
        }
        return dataMap.get(ResultName).toString();
    }


    /**
     * 处理空参数
     *
     * @param Param
     */
    private static void DealNullPara(Parameters Param) {
        if (Param.P0 == null) {
            Param.P0 = "";
        }
        if (Param.P1 == null) {
            Param.P1 = "";
        }
        if (Param.P2 == null) {
            Param.P2 = "";
        }
        if (Param.P3 == null) {
            Param.P3 = "";
        }
        if (Param.P4 == null) {
            Param.P4 = "";
        }
        if (Param.P5 == null) {
            Param.P5 = "";
        }
        if (Param.P6 == null) {
            Param.P6 = "";
        }
        if (Param.P7 == null) {
            Param.P7 = "";
        }
        if (Param.P8 == null) {
            Param.P8 = "";
        }
        if (Param.P9 == null) {
            Param.P9 = "";
        }
        if (Param.P10 == null) {
            Param.P10 = "";
        }
        if (Param.P11 == null) {
            Param.P11 = "";
        }
        if (Param.P12 == null) {
            Param.P12 = "";
        }
        if (Param.P13 == null) {
            Param.P13 = "";
        }
        if (Param.P14 == null) {
            Param.P14 = "";
        }
    }

    /**
     * 从http下载文本
     *
     * @param url  url
     * @param para 参数
     * @return 文本串
     * @throws Exception
     */
    private static String GetHttpStr(String url, String para) throws Exception {
        byte[] bytes = para.getBytes("UTF-8");

        //忽略证书
        if (url.contains("https://")) {
            TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }

                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            }
            };

            // Install the all-trusting trust manager
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

            // Create all-trusting host name verifier
            HostnameVerifier allHostsValid = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };
        }

        URL u = new URL(url);
        HttpURLConnection http = (HttpURLConnection) u.openConnection();
        http.setAllowUserInteraction(true);
        http.setDoOutput(Boolean.TRUE);
        http.setDoInput(Boolean.TRUE);
        http.setUseCaches(false);
        http.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
        http.setInstanceFollowRedirects(false);
        http.setRequestMethod("POST");
        http.connect();

        OutputStream outputStream = http.getOutputStream();
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();

        InputStream is = http.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder stringBuilder = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line + System.lineSeparator());
        }
        return stringBuilder.toString();
    }
}

加入Helper
JRT业务层共用虚拟支持_第2张图片
项目界面查询后台调虚拟M查询
JRT业务层共用虚拟支持_第3张图片

导出逻辑的虚拟M调整

import JRT.Core.Dto.OutParam;
import JRT.Core.Dto.OutValue;
import JRT.Core.Dto.ParamDto;
import JRT.Core.MultiPlatform.JRTContext;
import JRT.Model.Entity.BTTestCode;
import JRTBLLBase.BaseHttpHandlerNoSession;
import JRTBLLBase.Helper;

import java.util.ArrayList;
import java.util.List;

/**
 * 输出符合Query约定的数据供导出Excel用,所有的虚拟M方法参数约定就是这个样子
 */
public class ExportExcelTest extends BaseHttpHandlerNoSession {
    /**
     * 查询所有项目数据导出到Excel
     * @param Filter 筛选条件
     * @param DisplayCount 显示行数
     * @param P2
     * @param P3
     * @param P4
     * @param P5
     * @param P6
     * @param P7
     * @param P8
     * @param P9
     * @param P10
     * @param P11
     * @param P12
     * @param P13
     * @param Session
     * @param Output
     * @return
     */
    public String QryTestCode(String Filter, String DisplayCount, String P2, String P3, String P4, String P5, String P6, String P7, String P8, String P9, String P10, String P11, String P12, String P13, OutValue Session, OutValue Output) throws Exception{
        BTTestCode dto=new BTTestCode();
        //返回的参数,供Excel模板使用
        Session.Value="项目数据导出^"+JRT.Core.Util.TimeParser.GetNowDate()+"^张联珠";
        //参数
        List<ParamDto> para=new ArrayList<>();
        //sql连接符号
        List<String> joiner=new ArrayList<>();
        //sql比较符号
        List<String> operators=new ArrayList<>();
        boolean displayCount=false;
        if(DisplayCount.equals("1"))
        {
            displayCount=true;
        }
        //模糊查询
        if(!Filter.isEmpty())
        {
            ParamDto p=null;
            //代码
            p=new ParamDto();
            p.Key="Code";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //名称
            p=new ParamDto();
            p.Key="CName";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //全称
            p=new ParamDto();
            p.Key="LName";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //英文名称
            p=new ParamDto();
            p.Key="EName";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //缩写
            p=new ParamDto();
            p.Key="Synonym";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //单位
            p=new ParamDto();
            p.Key="Units";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //结果格式  {N:数字,A:血型ABO,R:血型RH,M:微生物,X:文本,S:列表}
            p=new ParamDto();
            p.Key="ResultFormat";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //LONIC码
            p=new ParamDto();
            p.Key="LonicCode";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //标准码
            p=new ParamDto();
            p.Key="SCode";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //SOP内容
            p=new ParamDto();
            p.Key="SOPContent";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //SOP文件
            p=new ParamDto();
            p.Key="SOPFile";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //HIS对照码
            p=new ParamDto();
            p.Key="HISCode";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //临床意义简述
            p=new ParamDto();
            p.Key="ClinicalSignifyS";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //临床意义描述
            p=new ParamDto();
            p.Key="ClinicalSignifyL";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //计算公式
            p=new ParamDto();
            p.Key="CalculateFormula";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //参考范围
            p=new ParamDto();
            p.Key="RefRanges";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //特殊报告用
            p=new ParamDto();
            p.Key="RefField";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //排除低值
            p=new ParamDto();
            p.Key="UnacceptLow";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //排除高值
            p=new ParamDto();
            p.Key="UnacceptHigh";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //结果互认标记(0:不互认,1:区域性互认,2:全市,3:全省,4:全国)
            p=new ParamDto();
            p.Key="ShareFlag";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //评估标准
            p=new ParamDto();
            p.Key="QCPatVisEval";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //默认结果
            p=new ParamDto();
            p.Key="DefaultResult";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //允许CV值来源
            p=new ParamDto();
            p.Key="CVSource";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //本室的σ值  1:σ<2,2:2≤σ<3,3:3≤σ<4,4:4≤σ<5,5:6≤σ<6,6:σ≥6
            p=new ParamDto();
            p.Key="SigmaValue";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
            //拆分医嘱名称
            p=new ParamDto();
            p.Key="SplitTSName";
            p.Value="%"+Filter+"%";
            para.add(p);
            joiner.add("or");
            operators.add("like");
        }
        //调用查询
        String json=EntityManager().QueryAllWithFK(dto,para,"",displayCount,-1,-1,"",joiner,operators);
        return json;
    }
}





这样就完成了虚拟M通用性支持,不光打印导出能用,业务脚本也能调用

你可能感兴趣的:(开发语言,java)