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

import java.util.HashMap;
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.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 org.springframework.web.bind.annotation.ResponseBody;

import cn.com.liandisys.ycjt.ggcx.common.util.MessageUtil;
import cn.com.liandisys.ycjt.ggcx.taxiGps.service.TaxiGpsService;
import cn.com.liandisys.ycjt.ggcx.taxiGps.vo.TaxiVo;

/**
 * 
 *     
 * 项目名称：ycjt-ggcx    
 * 类名称：TaxiGpsController    
 * 类描述：    出租车GPS实时
 * 创建人：zhangyn    
 * 创建时间：2016年12月6日   
 * 修改人：zhangyn   
 * 修改时间：2016年12月6日  
 * 修改备注：    
 * @version     
 *
 */
@Controller
public class TaxiGpsController {

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

    @Autowired
    private TaxiGpsService taxiGpsService;

    @RequestMapping(value = "getTaxiGPSList.do")
    @ResponseBody
    public Map<String, Object> getTaxiGpsList(Model model, @ModelAttribute("pageRequest") PageRequest pageRequest) {

        Map<String, Object> retMap = new HashMap<String, Object>();
        try {
            // 根据条件取得结果集
            List<TaxiVo> layerData = taxiGpsService.findAllForLayer();
            Page<TaxiVo> page = taxiGpsService.getTaxiGpsList(pageRequest);
            retMap.put("result", layerData);
            retMap.put("totalPage", page.getTotalPages());
            retMap.put("currentPage", page.getNumber());
            return MessageUtil.buildResponseMap(true, retMap);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return MessageUtil.buildResponseMap(false, MessageUtil.buildSystemErrorMessage(e), retMap);
        }
    }
    
    @RequestMapping(value = "getTaxiPage.do")
    @ResponseBody
    public Map<String, Object> getTaxiPage(Model model, @ModelAttribute("pageRequest") PageRequest pageRequest) {

        Map<String, Object> retMap = new HashMap<String, Object>();
        try {
            // 根据条件取得结果集
            Page<TaxiVo> page = taxiGpsService.getTaxiGpsList(pageRequest);
            retMap.put("result", page.getContent());
            retMap.put("totalPage", page.getTotalPages());
            retMap.put("currentPage", page.getNumber());
            return MessageUtil.buildResponseMap(true, retMap);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return MessageUtil.buildResponseMap(false, MessageUtil.buildSystemErrorMessage(e), retMap);
        }
    }

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

}
