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 implements INotifyConfigService { private static final Logger log = LoggerFactory.getLogger(NotifyConfigServiceImpl.class); private final BusinessNotifyMapper notifyBusinessMapper; private final List providers; private final DingTalkApi dingTalkApi; private final IOuterAppConfigService outerAppConfigService; public NotifyConfigServiceImpl(BusinessNotifyMapper notifyBusinessMapper, List 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 data = new HashMap<>(); data.put("agentid", agentid); data.put("corpid", corpid); data.put("corpsecret", corpsecret); NotifyConfigEntity config = getOne(Wrappers.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 data2 = new HashMap<>(); data2.put("appKey", appKey); data2.put("appSecret", appSecret); NotifyConfigEntity config2 = getOne(Wrappers.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.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 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.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.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 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 getServiceList() { Map typeInfoMap = getNotifyInfo(); List typeList = CollectionUtil.list(Boolean.FALSE.booleanValue(), typeInfoMap.keySet()); Map configMap = getConfigs(typeList); List businessNotifies = this.notifyBusinessMapper.selectList(Wrappers.lambdaQuery().in(BusinessNotify::getNotifyType, typeList)); /* List businessNotifies = this.notifyBusinessMapper.selectList((Wrapper) Wrappers.lambdaQuery().in((v0) -> { return v0.getNotifyType(); }, typeList));*/ Map> 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 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 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 getConfigs(List types) { List configList = list(Wrappers.lambdaQuery().in(NotifyConfigEntity::getType, types)); /* List 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 getAllNotifyType() { List result = new ArrayList<>(); Map> 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; } }