package com.lightinit.hsdatashow.controller.ms;

import com.lightinit.hsdatashow.entity.*;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.service.ms.IMSHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * @Author: sunxiang
 * @Date: 2018/9/7 11:26
 * @Description:
 **/
@Controller("msHomeController")
@RequestMapping(value = {"/ms", "/ms/home"})
public class MSHomeController {

    private static LinkedHashMap<String,String> getChartTable(){
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put("城镇绿化率","CityGreening");
        map.put("社会安全","SocietySecurity");
        map.put("污染指数","PollutionIndex");
        map.put("社会发展","SocietyDevelop");
        return map;
    }

    @Autowired
    private IMSHomeService msHomeService;
    @RequestMapping(value = {"/", "/index.action"})
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("ms/home/index");
        modelAndView.addObject("chartTable",getChartTable());
        return modelAndView;
    }

    @RequestMapping(value="list.action")
    public ModelAndView list(String tableType,int pageNo){
        ModelAndView modelAndView=new ModelAndView("ms/home/"+tableType+"_list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        List list=msHomeService.list(tableType,pager);
        long total=msHomeService.queryCount(tableType);
        pager.setTotal(total);
        modelAndView.addObject("list",list);
        modelAndView.addObject("pager",pager);
        return modelAndView;
    }

    @RequestMapping(value="add.htm")
    public ModelAndView add(String tableType,Long id){
        ModelAndView modelAndView=new ModelAndView("ms/home/"+tableType+"_add");
        Object obj=null;
        if(id !=null){
            obj=msHomeService.findById(tableType,id);
        }
        modelAndView.addObject("outputModel",obj);
        return modelAndView;
    }

    @RequestMapping(value="delete.action")
    @ResponseBody
    public ResultState delete(String tableType, Long id) {
        ResultState resultState =msHomeService.delete(tableType,id);
        return resultState;
    }

    @RequestMapping(value="addCityGreening.action")
    @ResponseBody
    public ResultState addCityGreening(CityGreening cityGreening){
        boolean isEdit=cityGreening.getId()==null?false:true;
        ResultState resultState=msHomeService.add(cityGreening,isEdit);
        return resultState;
    }

    @RequestMapping(value="addSocietySecurity.action")
    @ResponseBody
    public ResultState addSocietySecurity(SocietySecurity societySecurity){
        boolean isEdit=societySecurity.getId()==null?false:true;
        ResultState resultState=msHomeService.add(societySecurity,isEdit);
        return resultState;
    }

    @RequestMapping(value="addPollutionIndex.action")
    @ResponseBody
    public ResultState addPollutionIndex(PollutionIndex pollutionIndex){
        boolean isEdit=pollutionIndex.getId()==null?false:true;
        ResultState resultState=msHomeService.add(pollutionIndex,isEdit);
        return resultState;
    }

    @RequestMapping(value="addSocietyDevelop.action")
    @ResponseBody
    public ResultState addSocietyDevelop(SocietyDevelop  societyDevelop){
        boolean isEdit=societyDevelop.getId()==null?false:true;
        ResultState resultState=msHomeService.add(societyDevelop,isEdit);
        return resultState;
    }
}
