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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
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<BusinessNotifyMapper, BusinessNotify> 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<NotifyBusinessTreeVO> tree() {
        List<NotifyBusinessTreeDTO> notifyList = this.businessNotifyMapper.treeList(NotifyConstant.NOTIFY_BUSINESS_KEY);
        List<NotifyBusinessTreeVO> vos = BusinessNotifyConvert.INSTANCE.convertDTOToVO(notifyList);
        List<NotifyBusinessTreeVO> trees = ForestNodeMerger.merge(vos);
        count(trees);
        return trees;
    }
 
    @Override // org.springblade.modules.notify.service.IBusinessNotifyService
    public List<BusinessNotifyDTO> list(String businessKey) {
        Map<String, NotifyTypeInfo> typeInfoMap = this.notifyConfigService.getNotifyInfo();
        Map<String, BusinessNotifyDTO> businessMap = this.businessNotifyMapper.businessList(businessKey).stream().collect(Collectors.toMap(BusinessNotifyDTO::getNotifyType, notifyBusiness -> notifyBusiness));
        /*
        Map<String, BusinessNotifyDTO> businessMap = (Map) this.businessNotifyMapper.businessList(businessKey).stream().collect(Collectors.toMap((v0) -> {
            return v0.getNotifyType();
        }, notifyBusiness -> {
            return notifyBusiness;
        }));*/
        Map<String, NotifyConfigEntity> 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<BusinessNotifyDTO> result = new ArrayList<>();
        
        typeInfoMap.keySet().stream().map(businessMap::get).forEach(dto -> {
            if (dto != null)
              result.add(dto); 
          });
        /*
        Stream<String> 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.<NotifyConfigEntity>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<Long> getNoticeEmpIds(List<Long> orgIds, List<Long> empIds) {
        List<Employee> employees = this.employeeService.listByOrgIds(orgIds);
        List<Long> 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<BusinessNotify> list = list(Wrappers.<BusinessNotify>lambdaQuery().eq(BusinessNotify::getStatus, 1)
                .eq(BusinessNotify::getBusinessKey, businessKey));
        /*
        List<BusinessNotify> list = list((Wrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
            return v0.getStatus();
        }, 1)).eq((v0) -> {
            return v0.getBusinessKey();
        }, businessKey));*/
        List<Long> templateIds = list.stream().map((v0) -> {
            return v0.getNotifyTemplateId();
        }).collect(Collectors.toList());
        List<NotifyTemplateEntity> notifyTemplateEntities = this.notifyTemplateService.listByIds(templateIds);
        Map<Long, NotifyTemplateEntity> tempMap = notifyTemplateEntities.stream().collect(Collectors.toMap((v0) -> {
            return v0.getId();
        }, item -> {
            return item;
        }));
        List<NotifySendMethodDTO> 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<String, Object> data = notifySendDTO.getData();
        List<Long> empIds = notifySendDTO.getEmpIds();
        List<Long> orgIds = notifySendDTO.getOrgIds();
        NotifyDefaultObjectVO objectVO = this.notifyDefaultPersonService.getByBusinessKey(businessKey);
        List<Long> defaultEmpId = objectVO.getEmpIds();
        List<Long> defaultOrgIds = notifySendDTO.getOrgIds();
        if (defaultEmpId != null) {
            empIds.addAll(defaultEmpId);
        }
        if (defaultOrgIds != null) {
            orgIds.addAll(defaultOrgIds);
        }
        List<Long> 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.<NotifyTemplateEntity>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.<NotifyConfigEntity>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<NotifyBusinessTreeVO> 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));
            }
        }
    }
}