package com.lightinit.hsdatashow.controller.ms;

import com.lightinit.hsdatashow.controller.admin.BaseController;
import com.lightinit.hsdatashow.entity.*;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.ms.GarbageRegionModel;
import com.lightinit.hsdatashow.model.ms.GreenModel;
import com.lightinit.hsdatashow.service.ms.IMSAtmosphereService;
import com.lightinit.hsdatashow.service.ms.IMSGreenService;
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;

@Controller
@RequestMapping(value = "/ms/green")
public class MSGreenController extends BaseController {
    private static LinkedHashMap<String,String> getChartTable(){
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put("城市绿化率","GreenCity");
        map.put("森林覆盖率","GreenForest");
        return map;
    }
    @Autowired
    private IMSGreenService imsEcologicalCivilizationService;
    @RequestMapping(value="index.htm")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("ms/green/index");
        modelAndView.addObject("chartTable",getChartTable());
        return modelAndView;
    }

    @RequestMapping(value="list.action")
    public ModelAndView list(String tableType,int pageNo){
        ModelAndView modelAndView=new ModelAndView("ms/green/"+tableType+"_list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        List list=imsEcologicalCivilizationService.list(tableType,pager);
        long total=imsEcologicalCivilizationService.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/green/"+tableType+"_add");
        Object obj=null;
        if(id !=null){
            obj=imsEcologicalCivilizationService.findById(tableType,id);
        }
        modelAndView.addObject("outputModel",obj);
        return modelAndView;
    }

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

    @RequestMapping(value="addGreenCity.action")
    @ResponseBody
    public ResultState addGreenCity(GreenCity greenCity){
        boolean isEdit=greenCity.getId()==null?false:true;
        ResultState resultState=imsEcologicalCivilizationService.add(greenCity,isEdit);
        return resultState;
    }

    @RequestMapping(value="addGreenForest.action")
    @ResponseBody
    public ResultState addGreenForest(GreenForest greenForest){
        boolean isEdit=greenForest.getId()==null?false:true;
        ResultState resultState=imsEcologicalCivilizationService.add(greenForest,isEdit);
        return resultState;
    }

    @RequestMapping(value="buildIndex.htm")
    public ModelAndView reginIndex(){
        ModelAndView modelAndView = new ModelAndView("ms/green/buildIndex");
        return modelAndView;
    }
    @RequestMapping(value="distributionIndex.htm")
    public ModelAndView distributionIndex(){
        ModelAndView modelAndView = new ModelAndView("ms/green/distributionIndex");
        return modelAndView;
    }

    @RequestMapping(value="forestIndex.htm")
    public ModelAndView forestIndex(){
        ModelAndView modelAndView = new ModelAndView("ms/green/forestIndex");
        return modelAndView;
    }


    @RequestMapping(value="buildList.action")
    public ModelAndView reginList(String type,int pageNo){
        ModelAndView modelAndView=null;
        if(type.equals("城市建成区绿化率排名")){
             modelAndView=new ModelAndView("ms/green/buildList");
        }else if(type.equals("绿化分布情况")) {
             modelAndView=new ModelAndView("ms/green/distributionList");
        }else if(type.equals("森林覆盖情况")) {
             modelAndView=new ModelAndView("ms/green/forestList");
        }
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
            List<GreenModel> list=imsEcologicalCivilizationService.buildList(type,pager);
            long total=imsEcologicalCivilizationService.buildCount(type,pager);
            pager.setTotal(total);
            modelAndView.addObject("list",list);
            modelAndView.addObject("pager",pager);
        return modelAndView;
    }

    @RequestMapping(value="buildAdd.htm")
    public ModelAndView reginAdd( Long id,String type){
        ModelAndView modelAndView=null;
        if(type.equals("城市建成区绿化率排名")){
            modelAndView=new ModelAndView("ms/green/buildAdd");
        }else if(type.equals("绿化分布情况")) {
            modelAndView=new ModelAndView("ms/green/distributionAdd");
        }else if(type.equals("森林覆盖情况")) {
            modelAndView=new ModelAndView("ms/green/forestAdd");
        }
        GreenModel garbageRegionModel =imsEcologicalCivilizationService.findByBuildId(id,type);
        modelAndView.addObject("outputModel",garbageRegionModel);
        return modelAndView;
    }


    @RequestMapping(value="addBuild.action")
    @ResponseBody
    public ResultState addBuild(GreenModel garbageRegionModel){
        boolean isEdit=garbageRegionModel.getId()==null?false:true;
        ResultState resultState=imsEcologicalCivilizationService.doAdd(garbageRegionModel,isEdit);
        return resultState;
    }

    @RequestMapping(value="delBuild.action")
    @ResponseBody
    public ResultState delBuild(Long id,Long regionId,String type) {
        ResultState resultState =imsEcologicalCivilizationService.delBuild(id,regionId,type);
        return resultState;
    }

}
