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

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

import javax.servlet.http.HttpServletRequest;

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.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.liandisys.ycjt.ggcx.coachBus.dao.BusMainDao;
import cn.com.liandisys.ycjt.ggcx.coachBus.model.BusMain;
import cn.com.liandisys.ycjt.ggcx.coachBus.service.BusMainService;
import cn.com.liandisys.ycjt.ggcx.coachBus.vo.QueryParam;
import cn.com.liandisys.ycjt.ggcx.common.constant.PageUrl;
import cn.com.liandisys.ycjt.ggcx.common.constant.SystemConstant;
import cn.com.liandisys.ycjt.ggcx.common.service.DataDicService;
import cn.com.liandisys.ycjt.ggcx.common.util.JacksonUtil;
import cn.com.liandisys.ycjt.ggcx.common.util.MessageUtil;
import cn.com.liandisys.ycjt.ggcx.common.vo.Paging;

@Controller
@RequestMapping(value = "/table")
public class BusMainController {

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

    @Autowired
    private BusMainService busMainService;

    @Autowired
    private BusMainDao busMainDao;

    @Autowired
    private DataDicService dataDicService;

    /**
     * 跳转到查询界面
     * 2016.11.26
     * by zhangyn
     */
    @RequestMapping("/ctkyc")
    public ModelAndView showCtkyView(HttpServletRequest request) throws Exception {

        ModelAndView mv = new ModelAndView(PageUrl.CTKY_VIEW);
        String arrLocation = request.getParameter("carLocation");
        try {
            // 乘车地点
            mv.addObject("carLocationList",
                    JacksonUtil.toJsonNode(dataDicService.getDataDicInfo(SystemConstant.CAR_LOCATION)));
            // 到达站
            mv.addObject("carLocation ", arrLocation);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
        return mv;
    }

    /**
     * 初始化数据列表
     * 2016.11.24
     * by zhangyn
     **/
    @RequestMapping(value = "getCtkyGridData.do", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> getCtkyGridData(QueryParam queryParam, @ModelAttribute Paging paging) {

        Map<String, Object> retMap = new HashMap<String, Object>();
        try {
            Page<BusMain> busMains = findBySpecAndPaginate(paging.getPage() - 1, paging.getRows(), queryParam);
            retMap.put("page", paging.getPage());
            retMap.put("total", count(queryParam));
            List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
            if (busMains.getContent() != null) {
                List<BusMain> busMainList = busMains.getContent();
                for (BusMain d : busMainList) {
                    Map<String, Object> row = new HashMap<String, Object>();
                    row.put("id", d.getId());
                    row.put("busNum", d.getBusNum());
                    row.put("dTime", d.getDepartTime());
                    row.put("carLocation",
                            dataDicService.getDataDicName(SystemConstant.CAR_LOCATION, d.getCarLocation()));
                    row.put("arrLocation", d.getArrLocation());
                    row.put("price", d.getPrice());
                    row.put("surTick", d.getSurTick());
                    rows.add(row);
                }
            }
            retMap.put("rows", rows);
            return MessageUtil.buildResponseMap(true, retMap);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return MessageUtil.buildResponseMap(false, MessageUtil.buildSystemErrorMessage(e), retMap);
        }
    }

    /**
     * 返回分页查询结果
     * 2016.11.24
     * by zhangyn
     * */
    private Page<BusMain> findBySpecAndPaginate(int page, int size, QueryParam queryParam) {

        Page<BusMain> busMains = busMainDao.findAll(busMainService.getSpecification(queryParam),
                new PageRequest(page, size, new Sort(Direction.ASC, new String[] { "id" })));
        return busMains;
    }

    /**
     * 返回查询的总记录
     * 2016.11.24
     * by zhangyn
     * */
    private Long count(QueryParam queryParam) {

        Long count = busMainDao.count(busMainService.getSpecification(queryParam));
        return count;
    }

}
