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.GreenModel;
import com.lightinit.hsdatashow.service.ms.IMSCountrysideService;
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/countryside")
public class MSCountrysideController extends BaseController {
    private static LinkedHashMap<String,String> getChartTable(){
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put("农村生活垃圾无害化处理率","CountrysideGarbage");
        map.put("农村生活污水处理率","CountrysideSewage");
        map.put("农村自来水普及率","CountrysideWater");
        map.put("农村无害化厕所普及率","CountrysideWc");
        return map;
    }
    @Autowired
    private IMSCountrysideService imsEcologicalCivilizationService;
    @RequestMapping(value="index.htm")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("ms/countryside/index");
        modelAndView.addObject("chartTable",getChartTable());
        return modelAndView;
    }

    @RequestMapping(value="list.action")
    public ModelAndView list(String tableType,int pageNo){
        ModelAndView modelAndView=new ModelAndView("ms/countryside/"+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/countryside/"+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="addCountrysideGarbage.action")
    @ResponseBody
    public ResultState addCountrysideGarbage(CountrysideGarbage countrysideGarbage){
        boolean isEdit=countrysideGarbage.getId()==null?false:true;
        ResultState resultState=imsEcologicalCivilizationService.add(countrysideGarbage,isEdit);
        return resultState;
    }

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

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

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

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

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


    @RequestMapping(value="buildList.action")
    public ModelAndView reginList(String type,int pageNo){
        ModelAndView modelAndView=null;
        if(type.equals("农村生活垃圾无害化排名")){
             modelAndView=new ModelAndView("ms/countryside/garbageList");
        }else if(type.equals("农村生活污水排名")) {
             modelAndView=new ModelAndView("ms/countryside/sewageList");
        }else if(type.equals("农村无害化厕所分布")) {
             modelAndView=new ModelAndView("ms/countryside/harmlessWcList");
        }
        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/countryside/garbageAdd");
        }else if(type.equals("农村生活污水排名")) {
            modelAndView=new ModelAndView("ms/countryside/sewageAdd");
        }else if(type.equals("农村无害化厕所分布")) {
            modelAndView=new ModelAndView("ms/countryside/harmlessWcAdd");
        }
        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;
    }

}
