package com.lightinit.hsdataplatform.controller.admin;

import com.lightinit.hsdataplatform.dictionary.DicMonitoringAlertMethod;
import com.lightinit.hsdataplatform.dictionary.DicRuningMonitoringType;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.admin.ApiRuningMonitoringModel;
import com.lightinit.hsdataplatform.model.admin.UserLogRuningModel;
import com.lightinit.hsdataplatform.model.admin.MicroappRuningModel;
import com.lightinit.hsdataplatform.service.admin.IRuningMonitoringService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value="/admin/runing_monitoring")
public class RuningMonitoringController {
    @Autowired
    private IRuningMonitoringService runingMonitoringService;

    @RequestMapping(value="runing_monitoring.htm")
    public ModelAndView query() {
        ModelAndView modelAndView = new ModelAndView("/admin/runing_monitoring/runing_monitoring");
        modelAndView.addObject("runingMonitoringType", DicRuningMonitoringType.getInstacne().getDicMap());
        modelAndView.addObject("monitoringAlertMethod", DicMonitoringAlertMethod.getInstacne().getDicMap());
        modelAndView.addObject("apiType",runingMonitoringService.queryAPIType());
        modelAndView.addObject("microappType",runingMonitoringService.queryMicroappType());
        modelAndView.addObject("userLogType",runingMonitoringService.queryUserLogType());
        return modelAndView;
    }
    @RequestMapping(value="add.htm")
    public ResultState add(ApiRuningMonitoringModel inputModel, MicroappRuningModel mModel, UserLogRuningModel uModel) {
        ModelAndView modelAndView = new ModelAndView("/admin/runing_monitoring/runing_monitoring");

        ApiRuningMonitoringModel obj = runingMonitoringService.queryAPIType();
        MicroappRuningModel obj1 = runingMonitoringService.queryMicroappType();
        UserLogRuningModel obj2 = runingMonitoringService.queryUserLogType();
        if(obj!=null){
            int resultState =  runingMonitoringService.del(obj.getId());
        }
        if(obj1!=null){
            int resultState = runingMonitoringService.del(obj1.getId());
        }
        if(obj2!=null){
            int resultState = runingMonitoringService.del(obj2.getId());
        }

            if(inputModel.getApiMonitoringType()!="" && inputModel.getApiMonitoringType().equals(DicRuningMonitoringType.API)){
                inputModel.setMonitoringType(inputModel.getApiMonitoringType());
                inputModel.setAlertMethod(inputModel.getApiAlertMethod());
                inputModel.setMonitoringCycle(inputModel.getApiMonitoringCycle());
                inputModel.setMonitoringTimeStrFrom(inputModel.getApiMonitoringTimeStrFrom());
                inputModel.setAlertTimeStrFrom(inputModel.getApiAlertTimeStrFrom());
                inputModel.setMonitoringTimeStrTo(inputModel.getApiMonitoringTimeStrTo());
                inputModel.setAlertTimeStrTo(inputModel.getApiAlertTimeStrTo());
                ResultState<Long> resultState = runingMonitoringService.add(inputModel);
                //modelAndView.addObject("microappType",runingMonitoringService.add(inputModel));
            }
            if(mModel.getMicroappMonitoringType() != "" && mModel.getMicroappMonitoringType().equals(DicRuningMonitoringType.MICROAPP)){
                mModel.setMonitoringType(mModel.getMicroappMonitoringType());
                mModel.setAlertMethod(mModel.getMicroappAlertMethod());
                mModel.setMonitoringCycle(mModel.getMicroappMonitoringCycle());
                mModel.setMonitoringTimeStrFrom(mModel.getMicroappMonitoringTimeStrFrom());
                mModel.setAlertTimeStrFrom(mModel.getMicroappAlertTimeStrFrom());
                mModel.setMonitoringTimeStrTo(mModel.getMicroappMonitoringTimeStrTo());
                mModel.setAlertTimeStrTo(mModel.getMicroappAlertTimeStrTo());
                ResultState<Long> resultState = runingMonitoringService.add(mModel);
               // modelAndView.addObject("microappType",runingMonitoringService.add(inputModel));
            }
            if(uModel.getUserLogMonitoringType()!="" && uModel.getUserLogMonitoringType().equals(DicRuningMonitoringType.USERLOG)){
                uModel.setMonitoringType(uModel.getUserLogMonitoringType());
                uModel.setAlertMethod(uModel.getUserLogAlertMethod());
                uModel.setMonitoringCycle(uModel.getUserLogMonitoringCycle());
                uModel.setMonitoringTimeStrFrom(uModel.getUserLogMonitoringTimeStrFrom());
                uModel.setMonitoringTimeStrTo(uModel.getUserLogMonitoringTimeStrTo());
                uModel.setAlertTimeStrFrom(uModel.getUserLogAlertTimeStrFrom());
                uModel.setAlertTimeStrTo(uModel.getUserLogAlertTimeStrTo());
                ResultState<Long> resultState = runingMonitoringService.add(uModel);
                //modelAndView.addObject("microappType",runingMonitoringService.add(inputModel));
            }
        return null;
    }
}
