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.service.ms.IMSAtmosphereService;
import com.lightinit.hsdatashow.service.ms.IMSGarbageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
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/garbage")
public class MSGarbageController extends BaseController {
    private static LinkedHashMap<String,String> getChartTable(){
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put("生活垃圾无害化处理率","GarbageTreatment");
        map.put("工业危险废物处理率","GarbagedangerRegion");
        map.put("生活垃圾无害化处理趋势","GarbageTrend");
        map.put("工业危险废物处理趋势","GarbagedangerTrend");
        return map;
    }
    @Autowired
    private IMSGarbageService imsEcologicalCivilizationService;
    @RequestMapping(value="index.htm")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("ms/garbage/index");
        modelAndView.addObject("chartTable",getChartTable());
        return modelAndView;
    }

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

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

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

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

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


    @RequestMapping(value="regionList.action")
    public ModelAndView reginList(String tableType,int pageNo){
        ModelAndView modelAndView=new ModelAndView("ms/garbage/regionList");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        List<GarbageRegionModel> list=imsEcologicalCivilizationService.regionList(pager);
        long total=imsEcologicalCivilizationService.regionCount(pager);
        pager.setTotal(total);
        modelAndView.addObject("list",list);
        modelAndView.addObject("pager",pager);
        return modelAndView;
    }

    @RequestMapping(value="regionAdd.htm")
    public ModelAndView reginAdd( Long id){
        ModelAndView modelAndView=new ModelAndView("ms/garbage/regionAdd");
            GarbageRegionModel garbageRegionModel =imsEcologicalCivilizationService.findByRegionId(id);
        modelAndView.addObject("outputModel",garbageRegionModel);
        return modelAndView;
    }


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

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

}
