package com.engine.htsc.payment.eusp;



import com.alibaba.fastjson.JSON;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.interfaces.htsc.comInfo.PropBean;


/**
 * 
 * 新意资金结算系统客户端
 *
 */
public class EUSPWsSoapClient {
    // 产品名
    private static final String pid = "ESIMX";
    // 服务名
    private static final String serviceName = "eafsif";

    private static final String logPre = " ===eusp=== ";

    private PropBean pp = new PropBean();

    private Log log = LogFactory.getLog(EUSPWsSoapClient.class);
    
    private String username = pp.getPropName("webservice.EUSP.username");
    
    private String password = pp.getPropName("webservice.EUSP.password");

    private EUSP getEUSP() throws Exception {
        String endpoint = pp.getPropName("webservice.EUSP");
        EUSPService euspsl = new EUSPServiceLocator();
        EUSP ei = euspsl.getEUSPWs(new java.net.URL(endpoint));
        return ei;
    }

    /**
     * 登录新意
     * @param ei
     * @return
     * @throws Exception
     */
    private String doLogin(EUSP ei) throws Exception {
        
        EUSPOutput loginOutput = ei.login(username, password);
        if (loginOutput.getRetCode() != 0) {
            throw new Exception("登陆失败：" + loginOutput.getRetMessage());
        }
        // 登录成功，取得令牌
        String token = loginOutput.getRetValue();
        System.out.println("EUSP获取到的token:"+token);
        return token;
    }

    /**
     * 登出新轩逸
     * @param ei
     * @param token
     * @throws Exception
     */
    private void doLoginOut(EUSP ei, String token) throws Exception {
        ei.logout(token);
    }
    
    /**
     * 写入oa pdf文件流接口
     * @param workFlowNum
     * @param fileFormat
     * @param fileStream
     * @throws Exception
     */
    public void sendOAFile(String workFlowNum,String fileFormat,String fileStream) {
    	String methodName = "sendOAFile";
        log.info(" EUSP1调用 invoke :" + methodName+ "; requestmark : "+workFlowNum +
                " ; filestream length : " + fileStream.length());
    	EUSP ei = null;
    	String token = null;
	    try {	
	    	ei = this.getEUSP();
	        token = this.doLogin(ei);
	        EUSPParameter[] param = new EUSPParameter[3];
	        param[0] = new EUSPParameter("WORK_FLOW_NUM", workFlowNum);
	        param[1] = new EUSPParameter("FILE_FORMAT", fileFormat);
	        param[2] = new EUSPParameter("FILE_STREAM", fileStream);
	        EUSPOutput csOutput = ei.callService(token, pid, serviceName, methodName, param);
          doLoginOut(ei, token);
          log.info("EUSP1调用成功 EUSP1 csOutput return:" + JSON.toJSONString(csOutput));
	        log.info("EUSP1调用成功 EUSP1 return:" + csOutput.getRetMessage()+ " requetsmark : "+workFlowNum);
        }catch (Exception e) {
        	log.error("EUSP1调用失败 EUSP1 return: 新意资金结算系统EUSP调用sendOAFile出错 eusp error : "+" requestmark:"
                    +workFlowNum+" error "+e.getMessage());
        	log.error(e);

        	e.printStackTrace();
        }finally {
        	try {
        		doLoginOut(ei, token);
        	}catch(Exception e) {
        		e.printStackTrace();
        	}
        }
    }
    
    public void testAddStdExchReq() throws Exception {
        String methodName = "addStdExchReq";
        System.out.println("\n测试的方法:" + methodName);
        // 获取EUSP
        EUSP ei = this.getEUSP();
        // 登录EUSP
        String token = this.doLogin(ei);
        EUSPParameter[] param = new EUSPParameter[27];
        param[0] = new EUSPParameter("SETTLE_ENTITY_ID", "1");
        param[1] = new EUSPParameter("SETTLE_DEPT_CODE", "1001");
        param[2] = new EUSPParameter("INV_ACC", "99999999");
        param[3] = new EUSPParameter("SEAT_CODE", "8888");
        param[4] = new EUSPParameter("CUR_TYPE", "CNY");
        param[5] = new EUSPParameter("MKT_CODE", "01");
        param[6] = new EUSPParameter("TA_CODE", "49");
        param[7] = new EUSPParameter("SEC_CODE", "590001");
        param[8] = new EUSPParameter("YWLB", "01");
        param[9] = new EUSPParameter("ZQLB", "01");
        param[10] = new EUSPParameter("ORDER_NO", "1234556789");
        param[11] = new EUSPParameter("REPORT_DATE", "20150116");
        param[12] = new EUSPParameter("EXCH_NO", "11111111111");
        param[13] = new EUSPParameter("EXCH_DATE", "20150116");
        param[14] = new EUSPParameter("EXCH_QTY", "99");
        param[15] = new EUSPParameter("EXCH_PRICE", "99");
        param[16] = new EUSPParameter("EXCH_AMT", "999");
        param[17] = new EUSPParameter("COMMISSION_FEE", "1.01");
        param[18] = new EUSPParameter("STAMP_DUTY", "2.02");
        param[19] = new EUSPParameter("TRANSFER_FEE", "3.03");
        param[20] = new EUSPParameter("TRANSACT_FEE", "6");
        param[21] = new EUSPParameter("SUP_FEE", "99");
        param[22] = new EUSPParameter("OTHER_FEE", "99");
        param[23] = new EUSPParameter("SETTLE_FEE", "99");
        param[24] = new EUSPParameter("INTEREST_TAX", "99");
        param[25] = new EUSPParameter("RISK_FEE", "99");
        param[26] = new EUSPParameter("ZQZL", "01");
        EUSPOutput csOutput = ei.callService(token, pid, serviceName, methodName, param);
        System.out.println("返回的信息:" + csOutput.getRetMessage());
        this.doLoginOut(ei, token);
    }

    public void testQueryFundFtRep() throws Exception {
        String methodName = "queryFundFtRep";
        System.out.println("\n测试的方法:" + methodName);
        // 获取EUSP
        EUSP ei = this.getEUSP();
        // 登录EUSP
        String token = this.doLogin(ei);
        EUSPParameter[] param = new EUSPParameter[1];
        param[0] = new EUSPParameter("FUND_INST_ID", "1201512150000660");
        EUSPOutput csOutput = ei.callService(token, pid, serviceName, methodName, param);
        System.out.println("返回的信息:" + csOutput.getRetMessage());
        if (csOutput.getRetCode() == 0) {
            // 最多只有一个行结果集
            if (csOutput.getRowsetCount() == 1) {
                EUSPRowSet rs[] = csOutput.getRowsets();
                // 只有一个行结果集rs[0]
                String field[] = rs[0].getColumns();
                int flen = rs[0].getColumnCount();
                EUSPRecord recode[] = rs[0].getRecords();
                // 多行
                int rlen = rs[0].getRecordCount();
                for (int j = 0; j < rlen; j++) {
                    String data[] = recode[j].getDatas();
                    for (int i = 0; i < flen; i++) {
                        System.out.println(field[i] + ":" + data[i]);
                    }
                }
            }
        }
        this.doLoginOut(ei, token);
    }

}
