package com.gyee.alarm.controller; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.gyee.alarm.init.CacheContext; import com.gyee.alarm.model.auto.*; import com.gyee.alarm.model.vo.*; import com.gyee.alarm.service.AlarmRuleService; import com.gyee.alarm.service.TokenService; import com.gyee.alarm.service.auto.IProEconAlarmTypeService; import com.gyee.alarm.util.ExcelUtils; import com.gyee.alarm.util.SnowflakeGenerator; import com.gyee.alarm.util.StringUtils; import com.gyee.common.util.DateUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.*; @Slf4j @RestController @RequestMapping("/alertrule") @CrossOrigin @Api(value = "自定义报警规则", tags = "自定义报警规则") public class AlertRuleController { @Autowired private AlarmRuleService alarmRuleService; @Resource private IProEconAlarmTypeService proEconAlarmTypeService; @Resource private TokenService tokenService; @GetMapping(value = "/querywtalarmdesclist") @ApiOperation(value = "通过场站、型号和部件查询报警描述信息", notes = "通过场站、型号和部件查询报警描述信息") @ApiImplicitParams({ @ApiImplicitParam(name = "wpId", value = "风场编号", required = true, dataType = "string", paramType = "query"), @ApiImplicitParam(name = "modelId", value = "型号", required = true, dataType = "string", paramType = "query"), @ApiImplicitParam(name = "components", value = "部件", required = true, dataType = "string", paramType = "query")}) public AjaxResult queryWtAlarmDescList(String wpId, String modelId,String components) { List vos=new ArrayList<>(); if (StringUtils.notEmp(wpId) && StringUtils.notEmp(modelId) && StringUtils.notEmp(components)) { vos= alarmRuleService.queryWtAlarmDescList(wpId, modelId,components); } if (StringUtils.notEmp(vos)) { return AjaxResult.successData(AjaxStatus.success.code, vos); } else { return AjaxResult.successData(AjaxStatus.loginexpire.code, "error"); } } @GetMapping(value = "/querymodellist") @ApiOperation(value = "查询型号信息", notes = "查询型号信息") public AjaxResult queryModelList(String wpId) { List modells=new ArrayList<>(); if(StringUtils.notEmp( wpId) && CacheContext.wpwtmap.containsKey(wpId)) { List wtls=CacheContext.wpwtmap.get(wpId); for(ProBasicEquipment wt:wtls) { if(!modells.contains(wt.getModelId())) { modells.add(wt.getModelId()); } } }else if(StringUtils.notEmp( wpId) && CacheContext.subWtsMap.containsKey(wpId)) { List wtls=CacheContext.subWtsMap.get(wpId); for(ProBasicEquipment wt:wtls) { if(!modells.contains(wt.getModelId())) { modells.add(wt.getModelId()); } } }else { List wtls=CacheContext.equipmentmodels; for(ProEconEquipmentmodel wt:wtls) { if(!modells.contains(wt.getId())) { modells.add(wt.getId()); } } } if (StringUtils.notEmp(modells)) { return AjaxResult.successData(AjaxStatus.success.code, modells); } else { return AjaxResult.successData(AjaxStatus.loginexpire.code, "error"); } } @GetMapping(value = "/list") @ApiOperation(value = "查询报警关联部件名称", notes = "查询报警关联部件名称") public AjaxResult queryTree() { List alertrules = alarmRuleService.queryTree(); if (StringUtils.notEmp(alertrules)) { return AjaxResult.successData(AjaxStatus.success.code, alertrules); } else { return AjaxResult.successData(AjaxStatus.loginexpire.code, "error"); } } @GetMapping(value = "/queryalarmswitchllist") @ApiOperation(value = "查询报警开关", notes = "查询报警开关") public AjaxResult queryAlarmSwitchlList() { List vos = CacheContext.asls; if (StringUtils.notEmp(vos)) { return AjaxResult.successData(AjaxStatus.success.code, vos); } else { return AjaxResult.successData(AjaxStatus.loginexpire.code, "error"); } } @GetMapping(value = "/queryalarmtypellist") @ApiOperation(value = "查询报警类型和预警类型", notes = "查询报警类型和预警类型") public AjaxResult queryalAarmTypelList() { Map> map = new HashMap<>(); List typels = proEconAlarmTypeService.list(); List fjbjls = new ArrayList<>(); List gfbjls = new ArrayList<>(); List yjls = new ArrayList<>(); List sdls = new ArrayList<>(); if (!typels.isEmpty()) { for (ProEconAlarmType type : typels) { if (type.getCategory().equals(AlarmType.FJBJ.getCode())) { fjbjls.add(type); }else if (type.getCategory().equals(AlarmType.GFBJ.getCode())) { gfbjls.add(type); } else if (type.getCategory().equals(AlarmType.YJ.getCode())) { yjls.add(type); } else if (type.getCategory().equals(AlarmType.SD.getCode())) { sdls.add(type); } } } map.put("fjbj", fjbjls); map.put("gfbj", gfbjls); map.put("yj", yjls); map.put("sd", sdls); if (StringUtils.notEmp(map)) { return AjaxResult.successData(AjaxStatus.success.code, map); } else { return AjaxResult.successData(AjaxStatus.loginexpire.code, "error"); } } @GetMapping(value = "/querywpllist") @ApiOperation(value = "查询场站列表", notes = "查询场站列表") public AjaxResult queryWplList(String types) { HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest(); List wpls = tokenService.getWpls(request); List vos = new ArrayList<>(); if(!wpls.isEmpty()) { for(ProBasicPowerstation wp:wpls) { if(StringUtils.notEmp(types)) { if(types.equals(AlarmTypeValue.WT.getCode()) && wp.getId().contains("FDC")) { vos.add(wp); }else if(types.equals(AlarmTypeValue.IN.getCode()) && wp.getId().contains("GDC")) { vos.add(wp); }else if(types.equals(AlarmTypeValue.BT.getCode())) { List subwpls = tokenService.getSubwpls(request); return AjaxResult.successData(AjaxStatus.success.code, subwpls); } }else { vos =wpls; } } } if (StringUtils.notEmp(vos)) { return AjaxResult.successData(AjaxStatus.success.code, vos); } else { return AjaxResult.successData(AjaxStatus.loginexpire.code, "error"); } } @GetMapping(value = "/querywtlist") @ApiOperation(value = "通过场站获得设备信息", notes = "通过场站获得设备信息") @ApiImplicitParams({ @ApiImplicitParam(name = "wpId", value = "风场编号", required = true, dataType = "string", paramType = "query")}) public AjaxResult queryWtList(String wpId) { List wtls = new ArrayList<>(); if (StringUtils.notEmp(wpId) && CacheContext.wpwtmap.containsKey(wpId)) { wtls = CacheContext.wpwtmap.get(wpId); } if (StringUtils.notEmp(wtls)) { return AjaxResult.successData(AjaxStatus.success.code, wtls); } else { return AjaxResult.successData(AjaxStatus.loginexpire.code, "error"); } } @GetMapping(value = "/querywtpointlist") @ApiOperation(value = "通过场站和型号查询测点信息", notes = "通过场站和型号查询测点信息") @ApiImplicitParams({ @ApiImplicitParam(name = "wpId", value = "风场编号", required = true, dataType = "string", paramType = "query"), @ApiImplicitParam(name = "modelId", value = "型号", required = true, dataType = "string", paramType = "query")}) public AjaxResult queryWtPointlList(String wpId, String modelId) { Map> map = new HashMap<>(); if (StringUtils.notEmp(wpId) && StringUtils.notEmp(modelId) && CacheContext.wpwtmap.containsKey(wpId)) { List wtls = CacheContext.wpwtmap.get(wpId); List ailist = new ArrayList<>(); List dilist = new ArrayList<>(); List list = new ArrayList<>(); for (ProBasicEquipment wt : wtls) { if (wt.getModelId().equals(modelId) && CacheContext.wtpAimap.containsKey(wt.getId())) { Map wtpointmap = CacheContext.wtpAimap.get(wt.getId()); for (Map.Entry entry : wtpointmap.entrySet()) { if (entry.getKey().contains("AI")) { ailist.add(entry.getValue()); } else if (entry.getKey().contains("DI")) { dilist.add(entry.getValue()); } list.add(entry.getValue()); } break; } } map.put("ai", ailist); map.put("di", dilist); map.put("total", list); } if (StringUtils.notEmp(map)) { return AjaxResult.successData(AjaxStatus.success.code, map); } else { return AjaxResult.successData(AjaxStatus.loginexpire.code, "error"); } } @GetMapping(value = "/querysubllist") @ApiOperation(value = "获得升压站列表信息", notes = "获得升压站列表信息") public AjaxResult querySublList() { HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest(); List subwpls = tokenService.getSubwpls(request); if (StringUtils.notEmp(subwpls)) { return AjaxResult.successData(AjaxStatus.success.code, subwpls); } else { return AjaxResult.successData(AjaxStatus.loginexpire.code, "error"); } } @GetMapping(value = "/queryalarmrule") @ApiOperation(value = "根据主键获得报警规则", notes = "根据主键获得报警规则") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "报警规则主键", required = true, dataType = "string", paramType = "query")}) public AjaxResult queryAlarmRule(String id) { ProEconAlarmRule alarmRule = null; if (StringUtils.notEmp(id)) { alarmRule = alarmRuleService.selectByAlertruleId(id); } if (StringUtils.notEmp(alarmRule)) { return AjaxResult.successData(AjaxStatus.success.code, alarmRule); } else { return AjaxResult.successData(AjaxStatus.loginexpire.code, "error"); } } @GetMapping(value = "/querysubpointllist") @ApiOperation(value = "获得升压站主键获得测点信息", notes = "获得升压站主键获得测点信息") public AjaxResult querySubPointlList(String subId) { Map> map = new HashMap<>(); if (StringUtils.notEmp(subId) && CacheContext.subwppointmap.containsKey(subId)) { Map submap = CacheContext.subwppointmap.get(subId); List ailist = new ArrayList<>(); List dilist = new ArrayList<>(); List list = new ArrayList<>(); for (Map.Entry entry : submap.entrySet()) { if (entry.getKey().contains("AI")) { ailist.add(entry.getValue()); } else if (entry.getKey().contains("DI")) { dilist.add(entry.getValue()); } list.add(entry.getValue()); } map.put("ai", ailist); map.put("di", dilist); map.put("total", list); } if (StringUtils.notEmp(map)) { return AjaxResult.successData(AjaxStatus.success.code, map); } else { return AjaxResult.successData(AjaxStatus.loginexpire.code, "error"); } } @GetMapping(value = "/querywpbyymodellist") @ApiOperation(value = "通过场站编号获得型号下拉列表", notes = "通过场站编号获得型号下拉列表") public AjaxResult queryWpByModelList() { Map> wpByEmMap = CacheContext.wpByEmMap; if (StringUtils.notEmp(wpByEmMap)) { return AjaxResult.successData(AjaxStatus.success.code, wpByEmMap); } else { return AjaxResult.successData(AjaxStatus.loginexpire.code, "error"); } } @GetMapping(value = "/queryctalarmdesclist") @ApiOperation(value = "通过场站、型号和部件查询报警描述信息", notes = "通过场站、型号和部件查询报警描述信息") @ApiImplicitParams({ @ApiImplicitParam(name = "wpId", value = "风场编号", required = true, dataType = "string", paramType = "query"), @ApiImplicitParam(name = "modelId", value = "型号", required = true, dataType = "string", paramType = "query"), @ApiImplicitParam(name = "components", value = "部件", required = true, dataType = "string", paramType = "query")}) public AjaxResult queryCtAlarmDescList(String wpId, String modelId,String components) { List vos=new ArrayList<>(); if (StringUtils.notEmp(wpId) && StringUtils.notEmp(modelId) && StringUtils.notEmp(components)) { vos= alarmRuleService.queryCtAlarmDescList(wpId, modelId,components); } if (StringUtils.notEmp(vos)) { return AjaxResult.successData(AjaxStatus.success.code, vos); } else { return AjaxResult.successData(AjaxStatus.loginexpire.code, "error"); } } //@UserLoginToken @PostMapping(value = "/save") @ResponseBody @ApiOperation(value = "进行修改和保存", notes = "进行修改和保存") public AjaxResult saveAndUpdateAlertrule(HttpServletRequest request, @RequestBody ProEconAlarmRule alarmRule) throws Exception { if (StringUtils.notEmp(alarmRule)) { int result = alarmRuleService.saveAndUpdateAlertrule(alarmRule); return AjaxResult.successData(AjaxStatus.success.code, result); } else { return AjaxResult.successData(AjaxStatus.loginexpire.code, "error"); } } @DeleteMapping(value = "/delete") @ApiOperation(value = "删除", notes = "删除") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "自定义报警主键", required = true, dataType = "string", paramType = "query")}) public AjaxResult deleteAlertrule(String id) { int i = alarmRuleService.deleteAlertrule(id); if (StringUtils.notEmp(i)) { return AjaxResult.successData(AjaxStatus.success.code, i); } else { return AjaxResult.successData(AjaxStatus.error.code, "error"); } } //@UserLoginToken @GetMapping(value = "/page") @ApiOperation(value = "分页查询", notes = "分页查询") @ApiImplicitParams({ @ApiImplicitParam(name = "pageNum", value = "页号", required = true, dataType = "Integer", paramType = "query"), @ApiImplicitParam(name = "pageSize", value = "每页显示多少行", required = true, dataType = "Integer", paramType = "query"), @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "query"), @ApiImplicitParam(name = "wpId", value = "风场编号", required = false, dataType = "string", paramType = "query"), @ApiImplicitParam(name = "modelId", value = "型号编号", required = false, dataType = "string", paramType = "query"), @ApiImplicitParam(name = "rank", value = "级别", required = false, dataType = "string", paramType = "query"), @ApiImplicitParam(name = "category", value = "类别(booststation:升压站报警,windturbine:风机报警,inverter:逆变器)", required = false, dataType = "string", paramType = "query"), @ApiImplicitParam(name = "enabled", value = "是否可用", required = false, dataType = "string", paramType = "query"), @ApiImplicitParam(name = "relatedparts", value = "部件", required = false, dataType = "string", paramType = "query")}) public AjaxResult queryByPage(HttpServletRequest request, @RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "pageSize") Integer pageSize, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "wpId", required = false) String wpId, @RequestParam(value = "modelId", required = false) String modelId, @RequestParam(value = "rank", required = false) Integer rank, @RequestParam(value = "category", required = false) String category, @RequestParam(value = "enabled", required = false) Boolean enabled, @RequestParam(value = "relatedparts", required = false) String relatedparts ) { Page page = new Page(pageNum, pageSize); IPage pageResult = alarmRuleService.pageQueryAll(page, name, wpId, modelId, rank, category, enabled, relatedparts); if (StringUtils.notEmp(pageResult)) { return AjaxResult.successData(AjaxStatus.success.code, pageResult); } else { return AjaxResult.successData(AjaxStatus.error.code, "error"); } } //@UserLoginToken @PostMapping(value = "/save-batch") @ResponseBody @ApiOperation(value = "批量保存", notes = "分页查询") public AjaxResult saveAlertruleBatch(HttpServletRequest request, @RequestBody List lst) throws Exception { boolean allCheck = true; for (ProEconAlarmRule alertrule : lst) { String msg = ""; boolean result = true; if (StringUtils.isBlank(alertrule.getName())) { msg = "报警名称不能为空"; result = false; } else if (StringUtils.isBlank(alertrule.getDescription())) { msg = "规则描述不能为空"; result = false; } else if (StringUtils.isBlank(alertrule.getExpression())) { msg = "报警规则不能为空"; result = false; } else if (StringUtils.empty(alertrule.getRank())) { msg = "报警级别不能为空"; result = false; } else if (StringUtils.isBlank(alertrule.getStationId())) { msg = "风场不能为空"; result = false; } else if (StringUtils.isBlank(alertrule.getCategory())) { msg = "报警类别不能为空"; result = false; } else if (StringUtils.isBlank(alertrule.getUniformCode())) { msg = "统一编码不能为空"; result = false; } if (alertrule.getCategory().equals(AlarmCustomType.WT.getCode())) { if (StringUtils.isBlank(alertrule.getModelId())) { msg = "风机型号不能为空"; result = false; } } if (result != true) { allCheck = false; if (!result) { return AjaxResult.successData(AjaxStatus.error.code, msg); } } } if (allCheck) { for (ProEconAlarmRule alertRule : lst) { int result = alarmRuleService.saveAndUpdateAlertrule(alertRule); if (result <= 0) { return AjaxResult.successData(AjaxStatus.error.code, alertRule.getName() + ":" + "操作数据库失败"); } } return AjaxResult.successData(AjaxStatus.success.code, "ok"); } else { return AjaxResult.successData(AjaxStatus.error.code, "error"); } } @GetMapping("/get-import-template") @ApiOperation(value = "获得导入模板") public void importTemplate(HttpServletResponse response) throws IOException { // 手动创建导出 demo Date current = com.gyee.common.util.DateUtils.getCurrentDate(); Calendar calendar = Calendar.getInstance(); calendar.setTime(current); calendar.add(Calendar.DAY_OF_MONTH, -1); Date previousDay = calendar.getTime(); List list = Arrays.asList( ProEconAlarmRule.builder().id("").name("单机变桨电池柜温度1温度传感器异常值").description("温度等于850度或者温度小于零下40度") .expression("AI242 == 850 || AI242 <-40").tag("").rank(4).modelId("UP2000") .category("windturbine").range("parts") .stationId("SXJ_KGDL_GJY_FDC_STA").projectId("SXJ_KGDL_GJYF01_EG").lineId("SXJ_KGDL_GJYF01_LN").deviceId("SXJ_KGDL_GJY_F_WT_0001_EQ") .electricalId("").relatedParts("BJXT").createTime(DateUtils.truncate(previousDay)) .enable(true).uniformCode("AI091") .build(), ProEconAlarmRule.builder().id("").name("单机变桨电池柜温度1温度传感器异常值").description("温度等于850度或者温度小于零下40度") .expression("AI242 == 850 || AI242 <-40").tag("").rank(4).modelId("UP2000") .category("windturbine").range("parts") .stationId("SXJ_KGDL_GJY_FDC_STA").projectId("SXJ_KGDL_GJYF01_EG").lineId("SXJ_KGDL_GJYF01_LN").deviceId("SXJ_KGDL_GJY_F_WT_0001_EQ") .electricalId("").relatedParts("BJXT").createTime(DateUtils.truncate(previousDay)) .enable(true).uniformCode("AI091") .build() ); // 输出 ExcelUtils.write(response, "导入模板.xls", "导入模板", ProEconAlarmRule.class, list); } @PostMapping("/import") @ApiOperation(value = "导入", notes = "导入") public AjaxResult importExcel(@RequestParam("file") MultipartFile file) throws Exception { List list = ExcelUtils.read(file, ProEconAlarmRule.class); if (com.gyee.common.model.StringUtils.notEmp(list) && !list.isEmpty()) { boolean allCheck = true; for (ProEconAlarmRule alertrule : list) { String msg = ""; boolean result = true; if (StringUtils.isBlank(alertrule.getName())) { msg = "报警名称不能为空"; result = false; } else if (StringUtils.isBlank(alertrule.getDescription())) { msg = "规则描述不能为空"; result = false; } else if (StringUtils.isBlank(alertrule.getExpression())) { msg = "报警规则不能为空"; result = false; } else if (StringUtils.empty(alertrule.getRank())) { msg = "报警级别不能为空"; result = false; } else if (StringUtils.isBlank(alertrule.getStationId())) { msg = "风场不能为空"; result = false; } else if (StringUtils.isBlank(alertrule.getCategory())) { msg = "报警类别不能为空"; result = false; } else if (StringUtils.isBlank(alertrule.getUniformCode())) { msg = "统一编码不能为空"; result = false; } if (alertrule.getCategory().equals(AlarmCustomType.WT.getCode())) { if (StringUtils.isBlank(alertrule.getModelId())) { msg = "风机型号不能为空"; result = false; } } if (result != true) { allCheck = false; if (!result) { return AjaxResult.successData(AjaxStatus.error.code, msg); } } if (allCheck) { alarmRuleService.saveAndUpdateAlertrule(alertrule); } } } if (com.gyee.common.model.StringUtils.isNotNull(list)) { return AjaxResult.successData(AjaxStatus.success.code, list); } else { return AjaxResult.successData(AjaxStatus.error.code, "error"); } } }