package com.jay.monitor.data.server.controllers;//package com.jay.monitor.data.server.controllers;

import com.jay.monitor.data.core.model.response.PageResponseModel;
import com.jay.monitor.data.core.utils.JsonHelper;
import com.jay.monitor.data.core.utils.ResponseUtils;
import com.jay.monitor.data.server.componts.html.DefaultDataRender;
import com.jay.monitor.data.server.controllers.crud.BaseSimpleCrudController;
import com.jay.monitor.data.server.enums.AlertMsgEnums;
import com.jay.monitor.data.server.models.request.alert.AlertPlatformConfigRequest;
import com.jay.monitor.data.server.models.store.AlertPlatformConfig;
import com.jay.monitor.data.server.report.alert.AlertDataService;
import com.jay.monitor.data.server.store.IAlertPlatformProcess;
import com.jay.monitor.data.server.store.ISimpleCrudProcess;
import com.jay.monitor.data.server.utils.HtmlComponentUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import static com.jay.monitor.data.server.controllers.AlertPlatformController.PREFIX_URL;

/**
 * 后台路由管理
 *
 * @author liukx
 * @Date 2019-02-28 15:16
 */
@Controller
@RequestMapping(value = PREFIX_URL)
public class AlertPlatformController extends BaseSimpleCrudController<AlertPlatformConfigRequest, AlertPlatformConfig> {

    public final static String PREFIX_URL = "/alert/platform";
    public final static String LIST_URL = PREFIX_URL + "/pageList";
    public final static String ADD_URL = PREFIX_URL + "/add";
    public final static String UPDATE_URL = PREFIX_URL + "/update";
    public final static String DEL_URL = PREFIX_URL + "/del";
    public final static String TYPE_LIST_URL = PREFIX_URL + "/typeList";
    public static final String PAGE_PATH = "/alertPlatform";

    @Override
    public String getViewPath() {
        return PAGE_PATH;
    }

    @Autowired
    private IAlertPlatformProcess alertPlatformProcess;

    @Autowired
    private AlertDataService alertDataService;

    @Override
    public ISimpleCrudProcess getCrudProcess() {
        return alertPlatformProcess;
    }

    @Override
    @RequestMapping(value = "/pageList",
        method = RequestMethod.POST,
        produces = "application/json;charset=UTF-8")
    @ResponseBody
    public PageResponseModel<AlertPlatformConfig> pageList(
        @RequestBody
            AlertPlatformConfigRequest request) throws Exception {
        AlertPlatformConfig config = new AlertPlatformConfig();
        BeanUtils.copyProperties(request, config);
        // 将参数对应的全部匹配赋值
        PageResponseModel pageResponseModel =
            alertPlatformProcess.searchDataByList(request.getPageNo(), request.getPageSize(), config);
        return pageResponseModel;
    }

    @RequestMapping(value = "/typeList",
        method = RequestMethod.POST,
        produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<AlertPlatformConfig> typeList(
        @RequestBody
            AlertPlatformConfigRequest request) throws Exception {
        AlertPlatformConfig config = new AlertPlatformConfig();
        config.setPlatformType(request.getPlatformType());
        config.setStatus(1);
        return alertPlatformProcess.queryList(config);
    }

    @Override
    protected void addBefore(AlertPlatformConfig req) {
        checkExpression(req);
    }

    @Override
    protected void addAfter(AlertPlatformConfig req, boolean result) {
        refreshPlatformId(req.getId());
    }

    @Override
    protected void addInModel(Model model, AlertPlatformConfig entity) {
        AlertPlatformConfig alertPlatformConfig = new AlertPlatformConfig();
        alertPlatformConfig.setPlatformType(AlertMsgEnums.DingDing);
        alertPlatformConfig.setStatus(1);
        model.addAttribute("obj", alertPlatformConfig);
        modelAfter(model, alertPlatformConfig);
    }

    @Override
    protected void updateBefore(AlertPlatformConfig req) {
        checkExpression(req);
    }

    @Override
    protected void updateAfter(AlertPlatformConfig req, boolean result) {
        refreshPlatformId(req.getId());
    }

    public void refreshPlatformId(String id) {
        alertDataService.refreshPlatformId(id);
    }

    @Override
    protected void deleteAfter(AlertPlatformConfig req, boolean result) {
        refreshPlatformId(req.getId());
    }

    @Override
    protected void modelAfter(Model model, AlertPlatformConfig entity) {
        List<DefaultDataRender> alertPlatformList = HtmlComponentUtils.convertDataRender(AlertMsgEnums.class);
        model.addAttribute("alertPlatformList", JsonHelper.toJsonString(alertPlatformList));
    }

    private void checkExpression(AlertPlatformConfig req) {
        ResponseUtils.checkNull(req.getAlertName(), "告警组名称不能为空!");
        ResponseUtils.checkNull(req.getPlatformType(), "告警类型不能为空!");
        ResponseUtils.checkNull(req.getUrl(), "告警url不能为空!");
    }
}
