MrShi
2 天以前 eb82684152ffb0acddf67da92e4533a0190eb258
server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/WarningServiceImpl.java
@@ -1,59 +1,128 @@
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.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.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 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.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;
    @Override
    public Integer create(Warning warning) {
        if(warningMapper.selectCount(new QueryWrapper<Warning>().lambda()
                .eq(Warning::getIsdeleted,Constants.ZERO)
                .eq(Warning::getName,warning.getName()) )>0){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,事件类型已存在,请返回刷新页面查看!");
        }
        warning.setStatus(Constants.formatIntegerNum(warning.getStatus()));
        warning.setIsdeleted(Constants.ZERO);
        warning.setCreator(warning.getLoginUserInfo().getId());
        warning.setCreateDate(new Date());
        warning.setSubscribeStatus(Constants.ZERO);
        warning.setEditDate(warning.getCreateDate());
        warning.setEditor(warning.getCreator());
        warningMapper.insert(warning);
        return warning.getId();
    }
    @Override
    public void deleteById(Integer id) {
        warningMapper.deleteById(id);
    public void deleteById(Integer id, LoginUserInfo user) {
        warningMapper.update(null,new UpdateWrapper<Warning>().lambda()
                .set(Warning::getIsdeleted,Constants.ONE)
                .set(Warning::getEditor,user.getId())
                .set(Warning::getEditDate,new Date())
                .eq(Warning::getId,id)
        );
    }
    @Override
    public void delete(Warning warning) {
    public void delete(Warning warning ) {
        UpdateWrapper<Warning> deleteWrapper = new UpdateWrapper<>(warning);
        warningMapper.delete(deleteWrapper);
    }
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
    public void deleteByIdInBatch(List<Integer> ids, LoginUserInfo user) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        warningMapper.deleteBatchIds(ids);
        warningMapper.update(null,new UpdateWrapper<Warning>().lambda()
                .set(Warning::getEditor,user.getId())
                .set(Warning::getEditDate,new Date())
                .set(Warning::getIsdeleted,Constants.ONE)
                .in(Warning::getId,ids)
        );
    }
    @Override
    public void updateById(Warning warning) {
        if(warningMapper.selectCount(new QueryWrapper<Warning>().lambda()
                .ne(Warning::getId,warning.getId())
                .eq(Warning::getIsdeleted,Constants.ZERO)
                .eq(Warning::getName,warning.getName()) )>0){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,事件类型已存在,请返回刷新页面查看!");
        }
        warning.setEditDate(warning.getCreateDate());
        warning.setEditor(warning.getCreator());
        warningMapper.updateById(warning);
    }
@@ -80,6 +149,7 @@
    @Override
    public List<Warning> findList(Warning warning) {
        warning.setIsdeleted(Constants.ZERO);
        QueryWrapper<Warning> wrapper = new QueryWrapper<>(warning);
        return warningMapper.selectList(wrapper);
    }
@@ -89,6 +159,7 @@
        IPage<Warning> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<Warning> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        pageWrap.getModel().setIsdeleted(Constants.ZERO);
        if (pageWrap.getModel().getId() != null) {
            queryWrapper.lambda().eq(Warning::getId, pageWrap.getModel().getId());
        }
@@ -110,7 +181,7 @@
            queryWrapper.lambda().eq(Warning::getIsdeleted, pageWrap.getModel().getIsdeleted());
        }
        if (pageWrap.getModel().getName() != null) {
            queryWrapper.lambda().eq(Warning::getName, pageWrap.getModel().getName());
            queryWrapper.lambda().like(Warning::getName, pageWrap.getModel().getName());
        }
        if (pageWrap.getModel().getInfo() != null) {
            queryWrapper.lambda().eq(Warning::getInfo, pageWrap.getModel().getInfo());
@@ -149,13 +220,8 @@
        if (pageWrap.getModel().getSubscribeMemberId() != null) {
            queryWrapper.lambda().eq(Warning::getSubscribeMemberId, pageWrap.getModel().getSubscribeMemberId());
        }
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        queryWrapper.lambda().orderByDesc(Warning::getSortnum);
        return PageData.from(warningMapper.selectPage(page, queryWrapper));
    }
@@ -164,4 +230,121 @@
        QueryWrapper<Warning> wrapper = new QueryWrapper<>(warning);
        return warningMapper.selectCount(wrapper);
    }
    @Override
    public  void updateStatus(Warning param) {
        Warning model = warningMapper.selectById(param.getId());
        if (model==null || Constants.equalsInteger(model.getIsdeleted(), Constants.ONE)) {
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(Constants.equalsInteger(model.getStatus(),param.getStatus())){
            return;
        }
        model.setStatus(param.getStatus());
        model.setEditDate(new Date());
        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),
                                    dingTalk.getWeatherNoticeMsg(title,text));
                            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){
        }
    }
}