package com.qianwen.smartman.modules.notify.service.impl; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; 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.BaseEntity; import com.qianwen.core.mp.base.BaseServiceImpl; import com.qianwen.core.tool.node.ForestNodeMerger; import com.qianwen.core.tool.utils.Func; import com.qianwen.smartman.common.constant.NotifyConstant; import com.qianwen.smartman.common.utils.MessageUtils; import com.qianwen.smartman.modules.cps.entity.Employee; import com.qianwen.smartman.modules.cps.service.IEmployeeService; import com.qianwen.smartman.modules.notify.convert.BusinessNotifyConvert; import com.qianwen.smartman.modules.notify.dto.BusinessNotifyDTO; import com.qianwen.smartman.modules.notify.dto.NotifyBusinessTreeDTO; import com.qianwen.smartman.modules.notify.dto.NotifySendDTO; import com.qianwen.smartman.modules.notify.dto.NotifySendMethodDTO; 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.entity.NotifyTemplateEntity; import com.qianwen.smartman.modules.notify.manager.NotifyBusinessManager; import com.qianwen.smartman.modules.notify.mapper.BusinessNotifyMapper; import com.qianwen.smartman.modules.notify.service.IBusinessNotifyService; import com.qianwen.smartman.modules.notify.service.INotifyDefaultPersonService; import com.qianwen.smartman.modules.notify.service.INotifyTemplateService; import com.qianwen.smartman.modules.notify.vo.NotifyBusinessChangeStatusVO; import com.qianwen.smartman.modules.notify.vo.NotifyBusinessSaveVO; import com.qianwen.smartman.modules.notify.vo.NotifyBusinessTreeVO; import com.qianwen.smartman.modules.notify.vo.NotifyDefaultObjectVO; import com.qianwen.smartman.modules.system.service.IDictService; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.ObjectUtil; @Transactional(rollbackFor = {Exception.class}) @Service /* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/modules/notify/service/impl/BusinessNotifyServiceImpl.class */ public class BusinessNotifyServiceImpl extends BaseServiceImpl implements IBusinessNotifyService { private static final Logger log = LoggerFactory.getLogger(BusinessNotifyServiceImpl.class); private final NotifyConfigServiceImpl notifyConfigService; private final INotifyTemplateService notifyTemplateService; private final BusinessNotifyMapper businessNotifyMapper; private final IDictService dictService; private final IEmployeeService employeeService; @Autowired private INotifyDefaultPersonService notifyDefaultPersonService; @Autowired @Lazy private NotifyBusinessManager notifyBusinessManager; public BusinessNotifyServiceImpl(final NotifyConfigServiceImpl notifyConfigService, final INotifyTemplateService notifyTemplateService, final BusinessNotifyMapper businessNotifyMapper, final IDictService dictService, final IEmployeeService employeeService, final INotifyDefaultPersonService notifyDefaultPersonService, final NotifyBusinessManager notifyBusinessManager) { this.notifyConfigService = notifyConfigService; this.notifyTemplateService = notifyTemplateService; this.businessNotifyMapper = businessNotifyMapper; this.dictService = dictService; this.employeeService = employeeService; this.notifyDefaultPersonService = notifyDefaultPersonService; this.notifyBusinessManager = notifyBusinessManager; } @Override // org.springblade.modules.notify.service.IBusinessNotifyService public List tree() { List notifyList = this.businessNotifyMapper.treeList(NotifyConstant.NOTIFY_BUSINESS_KEY); List vos = BusinessNotifyConvert.INSTANCE.convertDTOToVO(notifyList); List trees = ForestNodeMerger.merge(vos); count(trees); return trees; } @Override // org.springblade.modules.notify.service.IBusinessNotifyService public List list(String businessKey) { Map typeInfoMap = this.notifyConfigService.getNotifyInfo(); Map businessMap = this.businessNotifyMapper.businessList(businessKey).stream().collect(Collectors.toMap(BusinessNotifyDTO::getNotifyType, notifyBusiness -> notifyBusiness)); /* Map businessMap = (Map) this.businessNotifyMapper.businessList(businessKey).stream().collect(Collectors.toMap((v0) -> { return v0.getNotifyType(); }, notifyBusiness -> { return notifyBusiness; }));*/ Map configMap = this.notifyConfigService.getConfigs(CollectionUtil.list(Boolean.FALSE.booleanValue(), typeInfoMap.keySet())); configMap.forEach((typeId, config) -> { BusinessNotifyDTO businessNotify = (BusinessNotifyDTO) businessMap.get(typeId); boolean isExit = ObjectUtil.isNull(businessNotify); BusinessNotifyDTO businessNotify2 = isExit ? new BusinessNotifyDTO() : businessNotify; NotifyTypeInfo typeInfo = (NotifyTypeInfo) typeInfoMap.get(typeId); ProviderInfo providerInfo = typeInfo.getProviderInfos().parallelStream().filter(p -> { return p.getId().equals(config.getProvider()); }).findFirst().orElse(new ProviderInfo("未知服务类型", "未知id", "未知服务商")); businessNotify2.setProviderName(providerInfo.getName()); businessNotify2.setNotifyName(typeInfo.getName()); businessNotify2.setNotifyType(typeId); businessNotify2.setProviderType(providerInfo.getId()); businessNotify2.setBusinessName(isExit ? this.dictService.getValue(NotifyConstant.NOTIFY_BUSINESS_KEY, businessKey) : businessNotify2.getBusinessName()); businessNotify2.setBusinessKey(businessKey); businessMap.put(typeId, businessNotify2); }); List result = new ArrayList<>(); typeInfoMap.keySet().stream().map(businessMap::get).forEach(dto -> { if (dto != null) result.add(dto); }); /* Stream stream = typeInfoMap.keySet().stream(); businessMap.getClass(); stream.map((v1) -> { return r1.get(v1); }).forEach(dto -> { if (dto != null) { result.add(dto); } });*/ return result; } @Override // org.springblade.modules.notify.service.IBusinessNotifyService public NotifyBusinessSaveVO save(NotifyBusinessSaveVO notifyBusinessVO) { NotifyTemplateEntity templateEntity = notifyBusinessVO.getTemplateEntity(); if (Func.isNotEmpty(templateEntity) && Func.isNotEmpty(templateEntity.getId())) { CacheUtil.evict("blade:notify", "template:id:", templateEntity.getId(), false); } checkTemplateAndType(notifyBusinessVO); NotifyConfigEntity entity = this.notifyConfigService.getOne(Wrappers.lambdaQuery() .eq(NotifyConfigEntity::getType, notifyBusinessVO.getBusinessNotify().getNotifyType())); /* NotifyConfigEntity entity = (NotifyConfigEntity) this.notifyConfigService.getOne((Wrapper) Wrappers.lambdaQuery().eq((v0) -> { return v0.getType(); }, notifyBusinessVO.getBusinessNotify().getNotifyType()));*/ notifyBusinessVO.getBusinessNotify().setNotifyId(entity.getId()); this.notifyTemplateService.save(notifyBusinessVO.getTemplateEntity()); notifyBusinessVO.getBusinessNotify().setNotifyTemplateId(notifyBusinessVO.getTemplateEntity().getId()); save(notifyBusinessVO.getBusinessNotify()); return notifyBusinessVO; } @Override // org.springblade.modules.notify.service.IBusinessNotifyService public Boolean change(NotifyBusinessChangeStatusVO changeStatusVO) { BusinessNotify businessNotify = (BusinessNotify)getOne(Wrappers.lambdaQuery(BusinessNotify.class) .eq(BusinessNotify::getBusinessKey, changeStatusVO.getBusinessKey()).eq(BusinessNotify::getNotifyType, changeStatusVO.getNotifyType())); /* BusinessNotify businessNotify = (BusinessNotify) getOne((Wrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery(BusinessNotify.class).eq((v0) -> { return v0.getBusinessKey(); }, changeStatusVO.getBusinessKey())).eq((v0) -> { return v0.getNotifyType(); }, changeStatusVO.getNotifyType()));*/ if (Objects.isNull(businessNotify)) { throw new ServiceException(MessageUtils.message("notify.business.template.status.not.be.change", new Object[0])); } return Boolean.valueOf(changeStatus(Collections.singletonList(businessNotify.getId()), changeStatusVO.getStatus())); } @Override // org.springblade.modules.notify.service.IBusinessNotifyService public NotifyTemplateEntity modify(NotifyTemplateEntity notifyTemplateEntity) { if (Func.isNotEmpty(notifyTemplateEntity) && Func.isNotEmpty(notifyTemplateEntity.getId())) { CacheUtil.evict("blade:notify", "template:id:", notifyTemplateEntity.getId(), false); } checkTemplate(notifyTemplateEntity); this.notifyTemplateService.updateById(notifyTemplateEntity); return notifyTemplateEntity; } @Override // org.springblade.modules.notify.service.IBusinessNotifyService public boolean remove(Long templateId) { this.notifyTemplateService.removeById(templateId); return remove(Wrappers.lambdaQuery(BusinessNotify.class).eq(BusinessNotify::getNotifyTemplateId, templateId)); /* return remove((Wrapper) Wrappers.lambdaQuery(BusinessNotify.class).eq((v0) -> { return v0.getNotifyTemplateId(); }, templateId));*/ } public List getNoticeEmpIds(List orgIds, List empIds) { List employees = this.employeeService.listByOrgIds(orgIds); List itemEmpIds = employees.stream().map((v0) -> { return v0.getId(); }).collect(Collectors.toList()); empIds.addAll(itemEmpIds); return empIds.stream().distinct().collect(Collectors.toList()); } @Override // org.springblade.modules.notify.service.IBusinessNotifyService public void sendMessage(NotifySendDTO notifySendDTO) { String businessKey = notifySendDTO.getBusinessKey(); List list = list(Wrappers.lambdaQuery().eq(BusinessNotify::getStatus, 1) .eq(BusinessNotify::getBusinessKey, businessKey)); /* List list = list((Wrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> { return v0.getStatus(); }, 1)).eq((v0) -> { return v0.getBusinessKey(); }, businessKey));*/ List templateIds = list.stream().map((v0) -> { return v0.getNotifyTemplateId(); }).collect(Collectors.toList()); List notifyTemplateEntities = this.notifyTemplateService.listByIds(templateIds); Map tempMap = notifyTemplateEntities.stream().collect(Collectors.toMap((v0) -> { return v0.getId(); }, item -> { return item; })); List dtoList = new ArrayList<>(); for (BusinessNotify businessNotify : list) { Long notifyTemplateId = businessNotify.getNotifyTemplateId(); NotifySendMethodDTO notifySendMethodDTO = new NotifySendMethodDTO(); NotifyTemplateEntity notifyTemplateEntity = tempMap.get(notifyTemplateId); notifySendMethodDTO.setTemplateId(String.valueOf(notifyTemplateEntity.getId())); notifySendMethodDTO.setNotifyId(String.valueOf(businessNotify.getNotifyId())); notifySendMethodDTO.setNotifyType(businessNotify.getNotifyType()); dtoList.add(notifySendMethodDTO); } Map data = notifySendDTO.getData(); List empIds = notifySendDTO.getEmpIds(); List orgIds = notifySendDTO.getOrgIds(); NotifyDefaultObjectVO objectVO = this.notifyDefaultPersonService.getByBusinessKey(businessKey); List defaultEmpId = objectVO.getEmpIds(); List defaultOrgIds = notifySendDTO.getOrgIds(); if (defaultEmpId != null) { empIds.addAll(defaultEmpId); } if (defaultOrgIds != null) { orgIds.addAll(defaultOrgIds); } List empIds2 = getNoticeEmpIds(orgIds, empIds); log.info("发送消息,时间->{}", LocalDateTime.now()); this.notifyBusinessManager.send(dtoList, empIds2, data); } private void checkTemplate(NotifyTemplateEntity notifyTemplateEntity) { long count = this.notifyTemplateService.count(Wrappers.lambdaQuery() .ne(BaseEntity::getId, notifyTemplateEntity.getId()) .eq(NotifyTemplateEntity::getType, notifyTemplateEntity.getType()) .eq(NotifyTemplateEntity::getBusiness, notifyTemplateEntity.getBusiness())); /* long count = this.notifyTemplateService.count((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().ne((v0) -> { return v0.getId(); }, notifyTemplateEntity.getId())).eq((v0) -> { return v0.getType(); }, notifyTemplateEntity.getType())).eq((v0) -> { return v0.getBusiness(); }, notifyTemplateEntity.getBusiness()));*/ if (count > 0) { throw new ServiceException(MessageUtils.message("notify.business.template.not.be.repeated", new Object[0])); } } private void checkTemplateAndType(NotifyBusinessSaveVO business) { long count = count(Wrappers.lambdaQuery(BusinessNotify.class) .ne(Func.isNotEmpty(business.getBusinessNotify().getId()), BaseEntity::getId, business.getBusinessNotify().getId()) .eq(BusinessNotify::getNotifyType, business.getBusinessNotify().getNotifyType()) .eq(BusinessNotify::getBusinessKey, business.getBusinessNotify().getBusinessKey())); /* long count = count((Wrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery(BusinessNotify.class).ne(Func.isNotEmpty(business.getBusinessNotify().getId()), (v0) -> { return v0.getId(); }, business.getBusinessNotify().getId()).eq((v0) -> { return v0.getNotifyType(); }, business.getBusinessNotify().getNotifyType())).eq((v0) -> { return v0.getBusinessKey(); }, business.getBusinessNotify().getBusinessKey()));*/ if (count > 0) { throw new ServiceException(MessageUtils.message("notify.business.template.not.be.repeated", new Object[0])); } long typeCount = this.notifyConfigService.count(Wrappers.lambdaQuery() .eq(NotifyConfigEntity::getType, business.getBusinessNotify().getNotifyType())); /* long typeCount = this.notifyConfigService.count((Wrapper) Wrappers.lambdaQuery().eq((v0) -> { return v0.getType(); }, business.getBusinessNotify().getNotifyType()));*/ if (typeCount <= 0) { throw new ServiceException(MessageUtils.message("notify.service.config.is.null", new Object[0])); } } private void count(List trees) { for (NotifyBusinessTreeVO tree : trees) { if (tree.getChildren() != null) { for (NotifyBusinessTreeVO child : tree.getChildren()) { child.setStartCount((Integer) child.getChildren().stream().map((v0) -> { return v0.getStartCount(); }).reduce((v0, v1) -> { return Integer.sum(v0, v1); }).orElse(0)); } tree.setStartCount((Integer) tree.getChildren().stream().map((v0) -> { return v0.getStartCount(); }).reduce((v0, v1) -> { return Integer.sum(v0, v1); }).orElse(0)); } } } }