yangys
2024-04-02 6bed83e92f67954cd2135071133329f2205efe4f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package com.qianwen.smartman.modules.notify.service.impl;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qianwen.core.cache.utils.CacheUtil;
import com.qianwen.core.log.exception.ServiceException;
import com.qianwen.core.mp.base.BaseServiceImpl;
import com.qianwen.core.notify.DefaultNotifyType;
import com.qianwen.core.notify.NotifyType;
import com.qianwen.core.notify.notifier.NotifierProvider;
import com.qianwen.core.tool.metadata.ConfigMetadata;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.smartman.common.constant.CommonConstant;
import com.qianwen.smartman.common.constant.NotifyConstant;
import com.qianwen.smartman.common.utils.MessageUtils;
import com.qianwen.smartman.modules.notify.api.DingTalkApi;
import com.qianwen.smartman.modules.notify.dto.NotifyTypeInfo;
import com.qianwen.smartman.modules.notify.dto.ProviderInfo;
import com.qianwen.smartman.modules.notify.entity.BusinessNotify;
import com.qianwen.smartman.modules.notify.entity.NotifyConfigEntity;
import com.qianwen.smartman.modules.notify.mapper.BusinessNotifyMapper;
import com.qianwen.smartman.modules.notify.mapper.NotifyConfigMapper;
import com.qianwen.smartman.modules.notify.service.INotifyConfigService;
import com.qianwen.smartman.modules.sync.constant.OuterAppConfigConstant;
import com.qianwen.smartman.modules.sync.entity.OuterAppConfig;
import com.qianwen.smartman.modules.sync.service.IOuterAppConfigService;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
 
@Service
/* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/modules/notify/service/impl/NotifyConfigServiceImpl.class */
public class NotifyConfigServiceImpl extends BaseServiceImpl<NotifyConfigMapper, NotifyConfigEntity> implements INotifyConfigService {
    private static final Logger log = LoggerFactory.getLogger(NotifyConfigServiceImpl.class);
    private final BusinessNotifyMapper notifyBusinessMapper;
    private final List<NotifierProvider> providers;
    private final DingTalkApi dingTalkApi;
    private final IOuterAppConfigService outerAppConfigService;
 
    public NotifyConfigServiceImpl(BusinessNotifyMapper notifyBusinessMapper, List<NotifierProvider> providers, @Lazy DingTalkApi dingTalkApi, @Lazy IOuterAppConfigService outerAppConfigService) {
        this.notifyBusinessMapper = notifyBusinessMapper;
        this.providers = providers;
        this.dingTalkApi = dingTalkApi;
        this.outerAppConfigService = outerAppConfigService;
    }
 
    @Override // org.springblade.modules.notify.service.INotifyConfigService
    public void updateServerConfig(OuterAppConfig outerAppConfig) {
        Integer appType = outerAppConfig.getAppType();
        if (appType.intValue() == 1) {
            Object agentid = outerAppConfig.getWxAgentId();
            Object corpid = outerAppConfig.getWxCorpId();
            Object corpsecret = outerAppConfig.getWxAppSecret();
            Map<String, Object> data = new HashMap<>();
            data.put("agentid", agentid);
            data.put("corpid", corpid);
            data.put("corpsecret", corpsecret);
            NotifyConfigEntity config = getOne(Wrappers.<NotifyConfigEntity>lambdaQuery()
                    .eq(NotifyConfigEntity::getStatus, 1).eq(NotifyConfigEntity::getType, DefaultNotifyType.weiXinQY.name()));
            /*
            NotifyConfigEntity config = (NotifyConfigEntity) getOne((Wrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
                return v0.getStatus();
            }, 1)).eq((v0) -> {
                return v0.getType();
            }, DefaultNotifyType.weiXinQY.name()));*/
            if (config == null) {
                config = new NotifyConfigEntity();
                config.setName(DefaultNotifyType.weiXinQY.getName());
                config.setType(DefaultNotifyType.weiXinQY.name());
                config.setProvider("qyTextMessage");
                config.setStatus(1);
                config.setConfiguration(data);
            } else {
                config.setConfiguration(data);
            }
            saveOrUpdateConfig(config);
        } else if (appType.intValue() == 2) {
            Object appKey = outerAppConfig.getDingAppKey();
            Object appSecret = outerAppConfig.getDingAppSecret();
            Map<String, Object> data2 = new HashMap<>();
            data2.put("appKey", appKey);
            data2.put("appSecret", appSecret);
            NotifyConfigEntity config2 = getOne(Wrappers.<NotifyConfigEntity>lambdaQuery().eq(NotifyConfigEntity::getStatus, 1)
                    .eq(NotifyConfigEntity::getType, DefaultNotifyType.dingTalk.name()));
            /*
            NotifyConfigEntity config2 = (NotifyConfigEntity) getOne((Wrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
                return v0.getStatus();
            }, 1)).eq((v0) -> {
                return v0.getType();
            }, DefaultNotifyType.dingTalk.name()));*/
            if (config2 == null) {
                config2 = new NotifyConfigEntity();
                config2.setName(DefaultNotifyType.dingTalk.getName());
                config2.setType(DefaultNotifyType.dingTalk.name());
                config2.setProvider("dingTalkMessage");
                config2.setStatus(1);
                config2.setConfiguration(data2);
            } else {
                config2.setConfiguration(data2);
            }
            saveOrUpdateConfig(config2);
        }
    }
 
    public boolean checkMatchingByOuterAppConfig(String notifyType) {
        if (Func.equals(notifyType, DefaultNotifyType.dingTalk.name())) {
            OuterAppConfig outerAppConfigDing = this.outerAppConfigService.getAppConfig(OuterAppConfigConstant.DING);
            if (Func.isNotEmpty(outerAppConfigDing) && outerAppConfigDing.getStatus().equals(CommonConstant.ENABLE)) {
                NotifyConfigEntity config = getOne(Wrappers.<NotifyConfigEntity>lambdaQuery()
                        .eq(NotifyConfigEntity::getType, DefaultNotifyType.dingTalk.name()).eq(NotifyConfigEntity::getStatus, 1));
                /*
                NotifyConfigEntity config = (NotifyConfigEntity) getOne((Wrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
                    return v0.getType();
                }, DefaultNotifyType.dingTalk.name())).eq((v0) -> {
                    return v0.getStatus();
                }, 1));*/
                Map<String, Object> configuration = config.getConfiguration();
                if (!configuration.containsKey("appKey") || !configuration.containsKey("appSecret")) {
                    updateServerConfig(outerAppConfigDing);
                    return true;
                }
                Object appKey = configuration.get("appKey");
                Object appSecret = configuration.get("appSecret");
                String dingAppKey = outerAppConfigDing.getDingAppKey();
                String dingAppSecret = outerAppConfigDing.getDingAppSecret();
                if (!Func.equals(appKey, dingAppKey) || !Func.equals(appSecret, dingAppSecret)) {
                    updateServerConfig(outerAppConfigDing);
                    return true;
                }
                return true;
            }
            return false;
        }
        return false;
    }
 
    @Override // org.springblade.modules.notify.service.INotifyConfigService
    public NotifyConfigEntity saveOrUpdateConfig(NotifyConfigEntity notificationConfig) {
        if (Func.isNotEmpty(notificationConfig.getId())) {
            CacheUtil.evict("blade:notify", "notifier:id:", notificationConfig.getId(), false);
        }
        long count = count(Wrappers.<NotifyConfigEntity>lambdaQuery()
                .ne(Func.isNotEmpty(notificationConfig.getId()), NotifyConfigEntity::getId, notificationConfig.getId())
                .eq(NotifyConfigEntity::getType, notificationConfig.getType()));
        /*
        long count = count((Wrapper) Wrappers.lambdaQuery().ne(Func.isNotEmpty(notificationConfig.getId()), (v0) -> {
            return v0.getId();
        }, notificationConfig.getId()).eq((v0) -> {
            return v0.getType();
        }, notificationConfig.getType()));*/
        if (count > 0) {
            throw new ServiceException(MessageUtils.message("notify.service.config.not.be.repeated", new Object[0]));
        }
        checkProvider(notificationConfig);
        saveOrUpdate(notificationConfig);
        return notificationConfig;
    }
 
    @Override // org.springblade.modules.notify.service.INotifyConfigService
    public boolean checkUsedByBusinessNotify(String id) {
        Long count = this.notifyBusinessMapper.selectCount(Wrappers.<BusinessNotify>lambdaQuery()
                .eq(BusinessNotify::getNotifyId, id));
        /*
        Long count = this.notifyBusinessMapper.selectCount((Wrapper) Wrappers.lambdaQuery().eq((v0) -> {
            return v0.getNotifyId();
        }, id));*/
        if (count.longValue() > 0) {
            throw new ServiceException(MessageUtils.message("notify.service.config.not.delete", new Object[0]));
        }
        return true;
    }
 
    public Map<String, NotifyTypeInfo> getNotifyInfo() {
        return getAllNotifyType().stream().filter(n -> {
            return ("weiXinMini".equals(n.getId()) || "weiXinMp".equals(n.getId())) ? false : true;
        }).collect(Collectors.toMap((v0) -> {
            return v0.getId();
        }, notifyTypeInfo -> {
            return notifyTypeInfo;
        }));
    }
 
    @Override // org.springblade.modules.notify.service.INotifyConfigService
    public List<NotifyTypeInfo> getServiceList() {
        Map<String, NotifyTypeInfo> typeInfoMap = getNotifyInfo();
        List<String> typeList = CollectionUtil.list(Boolean.FALSE.booleanValue(), typeInfoMap.keySet());
        Map<String, NotifyConfigEntity> configMap = getConfigs(typeList);
        List<BusinessNotify> businessNotifies = this.notifyBusinessMapper.selectList(Wrappers.<BusinessNotify>lambdaQuery().in(BusinessNotify::getNotifyType, typeList));
        /*
        List<BusinessNotify> businessNotifies = this.notifyBusinessMapper.selectList((Wrapper) Wrappers.lambdaQuery().in((v0) -> {
            return v0.getNotifyType();
        }, typeList));*/
        Map<String, List<BusinessNotify>> notifyMap = businessNotifies.stream().collect(Collectors.groupingBy((v0) -> {
            return v0.getNotifyType();
        }));
        typeInfoMap.forEach((typeId, typeInfo) -> {
            if (configMap.containsKey(typeId)) {
                NotifyConfigEntity config = (NotifyConfigEntity) configMap.get(typeId);
                typeInfo.setConfigEntity(config);
                if (Func.equals(config.getType(), DefaultNotifyType.dingTalk.name()) && checkMatchingByOuterAppConfig(DefaultNotifyType.dingTalk.name())) {
                    String companyName = this.dingTalkApi.getCompanyName();
                    typeInfo.setCompanyName(companyName);
                }
            }
            typeInfo.setIsBindBusiness(Boolean.valueOf(notifyMap.containsKey(typeId)));
        });
        return new ArrayList<>(typeInfoMap.values());
    }
 
    @Override // org.springblade.modules.notify.service.INotifyConfigService
    public ConfigMetadata getMetadataByTypeAndProvider(String type, String provider) {
        ConfigMetadata result = null;
        List<NotifierProvider> providerList = this.providers.stream().filter(prov -> {
            return prov.getType().getId().equalsIgnoreCase(type) && prov.getProvider().getId().equalsIgnoreCase(provider);
        }).collect(Collectors.toList());
        if (Func.isNotEmpty(providerList)) {
            result = providerList.get(0).getNotifierConfigMetadata();
        }
        return result;
    }
 
    public void checkProvider(NotifyConfigEntity notificationConfig) {
        if (Func.isEmpty(notificationConfig.getId())) {
            return;
        }
        String type = notificationConfig.getType();
        if (!type.equals(NotifyConstant.SMS_TYPE_VALUE)) {
            return;
        }
        NotifyConfigEntity configEntity = (NotifyConfigEntity) getOne(Wrappers.lambdaQuery(NotifyConfigEntity.class).eq((v0) -> {
            return v0.getId();
        }, notificationConfig.getId()));
        if (ObjectUtil.isNull(configEntity)) {
            throw new ServiceException(MessageUtils.message("notify.service.config.is.null", new Object[0]));
        }
        if (notificationConfig.getProvider().equals(configEntity.getProvider())) {
            return;
        }
        Optional<ProviderInfo> first = getNotifyInfo().get(type).getProviderInfos().stream().filter(p -> {
            return p.getId().equals(configEntity.getProvider());
        }).findFirst();
        if (!first.isPresent()) {
            throw new ServiceException(MessageUtils.message("notify.service.config.is.null", new Object[0]));
        }
        if (this.notifyBusinessMapper.exists((Wrapper) Wrappers.lambdaQuery(BusinessNotify.class).eq((v0) -> {
            return v0.getNotifyId();
        }, configEntity.getId()))) {
            throw new ServiceException(first.get().getName() + MessageUtils.message("notify.config.not.be.edit", new Object[0]));
        }
    }
 
    public Map<String, NotifyConfigEntity> getConfigs(List<String> types) {
        List<NotifyConfigEntity> configList = list(Wrappers.<NotifyConfigEntity>lambdaQuery().in(NotifyConfigEntity::getType, types));
        /*
        List<NotifyConfigEntity> configList = list((Wrapper) Wrappers.lambdaQuery().in((v0) -> {
            return v0.getType();
        }, types));*/
        return configList.stream().collect(Collectors.toMap((v0) -> {
            return v0.getType();
        }, notifyConfigEntity -> {
            return notifyConfigEntity;
        }, (a, b) -> {
            return a;
        }));
    }
 
    @Override // org.springblade.modules.notify.service.INotifyConfigService
    public List<NotifyTypeInfo> getAllNotifyType() {
        List<NotifyTypeInfo> result = new ArrayList<>();
        Map<NotifyType, List<NotifierProvider>> groups = this.providers.stream().collect(Collectors.groupingBy((v0) -> {
            return v0.getType();
        }));
        groups.forEach((type, providerList) -> {
            NotifyTypeInfo typeInfo = new NotifyTypeInfo();
            typeInfo.setId(type.getId());
            typeInfo.setName(type.getName());
            typeInfo.setProviderInfos(providerList.stream().map(ProviderInfo::of).collect(Collectors.toList()));
            result.add(typeInfo);
        });
        return result;
    }
}