From 3a154bdb0a5aaa2c0ac3eac95a6ba747068bd454 Mon Sep 17 00:00:00 2001
From: MrShi <1878285526@qq.com>
Date: 星期二, 13 一月 2026 10:00:37 +0800
Subject: [PATCH] 优化

---
 server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/WarningServiceImpl.java |  460 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 460 insertions(+), 0 deletions(-)

diff --git a/server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/WarningServiceImpl.java b/server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/WarningServiceImpl.java
new file mode 100644
index 0000000..da0bdca
--- /dev/null
+++ b/server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/WarningServiceImpl.java
@@ -0,0 +1,460 @@
+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.haikang.model.HKConstants;
+import com.doumee.core.haikang.model.param.BaseResponse;
+import com.doumee.core.haikang.model.param.request.EventSubRequest;
+import com.doumee.core.haikang.service.HKService;
+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.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.*;
+import java.util.stream.Collectors;
+
+/**
+ * 鎶ヨ璁板綍淇℃伅琛⊿ervice瀹炵幇
+ * @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) {
+        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(),"瀵逛笉璧凤紝浜嬩欢绫诲瀷宸插瓨鍦紝璇疯繑鍥炲埛鏂伴〉闈㈡煡鐪嬶紒");
+        }
+
+        if(StringUtils.isNotBlank(warning.getCode())&&Constants.equalsInteger(warning.getType(),Constants.ZERO)||Constants.equalsInteger(warning.getType(),Constants.ONE)){
+            EventSubRequest eventParam = new EventSubRequest();
+            String path =systemDictDataBiz.queryByCode(Constants.HK_PARAM, Constants.HK_PUSH_URL).getCode();
+            eventParam.setEventDest(path+"/notice");
+            try {
+                eventParam.setEventTypes(new Integer[]{Integer.valueOf(warning.getCode())});
+            }catch (Exception e){
+                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"浜嬩欢缂栫爜閿欒锛�");
+            }
+            HKService.cancelEventSub(eventParam);//鍏堝彇娑�
+            if(Constants.equalsInteger(warning.getStatus(),Constants.ONE)){
+                HKService.eventSub(eventParam);//瀹夐槻浜嬩欢
+            }
+        }
+
+
+        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, LoginUserInfo user) {
+        Warning warning = warningMapper.selectById(id);
+        if(Objects.isNull(warning)){
+            throw new BusinessException(ResponseStatus.DATA_EMPTY);
+        }
+
+        if(StringUtils.isNotBlank(warning.getCode())&&Constants.equalsInteger(warning.getType(),Constants.ZERO)||Constants.equalsInteger(warning.getType(),Constants.ONE)){
+            EventSubRequest eventParam = new EventSubRequest();
+            String path =systemDictDataBiz.queryByCode(Constants.HK_PARAM, Constants.HK_PUSH_URL).getCode();
+            eventParam.setEventDest(path+"/notice");
+            try {
+                eventParam.setEventTypes(new Integer[]{Integer.valueOf(warning.getCode())});
+            }catch (Exception e){
+                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"浜嬩欢缂栫爜閿欒锛�");
+            }
+            HKService.cancelEventSub(eventParam);//鍏堝彇娑�
+        }
+
+        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 ) {
+        UpdateWrapper<Warning> deleteWrapper = new UpdateWrapper<>(warning);
+        warningMapper.delete(deleteWrapper);
+    }
+
+    @Override
+    public void deleteByIdInBatch(List<Integer> ids, LoginUserInfo user) {
+        if (CollectionUtils.isEmpty(ids)) {
+            return;
+        }
+        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(),"瀵逛笉璧凤紝浜嬩欢绫诲瀷宸插瓨鍦紝璇疯繑鍥炲埛鏂伴〉闈㈡煡鐪嬶紒");
+        }
+
+        if(StringUtils.isNotBlank(warning.getCode())&&Constants.equalsInteger(warning.getType(),Constants.ZERO)||Constants.equalsInteger(warning.getType(),Constants.ONE)){
+            EventSubRequest eventParam = new EventSubRequest();
+            String path =systemDictDataBiz.queryByCode(Constants.HK_PARAM, Constants.HK_PUSH_URL).getCode();
+            eventParam.setEventDest(path+"/notice");
+            try {
+                eventParam.setEventTypes(new Integer[]{Integer.valueOf(warning.getCode())});
+            }catch (Exception e){
+                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"浜嬩欢缂栫爜閿欒锛�");
+            }
+            HKService.cancelEventSub(eventParam);//鍏堝彇娑�
+            if(Constants.equalsInteger(warning.getStatus(),Constants.ONE)){
+                HKService.eventSub(eventParam);//瀹夐槻浜嬩欢
+            }
+        }
+
+        warning.setEditDate(warning.getCreateDate());
+        warning.setEditor(warning.getCreator());
+        warningMapper.updateById(warning);
+    }
+
+    @Override
+    public void updateByIdInBatch(List<Warning> warnings) {
+        if (CollectionUtils.isEmpty(warnings)) {
+            return;
+        }
+        for (Warning warning: warnings) {
+            this.updateById(warning);
+        }
+    }
+
+    @Override
+    public Warning findById(Integer id) {
+        return warningMapper.selectById(id);
+    }
+
+    @Override
+    public Warning findOne(Warning warning) {
+        QueryWrapper<Warning> wrapper = new QueryWrapper<>(warning);
+        return warningMapper.selectOne(wrapper);
+    }
+
+    @Override
+    public List<Warning> findList(Warning warning) {
+        warning.setIsdeleted(Constants.ZERO);
+        QueryWrapper<Warning> wrapper = new QueryWrapper<>(warning);
+        return warningMapper.selectList(wrapper);
+    }
+  
+    @Override
+    public PageData<Warning> findPage(PageWrap<Warning> pageWrap) {
+        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());
+        }
+        if (pageWrap.getModel().getCreator() != null) {
+            queryWrapper.lambda().eq(Warning::getCreator, pageWrap.getModel().getCreator());
+        }
+        if (pageWrap.getModel().getCreateDate() != null) {
+            queryWrapper.lambda().ge(Warning::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateDate()));
+            queryWrapper.lambda().le(Warning::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateDate()));
+        }
+        if (pageWrap.getModel().getEditor() != null) {
+            queryWrapper.lambda().eq(Warning::getEditor, pageWrap.getModel().getEditor());
+        }
+        if (pageWrap.getModel().getEditDate() != null) {
+            queryWrapper.lambda().ge(Warning::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate()));
+            queryWrapper.lambda().le(Warning::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate()));
+        }
+        if (pageWrap.getModel().getIsdeleted() != null) {
+            queryWrapper.lambda().eq(Warning::getIsdeleted, pageWrap.getModel().getIsdeleted());
+        }
+        if (pageWrap.getModel().getName() != null) {
+            queryWrapper.lambda().like(Warning::getName, pageWrap.getModel().getName());
+        }
+        if (pageWrap.getModel().getInfo() != null) {
+            queryWrapper.lambda().eq(Warning::getInfo, pageWrap.getModel().getInfo());
+        }
+        if (pageWrap.getModel().getType() != null) {
+            queryWrapper.lambda().eq(Warning::getType, pageWrap.getModel().getType());
+        }
+        if (pageWrap.getModel().getObjId() != null) {
+            queryWrapper.lambda().eq(Warning::getObjId, pageWrap.getModel().getObjId());
+        }
+        if (pageWrap.getModel().getObjType() != null) {
+            queryWrapper.lambda().eq(Warning::getObjType, pageWrap.getModel().getObjType());
+        }
+        if (pageWrap.getModel().getCode() != null) {
+            queryWrapper.lambda().eq(Warning::getCode, pageWrap.getModel().getCode());
+        }
+        if (pageWrap.getModel().getSortnum() != null) {
+            queryWrapper.lambda().eq(Warning::getSortnum, pageWrap.getModel().getSortnum());
+        }
+        if (pageWrap.getModel().getLevel() != null) {
+            queryWrapper.lambda().eq(Warning::getLevel, pageWrap.getModel().getLevel());
+        }
+        if (pageWrap.getModel().getStatus() != null) {
+            queryWrapper.lambda().eq(Warning::getStatus, pageWrap.getModel().getStatus());
+        }
+        if (pageWrap.getModel().getSubscribeStatus() != null) {
+            queryWrapper.lambda().eq(Warning::getSubscribeStatus, pageWrap.getModel().getSubscribeStatus());
+        }
+        if (pageWrap.getModel().getSubscribeDate() != null) {
+            queryWrapper.lambda().ge(Warning::getSubscribeDate, Utils.Date.getStart(pageWrap.getModel().getSubscribeDate()));
+            queryWrapper.lambda().le(Warning::getSubscribeDate, Utils.Date.getEnd(pageWrap.getModel().getSubscribeDate()));
+        }
+        if (pageWrap.getModel().getSubscribeInfo() != null) {
+            queryWrapper.lambda().eq(Warning::getSubscribeInfo, pageWrap.getModel().getSubscribeInfo());
+        }
+        if (pageWrap.getModel().getSubscribeMemberId() != null) {
+            queryWrapper.lambda().eq(Warning::getSubscribeMemberId, pageWrap.getModel().getSubscribeMemberId());
+        }
+        queryWrapper.lambda().orderByDesc(Warning::getSortnum);
+
+        return PageData.from(warningMapper.selectPage(page, queryWrapper));
+    }
+
+    @Override
+    public long count(Warning warning) {
+        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;
+        }
+
+        if(StringUtils.isNotBlank(model.getCode())&&(Constants.equalsInteger(model.getType(),Constants.ZERO)||Constants.equalsInteger(model.getType(),Constants.ONE))){
+            EventSubRequest eventParam = new EventSubRequest();
+            String path =systemDictDataBiz.queryByCode(Constants.HK_PARAM, Constants.HK_PUSH_URL).getCode();
+            eventParam.setEventDest(path+"/notice");
+            try {
+                eventParam.setEventTypes(new Integer[]{Integer.valueOf(model.getCode())});
+            }catch (Exception e){
+                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"浜嬩欢缂栫爜閿欒锛�");
+            }
+            HKService.cancelEventSub(eventParam);//鍏堝彇娑�
+            if(Constants.equalsInteger(param.getStatus(),Constants.ONE)){
+                BaseResponse response =  HKService.eventSub(eventParam);//瀹夐槻浜嬩欢
+                if(Objects.nonNull(response)&&StringUtils.equals(response.getCode(), HKConstants.RESPONSE_SUCCEE)){
+                    model.setSubscribeStatus(Constants.ONE);
+                }else{
+                    model.setSubscribeStatus(Constants.TWO);
+                    model.setSubscribeInfo(response.getMsg());
+                }
+
+               startCheckThirdDest(eventParam);
+
+            }else{
+                model.setSubscribeStatus(Constants.ZERO);
+            }
+        }
+        model.setStatus(param.getStatus());
+        model.setEditDate(new Date());
+        model.setEditor(param.getLoginUserInfo().getId());
+        warningMapper.updateById(model);
+    }
+
+    /**
+     * 妫�鏌ヤ笁鏂规槸鍚﹂渶瑕佽闃呮捣搴蜂簨浠�
+     * @param eventParam
+     */
+
+    private void startCheckThirdDest(EventSubRequest eventParam) {
+        try {
+            String thirdpath =systemDictDataBiz.queryByCode(Constants.HK_PARAM, Constants.HK_PUSH_THIRD_URL).getCode();
+            if(StringUtils.isNotBlank(thirdpath)){
+                String[] ss = thirdpath.split(";");
+                for(String s : ss){
+                    eventParam.setEventDest(s);
+                    BaseResponse response =   HKService.eventSub(eventParam);
+                    if(Objects.nonNull(response)&&StringUtils.equals(response.getCode(), HKConstants.RESPONSE_SUCCEE)){
+                        log.error("========璁㈤槄鍏朵粬涓夋柟鍦板潃鎴愬姛:"+s);
+                    }else{
+                        log.error("========璁㈤槄鍏朵粬涓夋柟鍦板潃閿欒:"+s);
+                    }
+                }
+            }
+        }catch (Exception e){
+        log.error("========璁㈤槄鍏朵粬涓夋柟鍦板潃閿欒"+e.getMessage());
+        }
+
+    }
+
+
+    /**
+     * 瀹氭椂鑾峰彇澶╂皵淇℃伅鎵ц
+     */
+    @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){
+
+        }
+
+    }
+
+
+
+}

--
Gitblit v1.9.3