rk
15 小时以前 2f65c9f0c9dae7c192a81a79d94b7eba614c8b23
server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/WarningServiceImpl.java
@@ -1,38 +1,75 @@
package com.doumee.service.business.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.dingTalk.DingTalk;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.utils.Constants;
import com.doumee.dao.business.model.Category;
import com.doumee.dao.business.model.PlatformGroup;
import com.doumee.core.utils.DateUtil;
import com.doumee.core.utils.WeatherUtil;
import com.doumee.dao.business.*;
import com.doumee.dao.business.dao.MemberMapper;
import com.doumee.dao.business.model.*;
import com.doumee.dao.business.vo.CabinetBoardWaningRataDataVO;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.service.business.third.model.LoginUserInfo;
import com.doumee.service.business.third.model.PageData;
import com.doumee.service.business.third.model.PageWrap;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.WarningMapper;
import com.doumee.dao.business.model.Warning;
import com.doumee.service.business.WarningService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
 * 报警记录信息表Service实现
 * @author 江蹄蹄
 * @date 2025/09/28 09:01
 */
@Slf4j
@Service
public class WarningServiceImpl implements WarningService {
    @Autowired
    private WarningMapper warningMapper;
    @Autowired
    private WarningRuleMapper warningRuleMapper;
    @Autowired
    private WarningRuleDetailMapper warningRuleDetailMapper;
    @Autowired
    private WarningPushMapper warningPushMapper;
    @Autowired
    private WarningEventMapper warningEventMapper;
    @Autowired
    private DingTalk dingTalk;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
    @Autowired
    private DdNoticeConfigMapper ddNoticeConfigMapper;
    @Override
    public Integer create(Warning warning) {
@@ -118,6 +155,7 @@
    @Override
    public List<Warning> findList(Warning warning) {
        warning.setIsdeleted(Constants.ZERO);
        QueryWrapper<Warning> wrapper = new QueryWrapper<>(warning);
        return warningMapper.selectList(wrapper);
    }
@@ -212,4 +250,173 @@
        model.setEditor(param.getLoginUserInfo().getId());
        warningMapper.updateById(model);
    }
    /**
     * 定时获取天气信息执行
     */
    @Override
    public void getWeatherInfo(){
        try{
            String response = WeatherUtil.getWeatherWarningInfo(systemDictDataBiz.queryByCode(Constants.WEATHER_CONFIG,Constants.REQUEST_URL).getCode(),
                    systemDictDataBiz.queryByCode(Constants.WEATHER_CONFIG,Constants.API_KEY).getCode(),
                    systemDictDataBiz.queryByCode(Constants.WEATHER_CONFIG,Constants.LOCATION).getCode());
            if(StringUtils.isBlank(response)){
                log.error("无返回数据");
            }
            JSONObject jsonObject = JSONObject.parseObject(response);
            if(jsonObject.getString("code").equals("200")){
                if(jsonObject.containsKey("warning")){
                    Warning warning = warningMapper.selectOne(new QueryWrapper<Warning>().lambda()
                            .eq(Warning::getType,Constants.FOUR).eq(Warning::getIsdeleted,Constants.ZERO).last("limt 1"));
                    if(Objects.isNull(warning)){
                        return;
                    }
                    //查询通知人员
                    List<WarningRule> warningRuleList = warningRuleMapper.selectList(new QueryWrapper<WarningRule>().lambda()
                            .eq(WarningRule::getIsdeleted,Constants.ZERO)
                            .apply(" id in ( select w.RULE_ID from warning_rule_detail w where w.WARNING_ID = '"+warning.getId()+"' and w.ISDELETED = 0  ) ")
                    );
                    List<Member> memberList = new ArrayList<>();
                    if(org.apache.commons.collections.CollectionUtils.isNotEmpty(warningRuleList)){
                        List<String> memberIdStr =  warningRuleList.stream().map(i->i.getMemberIds()).collect(Collectors.toList());
                        if(org.apache.commons.collections.CollectionUtils.isNotEmpty(memberIdStr)){
                            List<String> memberIdList = new ArrayList<>();
                            for (String s:memberIdStr) {
                                memberIdList.addAll(
                                        Arrays.asList(s.split(","))
                                );
                            }
                            memberList = memberMapper.selectList(new QueryWrapper<Member>().lambda().eq(Member::getIsdeleted,Constants.ZERO)
                                    .notIn(Member::getErpId)
                                    .in(Member::getId,memberIdList)
                            );
                        }
                    }
                    JSONArray jsonArray = jsonObject.getJSONArray("warning");
                    for (int i = 0; i < jsonArray.size(); i++) {
                        List<WarningPush> warningPushList = new ArrayList<>();
                        JSONObject  weatherInfo = jsonArray.getJSONObject(i);
                        String code = weatherInfo.getString("id");
                        String title = weatherInfo.getString("title");
                        String text = weatherInfo.getString("text");
                        //存在预警 则存储预警信息
                        if(warningEventMapper.selectCount(new QueryWrapper<WarningEvent>()
                                .lambda()
                                .eq(WarningEvent::getEventId,code)
                        )>Constants.ZERO){
                            continue;
                        }
                        WarningEvent warningEvent = new WarningEvent();
                        warningEvent.setCreateDate(new Date());
                        warningEvent.setIsdeleted(Constants.ZERO);
                        warningEvent.setWarningId(warning.getId());
                        warningEvent.setTitle(title);
                        warningEvent.setContent(text);
                        warningEvent.setEventId(code);
                        warningEvent.setHappenTime(DateUtil.getCurrDateTime());
                        warningEvent.setJsonContent(weatherInfo.toJSONString());
                        warningEventMapper.insert(warningEvent);
                        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(memberList)&&memberList.size()>Constants.ZERO){
                            List<String> ddUserIdList = memberList.stream().map(j->j.getErpId()).collect(Collectors.toList());
                            //发送推送钉钉消息
                            Boolean isSuccess = dingTalk.workInfoOANotice(Long.valueOf(systemDictDataBiz.queryByCode(Constants.DD_TALK,Constants.AGENT_ID).getCode()),
                                    String.join(",",ddUserIdList),
                                    ddNoticeConfigMapper.selectCount(new QueryWrapper<DdNoticeConfig>().lambda()
                                            .eq(DdNoticeConfig::getIsdeleted,Constants.ZERO).eq(DdNoticeConfig::getStatus,Constants.ZERO)
                                            .eq(DdNoticeConfig::getObjType,13))>Constants.ZERO?
                                    dingTalk.getWeatherNoticeMsg(title,text):null);
                            for (Member member:memberList) {
                                WarningPush warningPush = new WarningPush();
                                warningPush.setCreateDate(new Date());
                                warningPush.setWarningId(warning.getId());
                                warningPush.setTitle(warningEvent.getTitle());
                                warningPush.setContent(warningEvent.getContent());
                                warningPush.setStatus(isSuccess?Constants.ONE:Constants.TWO);
                                warningPush.setPushType(Constants.ZERO);
                                warningPush.setMemberId(member.getId());
                                warningPushList.add(warningPush);
                            }
                        }
                        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(warningPushList)){
                            warningPushMapper.insert(warningPushList);
                        }
                    }
                }
            }
        }catch (Exception e){
        }
    }
    @Override
    public PageData<WarningEvent> getCabinetBoardWarningPage(PageWrap<WarningEvent> pageWrap){
        IPage<WarningEvent> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        Utils.MP.blankToNull(pageWrap.getModel());
        IPage<WarningEvent> pageData = warningEventMapper.selectJoinPage(page,WarningEvent.class,
                new MPJLambdaWrapper<WarningEvent>()
                        .selectAll(WarningEvent.class)
                        .selectAs(SystemUser::getRealname,WarningEvent::getCreateUserName)
                        .leftJoin(SystemUser.class,SystemUser::getId,WarningEvent::getCreator)
                        .leftJoin(Warning.class,Warning::getId,WarningEvent::getWarningId)
                        .eq(WarningEvent::getIsdeleted,Constants.ZERO)
                        .in(Warning::getCode,Constants.WarningConfig.KEY_TIME_OUT_BACK.getKey(),Constants.WarningConfig.ALARM_TEST.getKey(),Constants.WarningConfig.GRID_TIME_OUT_INFO.getKey())
                        .like(WarningEvent::getCreateDate,DateUtil.getCurrDate())
                        .orderByDesc(WarningEvent::getId)
        );
        return PageData.from(pageData);
    }
    @Override
    public CabinetBoardWaningRataDataVO getCabinetBoardWarningRataData(Integer type){
        CabinetBoardWaningRataDataVO vo = new CabinetBoardWaningRataDataVO();
        vo.setAlcoholNum(Constants.ZERO);
        vo.setUnCloseNum(Constants.ZERO);
        vo.setTimeOutNum(Constants.ZERO);
        List<WarningEvent> warningEventList = warningEventMapper.selectJoinList(WarningEvent.class,
                new MPJLambdaWrapper<WarningEvent>().
                selectAll(WarningEvent.class)
                .selectAs(Warning::getCode,WarningEvent::getWarningId)
                .leftJoin(Warning.class,Warning::getId,WarningEvent::getWarningCode)
                .eq(WarningEvent::getIsdeleted,Constants.ZERO)
                .in(Warning::getCode,Constants.WarningConfig.KEY_TIME_OUT_BACK.getKey(),Constants.WarningConfig.ALARM_TEST.getKey(),Constants.WarningConfig.GRID_TIME_OUT_INFO.getKey())
                .like(Objects.nonNull(type)&&Constants.equalsInteger(Constants.ZERO,type),WarningEvent::getCreateDate,DateUtil.getCurrDate())
                .like(Objects.nonNull(type)&&Constants.equalsInteger(Constants.ONE,type),WarningEvent::getCreateDate,DateUtil.getFomartDate(new Date(),"yyyy-MM"))
                .like(Objects.nonNull(type)&&Constants.equalsInteger(Constants.TWO,type),WarningEvent::getCreateDate,DateUtil.getFomartDate(new Date(),"yyyy"))
        );
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(warningEventList)){
            vo.setAlcoholNum(
                    warningEventList.stream().filter(i->StringUtils.isNotBlank(i.getWarningCode())&&
                            i.getWarningCode().equals(Constants.WarningConfig.ALARM_TEST.getKey())).collect(Collectors.toList()).size()
            );
            vo.setUnCloseNum(
                    warningEventList.stream().filter(i->StringUtils.isNotBlank(i.getWarningCode())&&
                            i.getWarningCode().equals(Constants.WarningConfig.GRID_TIME_OUT_INFO.getKey())).collect(Collectors.toList()).size()
            );
            vo.setTimeOutNum(
                    warningEventList.stream().filter(i->StringUtils.isNotBlank(i.getWarningCode())&&
                            i.getWarningCode().equals(Constants.WarningConfig.KEY_TIME_OUT_BACK.getKey())).collect(Collectors.toList()).size()
            );
        }
        return vo;
    }
}