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

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.com.liandisys.ycjt.ggcx.common.controller.BaseController;
import cn.com.liandisys.ycjt.ggcx.common.util.StringUtil;
import cn.com.liandisys.ycjt.ggcx.train.dao.TrainDao;
import cn.com.liandisys.ycjt.ggcx.train.model.Train;
import cn.com.liandisys.ycjt.ggcx.train.service.TrainService;
import cn.com.liandisys.ycjt.ggcx.train.vo.QueryParam;
import cn.com.liandisys.ycjt.ggcx.train.vo.TrainViewVo;

@Controller
@RequestMapping("/table")
public class TrainController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(TrainController.class);

    @Autowired
    TrainService trainService;

    @Autowired
    TrainDao trainDao;

    /*
     * 初始化数据列表
     * 2016.11.25
     * by zhangyn
     **/
    @RequestMapping(value = "getTrainGridData")
    public String getTrainGridData(Model model, @ModelAttribute("pageRequest") PageRequest pageRequest,
                                   @RequestParam(value = "stationS", required = false, defaultValue = "") String stationS,
                                   @RequestParam(value = "stationE", required = false, defaultValue = "") String stationE, String message) {

        if (stationS.equals("") && stationE.equals("")) {
            stationS = "盐城";
        }

        model.addAttribute("stationS", stationS);

        if (!stationE.equals("")) {
            model.addAttribute("stationE", stationE);
        }
        try {
            List<Train> allList = trainService.getTrainList();
            List<TrainViewVo> resultList = dealSearch(stationS, stationE, allList);
            List<TrainViewVo> displayList = new ArrayList<TrainViewVo>();
            int maxPage = 0;
            int startIndex = 0;
            int endIndex = 0;
            if (resultList.size() > 0) {
                // 结果集最大页数
                if (resultList.size() % pageRequest.getPageSize() == 0) {
                    maxPage = resultList.size() / pageRequest.getPageSize();
                } else {
                    maxPage = resultList.size() / pageRequest.getPageSize() + 1;
                }
                // 页面输入page大于最大page 显示最后一页
                if (pageRequest.getPageNumber() + 1 > maxPage) {
                    // 开始index
                    startIndex = (maxPage - 1) * pageRequest.getPageSize();
                    // 终了index
                    endIndex = resultList.size();
                } else {
                    // 开始index
                    startIndex = pageRequest.getPageNumber() * pageRequest.getPageSize();
                    // 终了index
                    endIndex = (pageRequest.getPageNumber() + 1) * pageRequest.getPageSize();

                    if (endIndex > resultList.size()) {
                        endIndex = resultList.size();
                    }
                }
                //页面显示List
                for (int i = startIndex; i < endIndex; i++) {
                    displayList.add(resultList.get(i));
                }
            }

            cn.com.liandisys.ycjt.ggcx.admin.model.Page<TrainViewVo> page =
                    new cn.com.liandisys.ycjt.ggcx.admin.model.Page<TrainViewVo>((pageRequest.getPageNumber() + 1) > maxPage ? maxPage : pageRequest.getPageNumber() + 1,
                            pageRequest.getPageSize(), displayList.size());
            page.setList(displayList);
            model.addAttribute("page", page);
            model.addAttribute("message", message);
            model.addAttribute("trainInfo", new QueryParam());

        } catch (Exception e) {
            model.addAttribute("message", e.getMessage());
            return "table/lcdt";
        }
        return "table/lcdt";
    }

    /*
     * 数据集成
     * 2016.11.25
     * by wangqx
     * */
    private List<TrainViewVo> dealSearch(String stationS, String stationE, List<Train> allList) {

        List<TrainViewVo> resultTrains = new ArrayList<TrainViewVo>();
        String trainNo = "";
        String startStation = "", endStation = "", departStation = "", arrStation = "", arrTime = "", departTime = "";
        float dMile = 0, aMile = 0;
        int days = 0;
        boolean haveDepart = false, haveArr = false;

        for (int i = 0; i < allList.size(); i++) {
            Train tempTrain = allList.get(i);

            String temptrainNo = tempTrain.getTrainNum();
            //设定trainNO
            if (i == 0 || !StringUtil.equals(temptrainNo, trainNo)) {
                //车次切换时
                if (i > 0) {
                    //该车次线路包含出发站 目的地站 取这条数据为结果
                    if (haveDepart && haveArr) {
                        //临时结果
                        TrainViewVo resultTrain = new TrainViewVo();
                        resultTrain.setTrainNo(trainNo);
                        resultTrain.setStartStation(startStation);
                        resultTrain.setEndStation(endStation);
                        resultTrain.setDepartStation(departStation);
                        resultTrain.setArrStation(arrStation);
                        resultTrain.setArrTime(arrTime);
                        resultTrain.setDepartTime(departTime);
                        // 里程 终点-起点
                        resultTrain.setMile(aMile - dMile);
                        resultTrain.setDays(days);
                        resultTrains.add(resultTrain);
                    }
                    // 复位
                    haveDepart = false;
                    haveArr = false;
                }
                // 设定始发站
                if (tempTrain.getTimes() == 1) {
                    // 每车次第一站
                    startStation = tempTrain.getStation();
                }
                trainNo = temptrainNo;
            }

            // 设定终点站(同一车次 覆盖终点站)
            if (StringUtil.equals(temptrainNo, trainNo)) {
                endStation = tempTrain.getStation();
            }

            //设定出发站 若发车站为空
            if (StringUtil.equals(stationS, "") && haveDepart == false) {
                departStation = tempTrain.getStation();
                dMile = tempTrain.getMile();
                //在该班次中找到了出发站
                haveDepart = true;
            } else if ((StringUtil.equals(stationS, tempTrain.getStation())
                    || StringUtil.contains(tempTrain.getStation(), stationS)) && haveDepart == false) {
                departStation = tempTrain.getStation();
                dMile = tempTrain.getMile();
                if (StringUtil.equals(tempTrain.getStation(), "盐城")) {
                    arrTime = tempTrain.getEstTime();
                    departTime = tempTrain.getDepartTime();
                }
                //在该班次中找到了出发站
                haveDepart = true;
            }
            //设定目的地站(有出发站)
            if (haveDepart && (StringUtil.equals(stationE, tempTrain.getStation())
                    || StringUtil.contains(tempTrain.getStation(), stationE))) {
                arrStation = tempTrain.getStation();
                aMile = tempTrain.getMile();
                days = tempTrain.getDays();
                if (StringUtil.equals(tempTrain.getStation(), "盐城")) {
                    arrTime = tempTrain.getEstTime();
                    departTime = tempTrain.getDepartTime();
                }
                //在该班次中找到了出发站
                haveArr = true;
            }
            //最后记录
            if (i == allList.size() - 1) {
                //该车次线路包含出发站 目的地站 取这条数据为结果
                if (haveDepart && haveArr) {
                    //临时结果
                    TrainViewVo resultTrain = new TrainViewVo();
                    resultTrain.setTrainNo(trainNo);
                    resultTrain.setStartStation(startStation);
                    resultTrain.setEndStation(endStation);
                    resultTrain.setDepartStation(departStation);
                    resultTrain.setArrStation(arrStation);
                    resultTrain.setArrTime(arrTime);
                    resultTrain.setDepartTime(departTime);
                    // 里程 终点-起点
                    resultTrain.setMile(aMile - dMile);
                    resultTrain.setDays(days);
                    resultTrains.add(resultTrain);
                }
                // 复位
                haveDepart = false;
                haveArr = false;
            }
        }
        return resultTrains;
    }
}
