/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  org.dom4j.Document
 *  org.dom4j.Element
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.stereotype.Repository
 *  org.springframework.transaction.annotation.Propagation
 *  org.springframework.transaction.annotation.Transactional
 */
package com.hst.taxService.dao.imp;

import com.hst.common.domain.TaxCommonReturnDomain;
import com.hst.common.util.OracleUtil;
import com.hst.common.util.Xml;
import com.hst.framework.common.HstSqlMapClientTemplate;
import com.hst.framework.constants.WebConstants;
import com.hst.framework.util.HstToolsUtil;
import com.hst.taxService.dao.TaxCtais2AppDao;
import com.hst.taxService.domain.ctais2.FppzjbxxcxReturnDomain;
import com.hst.taxService.domain.ctais2.FppzxxDomain;
import com.hst.taxService.domain.ctais2.FpyjDomain;
import com.hst.taxService.domain.ctais2.FpyjReturnDomain;
import com.hst.taxService.domain.ctais2.GpyxxDomain;
import com.hst.taxService.domain.ctais2.NsrFpyjxxAppDomain;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class TaxCtais2NsrfpyjxxAppDaoImp
implements TaxCtais2AppDao {
    @Autowired
    private HstSqlMapClientTemplate sqlMapClientTemplate;

    public TaxCommonReturnDomain queryEjb(NsrFpyjxxAppDomain domain) throws Exception {
        String reqStr = this.createXml(domain);
        _logger.error((Object)("\u7968\u79cd\u57fa\u672c\u4fe1\u606f\u67e5\u8be2\u8bf7\u6c42\u62a5\u6587xml \uff1a" + reqStr));
        String resStr = this.doEjbServcie(reqStr);
        _logger.info((Object)("\u7968\u79cd\u57fa\u672c\u4fe1\u606f\u67e5\u8be2\u63a5\u53e3\u54cd\u5e94\u62a5\u6587xml" + resStr));
        FppzjbxxcxReturnDomain returnDomain = this.parserXml(resStr);
        return returnDomain;
    }

    public TaxCommonReturnDomain query(NsrFpyjxxAppDomain domain) throws Exception {
        FpyjReturnDomain returnDomain = new FpyjReturnDomain();
        Connection conn = null;
        Statement cstmt = null;
        ResultSet rs = null;
        FpyjDomain dom = null;
        ArrayList<FpyjDomain> dataList = new ArrayList<FpyjDomain>();
        for (FpyjDomain parDom : domain.getDataList()) {
            try {
                try {
                    conn = OracleUtil.getConnection(WebConstants.FWSK_IP, WebConstants.FWSK_SID, WebConstants.FWSK_PORT, WebConstants.FWSK_USERNAME, WebConstants.FWSK_PASSWORD);
                }
                catch (Exception exception) {
                    returnDomain.setCode("01");
                    returnDomain.setMess("\u7cfb\u7edf\u9519\u8bef\uff0c\u65e0\u6cd5\u83b7\u53d6\u9632\u4f2a\u7a0e\u63a7\u6570\u636e,\u8bf7\u7a0d\u5019\u518d\u8bd5\uff01");
                    FpyjReturnDomain fpyjReturnDomain = returnDomain;
                    if (rs != null) {
                        rs.close();
                    }
                    if (cstmt != null) {
                        cstmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                    return fpyjReturnDomain;
                }
                try {
                    conn.setAutoCommit(false);
                    String proc = "{? = call HTJS.P_GET_FWSK_BSXX_HMDYJ(?,?,?,?,?,?,?)}";
                    int parIndex = 1;
                    String fpzlDm = this.selectFplbDm(parDom.getFpzlDm());
                    cstmt = conn.prepareCall(proc);
                    cstmt.registerOutParameter(parIndex++, 4);
                    cstmt.setString(parIndex++, domain.getNsrsbh());
                    cstmt.setString(parIndex++, fpzlDm);
                    cstmt.setString(parIndex++, parDom.getFpDm());
                    cstmt.setString(parIndex++, parDom.getFpqh());
                    cstmt.setString(parIndex++, parDom.getFpzh());
                    cstmt.registerOutParameter(parIndex++, -10);
                    cstmt.registerOutParameter(parIndex++, 12);
                    _logger.info((Object)("\u5b58\u50a8\u8fc7\u7a0b[" + proc + "],\u53c2\u6570[" + domain.getNsrsbh() + "," + fpzlDm + "," + parDom.getFpDm() + "," + parDom.getFpqh() + "," + parDom.getFpzh() + "]"));
                    cstmt.execute();
                    int rint = cstmt.getInt(1);
                    if (rint == 100) {
                        rs = (ResultSet)cstmt.getObject(7);
                        while (rs.next()) {
                            dom = new FpyjDomain();
                            dom.setFpzlDm(parDom.getFpzlDm());
                            dom.setFpDm(rs.getString("AVC_FPDM"));
                            dom.setFpqh(rs.getString("AVC_FPQH"));
                            dom.setFpzh(rs.getString("AVC_FPZH"));
                            dom.setFs(rs.getString("FS"));
                            dom.setJe(rs.getString("AN_JE"));
                            dom.setSe(rs.getString("AN_SE"));
                            dom.setFpYjjgDm(rs.getString("AC_FPTKZT"));
                            dom.setRqQ(rs.getString("AVC_KPRQQ"));
                            dom.setRqZ(rs.getString("AVC_KPRQZ"));
                            dataList.add(dom);
                        }
                    } else {
                        this.queryPtfpAndHyz(domain, parDom, dataList);
                    }
                    conn.commit();
                }
                catch (Exception e) {
                    conn.rollback();
                    e.printStackTrace();
                    throw e;
                }
            }
            finally {
                if (rs != null) {
                    rs.close();
                }
                if (cstmt != null) {
                    cstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            }
        }
        returnDomain.setDataList(dataList);
        returnDomain.setCode("00");
        return returnDomain;
    }

    @Transactional(propagation=Propagation.REQUIRED)
    protected void queryPtfpAndHyz(NsrFpyjxxAppDomain domain, FpyjDomain parDom, List<FpyjDomain> dataList) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("nsrsbh", domain.getNsrsbh());
        map.put("fpzlDm", parDom.getFpzlDm());
        map.put("fpDm", parDom.getFpDm());
        map.put("fpqh", parDom.getFpqh());
        map.put("fpzh", parDom.getFpzh());
        map.put("nsrfpyjxxList", null);
        this.sqlMapClientTemplate.queryForObjectByCurr("queryNsrfpyjxx", "nsrfpyjxxList", map);
        List domList = (List)map.get("nsrfpyjxxList");
        if (domList != null && !domList.isEmpty()) {
            dataList.addAll(domList);
        }
    }

    public String createXml(NsrFpyjxxAppDomain domain) throws Exception {
        if (domain == null) {
            return null;
        }
        StringBuffer xmlStr = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("").append("<taxML xmlns=\"http://www.chinatax.gov.cn/dataspec/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"slCxFppzjbxxRequest\" cnName=\"\u7968\u79cd\u57fa\u672c\u4fe1\u606f\u67e5\u8be2\u63a5\u53e3\u8bf7\u6c42\u62a5\u6587\" name=\"slCxFppzjbxxRequest\" version=\"SW5001-2006\">").append("\t<nsrsbh>").append(domain.getNsrsbh()).append("</nsrsbh>").append("</taxML>");
        return xmlStr.toString();
    }

    public String doEjbServcie(String reqStr) throws Exception {
        String retStr = "<?xml version=\"1.0\" encoding=\"GBK\"?><tiripPackage version=\"1.0\" xmlns=\"http://www.chinatax.gov.cn/tirip/dataspec\"><identity>  <serviceId>PZHDCX</serviceId>  <channelId>Z110</channelId></identity><contentControl>  <zip>    <isZip>false</isZip>  </zip>  <encrypt>   <isEncrypt>false</isEncrypt> </encrypt>  <code>    <isCode>false</isCode> </code></contentControl><routerSession/><businessContent>  <subPackage>    <id>1</id>    <content><![CDATA[<?xml version='1.0' encoding='GBK'?><taxML xmlns=\"http://www.chinatax.gov.cn/dataspec/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"slCxFppzjbxxResponse\" cnName=\"\u7968\u79cd\u57fa\u672c\u4fe1\u606f\u67e5\u8be2\u63a5\u53e3\u54cd\u5e94\u62a5\u6587\" name=\"slCxFppzjbxxResponse\" version=\"SW5001-2006\"><gpyxx><fpjbr>\u5eb7\u8679</fpjbr><jbrzjlxDm>10</jbrzjlxDm><zjhm>620522198010010542</zjhm></gpyxx><fppzxx><fpzlDm>1140</fpzlDm><mygpzgsl>25</mygpzgsl><zdfpje>100000</zdfpje><lgsl>25</lgsl><cpzgsl>25</cpzgsl><lcpbbz>N</lcpbbz><fpBgfsDm>1</fpBgfsDm><fpGpfsDm>1</fpGpfsDm></fppzxx><fppzxx><fpzlDm>92620</fpzlDm><mygpzgsl>25</mygpzgsl><zdfpje>100000</zdfpje><lgsl>25</lgsl><cpzgsl>25</cpzgsl><lcpbbz>N</lcpbbz><fpBgfsDm>1</fpBgfsDm><fpGpfsDm>1</fpGpfsDm></fppzxx></taxML>]]></content>  </subPackage></businessContent><returnState>  <returnCode>000</returnCode>  <returnMessage>\u64cd\u4f5c\u6210\u529f\uff08\u64cd\u4f5c\u6210\u529f\uff09!</returnMessage> </returnState></tiripPackage>";
        return retStr;
    }

    public FppzjbxxcxReturnDomain parserXml(String xmlStr) throws Exception {
        FppzjbxxcxReturnDomain domain = new FppzjbxxcxReturnDomain();
        Document doc = new Xml().createDocument(xmlStr);
        Element root = doc.getRootElement();
        Element groupElement = root.element("returnState");
        if (groupElement == null) {
            return domain;
        }
        Element returnCodeElement = groupElement.element("returnCode");
        if (returnCodeElement != null) {
            domain.setCode(returnCodeElement.getText());
        }
        Element returnMessageElement = groupElement.element("returnMessage");
        if (returnCodeElement != null) {
            domain.setMess(returnMessageElement.getText());
        }
        if (!"000".equals(domain.getCode())) {
            return domain;
        }
        domain.setCode("00");
        groupElement = root.element("businessContent");
        if (groupElement == null) {
            return domain;
        }
        Element subPackageElement = groupElement.element("subPackage");
        if (subPackageElement == null) {
            return domain;
        }
        Element contentElement = subPackageElement.element("content");
        if (contentElement != null) {
            String resStr = contentElement.getText();
            Document resDoc = new Xml().createDocument(resStr);
            Element resRoot = resDoc.getRootElement();
            if (resRoot == null) {
                return domain;
            }
            ArrayList<GpyxxDomain> gpyxxList = new ArrayList<GpyxxDomain>();
            GpyxxDomain gpyxxDomain = null;
            List listGpyxx = resRoot.elements("gpyxx");
            for (Element element : listGpyxx) {
                gpyxxDomain = new GpyxxDomain();
                gpyxxDomain.setFpjbr(HstToolsUtil.getNullStr(element.element("fpjbr").getText(), ""));
                gpyxxDomain.setJbrzjlxDm(HstToolsUtil.getNullStr(element.element("jbrzjlxDm").getText(), ""));
                gpyxxDomain.setZjhm(HstToolsUtil.getNullStr(element.element("zjhm").getText(), ""));
                gpyxxList.add(gpyxxDomain);
            }
            domain.setGpyxxList(gpyxxList);
            ArrayList<FppzxxDomain> dataList = new ArrayList<FppzxxDomain>();
            FppzxxDomain pzxxDomain = null;
            List listPzxx = resRoot.elements("fppzxx");
            for (Element element : listPzxx) {
                pzxxDomain = new FppzxxDomain();
                pzxxDomain.setFpzlDm(HstToolsUtil.getNullStr(element.element("fpzlDm").getText(), ""));
                pzxxDomain.setMygpzgsl(HstToolsUtil.getNullStr(element.element("mygpzgsl").getText(), ""));
                pzxxDomain.setZdfpje(HstToolsUtil.getNullStr(element.element("zdfpje").getText(), ""));
                pzxxDomain.setLgsl(HstToolsUtil.getNullStr(element.element("lgsl").getText(), ""));
                pzxxDomain.setCpzgsl(HstToolsUtil.getNullStr(element.element("cpzgsl").getText(), ""));
                pzxxDomain.setLcpbbz(HstToolsUtil.getNullStr(element.element("lcpbbz").getText(), ""));
                pzxxDomain.setFpBgfsDm(HstToolsUtil.getNullStr(element.element("fpBgfsDm").getText(), ""));
                pzxxDomain.setFpGpfsDm(HstToolsUtil.getNullStr(element.element("fpGpfsDm").getText(), ""));
                dataList.add(pzxxDomain);
            }
            domain.setDataList(dataList);
        }
        return domain;
    }

    public String selectFplbDm(String fpzlDm) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("fpzlDm", fpzlDm);
        return (String)this.sqlMapClientTemplate.queryForObject("selectFplbDmByFpzlDm", map);
    }
}
