Bladeren bron

Merge remote-tracking branch 'origin/master_rule_engine' into master_rule_engine

syh 4 jaren geleden
bovenliggende
commit
a2bd4f6268
18 gewijzigde bestanden met toevoegingen van 668 en 51 verwijderingen
  1. 248 0
      module-alarm/src/main/java/cn/com/ctop/alarm/modules/controller/RuleConditionController.java
  2. 23 29
      module-alarm/src/main/java/cn/com/ctop/alarm/modules/controller/RuleIndicatorController.java
  3. 35 9
      module-alarm/src/main/java/cn/com/ctop/alarm/modules/controller/RuleTemplateController.java
  4. 4 1
      module-alarm/src/main/java/cn/com/ctop/alarm/modules/entity/RuleBase.java
  5. 63 0
      module-alarm/src/main/java/cn/com/ctop/alarm/modules/entity/RuleCondition.java
  6. 16 1
      module-alarm/src/main/java/cn/com/ctop/alarm/modules/entity/RuleGroup.java
  7. 1 1
      module-alarm/src/main/java/cn/com/ctop/alarm/modules/entity/RuleIndicator.java
  8. 1 1
      module-alarm/src/main/java/cn/com/ctop/alarm/modules/entity/RuleTemplate.java
  9. 5 4
      module-alarm/src/main/java/cn/com/ctop/alarm/modules/mapper/RuleAccountThresholdMapper.java
  10. 15 0
      module-alarm/src/main/java/cn/com/ctop/alarm/modules/mapper/RuleConditionMapper.java
  11. 4 0
      module-alarm/src/main/java/cn/com/ctop/alarm/modules/mapper/xml/RuleAccountThresholdMapper.xml
  12. 5 0
      module-alarm/src/main/java/cn/com/ctop/alarm/modules/mapper/xml/RuleConditionMapper.xml
  13. 14 0
      module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/IRuleConditionService.java
  14. 2 0
      module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/IRuleGroupService.java
  15. 2 0
      module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/IRuleTemplateService.java
  16. 19 0
      module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/impl/RuleConditionServiceImpl.java
  17. 103 2
      module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/impl/RuleGroupServiceImpl.java
  18. 108 3
      module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/impl/RuleTemplateServiceImpl.java

+ 248 - 0
module-alarm/src/main/java/cn/com/ctop/alarm/modules/controller/RuleConditionController.java

@@ -0,0 +1,248 @@
+package cn.com.ctop.alarm.modules.controller;
+
+import cn.com.ctop.alarm.modules.entity.RuleCondition;
+import cn.com.ctop.alarm.modules.service.IRuleConditionService;
+import cn.com.ctop.common.module.annotation.AutoLog;
+import com.alibaba.fastjson.JSON;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.jeecg.common.api.vo.Result;
+import org.jeecg.common.system.query.QueryGenerator;
+import org.jeecg.common.util.oConvertUtils;
+import org.jeecgframework.poi.excel.ExcelImportUtil;
+import org.jeecgframework.poi.excel.def.NormalExcelConstants;
+import org.jeecgframework.poi.excel.entity.ExportParams;
+import org.jeecgframework.poi.excel.entity.ImportParams;
+import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.multipart.MultipartHttpServletRequest;
+import org.springframework.web.servlet.ModelAndView;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 规则运算符配置表
+ *
+ * @author jeecg-boot
+ * @version V1.0
+ * @date 2020-11-16
+ */
+@Slf4j
+@Api(tags = "规则运算符配置表")
+@RestController
+@RequestMapping("/rule/ruleCondition")
+public class RuleConditionController {
+    @Autowired
+    private IRuleConditionService ruleConditionService;
+
+    /**
+     * 分页列表查询
+     *
+     * @param ruleCondition
+     * @param pageNo
+     * @param pageSize
+     * @param req
+     * @return
+     */
+    @AutoLog(value = "规则运算符配置表-分页列表查询")
+    @ApiOperation(value = "规则运算符配置表-分页列表查询", notes = "规则运算符配置表-分页列表查询")
+    @GetMapping(value = "/list")
+    public Result<IPage<RuleCondition>> queryPageList(RuleCondition ruleCondition,
+                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
+                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
+                                                      HttpServletRequest req) {
+        Result<IPage<RuleCondition>> result = new Result<>();
+        QueryWrapper<RuleCondition> queryWrapper = QueryGenerator.initQueryWrapper(ruleCondition, req.getParameterMap());
+        Page<RuleCondition> page = new Page<RuleCondition>(pageNo, pageSize);
+        IPage<RuleCondition> pageList = ruleConditionService.page(page, queryWrapper);
+        result.setSuccess(true);
+        result.setResult(pageList);
+        return result;
+    }
+
+    /**
+     * 添加
+     *
+     * @param ruleCondition
+     * @return
+     */
+    @AutoLog(value = "规则运算符配置表-添加")
+    @ApiOperation(value = "规则运算符配置表-添加", notes = "规则运算符配置表-添加")
+    @PostMapping(value = "/add")
+    public Result<RuleCondition> add(@RequestBody RuleCondition ruleCondition) {
+        Result<RuleCondition> result = new Result<>();
+        try {
+            ruleConditionService.save(ruleCondition);
+            result.success("添加成功!");
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+            result.error500("操作失败");
+        }
+        return result;
+    }
+
+    /**
+     * 编辑
+     *
+     * @param ruleCondition
+     * @return
+     */
+    @AutoLog(value = "规则运算符配置表-编辑")
+    @ApiOperation(value = "规则运算符配置表-编辑", notes = "规则运算符配置表-编辑")
+    @PutMapping(value = "/edit")
+    public Result<RuleCondition> edit(@RequestBody RuleCondition ruleCondition) {
+        Result<RuleCondition> result = new Result<RuleCondition>();
+        RuleCondition ruleConditionEntity = ruleConditionService.getById(ruleCondition.getId());
+        if (ruleConditionEntity == null) {
+            result.error500("未找到对应实体");
+        } else {
+            boolean ok = ruleConditionService.updateById(ruleCondition);
+            if (ok) {
+                result.success("修改成功!");
+            }
+        }
+
+        return result;
+    }
+
+    /**
+     * 通过id删除
+     *
+     * @param id
+     * @return
+     */
+    @AutoLog(value = "规则运算符配置表-通过id删除")
+    @ApiOperation(value = "规则运算符配置表-通过id删除", notes = "规则运算符配置表-通过id删除")
+    @DeleteMapping(value = "/delete")
+    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
+        try {
+            ruleConditionService.removeById(id);
+        } catch (Exception e) {
+            log.error("删除失败", e.getMessage());
+            return Result.error("删除失败!");
+        }
+        return Result.ok("删除成功!");
+    }
+
+    /**
+     * 批量删除
+     *
+     * @param ids
+     * @return
+     */
+    @AutoLog(value = "规则运算符配置表-批量删除")
+    @ApiOperation(value = "规则运算符配置表-批量删除", notes = "规则运算符配置表-批量删除")
+    @DeleteMapping(value = "/deleteBatch")
+    public Result<RuleCondition> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
+        Result<RuleCondition> result = new Result<>();
+        if (ids == null || "".equals(ids.trim())) {
+            result.error500("参数不识别!");
+        } else {
+            this.ruleConditionService.removeByIds(Arrays.asList(ids.split(",")));
+            result.success("删除成功!");
+        }
+        return result;
+    }
+
+    /**
+     * 通过id查询
+     *
+     * @param id
+     * @return
+     */
+    @AutoLog(value = "规则运算符配置表-通过id查询")
+    @ApiOperation(value = "规则运算符配置表-通过id查询", notes = "规则运算符配置表-通过id查询")
+    @GetMapping(value = "/queryById")
+    public Result<RuleCondition> queryById(@RequestParam(name = "id", required = true) String id) {
+        Result<RuleCondition> result = new Result<>();
+        RuleCondition ruleCondition = ruleConditionService.getById(id);
+        if (ruleCondition == null) {
+            result.error500("未找到对应实体");
+        } else {
+            result.setResult(ruleCondition);
+            result.setSuccess(true);
+        }
+        return result;
+    }
+
+    /**
+     * 导出excel
+     *
+     * @param request
+     * @param response
+     */
+    @RequestMapping(value = "/exportXls")
+    public ModelAndView exportXls(HttpServletRequest request, HttpServletResponse response) {
+        // Step.1 组装查询条件
+        QueryWrapper<RuleCondition> queryWrapper = null;
+        try {
+            String paramsStr = request.getParameter("paramsStr");
+            if (oConvertUtils.isNotEmpty(paramsStr)) {
+                String deString = URLDecoder.decode(paramsStr, "UTF-8");
+                RuleCondition ruleCondition = JSON.parseObject(deString, RuleCondition.class);
+                queryWrapper = QueryGenerator.initQueryWrapper(ruleCondition, request.getParameterMap());
+            }
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        }
+
+        //Step.2 AutoPoi 导出Excel
+        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
+        List<RuleCondition> pageList = ruleConditionService.list(queryWrapper);
+        //导出文件名称
+        mv.addObject(NormalExcelConstants.FILE_NAME, "规则运算符配置表列表");
+        mv.addObject(NormalExcelConstants.CLASS, RuleCondition.class);
+        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("规则运算符配置表列表数据", "导出人:Jeecg", "导出信息"));
+        mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
+        return mv;
+    }
+
+    /**
+     * 通过excel导入数据
+     *
+     * @param request
+     * @param response
+     * @return
+     */
+    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
+    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
+        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
+        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
+        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
+            MultipartFile file = entity.getValue();
+            ImportParams params = new ImportParams();
+            params.setTitleRows(2);
+            params.setHeadRows(1);
+            params.setNeedSave(true);
+            try {
+                List<RuleCondition> listRuleConditions = ExcelImportUtil.importExcel(file.getInputStream(), RuleCondition.class, params);
+                ruleConditionService.saveBatch(listRuleConditions);
+                return Result.ok("文件导入成功!数据行数:" + listRuleConditions.size());
+            } catch (Exception e) {
+                log.error(e.getMessage(), e);
+                return Result.error("文件导入失败:" + e.getMessage());
+            } finally {
+                try {
+                    file.getInputStream().close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        return Result.ok("文件导入失败!");
+    }
+
+}

+ 23 - 29
module-alarm/src/main/java/cn/com/ctop/alarm/modules/controller/RuleIndicatorController.java

@@ -3,10 +3,10 @@ package cn.com.ctop.alarm.modules.controller;
 import cn.com.ctop.alarm.modules.entity.RuleIndicator;
 import cn.com.ctop.alarm.modules.service.IRuleIndicatorService;
 import cn.com.ctop.common.module.annotation.AutoLog;
+import cn.com.ctop.common.module.utils.Check;
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.baomidou.mybatisplus.core.metadata.IPage;
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
@@ -19,15 +19,7 @@ import org.jeecgframework.poi.excel.entity.ExportParams;
 import org.jeecgframework.poi.excel.entity.ImportParams;
 import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.DeleteMapping;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.PutMapping;
-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.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.multipart.MultipartHttpServletRequest;
 import org.springframework.web.servlet.ModelAndView;
@@ -56,28 +48,30 @@ public class RuleIndicatorController {
     @Autowired
     private IRuleIndicatorService ruleIndicatorService;
 
-    /**
-     * 分页列表查询
-     *
-     * @param ruleIndicator
-     * @param pageNo
-     * @param pageSize
-     * @param req
-     * @return
-     */
+
     @AutoLog(value = "规则指标信息-分页列表查询")
     @ApiOperation(value = "规则指标信息-分页列表查询", notes = "规则指标信息-分页列表查询")
     @GetMapping(value = "/list")
-    public Result<IPage<RuleIndicator>> queryPageList(RuleIndicator ruleIndicator,
-                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
-                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
-                                                      HttpServletRequest req) {
-        Result<IPage<RuleIndicator>> result = new Result<>();
-        QueryWrapper<RuleIndicator> queryWrapper = QueryGenerator.initQueryWrapper(ruleIndicator, req.getParameterMap());
-        Page<RuleIndicator> page = new Page<RuleIndicator>(pageNo, pageSize);
-        IPage<RuleIndicator> pageList = ruleIndicatorService.page(page, queryWrapper);
+    public Result<JSONObject> queryPageList(RuleIndicator ruleIndicator) {
+        Result<JSONObject> result = new Result<>();
+        Integer mediaType = ruleIndicator.getMediaType();
+        QueryWrapper<RuleIndicator> queryWrapper = new QueryWrapper();
+        queryWrapper.eq("media_type", mediaType);
+        queryWrapper.groupBy("dimension");
+        List<RuleIndicator> list = ruleIndicatorService.list(queryWrapper);
+        JSONObject dataJson = new JSONObject();
+        for (RuleIndicator indicator : list) {
+            String dimension = indicator.getDimension();
+            QueryWrapper<RuleIndicator> queryWrapperData = new QueryWrapper();
+            queryWrapperData.eq("media_type", mediaType);
+            queryWrapperData.eq("dimension", dimension);
+            List<RuleIndicator> dataList = ruleIndicatorService.list(queryWrapperData);
+            if (!Check.isNull(dataList)) {
+                dataJson.put(dimension, dataList);
+            }
+        }
         result.setSuccess(true);
-        result.setResult(pageList);
+        result.setResult(dataJson);
         return result;
     }
 

+ 35 - 9
module-alarm/src/main/java/cn/com/ctop/alarm/modules/controller/RuleTemplateController.java

@@ -3,7 +3,9 @@ package cn.com.ctop.alarm.modules.controller;
 import cn.com.ctop.alarm.modules.entity.RuleTemplate;
 import cn.com.ctop.alarm.modules.service.IRuleTemplateService;
 import cn.com.ctop.common.module.annotation.AutoLog;
+import cn.com.ctop.common.module.utils.Check;
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -19,15 +21,7 @@ import org.jeecgframework.poi.excel.entity.ExportParams;
 import org.jeecgframework.poi.excel.entity.ImportParams;
 import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.DeleteMapping;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.PutMapping;
-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.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.multipart.MultipartHttpServletRequest;
 import org.springframework.web.servlet.ModelAndView;
@@ -81,6 +75,38 @@ public class RuleTemplateController {
         return result;
     }
 
+
+    /**
+     * 添加
+     *
+     * @param requestJson
+     * @return
+     */
+    @AutoLog(value = "创建规则模板")
+
+    @PostMapping(value = "/creativeTemplate")
+    public Result add(@RequestBody JSONObject requestJson) {
+        Result result = new Result<>();
+        try {
+            if (Check.isNull(requestJson)) {
+                throw new Exception("请传入模板入参");
+            }
+            JSONObject returnJson = ruleTemplateService.creativeTemplate(requestJson);
+            if (returnJson.getInteger("code") == 0) {
+                result.setSuccess(true);
+                result.setMessage(returnJson.getString("message"));
+            } else {
+                result.setSuccess(false);
+                result.setMessage(returnJson.getString("message"));
+            }
+        } catch (Exception e) {
+            result.setSuccess(false);
+            result.setMessage(e.getMessage());
+        }
+        return result;
+    }
+
+
     /**
      * 添加
      *

+ 4 - 1
module-alarm/src/main/java/cn/com/ctop/alarm/modules/entity/RuleBase.java

@@ -49,7 +49,7 @@ public class RuleBase {
      */
     @Excel(name = "判断条件 大于 等于 小于等", width = 15)
     @ApiModelProperty(value = "判断条件 大于 等于 小于等")
-    private String condition;
+    private String ruleCondition;
     /**
      * 阈值 比较值
      */
@@ -62,6 +62,9 @@ public class RuleBase {
     @Excel(name = "规则维度 账户、计划、组、创意等维度", width = 15)
     @ApiModelProperty(value = "规则维度 账户、计划、组、创意等维度")
     private String ruleDimension;
+
+    private Integer variableType;// 变量类型
+
     /**
      * createTime
      */

+ 63 - 0
module-alarm/src/main/java/cn/com/ctop/alarm/modules/entity/RuleCondition.java

@@ -0,0 +1,63 @@
+package cn.com.ctop.alarm.modules.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+import org.jeecgframework.poi.excel.annotation.Excel;
+
+import java.util.Date;
+
+/**
+ * 规则运算符配置表
+ *
+ * @author jeecg-boot
+ * @version V1.0
+ * @date 2020-11-16
+ */
+@Data
+@TableName("ctop_rule_condition")
+@EqualsAndHashCode(callSuper = false)
+@Accessors(chain = true)
+@ApiModel(value = "ctop_rule_condition对象", description = "规则运算符配置表")
+public class RuleCondition {
+
+    /**
+     * id
+     */
+    @TableId(type = IdType.AUTO)
+    @ApiModelProperty(value = "id")
+    private Long id;
+    /**
+     * 运算名称
+     */
+    @Excel(name = "运算名称", width = 15)
+    @ApiModelProperty(value = "运算名称")
+    private String conditionName;
+    /**
+     * 运算符
+     */
+    @Excel(name = "运算符", width = 15)
+    @ApiModelProperty(value = "运算符")
+    private String condition;
+    /**
+     * 数据类型
+     */
+    @Excel(name = "数据类型", width = 15)
+    @ApiModelProperty(value = "数据类型")
+    private String dataType;
+    /**
+     * createTime
+     */
+    @ApiModelProperty(value = "createTime")
+    private Date createTime;
+    /**
+     * updateTime
+     */
+    @ApiModelProperty(value = "updateTime")
+    private Date updateTime;
+}

+ 16 - 1
module-alarm/src/main/java/cn/com/ctop/alarm/modules/entity/RuleGroup.java

@@ -1,6 +1,7 @@
 package cn.com.ctop.alarm.modules.entity;
 
 import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
 import io.swagger.annotations.ApiModel;
@@ -11,6 +12,7 @@ import lombok.experimental.Accessors;
 import org.jeecgframework.poi.excel.annotation.Excel;
 
 import java.util.Date;
+import java.util.List;
 
 /**
  * 规则组
@@ -38,6 +40,7 @@ public class RuleGroup {
     @Excel(name = "规则id列表", width = 15)
     @ApiModelProperty(value = "规则id列表")
     private String ruleIds;
+    private String groupName;
     /**
      * 规则类型 group为组合规则 base 为单条规则
      */
@@ -50,11 +53,19 @@ public class RuleGroup {
     @Excel(name = "规则关系 当rule_type 为group时必填 and同时命中  or 命中一条", width = 15)
     @ApiModelProperty(value = "规则关系 当rule_type 为group时必填 and同时命中  or 命中一条")
     private String ruleRelationship;
+    /**
+     * 操作内容:SEND发送,PAUSE关停并发送
+     */
+    private String operate;
+    /**
+     * 预警发送方式: SMS:短信,WeChat:企业微信,EMAIL:电子邮件,TEL:电话
+     */
+    private String sendType;
 
     /**
      * 规则解释
      */
-    private String explain;
+    private String remark;
     /**
      * createTime
      */
@@ -65,4 +76,8 @@ public class RuleGroup {
      */
     @ApiModelProperty(value = "updateTime")
     private Date updateTime;
+
+    @TableField(exist = false)
+    private List<RuleBase> ruleBaseList;
+
 }

+ 1 - 1
module-alarm/src/main/java/cn/com/ctop/alarm/modules/entity/RuleIndicator.java

@@ -37,7 +37,7 @@ public class RuleIndicator {
      */
     @Excel(name = "1:头条2:快手", width = 15)
     @ApiModelProperty(value = "1:头条2:快手")
-    private Long mediaType;
+    private Integer mediaType;
     /**
      * 指标名称
      */

+ 1 - 1
module-alarm/src/main/java/cn/com/ctop/alarm/modules/entity/RuleTemplate.java

@@ -37,7 +37,7 @@ public class RuleTemplate {
      */
     @Excel(name = "1:头条2:快手", width = 15)
     @ApiModelProperty(value = "1:头条2:快手")
-    private Long mediaType;
+    private Integer mediaType;
     /**
      * 模板名称
      */

+ 5 - 4
module-alarm/src/main/java/cn/com/ctop/alarm/modules/mapper/RuleAccountThresholdMapper.java

@@ -1,17 +1,18 @@
 package cn.com.ctop.alarm.modules.mapper;
 
-import java.util.List;
-
-import org.apache.ibatis.annotations.Param;
 import cn.com.ctop.alarm.modules.entity.RuleAccountThreshold;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 
+import java.util.List;
+
 /**
  * 规则关联账户 阈值
+ *
  * @author: jeecg-boot
- * @date:   2020-11-15
+ * @date: 2020-11-15
  * @cersion: V1.0
  */
 public interface RuleAccountThresholdMapper extends BaseMapper<RuleAccountThreshold> {
 
+    List<RuleAccountThreshold> selectByAccountId(Long accountId);
 }

+ 15 - 0
module-alarm/src/main/java/cn/com/ctop/alarm/modules/mapper/RuleConditionMapper.java

@@ -0,0 +1,15 @@
+package cn.com.ctop.alarm.modules.mapper;
+
+import cn.com.ctop.alarm.modules.entity.RuleCondition;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+ * 规则运算符配置表
+ *
+ * @author: jeecg-boot
+ * @date: 2020-11-16
+ * @cersion: V1.0
+ */
+public interface RuleConditionMapper extends BaseMapper<RuleCondition> {
+
+}

+ 4 - 0
module-alarm/src/main/java/cn/com/ctop/alarm/modules/mapper/xml/RuleAccountThresholdMapper.xml

@@ -2,4 +2,8 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="cn.com.ctop.alarm.modules.mapper.RuleAccountThresholdMapper">
 
+    <select id="selectByAccountId" resultType="cn.com.ctop.alarm.modules.entity.RuleAccountThreshold">
+       SELECT * FROM ctop_rule_account_threshold
+       WHERE account_id = #{accountId}
+    </select>
 </mapper>

+ 5 - 0
module-alarm/src/main/java/cn/com/ctop/alarm/modules/mapper/xml/RuleConditionMapper.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="cn.com.ctop.alarm.modules.mapper.RuleConditionMapper">
+
+</mapper>

+ 14 - 0
module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/IRuleConditionService.java

@@ -0,0 +1,14 @@
+package cn.com.ctop.alarm.modules.service;
+
+import cn.com.ctop.alarm.modules.entity.RuleCondition;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+ * @Description: 规则运算符配置表
+ * @Author: jeecg-boot
+ * @Date: 2020-11-16
+ * @Version: V1.0
+ */
+public interface IRuleConditionService extends IService<RuleCondition> {
+
+}

+ 2 - 0
module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/IRuleGroupService.java

@@ -11,4 +11,6 @@ import com.baomidou.mybatisplus.extension.service.IService;
  */
 public interface IRuleGroupService extends IService<RuleGroup> {
 
+    void checkRules();
+
 }

+ 2 - 0
module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/IRuleTemplateService.java

@@ -1,6 +1,7 @@
 package cn.com.ctop.alarm.modules.service;
 
 import cn.com.ctop.alarm.modules.entity.RuleTemplate;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.extension.service.IService;
 
 /**
@@ -11,4 +12,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
  */
 public interface IRuleTemplateService extends IService<RuleTemplate> {
 
+    JSONObject creativeTemplate(JSONObject requestJson);
 }

+ 19 - 0
module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/impl/RuleConditionServiceImpl.java

@@ -0,0 +1,19 @@
+package cn.com.ctop.alarm.modules.service.impl;
+
+import cn.com.ctop.alarm.modules.entity.RuleCondition;
+import cn.com.ctop.alarm.modules.mapper.RuleConditionMapper;
+import cn.com.ctop.alarm.modules.service.IRuleConditionService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.stereotype.Service;
+
+/**
+ * 规则运算符配置表
+ *
+ * @author jeecg-boot
+ * @version V1.0
+ * @date 2020-11-16
+ */
+@Service
+public class RuleConditionServiceImpl extends ServiceImpl<RuleConditionMapper, RuleCondition> implements IRuleConditionService {
+
+}

+ 103 - 2
module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/impl/RuleGroupServiceImpl.java

@@ -1,19 +1,120 @@
 package cn.com.ctop.alarm.modules.service.impl;
 
+import cn.com.ctop.alarm.modules.entity.RuleAccountTemplate;
+import cn.com.ctop.alarm.modules.entity.RuleAccountThreshold;
 import cn.com.ctop.alarm.modules.entity.RuleGroup;
+import cn.com.ctop.alarm.modules.entity.RuleTemplate;
+import cn.com.ctop.alarm.modules.mapper.RuleAccountTemplateMapper;
+import cn.com.ctop.alarm.modules.mapper.RuleAccountThresholdMapper;
+import cn.com.ctop.alarm.modules.mapper.RuleBaseMapper;
 import cn.com.ctop.alarm.modules.mapper.RuleGroupMapper;
+import cn.com.ctop.alarm.modules.mapper.RuleIndicatorMapper;
+import cn.com.ctop.alarm.modules.mapper.RuleTemplateMapper;
 import cn.com.ctop.alarm.modules.service.IRuleGroupService;
+import cn.com.ctop.common.module.utils.Check;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.json.JSONObject;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 /**
  * 规则组
+ *
  * @author jeecg-boot
- * @date   2020-11-15
  * @version V1.0
+ * @date 2020-11-15
  */
 @Service
 public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup> implements IRuleGroupService {
 
+    @Autowired
+    private RuleBaseMapper ruleBaseMapper;
+    @Autowired
+    private RuleGroupMapper ruleGroupMapper;
+    @Autowired
+    private RuleIndicatorMapper ruleIndicatorMapper;
+    @Autowired
+    private RuleAccountThresholdMapper ruleAccountThresholdMapper;
+    @Autowired
+    private RuleAccountTemplateMapper ruleAccountTemplateMapper;
+    @Autowired
+    private RuleTemplateMapper ruleTemplateMapper;
+
+    @Override
+    public void checkRules() {
+        //账户绑定的规则模板
+        List<RuleAccountTemplate> ruleAccountTemplates = ruleAccountTemplateMapper.selectByMap(null);
+        //查询指标数据
+        List<JSONObject> dataList = getData();
+        //获取规则集
+        Map<Long, List<RuleGroup>> ruleGroupMap = getRuleGroupMap();
+        for (RuleAccountTemplate templates : ruleAccountTemplates) {
+            for (JSONObject data : dataList) {
+                if (templates.getAccountId() == data.getLong("accountId")) {
+                    List<RuleGroup> ruleGroups = ruleGroupMap.get(templates.getTemplateId());
+                    List<RuleAccountThreshold> list = ruleAccountThresholdMapper.selectByAccountId(templates.getAccountId());
+
+                }
+            }
+        }
+    }
+
+
+    /**
+     * 整理规则模板的数据,根据模板ID获取模板具体规则内容
+     *
+     * @param
+     * @return MAP key为模板ID,value是规则集
+     * @throws
+     * @author ZHAOXA
+     */
+    private Map<Long, List<RuleGroup>> getRuleGroupMap() {
+        Map<Long, List<RuleGroup>> map = new HashMap<>();
+        List<RuleTemplate> ruleTemplates = ruleTemplateMapper.selectByMap(null);
+        ruleTemplates.forEach(tem -> {
+            List<String> groupIds = strToList(tem.getGroupIds());
+            if (!Check.isNull(groupIds)) {
+                List<RuleGroup> ruleGroups = ruleGroupMapper.selectBatchIds(groupIds);
+                for (RuleGroup group : ruleGroups) {
+                    List<String> ruleIds = strToList(group.getRuleIds());
+                    if (!Check.isNull(ruleIds)) {
+                        group.setRuleBaseList(ruleBaseMapper.selectBatchIds(ruleIds));
+                    }
+                }
+                map.put(tem.getId(), ruleGroups);
+            }
+        });
+        return null;
+    }
+
+    //拆分逗号拼接数据
+    private List<String> strToList(String strings) {
+        if (Check.isNull(strings)) {
+            return null;
+        }
+        List<String> ids = new ArrayList<>();
+        String[] idArray = strings.split(",");
+        for (String id : idArray) {
+            ids.add(id);
+        }
+        return ids;
+    }
+
+    /**
+     * TODO 获取指标数据
+     *
+     * @param
+     * @return java.util.List<org.json.JSONObject>
+     * @throws
+     * @author ZHAOXA
+     */
+    private List<JSONObject> getData() {
+        return null;
+    }
+
 }

+ 108 - 3
module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/impl/RuleTemplateServiceImpl.java

@@ -1,19 +1,124 @@
 package cn.com.ctop.alarm.modules.service.impl;
 
+import cn.com.ctop.alarm.modules.entity.RuleBase;
+import cn.com.ctop.alarm.modules.entity.RuleGroup;
 import cn.com.ctop.alarm.modules.entity.RuleTemplate;
+import cn.com.ctop.alarm.modules.mapper.RuleBaseMapper;
 import cn.com.ctop.alarm.modules.mapper.RuleTemplateMapper;
+import cn.com.ctop.alarm.modules.service.IRuleBaseService;
+import cn.com.ctop.alarm.modules.service.IRuleGroupService;
 import cn.com.ctop.alarm.modules.service.IRuleTemplateService;
-import org.springframework.stereotype.Service;
-
+import cn.com.ctop.common.module.utils.Check;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
 
 /**
  * 规则模板
+ *
  * @author jeecg-boot
- * @date   2020-11-15
  * @version V1.0
+ * @date 2020-11-15
  */
 @Service
 public class RuleTemplateServiceImpl extends ServiceImpl<RuleTemplateMapper, RuleTemplate> implements IRuleTemplateService {
+    @Autowired
+    private IRuleBaseService ruleBaseService;
+
+    @Autowired
+    private RuleBaseMapper ruleBaseMapper;
+
+    @Autowired
+    private IRuleGroupService ruleGroupService;
+
+    /**
+     * 创建规则模板
+     *
+     * @param requestJson
+     * @return
+     */
+    @Override
+    public JSONObject creativeTemplate(JSONObject requestJson) {
+        System.err.println(requestJson);
+        JSONObject returnJson = new JSONObject();
+        try {
+            Integer mediaType = requestJson.getInteger("mediaType");
+            if (Check.isNull(mediaType)) {
+                throw new Exception("请传入媒体类型");
+            }
+            String templateName = requestJson.getString("templateName");
+            if (Check.isNull(templateName)) {
+                throw new Exception("请传入模板名称");
+            }
+            QueryWrapper<RuleTemplate> queryQueryWrapper = new QueryWrapper<>();
+            queryQueryWrapper.eq("media_type", mediaType);
+            queryQueryWrapper.eq("template_name", templateName);
+            RuleTemplate ruleTemplate = this.getOne(queryQueryWrapper);
+            if (!Check.isNull(ruleTemplate)) {
+                throw new Exception("同媒体下模板名称不能相同");
+            }
+            RuleTemplate template = new RuleTemplate();
+            template.setMediaType(mediaType);
+            template.setTemplateName(templateName);
+
+            JSONArray groupIds = new JSONArray();
+
+            JSONArray ruleList = requestJson.getJSONArray("ruleList"); // 模板规则详情
+            if (Check.isNull(ruleList)) {
+                throw new Exception("模板具体规则不能为空");
+            }
+            for (int i = 0; i < ruleList.size(); i++) {
+                JSONObject ruleGroupJson = ruleList.getJSONObject(i);
+                if (!Check.isNull(ruleGroupJson)) {
+                    JSONArray ruleDetailArr = ruleGroupJson.getJSONArray("ruleDetail");
+                    if (!Check.isNull(ruleDetailArr)) {
+                        JSONArray ruleIds = new JSONArray();
+                        for (int j = 0; j < ruleDetailArr.size(); j++) {
+                            JSONObject ruleDetailJson = ruleDetailArr.getJSONObject(j);
+                            if (!Check.isNull(ruleDetailJson)) {
+                                RuleBase ruleBase = new RuleBase();
+                                ruleBase.setRuleName(ruleDetailJson.getString("ruleName"));
+                                ruleBase.setIndicatorCode(ruleDetailJson.getString("indicatorCode"));
+                                ruleBase.setRuleCondition(ruleDetailJson.getString("ruleCondition"));
+                                ruleBase.setThreshold(ruleDetailJson.getString("threshold"));
+                                ruleBase.setVariableType(ruleDetailJson.getInteger("variableType"));
+                                ruleBase.setRuleDimension(ruleDetailJson.getString("ruleDimension"));
+                                int insert = ruleBaseMapper.insert(ruleBase);
+                                if (insert > 0) {
+                                    ruleIds.add(ruleBase.getId());
+                                }
+                            }
+                        }
+
+                        RuleGroup ruleGroup = new RuleGroup();
+                        ruleGroup.setRuleIds(ruleIds.toJSONString());
+                        ruleGroup.setRuleType(ruleGroupJson.getString("ruleType"));
+                        ruleGroup.setRuleRelationship(ruleGroupJson.getString("ruleRelationship"));
+                        ruleGroup.setGroupName(ruleGroupJson.getString("groupName"));
+                        ruleGroup.setRemark(ruleGroupJson.getString("remark"));
+                        boolean save = ruleGroupService.save(ruleGroup);
+                        if (save) {
+                            groupIds.add(ruleGroup.getId());
+
+                        }
+                    }
+                }
+            }
+            template.setGroupIds(groupIds.toJSONString());
+            this.save(template);
+            returnJson.put("code", 0);
+            returnJson.put("message", "创建成功");
+
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            returnJson.put("code", -1);
+            returnJson.put("message", e.getMessage());
+        }
 
+        return returnJson;
+    }
 }