package cn.com.liandisys.ycjt.ggcx.publicBicycleSat.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.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 org.springframework.web.bind.annotation.ResponseBody;

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.util.MessageUtil;
import cn.com.liandisys.ycjt.ggcx.publicBicycleSat.model.PublicBicycleSatEntity;
import cn.com.liandisys.ycjt.ggcx.publicBicycleSat.service.PublicBicycleSatService;

/**
 * 
 * 项目名称：ycjt-ggcx    
 * 类名称：PublicBicycleSatController    
 * 类描述：   公共自行车
 * 创建人：wangqx    
 * 创建时间：2016年11月23日 下午2:23:40    
 * 修改人：wangqx    
 * 修改时间：2016年11月23日 下午2:23:40    
 * 修改备注：    
 * @version     
 *
 */
@Controller
@RequestMapping(value = "/table")
public class PublicBicycleSatController {

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

    @Autowired
    private PublicBicycleSatService publicBicycleSatService;

    //查询公共自行车站
    /**
     * 初始化数据
     * @param queryParam
     * @param paging
     * @return
     */
    @RequestMapping(value = "getPbsGridData")
    public String getPbsGridData(Model model, @ModelAttribute("pageRequest") PageRequest pageRequest,
            @RequestParam(value = "stationName", required = false, defaultValue = "") String sName,
            @RequestParam(value = "location", required = false, defaultValue = "") String location, String message) {

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

        if (!sName.equals("")) {
            filters.put("stationName", new SearchFilter("stationName", Operator.LIKE, sName));
            model.addAttribute("stationName", sName);
        }
        if (!location.equals("")) {
            filters.put("location", new SearchFilter("location", Operator.LIKE, location));
            model.addAttribute("location", location);
        }

        Specification<PublicBicycleSatEntity> spec =
                DynamicSpecifications.bySearchFilter(filters.values(), PublicBicycleSatEntity.class);

        Page<PublicBicycleSatEntity> jpaPage = publicBicycleSatService.findAll(spec, pageRequest);

        cn.com.liandisys.ycjt.ggcx.admin.model.Page<PublicBicycleSatEntity> page =
                new cn.com.liandisys.ycjt.ggcx.admin.model.Page<PublicBicycleSatEntity>(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 = publicBicycleSatService.findAll(spec, pageRequest);
            }
        }
        page.setList(jpaPage.getContent());
        model.addAttribute("page", page);
        model.addAttribute("message", message);
        model.addAttribute("pbs", new PublicBicycleSatEntity());
        return "table/ggzxc";
    }

    /**
     *地图左侧的公共自行车LIST取得 
     *
     */

    /* @RequestMapping(value = "getBibycleList.do")
    @ResponseBody
    public Map<String, Object> getBibycleList(Model model, @ModelAttribute("pageRequest") PageRequest pageRequest) {
    
        Map<String, Object> retMap = new HashMap<String, Object>();
        try {
            // 根据条件取得结果集
            Page<PublicBicycleSatEntity> page = publicBicycleSatService.getBibycleList(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);
        }
    }*/
    @RequestMapping(value = "getBibycleList.do")
    @ResponseBody
    public Map<String, Object> getBibycleList(Model model, @ModelAttribute("pageRequest") PageRequest pageRequest) {

        Map<String, Object> retMap = new HashMap<String, Object>();
        try {
            // 根据条件取得结果集
            List<PublicBicycleSatEntity> pbsList = publicBicycleSatService.getBibycleList();
            retMap.put("result", pbsList);
            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 = "15") String size) {

        Sort sort = new Sort(Direction.DESC, new String[] { "stationId" });
        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;
    }

}
