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

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
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 com.google.common.collect.Maps;

import cn.com.liandisys.ycjt.ggcx.admin.utils.DynamicSpecifications;
import cn.com.liandisys.ycjt.ggcx.admin.utils.SearchFilter;
import cn.com.liandisys.ycjt.ggcx.admin.utils.SearchFilter.Operator;
import cn.com.liandisys.ycjt.ggcx.airline.dao.AirLineDao;
import cn.com.liandisys.ycjt.ggcx.airline.model.AirLineEntity;
import cn.com.liandisys.ycjt.ggcx.airline.service.AirLineService;
import cn.com.liandisys.ycjt.ggcx.common.constant.SystemConstant;
import cn.com.liandisys.ycjt.ggcx.common.controller.BaseController;
import cn.com.liandisys.ycjt.ggcx.common.service.DataDicService;

/**
 * 
 *     
 * 项目名称：ycjt-ggcx    
 * 类名称：DepartureController    
 * 类描述：    
 * 创建人：yangd    
 * 创建时间：2016年11月25日 下午4:33:07    
 * 修改人：yangd    
 * 修改时间：2016年11月25日 下午4:33:07    
 * 修改备注：    
 * @version     
 *
 */
@Controller
@RequestMapping(value = "/table")
public class DepartureController extends BaseController {

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

    @Autowired
    private AirLineService airLineService;

    @Autowired
    private AirLineDao airLineDao;

    @Autowired
    private DataDicService dataDicService;

    //查询出港航班
    @RequestMapping(value = "initDepartureData")
    public String initDepartureData(Model model, @ModelAttribute("pageRequest") PageRequest pageRequest,
            String message) {

        // 查询条件
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("TYPE", new SearchFilter("TYPE", Operator.EQ, "2"));
        Specification<AirLineEntity> spec = DynamicSpecifications.bySearchFilter(filters.values(), AirLineEntity.class);

        Page<AirLineEntity> jpaPage = airLineService.findAll(spec, pageRequest);

        cn.com.liandisys.ycjt.ggcx.admin.model.Page<AirLineEntity> page =
                new cn.com.liandisys.ycjt.ggcx.admin.model.Page<AirLineEntity>(jpaPage.getNumber() + 1,
                        jpaPage.getSize(), jpaPage.getTotalElements());
        //当分页页码超过查询上限，则重置为最后一页，再查询一遍
        int lastPage = jpaPage.getTotalPages();
        if (lastPage < pageRequest.getPageNumber() + 1) {
            pageRequest = new PageRequest(lastPage - 1, pageRequest.getPageSize(), pageRequest.getSort());
            jpaPage = airLineService.findAll(spec, pageRequest);
        }

        //数据字典取出机型的具体信息
        page.setList(jpaPage.getContent());
        List<AirLineEntity> tempList = page.getList();
        List<AirLineEntity> resultList = new ArrayList<AirLineEntity>();
        try {
            for (AirLineEntity d : tempList) {
                d.setFLIGHT_MODEL(dataDicService.getDataDicName(SystemConstant.FLIGHT_MODEL, d.getFLIGHT_MODEL()));
                resultList.add(d);
            }
        } catch (Exception e) {
            model.addAttribute("message", e.getMessage());
        }

        page.setList(jpaPage.getContent());
        model.addAttribute("page", page);
        model.addAttribute("message", message);
        return "table/departure";
    }

    /**
     * 分页信息获取
     * 
     */
    @ModelAttribute("pageRequest")
    public PageRequest getPageRequest(@RequestParam(value = "pageNo", required = false, defaultValue = "1") String page,
            @RequestParam(value = "pageSize", required = false, defaultValue = "18") String size) {

        Sort sort = new Sort(Direction.DESC, new String[] { "ID" });
        if (org.springframework.util.StringUtils.hasText(page) && org.springframework.util.StringUtils.hasText(size)
                && null != sort) {
            // 页码从0开始
            PageRequest pageable = new PageRequest(Integer.parseInt(page) - 1, Integer.parseInt(size), sort);
            return pageable;
        }
        return null;
    }
}
