package cn.com.liandisys.ycjt.ggcx.latetrain.controller;

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

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.com.liandisys.ycjt.ggcx.latetrain.model.TrainZWD;
import cn.com.liandisys.ycjt.ggcx.latetrain.vo.QueryParam;

/**
 * 
 *     
 * 项目名称：ycjt-ggcx    
 * 类名称：LateTrainController    
 * 类描述：    
 * 创建人：wangqx   
 * 创建时间：2017年3月22日 下午4:36:58    
 * 修改人：zhangyn    
 * 修改时间：2017年3月23日 下午2:36:58    
 * 修改备注：    
 * @version     
 *
 */
@Controller
@RequestMapping(value = "/table")
public class LateTrainController {

    public static final String USERNAME = "njcd";

    public static final String PASSWORD = "njcdjsyl";

    public static final String WSDL = "http://www.jsyl.com.cn/NJCDWS/njcdws";

    @RequestMapping(value = "fechZWD")
    public String fechZWD(Model model,
            @RequestParam(value = "laterNo", required = false, defaultValue = "") String laterNo,
            @RequestParam(value = "departStation", required = false, defaultValue = "") String departStation) {

        List<TrainZWD> allList = new ArrayList<TrainZWD>();

        if (!laterNo.equals("")) {
            model.addAttribute("laterNo", laterNo);
        }

        if (departStation.equals("")) {
            departStation = "盐城";
        }
        model.addAttribute("departStation", departStation);

        try {
            Object[] obj = invokeWebService("FechZWD", new Object[] { laterNo, departStation });
            if (obj != null) {
                obj = obj[0].toString().split(";");
                for (Object ss : obj) {
                    String arr[] = ss.toString().split(":");
                    TrainZWD trainZWD = new TrainZWD();
                    for (int i = 0; i < arr.length; i++) {

                        if (i == 0) {
                            trainZWD.setLaterNo(arr[0]);
                        }
                        if (i == 1) {
                            trainZWD.setStartStation(arr[1]);
                        }
                        if (i == 2) {
                            trainZWD.setStartTime(arr[2]);
                        }
                        if (i == 3) {
                            trainZWD.setEndStation(arr[3]);
                        }
                        if (i == 4) {
                            trainZWD.setEndTime(arr[4]);
                        }
                        if (i == 5) {
                            trainZWD.setDepartStation(arr[5]);
                        }
                        if (i == 6) {
                            trainZWD.setArrTime(arr[6]);
                        }
                        if (i == 7) {
                            if (Integer.valueOf(arr[7]) > 0) {
                                trainZWD.setLaterTime(
                                        arr[7].replaceAll(arr[7], "<font style='color:red'>" + arr[7] + "</font>"));
                            } else {
                                trainZWD.setLaterTime(arr[7]);
                            }
                        }
                        if (i == 8) {
                            trainZWD.setDepartTimePlan(arr[8]);
                        }
                        if (i == 9) {
                            trainZWD.setGoTime(arr[9]);
                        }

                    }
                    allList.add(trainZWD);
                }
            }
            model.addAttribute("allList", allList);
            model.addAttribute("laterTrainInfo", new QueryParam());

            return "table/latetrain";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "table/latetrain";
    }

    private static Object[] invokeWebService(String method, Object[] pars) {

        try {
            OMFactory factory = OMAbstractFactory.getOMFactory();
            OMNamespace SecurityElementNamespace = factory.createOMNamespace(
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "wsse");
            OMElement authenticationOM = factory.createOMElement("Security", SecurityElementNamespace);
            OMElement usertokenOM = factory.createOMElement("UsernameToken", SecurityElementNamespace);
            OMElement usernameOM = factory.createOMElement("Username", SecurityElementNamespace);
            OMElement passwordOM = factory.createOMElement("Password", SecurityElementNamespace);
            passwordOM.addAttribute("Type",
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText",
                    null);
            usernameOM.setText(USERNAME);
            passwordOM.setText(PASSWORD);
            usertokenOM.addChild(usernameOM);
            usertokenOM.addChild(passwordOM);
            authenticationOM.addChild(usertokenOM);
            // webservice
            EndpointReference targetEPR = new EndpointReference(WSDL);
            RPCServiceClient serviceClient = new RPCServiceClient();
            serviceClient.addHeader(authenticationOM);
            Options options = serviceClient.getOptions();
            options.setTo(targetEPR);
            QName opAddEntry = new QName("http://service.njcdws.jsyl.com/", method);
            Object[] opAddEntryArgs = pars;
            Class[] returnTypes = new Class[] { String.class };
            Object[] obj = serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, returnTypes);
            //String rst = (String) obj[0];
            return obj;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }
}
