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.server.models.request.alert.AlertRuleRequest;
import com.jay.monitor.data.server.models.store.AlertRuleConfig;
import com.jay.monitor.data.server.report.alert.rule.config.ExpressionValueCondition;
import com.jay.monitor.data.server.store.IAlertRuleProcess;
import com.jay.monitor.data.server.store.ISimpleCrudProcess;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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 com.jay.monitor.data.server.services.impl.DefaultBackstageRouteService;

/**
 * 后台路由管理
 *
 * @author liukx
 * @Date 2019-02-28 15:16
 */
@Controller
@RequestMapping(value = "/alert/rule")
public class AlertRuleController extends BaseSimpleCrudController<AlertRuleConfig> {

    ///////////////////////////////////////////// API ////////////////////////////////////////////
    public final static String PREFIX_URL = "/alert/rule";
    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";

    @Autowired
    private IAlertRuleProcess alertRuleProcess;
    @Autowired
    private ExpressionValueCondition expressionValueCondition;

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

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

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

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

    private void checkExpression(AlertRuleConfig req) {
//        CommonDataDTO commonDataDTO = new CommonDataDTO(MsgTypeEnums.MQ);
//        MatchRuleEnums ruleType = req.getRuleType();
//        if (MatchRuleEnums.expression == ruleType) {
//            try {
//                boolean match = expressionValueCondition.match(commonDataDTO, req.getFieldValue());
//            } catch (SpelParseException e) {
//                ResponseUtils.checkNull(null, "expression表达式解析有误!");
//            }
//        }
    }
}
