package com.lightinit.hsdatashow.controller.ms;

import com.lightinit.hsdatashow.dictionary.DicCategoryIndex;
import com.lightinit.hsdatashow.dictionary.DicCategoryIndexDegree;
import com.lightinit.hsdatashow.entity.BaseExample;
import com.lightinit.hsdatashow.entity.Cockpit;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.service.ms.IMSCockpitService;
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.List;


/**
 * @Author: sunxiang
 * @Date: 2018/8/28 10:53
 * @Description:
 **/
@Controller
@RequestMapping(value="ms/cockpit")
public class MSCockpitController {

    @Autowired
    private IMSCockpitService msCockpitService;
    @RequestMapping(value="index.htm")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("ms/cockpit/index");
        return modelAndView;
    }

    @RequestMapping(value="add.htm")
    public ModelAndView add(){
        ModelAndView modelAndView = new ModelAndView("ms/cockpit/add");
        modelAndView.addObject("dicCategoryIndex", DicCategoryIndex.getInstacne().getDicMap());
        return modelAndView;
    }

    @RequestMapping(value="add.action")
    @ResponseBody
    public ResultState addDo(Cockpit cockpit){
        ResultState resultState = new ResultState();
        double quantity=cockpit.getQuantity();
        if(quantity<0 && quantity>100){
            resultState.setStateCode(500);
            resultState.setMsg("等级值必须在0到100之间！！");
            return resultState;
        }else if(quantity>=0 && quantity<=20){
            cockpit.setDegree(DicCategoryIndexDegree.LOWEST);
        }else if(quantity>20 && quantity<=40){
            cockpit.setDegree(DicCategoryIndexDegree.LOW);
        }else if(quantity>40 && quantity<=60){
            cockpit.setDegree(DicCategoryIndexDegree.NOMAL);
        }else if(quantity>60 && quantity<=80){
            cockpit.setDegree(DicCategoryIndexDegree.HIGH);
        }else{
            cockpit.setDegree(DicCategoryIndexDegree.HIGHEST);
        }
        resultState=msCockpitService.add(cockpit);
       /* MultiFunctionalModel multiFunctionalModel = new MultiFunctionalModel();
        multiFunctionalModel.setYear(year);
        multiFunctionalModel.setName(category);
        multiFunctionalModel.setTemp1(quantity);
        if(quantity<0 && quantity>100){
            resultState.setStateCode(500);
            resultState.setMsg("等级值必须在0到100之间！！");
            return resultState;
        }else if(quantity>=0 && quantity<=20){
            multiFunctionalModel.setTemp2(DicCategoryIndexDegree.LOWEST);
        }else if(quantity>20 && quantity<=40){
            multiFunctionalModel.setTemp2(DicCategoryIndexDegree.LOW);
        }else if(quantity>40 && quantity<=60){
            multiFunctionalModel.setTemp2(DicCategoryIndexDegree.NOMAL);
        }else if(quantity>60 && quantity<=80){
            multiFunctionalModel.setTemp2(DicCategoryIndexDegree.HIGH);
        }else{
            multiFunctionalModel.setTemp2(DicCategoryIndexDegree.HIGHEST);
        }
        resultState=msCockpitService.add(multiFunctionalModel);*/
        return resultState;
    }

    @RequestMapping(value="list.action")
    public ModelAndView list(int pageNo){
        ModelAndView modelAndView = new ModelAndView("ms/cockpit/list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        List<Cockpit> list=msCockpitService.list(pager);
        long total=msCockpitService.queryCount();
        pager.setTotal(total);
        modelAndView.addObject("list",list);
        modelAndView.addObject("pager",pager);
        return modelAndView;
    }

    @RequestMapping(value="edit.htm/{id}")
    public ModelAndView edit(@PathVariable Long id){
        ModelAndView modelAndView = new ModelAndView("ms/cockpit/edit");
        Cockpit model=msCockpitService.findById(id);
        modelAndView.addObject("dicCategoryIndex", DicCategoryIndex.getInstacne().getDicMap());
        modelAndView.addObject("outputModel",model);
        return  modelAndView;
    }

    @RequestMapping(value="edit.action")
    @ResponseBody
    public ResultState editDo(Cockpit cockpit){
        ResultState resultState = new ResultState();
       /* MultiFunctionalModel multiFunctionalModel = new MultiFunctionalModel();
        multiFunctionalModel.setId(id);
        multiFunctionalModel.setYear(year);
        multiFunctionalModel.setName(category);
        multiFunctionalModel.setTemp1(quantity);*/
        double quantity=cockpit.getQuantity();
        if(quantity<0 && quantity>100){
            resultState.setStateCode(500);
            resultState.setMsg("等级值必须在0到100之间！！");
            return resultState;
        }else if(quantity>=0 && quantity<=20){
            cockpit.setDegree(DicCategoryIndexDegree.LOWEST);
        }else if(quantity>20 && quantity<=40){
            cockpit.setDegree(DicCategoryIndexDegree.LOW);
        }else if(quantity>40 && quantity<=60){
            cockpit.setDegree(DicCategoryIndexDegree.NOMAL);
        }else if(quantity>60 && quantity<=80){
            cockpit.setDegree(DicCategoryIndexDegree.HIGH);
        }else{
            cockpit.setDegree(DicCategoryIndexDegree.HIGHEST);
        }
        resultState=msCockpitService.editDo(cockpit);
        return resultState;
    }


     @RequestMapping(value="delete.action/{id}")
     @ResponseBody
    public ResultState delete(@PathVariable Long id){
        ResultState resultState =msCockpitService.delete(id);
        return resultState;
    }

}
