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

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

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.common.controller.BaseController;
import cn.com.liandisys.ycjt.ggcx.ferryship.model.FerryShip;
import cn.com.liandisys.ycjt.ggcx.ferryship.service.FerryShipService;

@Controller
@RequestMapping(value = "/table")
public class FerryShipController extends BaseController {

    @Autowired
    private FerryShipService ferryShipService;

    //查询轮渡码头
    @RequestMapping(value = "getFerryList")
    public String getFerryList(Model model, @ModelAttribute("pageRequest") PageRequest pageRequest,
            @RequestParam(value = "departurePort", required = false, defaultValue = "") String departurePort,
            @RequestParam(value = "arrivalPort", required = false, defaultValue = "") String arrivalPort,
            String message) {

        // 查询条件
        Map<String, SearchFilter> filters = Maps.newHashMap();

        if (!departurePort.equals("")) {
            filters.put("departurePort", new SearchFilter("departurePort", Operator.LIKE, departurePort));
            model.addAttribute("departurePort", departurePort);
        }
        if (!arrivalPort.equals("")) {
            filters.put("arrivalPort", new SearchFilter("arrivalPort", Operator.LIKE, arrivalPort));
            model.addAttribute("arrivalPort", arrivalPort);
        }

        Specification<FerryShip> spec = DynamicSpecifications.bySearchFilter(filters.values(), FerryShip.class);
        Page<FerryShip> jpaPage = ferryShipService.findAll(spec, pageRequest);

        cn.com.liandisys.ycjt.ggcx.admin.model.Page<FerryShip> page =
                new cn.com.liandisys.ycjt.ggcx.admin.model.Page<FerryShip>(jpaPage.getNumber() + 1, jpaPage.getSize(),
                        jpaPage.getTotalElements());
        //当分页页码超过查询上限，则重置为最后一页，再查询一遍
        int lastPage = jpaPage.getTotalPages();
        if (lastPage > 0) {
            if (lastPage < pageRequest.getPageNumber() + 1) {
                pageRequest = new PageRequest(lastPage - 1, pageRequest.getPageSize(), pageRequest.getSort());
                jpaPage = ferryShipService.findAll(spec, pageRequest);
            }
        }
        List<FerryShip> pageList = jpaPage.getContent();
        page.setList(pageList);
        model.addAttribute("page", page);
        model.addAttribute("message", message);
        model.addAttribute("fs", new FerryShip());
        return "table/ferryship";
    }

    /**
     * 分页信息获取
     * 
     */
    @ModelAttribute("pageRequest")
    public PageRequest getPageRequest(@RequestParam(value = "pageNo", required = false, defaultValue = "1") String page,
            @RequestParam(value = "pageSize", required = false, defaultValue = "15") 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;
    }
}
