/**
 *
 */
package com.foc.offline;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.IOUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.foc.conf.Constants;
import com.foc.flightdynamics.bean.FlightDynamicsBean;
import com.foc.flightdynamics.entity.FlightDynamicsEntity;
import com.foc.flightdynamics.service.FlightDynamicsService;

/**
 * 
 * @team BBDTEK IT Team
 * @author renmb
 * @version 1.0
 * @time  2016年12月25日
 */
public class EstFlightDynamicsMain {

    /**
     * 
     * @author renmb
     * @time  2016年12月25日
     * @param args
     * @throws IOException 
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO Auto-generated method stub

//        args = new String[]{"2016-12-24", "2016-12-23 22:30:02", "W:\\WorkSpace\\JAVASpace\\foc-task-1.0\\target\\dynamics.2016122400\\2016122400\\dynamics.20161224000237.json"};
        
        String flightDate = args[0];//"2016-12-23 22:30:02";
        String modified = args[1];//"2016-12-23 22:30:02";
        String filePath = args[2];//"W:\\WorkSpace\\JAVASpace\\foc-task-1.0\\target\\dynamics.2016122400\\2016122400\\dynamics.20161224000237.json";
        
        FlightDynamicsService service = new FlightDynamicsService();
        
        String text = IOUtils.toString(new FileInputStream(filePath));
        JSONObject result = JSON.parseObject(text);
        JSONObject dataJson = (JSONObject) result.get("data");
        JSONArray  jsonArray = (JSONArray) dataJson.get("apiResp");
        
        List<FlightDynamicsBean> flightDynamicsBeans = JSONObject.parseArray(jsonArray.toJSONString(),
                FlightDynamicsBean.class);
        if (flightDynamicsBeans != null) {
            int index=0;
            int size = flightDynamicsBeans.size();
            for (FlightDynamicsBean flightDynamicsBean : flightDynamicsBeans) {

                index++;
                try {
                    System.out.print("【DYNAMIC】第【"+index+"/"+size+"】条【"+flightDynamicsBean.getFlightNo()+"】【"+flightDynamicsBean.getDatopChn()+"】【"+flightDynamicsBean.getStatus()+"】");
                    
                    if(flightDate.equals(flightDynamicsBean.getDatopChn())){// 如果是明天的航班则需要插入数据
                        
                        FlightDynamicsEntity flight = service.existFlightDynamics(flightDynamicsBean.getFlightNo(),
                                flightDynamicsBean.getDatopChn(), flightDynamicsBean.getDepStn(), flightDynamicsBean.getArrStn(), Constants.FOC_EST_FLIGHT_DYNAMICS);
                        
                        if (flight == null) {// 如果数据不存在则添加一条数据
                            System.out.print("【insertFlightDynamics】");
                            service.insertFlightDynamics(flightDynamicsBean, modified, Constants.FOC_EST_FLIGHT_DYNAMICS);
                        }
                    }
                    System.out.println("【正常】");
                } catch (Exception e) {
                    // TODO: handle exception
                    
                    System.out.println("【异常】");
                }
            }
        }
    }

}
