doum
4 天以前 76b629eea5c6b8f8567656fe533e8aa4940b45e1
server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/JkCabinetGridServiceImpl.java
@@ -7,6 +7,9 @@
import com.doumee.core.dingTalk.DingTalkStream;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.haikang.model.HKConstants;
import com.doumee.core.jiandaoyun.api.arch.DeptApiClient;
import com.doumee.core.jiandaoyun.api.jdy.FormDataApiClient;
import com.doumee.core.jiandaoyun.model.form.FormDataCreateParam;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.DateUtil;
import com.doumee.dao.business.*;
@@ -28,12 +31,14 @@
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
@@ -62,6 +67,12 @@
    @Autowired
    private WarningEventMapper warningEventMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private InterfaceLogMapper interfaceLogMapper;
    @Autowired
    private MemberMapper memberMapper;
@@ -698,27 +709,125 @@
                        warningPush.setMemberNames(StringUtils.join(memberList.stream().map(i->i.getName()).collect(Collectors.toList()),",")
                        );
                        //todo 暂时关闭 钉钉通知
                        //            Boolean noticeFlag = dingTalk.workInfoOANotice(Long.valueOf(systemDictDataBiz.queryByCode(Constants.DD_TALK,Constants.AGENT_ID).getCode()),
                        //                    StringUtils.join(memberList.stream().filter(i->StringUtils.isNotBlank(i.getDdId())).map(i->i.getDdId()).collect(Collectors.toList()),","),
                        //                dingTalk.getAlarmNoticeMsg(warningPush.getRegion(),DateUtil.getCurrDateTime(),content));
                        //            warningPush.setStatus(noticeFlag?Constants.ONE:Constants.TWO);
                        if(Constants.equalsInteger(warningRule.getMemberNotice(),Constants.ONE)){
                            //            Boolean noticeFlag = dingTalk.workInfoOANotice(Long.valueOf(systemDictDataBiz.queryByCode(Constants.DD_TALK,Constants.AGENT_ID).getCode()),
                            //                    StringUtils.join(memberList.stream().filter(i->StringUtils.isNotBlank(i.getDdId())).map(i->i.getDdId()).collect(Collectors.toList()),","),
                            //                      dingTalk.getAlarmNoticeMsg(warningPush.getRegion(),DateUtil.getCurrDateTime(),content));
                            //            warningPush.setStatus(noticeFlag?Constants.ONE:Constants.TWO);
                            List<String> ddUserIdList = memberList.stream().filter(i->StringUtils.isNotBlank(i.getDdId())).map(i->i.getDdId()).collect(Collectors.toList());
                            if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(ddUserIdList)){
                                for (String ddUserId:ddUserIdList) {
                                    this.getJDYData(warningEvent,ddUserId,
                                            systemDictDataBiz.queryByCode(Constants.JDY_CONFIG,Constants.JDY_ENTRY_ID).getCode()
                                    );
                                }
                            }
                        }
                        warningPushMapper.insert(warningPush);
                        warningEvent.setStatus(Constants.ONE);
                        warningEventMapper.updateById(warningEvent);
                        this.sendDeviceNotice(warningRule);
                    }catch (Exception e){
                        warningEvent.setStatus(Constants.TWO);
                        warningEventMapper.updateById(warningEvent);
                    }
                }
            }
        }
    }
    public void sendDeviceNotice(WarningRule warningRule){
        if(Constants.equalsInteger(warningRule.getDeviceNotice(),Constants.ONE)
                &&StringUtils.isNotBlank(warningRule.getDeviceIds())
                &&StringUtils.isNotBlank(warningRule.getDeviceNoticeContent())){
            List<Device> deviceList = deviceMapper.selectList(new QueryWrapper<Device>()
                    .lambda()
                    .eq(Device::getStatus,Constants.ZERO)
                    .in(Device::getId,Arrays.asList(warningRule.getDeviceIds().split(",")))
            );
            if(CollectionUtils.isNotEmpty(deviceList)){
                for (Device device:deviceList) {
                    device.setSendInfo(warningRule.getDeviceNoticeContent());
                    DeviceServiceImpl.sendBroadcaseBobaoHttpBiz(device,interfaceLogMapper);
                }
            }
        }
    }
    public List<Integer> processWarnFront(Constants.WarningConfig warningConfig){
    /**
     * 简道云消息推送 定制
     * @param warningEvent
     * @param ddUserId 接收人 钉钉主键
     * @param entryId 表单ID
     * @throws IOException
     */
    public void getJDYData(WarningEvent warningEvent,String ddUserId , String entryId)  {
        try {
            log.error("简道云发送通知返回信息开始"  );
            FormDataApiClient apiClient = new FormDataApiClient(systemDictDataBiz.queryByCode(Constants.JDY_CONFIG, Constants.JDY_API_KEY).getCode()
                    ,systemDictDataBiz.queryByCode(Constants.JDY_CONFIG, Constants.JDY_API_HOST).getCode());
            FormDataCreateParam param  =new FormDataCreateParam();
            param.setData(this.getJdyChildData(warningEvent,ddUserId));
            param.setApp_id(systemDictDataBiz.queryByCode(Constants.JDY_CONFIG, Constants.JDY_APPID).getCode());
            param.setEntry_id(entryId);
            param.setIs_start_trigger(false);
            param.setIs_start_trigger(true);
            Map<String, Object>  response =  apiClient.singleDataCreate(param,apiClient.getDefaultVersion());
            /*
             Map<String,Object> map = new HashMap<>();
            map.put("app_id",appid);
            map.put("entry_id",entryId);
            map.put("is_start_workflow","false");
            map.put("is_start_trigger","true");
            map.put("data",this.getJdyChildData(warningEvent,ddUserId));
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, JSONObject.toJSONString(map));//"{\"data\":{\"notice_time\":{\"value\":\"2025-12-23 04:56:29\"},\"title\":{\"value\":\"跨越生产线\"},\"user\":{\"value\":\"1568490244651036\"},\"content\":{\"value\":\"跨越生产线-芜湖物流中心北围墙过道东\"},\"notice_type\":{\"value\":\"跨越生产线\"}},\"is_start_trigger\":\"true\",\"app_id\":\"6909d0fbdb93b068192dc4a9\",\"entry_id\":\"694a19ecf57e071788efc20b\",\"is_start_workflow\":\"false\"}");
            log.error("简道云发送通知请求信息" + JSONObject.toJSONString(map));
            Request request = new Request.Builder()
                    .url(systemDictDataBiz.queryByCode(Constants.JDY_CONFIG, Constants.JDY_API_HOST).getCode()+"v5/app/entry/data/create")
                    .method("POST", body)
                    .addHeader("Authorization", "Bearer "+jdyApiKey)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();*/
            log.error("简道云发送通知返回信息" + JSONObject.toJSONString(response));
        }catch (Exception e){
            log.error("简道云发送通知返回信息异常:" + e.getMessage());
        }
    }
    public Map<String,Object> getJdyChildData(WarningEvent warningEvent,String ddUserId){
        Map<String,Object> dataMap = new HashMap<>();
        Map<String,Object> title = new HashMap<>();
        title.put("value",warningEvent.getTitle());
        dataMap.put("title",title);
        Map<String,Object> user = new HashMap<>();
        user.put("value",ddUserId);
        dataMap.put("user",user);
        Map<String,Object> notice_time = new HashMap<>();
        notice_time.put("value",DateUtil.getCurrDateTime());
        dataMap.put("notice_time",notice_time);
        Map<String,Object> notice_type = new HashMap<>();
        notice_type.put("value",warningEvent.getTitle());
        dataMap.put("notice_type",notice_type);
        Map<String,Object> content = new HashMap<>();
        content.put("value",warningEvent.getContent());
        dataMap.put("content",content);
        return dataMap;
    }
   /* public List<Integer> processWarnFront(Constants.WarningConfig warningConfig){
        List<WarningRuleDetail> warningRuleDetailList = warningRuleDetailMapper.selectJoinList(WarningRuleDetail.class,new MPJLambdaWrapper<WarningRuleDetail>()
                .selectAll(WarningRuleDetail.class)
                .leftJoin(Warning.class,Warning::getId,WarningRuleDetail::getWarningId)
@@ -737,6 +846,9 @@
            List<Integer> resultList = new ArrayList<>();
            for (Integer ruleId:ruleIdSet) {
                WarningRule warningRule = warningRuleMapper.selectById(ruleId);
                if(Constants.equalsInteger(warningRule.getMemberNotice(),Constants.ZERO)&&Constants.equalsInteger(warningRule.getDeviceNotice(),Constants.ZERO)){
                    continue;
                }
                //查询其他规则信息
                List<WarningRuleDetail> warningRuleDetails = warningRuleDetailMapper.selectJoinList(WarningRuleDetail.class,new MPJLambdaWrapper<WarningRuleDetail>()
                        .selectAll(WarningRuleDetail.class)
@@ -789,8 +901,96 @@
        return new ArrayList<>();
    }*/
    public List<Integer> processWarnFrontWithEvent(Constants.WarningConfig warningConfig,  List<WarningEvent> returnEventList){
        List<WarningRuleDetail> warningRuleDetailList = warningRuleDetailMapper.selectJoinList(WarningRuleDetail.class,new MPJLambdaWrapper<WarningRuleDetail>()
                .selectAll(WarningRuleDetail.class)
                .leftJoin(Warning.class,Warning::getId,WarningRuleDetail::getWarningId)
                .eq(WarningRuleDetail::getIsdeleted,Constants.ZERO)
                .eq(Warning::getIsdeleted,Constants.ZERO)
                .eq(Warning::getStatus,Constants.ONE)
                .eq(Warning::getCode,warningConfig.getKey())
        );
            if(CollectionUtils.isEmpty(warningRuleDetailList)){
            return new ArrayList<>();
        }
        //所有报警规则配置信息
        List<Integer> ruleIdList = warningRuleDetailList.stream().map(i->i.getRuleId()).collect(Collectors.toList());
        Set<Integer> ruleIdSet = new HashSet<>(ruleIdList);
            if(CollectionUtils.isNotEmpty(ruleIdSet)){
            List<Integer> resultList = new ArrayList<>();
            for (Integer ruleId:ruleIdSet) {
                WarningRule warningRule = warningRuleMapper.selectById(ruleId);
                //查询其他规则信息
                List<WarningRuleDetail> warningRuleDetails = warningRuleDetailMapper.selectJoinList(WarningRuleDetail.class,new MPJLambdaWrapper<WarningRuleDetail>()
                        .selectAll(WarningRuleDetail.class)
                        .selectAs(Warning::getCode,WarningRuleDetail::getCode)
                        .leftJoin(Warning.class,Warning::getId,WarningRuleDetail::getWarningId)
                        .eq(WarningRuleDetail::getIsdeleted,Constants.ZERO)
                        .eq(Warning::getIsdeleted,Constants.ZERO)
                        .eq(Warning::getStatus,Constants.ONE)
                        .eq(WarningRuleDetail::getRuleId,ruleId)
//                        .in(Warning::getCode,Constants.WarningConfig.ALARM_TEST.getKey(),
//                                Constants.WarningConfig.GRID_TIME_OUT_INFO.getKey(),
//                                Constants.WarningConfig.KEY_TIME_OUT_BACK.getKey()
//                        )
                );
                //无任何规则 直接跳过
                if(CollectionUtils.isEmpty(warningRuleDetails)){
                    return resultList;
                }else if(!Constants.equalsInteger(warningRuleDetails.stream().filter(i->!i.getCode().equals(warningConfig.getKey()+"")).collect(Collectors.toList()).size(),Constants.ZERO)){
                    List<WarningRuleDetail> wList = warningRuleDetails.stream().filter(i->!i.getCode().equals(warningConfig.getKey()+"")).collect(Collectors.toList());
                    //获取所有其他所有事件主键信息
                    List<Integer> warningIdList = wList.stream().map(i->i.getWarningId()).collect(Collectors.toList());
                    Set<Integer> setWarningIdList = new HashSet<>(warningIdList);
                    //获取时间内所有事件信息
                    List<WarningEvent> warningEventList = warningEventMapper.selectList(new QueryWrapper<WarningEvent>().lambda()
                            .eq(WarningEvent::getIsdeleted,Constants.ZERO)
                            .in(WarningEvent::getWarningId,setWarningIdList)
                            .apply(Objects.nonNull(warningRule.getIntervalSec())," DATE_ADD(CREATE_DATE , INTERVAL "+warningRule.getIntervalSec()+" SECOND) > now() ")
                    );
                    if(this.processWarnFlag(wList,warningEventList)){
                        resultList.add(ruleId);
                        if(returnEventList != null){
                            returnEventList.addAll(warningEventList);
                        }
                    };
                }else{
                    List<WarningRuleDetail> wList = warningRuleDetails.stream().filter(i->i.getCode().equals(warningConfig.getKey()+"")).collect(Collectors.toList());
                    //获取本次事件主键信息
                    List<Integer> warningIdList = wList.stream().map(i->i.getWarningId()).collect(Collectors.toList());
                    Set<Integer> setWarningIdList = new HashSet<>(warningIdList);
                    if(Objects.isNull(warningRule.getIntervalSec())){
                        resultList.add(ruleId);
                    }else{
                        //获取时间内所有事件信息
                        List<WarningEvent> warningEventList = warningEventMapper.selectList(new QueryWrapper<WarningEvent>().lambda()
                                .eq(WarningEvent::getIsdeleted,Constants.ZERO)
                                .in(WarningEvent::getWarningId,setWarningIdList)
                                .apply(Objects.nonNull(warningRule.getIntervalSec())," DATE_ADD(CREATE_DATE , INTERVAL "+warningRule.getIntervalSec()+" SECOND) > now() ")
                        );
                        if(this.processWarnFlag(wList,warningEventList)){
                            resultList.add(ruleId);
                            if(returnEventList != null){
                                returnEventList.addAll(warningEventList);
                            }
                        }
                    }
                }
            }
            return resultList;
        }
            return new ArrayList<>();
    }
    public List<Integer> processWarnFront(Constants.WarningConfig warningConfig){
        return  this.processWarnFrontWithEvent(warningConfig,null);
    }
    /**
@@ -803,9 +1003,9 @@
            return false;
        }
        for (WarningRuleDetail warningRuleDetail:warningRuleDetails) {
            if(Objects.nonNull(warningRuleDetail.getRegionId())){
            if(Objects.nonNull(warningRuleDetail.getDeviceId())){
                if(warningEventList.stream().filter(i->Constants.equalsInteger(i.getWarningId(),warningRuleDetail.getWarningId())
                        && StringUtils.isNotBlank(i.getRegionId()) && i.getRegionId().equals(warningRuleDetail.getRegionId())
                        && StringUtils.isNotBlank(i.getDeviceId()) && i.getDeviceId().equals(warningRuleDetail.getDeviceId())
                ).collect(Collectors.toList()).size()<=Constants.ZERO){
                    return false;
                }
@@ -907,13 +1107,25 @@
                    warningPush.setMemberNames(StringUtils.join(memberList.stream().map(i->i.getName()).collect(Collectors.toList()),",")
                    );
                    //todo 暂时关闭 钉钉通知
        //            Boolean noticeFlag = dingTalk.workInfoOANotice(Long.valueOf(systemDictDataBiz.queryByCode(Constants.DD_TALK,Constants.AGENT_ID).getCode()),
        //                    StringUtils.join(memberList.stream().filter(i->StringUtils.isNotBlank(i.getDdId())).map(i->i.getDdId()).collect(Collectors.toList()),","),
        //                dingTalk.getAlarmNoticeMsg(warningPush.getRegion(),DateUtil.getCurrDateTime(),warningConfig.getInfo()));
        //            warningPush.setStatus(noticeFlag?Constants.ONE:Constants.TWO);
                    if(Constants.equalsInteger(warningRule.getMemberNotice(),Constants.ONE)){
                        //            Boolean noticeFlag = dingTalk.workInfoOANotice(Long.valueOf(systemDictDataBiz.queryByCode(Constants.DD_TALK,Constants.AGENT_ID).getCode()),
                        //                    StringUtils.join(memberList.stream().filter(i->StringUtils.isNotBlank(i.getDdId())).map(i->i.getDdId()).collect(Collectors.toList()),","),
                        //                dingTalk.getAlarmNoticeMsg(warningPush.getRegion(),DateUtil.getCurrDateTime(),warningConfig.getInfo()));
//                    warningPush.setStatus(noticeFlag?Constants.ONE:Constants.TWO);
                        List<String> ddUserIdList = memberList.stream().filter(i->StringUtils.isNotBlank(i.getDdId())).map(i->i.getDdId()).collect(Collectors.toList());
                        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(ddUserIdList)){
                            for (String ddUserId:ddUserIdList) {
                                this.getJDYData(warningEvent,ddUserId,
                                        systemDictDataBiz.queryByCode(Constants.JDY_CONFIG,Constants.JDY_ENTRY_ID).getCode()
                                );
                            }
                        }
                    }
                    warningPushMapper.insert(warningPush);
                    warningEvent.setStatus(Constants.ONE);
                    warningEventMapper.updateById(warningEvent);
                    this.sendDeviceNotice(warningRule);
                }catch (Exception e){
                    warningEvent.setStatus(Constants.TWO);
                    warningEventMapper.updateById(warningEvent);