package cn.sh.stc.sict.theme.hphy.wd;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer;
import com.thoughtworks.xstream.io.xml.XppDriver;
import lombok.experimental.UtilityClass;

@UtilityClass
public class WanDaUtil {

    String ACCESS_ACCOUNT = "HPDSF";
    String SIGN = "HPDSF";

    String XML_HEADER = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:qyws=\"http://www.wondersgroup.com/qyws\">" +
            "   <soapenv:Header/>" +
            "   <soapenv:Body>";

    String COMMON_ARG = "<MessageHeader>" +
            "   <accessAccount>" + ACCESS_ACCOUNT + "</accessAccount >" +
            "   <sign>" + SIGN + "</sign>" +
            "</MessageHeader>";

    String XML_FOOTER = "   </soapenv:Body>" +
            "</soapenv:Envelope>";

    /**
     * 获取医院信息 xml
     *
     * @param hosInfo
     * @return
     */
    public String GetHospitalInfoServiceXML(HosInfo hosInfo) {
        String body = "<qyws:GetHospitalInfoService>" +
                "         <!--Optional:-->" +
                "         <arg0>" +
                "           <![CDATA[" +
                "           <Request>" +
                COMMON_ARG + objToXml(hosInfo) +
                "           </Request>" +
                "           ]]>" +
                "         </arg0>" +
                "      </qyws:GetHospitalInfoService>";

        String xml = (XML_HEADER +
                body +
                XML_FOOTER);

        return xml;
    }

    /**
     * 获取一级科室信息 xml
     *
     * @param hosInfo
     * @return
     */
    public String GetDeptInfoTopServiceXML(HosInfo hosInfo) {
        String body = "<qyws:GetDeptInfoTopService>" +
                "         <!--Optional:-->" +
                "         <arg0>" +
                "           <![CDATA[" +
                "           <Request>" +
                COMMON_ARG + objToXml(hosInfo) +
                "           </Request>" +
                "           ]]>" +
                "         </arg0>" +
                "      </qyws:GetDeptInfoTopService>";

        String xml = (XML_HEADER +
                body +
                XML_FOOTER);

        return xml;
    }

    /**
     * 获取二级科室 xml
     *
     * @return
     */
    public String GetDeptInfoTwoServiceXML(DeptInfo deptInfo) {
        String body = "<qyws:GetDeptInfoTwoService>" +
                "         <!--Optional:-->" +
                "         <arg0>" +
                "           <![CDATA[" +
                "           <Request>" +
                COMMON_ARG + objToXml(deptInfo) +
                "           </Request>" +
                "           ]]>" +
                "         </arg0>" +
                "      </qyws:GetDeptInfoTwoService>";

        String xml = (XML_HEADER +
                body +
                XML_FOOTER);

        return xml;
    }

    public String GetDoctInfoServiceXML(DeptInfo deptInfo) {
        String body = "<qyws:GetDoctInfoService>" +
                "         <!--Optional:-->" +
                "         <arg0>" +
                "           <![CDATA[" +
                "           <Request>" +
                COMMON_ARG + objToXml(deptInfo) +
                "           </Request>" +
                "           ]]>" +
                "         </arg0>" +
                "      </qyws:GetDoctInfoService>";

        String xml = (XML_HEADER +
                body +
                XML_FOOTER);

        return xml;
    }

    public String GetOutPatInfoServiceXML(DeptInfo deptInfo) {
        String body = "<qyws:GetOutPatInfoService>" +
                "         <!--Optional:-->" +
                "         <arg0>" +
                "           <![CDATA[" +
                "           <Request>" +
                COMMON_ARG + objToXml(deptInfo) +
                "           </Request>" +
                "           ]]>" +
                "         </arg0>" +
                "      </qyws:GetOutPatInfoService>";

        String xml = (XML_HEADER +
                body +
                XML_FOOTER);

        return xml;
    }

    public String GetOrderNumInfoServiceXML(NumSourceInfo numSourceInfo) {
        String body = "<qyws:GetOrderNumInfoService>" +
                "         <!--Optional:-->" +
                "         <arg0>" +
                "           <![CDATA[" +
                "           <Request>" +
                COMMON_ARG + objToXml(numSourceInfo) +
                "           </Request>" +
                "           ]]>" +
                "         </arg0>" +
                "      </qyws:GetOrderNumInfoService>";

        String xml = (XML_HEADER +
                body +
                XML_FOOTER);

        return xml;
    }

    public String SubmitOrderByUserInfoServiceXML(OrderInfo orderInfo) {
        String body = "<qyws:SubmitOrderByUserInfoService>" +
                "         <!--Optional:-->" +
                "         <arg0>" +
                "           <![CDATA[" +
                "           <Request>" +
                COMMON_ARG + objToXml(orderInfo) +
                "           </Request>" +
                "           ]]>" +
                "         </arg0>" +
                "      </qyws:SubmitOrderByUserInfoService>";

        String xml = (XML_HEADER +
                body +
                XML_FOOTER);

        return xml;
    }

    public String OrderCancelInfoServiceXML(CancelOrderInfo orderInfo) {
        String body = "<qyws:OrderCancelInfoService>" +
                "         <!--Optional:-->" +
                "         <arg0>" +
                "           <![CDATA[" +
                "           <Request>" +
                COMMON_ARG + objToXml(orderInfo) +
                "           </Request>" +
                "           ]]>" +
                "         </arg0>" +
                "      </qyws:OrderCancelInfoService>";

        String xml = (XML_HEADER +
                body +
                XML_FOOTER);

        return xml;
    }

    public String GetOrderListServiceXML(OrderInfo orderInfo) {
        String body = "<qyws:GetOrderListService>" +
                "         <!--Optional:-->" +
                "         <arg0>" +
                "           <![CDATA[" +
                "           <Request>" +
                COMMON_ARG + objToXml(orderInfo) +
                "           </Request>" +
                "           ]]>" +
                "         </arg0>" +
                "      </qyws:GetOrderListService>";

        String xml = (XML_HEADER +
                body +
                XML_FOOTER);

        return xml;
    }

    public String GetOrderDetailInfoServiceXML(OrderInfo orderInfo) {
        String body = "<qyws:GetOrderDetailInfoService>" +
                "         <!--Optional:-->" +
                "         <arg0>" +
                "           <![CDATA[" +
                "           <Request>" +
                COMMON_ARG + objToXml(orderInfo) +
                "           </Request>" +
                "           ]]>" +
                "         </arg0>" +
                "      </qyws:GetOrderDetailInfoService>";

        String xml = (XML_HEADER +
                body +
                XML_FOOTER);

        return xml;
    }

    public String CheckPatientInfoServiceXML(OrderInfo orderInfo) {
        String body = "<qyws:CheckPatientInfoService>" +
                "         <!--Optional:-->" +
                "         <arg0>" +
                "           <![CDATA[" +
                "           <Request>" +
                COMMON_ARG + objToXml(orderInfo) +
                "           </Request>" +
                "           ]]>" +
                "         </arg0>" +
                "      </qyws:CheckPatientInfoService>";

        String xml = (XML_HEADER +
                body +
                XML_FOOTER);

        return xml;
    }

    public String GetHospitalCfgServiceXML(HosInfo hosInfo) {
        String body = "<qyws:GetHospitalCfgService>" +
                "         <!--Optional:-->" +
                "         <arg0>" +
                "           <![CDATA[" +
                "           <Request>" +
                COMMON_ARG + objToXml(hosInfo) +
                "           </Request>" +
                "           ]]>" +
                "         </arg0>" +
                "      </qyws:GetHospitalCfgService>";

        String xml = (XML_HEADER +
                body +
                XML_FOOTER);

        return xml;
    }

    public String GetResourceDoctInfoServiceXML(DeptInfo deptInfo) {
        String body = "<qyws:GetResourceDoctInfoService>" +
                "         <!--Optional:-->" +
                "         <arg0>" +
                "           <![CDATA[" +
                "           <Request>" +
                COMMON_ARG + objToXml(deptInfo) +
                "           </Request>" +
                "           ]]>" +
                "         </arg0>" +
                "      </qyws:GetResourceDoctInfoService>";

        String xml = (XML_HEADER +
                body +
                XML_FOOTER);

        return xml;
    }

    public String GetResourceOutPatInfoServiceXML(DeptInfo deptInfo) {
        String body = "<qyws:GetResourceOutPatInfoService>" +
                "         <!--Optional:-->" +
                "         <arg0>" +
                "           <![CDATA[" +
                "           <Request>" +
                COMMON_ARG + objToXml(deptInfo) +
                "           </Request>" +
                "           ]]>" +
                "         </arg0>" +
                "      </qyws:GetResourceOutPatInfoService>";

        String xml = (XML_HEADER +
                body +
                XML_FOOTER);

        return xml;
    }


    private String objToXml(Object o) {
        XStream xStream = new XStream(new XppDriver(new XmlFriendlyReplacer("_-", "_")));
        xStream.processAnnotations(HosInfo.class);
        xStream.autodetectAnnotations(true);

        return xStream.toXML(o);
    }

}
