ny
23 小时以前 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package jnpf.flowable.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.ImmutableList;
import jnpf.base.Pagination;
import jnpf.base.UserInfo;
import jnpf.base.service.SuperServiceImpl;
import jnpf.base.vo.ListVO;
import jnpf.constant.MsgCode;
import jnpf.exception.WorkFlowException;
import jnpf.flowable.entity.DelegateEntity;
import jnpf.flowable.entity.DelegateInfoEntity;
import jnpf.flowable.entity.TemplateEntity;
import jnpf.flowable.entity.TemplateJsonEntity;
import jnpf.flowable.enums.TemplateStatueEnum;
import jnpf.flowable.mapper.DelegateInfoMapper;
import jnpf.flowable.mapper.DelegateMapper;
import jnpf.flowable.mapper.TemplateJsonMapper;
import jnpf.flowable.mapper.TemplateMapper;
import jnpf.flowable.model.candidates.CandidateUserVo;
import jnpf.flowable.model.delegate.DelegateCrForm;
import jnpf.flowable.model.delegate.DelegateListVO;
import jnpf.flowable.model.delegate.DelegatePagination;
import jnpf.flowable.model.delegate.DelegateUpForm;
import jnpf.flowable.model.message.DelegateModel;
import jnpf.flowable.model.util.FlowConstant;
import jnpf.flowable.model.util.FlowNature;
import jnpf.flowable.service.DelegateService;
import jnpf.flowable.util.FlowUtil;
import jnpf.flowable.util.MsgUtil;
import jnpf.flowable.util.OperatorUtil;
import jnpf.flowable.util.ServiceUtil;
import jnpf.message.model.SentMessageForm;
import jnpf.util.JsonUtil;
import jnpf.util.RandomUtil;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 类的描述
 *
 * @author JNPF@YinMai Info. Co., Ltd
 * @version 5.0.x
 * @since 2024/5/13 16:56
 */
@Service
public class DelegateServiceImpl extends SuperServiceImpl<DelegateMapper, DelegateEntity> implements DelegateService {
 
    @Autowired
    private MsgUtil msgUtil;
    @Autowired
    private OperatorUtil operatorUtil;
    @Autowired
    private ServiceUtil serviceUtil;
    @Autowired
    private FlowUtil flowUtil;
 
    @Autowired
    private DelegateInfoMapper delegateInfoMapper;
    @Autowired
    private TemplateMapper templateMapper;
    @Autowired
    private TemplateJsonMapper templateJsonMapper;
 
    @Override
    public List<DelegateListVO> getList(DelegatePagination pagination) {
        String userId = UserProvider.getLoginUserId();
        String keyword = pagination.getKeyword();
        QueryWrapper<DelegateEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(DelegateEntity::getUserId, userId);
 
        QueryWrapper<DelegateInfoEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtil.isNotBlank(keyword)) {
            queryWrapper.lambda().like(DelegateInfoEntity::getToUserName, keyword);
        }
        List<DelegateInfoEntity> delegateInfoList = delegateInfoMapper.selectList(queryWrapper);
        List<String> delegateIds = delegateInfoList.stream().map(DelegateInfoEntity::getDelegateId).distinct().collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(delegateIds)) {
            if (StringUtil.isNotBlank(keyword)) {
                wrapper.lambda().and(e -> e.in(DelegateEntity::getId, delegateIds).or().like(DelegateEntity::getFlowName, keyword));
            } else {
                wrapper.lambda().in(DelegateEntity::getId, delegateIds);
            }
        } else {
            if (StringUtil.isNotBlank(keyword)) {
                wrapper.lambda().like(DelegateEntity::getFlowName, keyword);
            } else {
                return new ArrayList<>();
            }
        }
        if (ObjectUtil.equals(pagination.getType(), 1)) {
            wrapper.lambda().eq(DelegateEntity::getType, 0);// 委托
        } else {
            wrapper.lambda().eq(DelegateEntity::getType, 1);// 代理
        }
        wrapper.lambda().orderByAsc(DelegateEntity::getSortCode).orderByDesc(DelegateEntity::getCreatorTime);
        Page<DelegateEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
        IPage<DelegateEntity> iPage = this.page(page, wrapper);
        List<DelegateEntity> dataList = pagination.setData(iPage.getRecords(), page.getTotal());
 
        List<DelegateListVO> voList = new ArrayList<>();
        long time = new Date().getTime();
        List<String> ids = dataList.stream().map(DelegateEntity::getId).distinct().collect(Collectors.toList());
        List<DelegateInfoEntity> list = delegateInfoMapper.getList(ids);
        for (DelegateEntity delegate : dataList) {
            DelegateListVO vo = JsonUtil.getJsonToBean(delegate, DelegateListVO.class);
            List<DelegateInfoEntity> infoList = list.stream()
                    .filter(e -> ObjectUtil.equals(e.getDelegateId(), delegate.getId())).collect(Collectors.toList());
            List<String> toUserNameList = infoList.stream().map(DelegateInfoEntity::getToUserName).collect(Collectors.toList());
            vo.setToUserName(String.join(",", toUserNameList));
            long rejectCount = infoList.stream().filter(e -> ObjectUtil.equals(e.getStatus(), 2)).count();
            long acceptCount = infoList.stream().filter(e -> ObjectUtil.equals(e.getStatus(), 1)).count();
            if (time >= vo.getEndTime() || rejectCount == infoList.size()) {// 已失效,1、所有人都拒绝;2、到达结束时间或终止委托
                vo.setStatus(2);
            } else if (time >= vo.getStartTime() && acceptCount > 0) {// 生效中,对方接受且到达开始时间的状态
                vo.setStatus(1);
            } else {// 未生效,两种场景1:对方已接受但未达到开始时间状态为未生效,2、对方未接受状态为未生效
                vo.setStatus(0);
            }
            if (acceptCount > 0) {
                vo.setIsEdit(false);
            }
            voList.add(vo);
        }
        return voList;
    }
 
    @Override
    public DelegateEntity getInfo(String id) {
        return this.baseMapper.getInfo(id);
    }
 
    @Override
    public void create(DelegateCrForm fo) {
        String userId = UserProvider.getLoginUserId();
        DelegateEntity entity = JsonUtil.getJsonToBean(fo, DelegateEntity.class);
        entity.setId(RandomUtil.uuId());
        entity.setSortCode(RandomUtil.parses());
        entity.setCreatorUserId(userId);
 
        this.save(entity);
        DelegateModel model = flowUtil.create(fo.getToUserId(), entity);
        msgUtil.delegateMsg(model);
    }
 
    @Override
    public boolean update(DelegateEntity entity, DelegateUpForm fo) {
        BeanUtil.copyProperties(fo, entity, CopyOptions.create().setIgnoreNullValue(true));
        UserInfo userInfo = UserProvider.getUser();
        entity.setLastModifyTime(new Date());
        entity.setLastModifyUserId(userInfo.getUserId());
        List<String> createList = flowUtil.update(fo.getToUserId(), entity);
        if (!createList.isEmpty()) {
            DelegateModel model = flowUtil.create(createList, entity);
            msgUtil.delegateMsg(model);
        }
        return this.updateById(entity);
    }
 
    @Override
    public boolean updateStop(String id, DelegateEntity entity) {
        UserInfo userInfo = UserProvider.getUser();
        entity.setId(id);
        entity.setLastModifyTime(new Date());
        entity.setLastModifyUserId(userInfo.getUserId());
        DelegateModel delegate = new DelegateModel();
        List<DelegateInfoEntity> infoList = delegateInfoMapper.getList(entity.getId());
        delegate.setToUserIds(infoList.stream().map(DelegateInfoEntity::getToUserId).collect(Collectors.toList()));
        delegate.setType(FlowNature.EndMsg);
        delegate.setUserInfo(userInfo);
        boolean isDelegate = ObjectUtil.equals(entity.getType(), 0);
        delegate.setDelegate(isDelegate);
        msgUtil.delegateMsg(delegate);
        return this.updateById(entity);
    }
 
    @Override
    public void delete(DelegateEntity entity) {
        this.baseMapper.delete(entity);
    }
 
    @Override
    public List<String> getToUser(String userId, String flowId) {
        return flowUtil.getToUser(userId, flowId);
    }
 
    @Override
    public List<DelegateEntity> getByToUserId(String toUserId) {
        return flowUtil.getByToUserId(toUserId);
    }
 
    // 根据 被委托人/代理人id 获取列表
    @Override
    public List<DelegateEntity> getByToUserId(String toUserId, Integer type) {
        return flowUtil.getByToUserId(toUserId, type);
    }
 
    @Override
    public ListVO<CandidateUserVo> getUserList(String templateId) throws WorkFlowException {
        String userId = UserProvider.getLoginUserId();
        TemplateEntity template = templateMapper.selectById(templateId);
        if (null == template) {
            TemplateJsonEntity jsonEntity = templateJsonMapper.selectById(templateId);
            if (null != jsonEntity) {
                template = templateMapper.selectById(jsonEntity.getTemplateId());
            }
        }
        if (null == template) {
            throw new WorkFlowException(MsgCode.WF122.get());
        }
        if (!ObjectUtil.equals(template.getStatus(), TemplateStatueEnum.up.getCode())) {
            throw new WorkFlowException(MsgCode.WF140.get());
        }
        List<String> userIds = this.getDelegateUser(userId, template);
 
        Pagination pagination = new Pagination();
        pagination.setPageSize(10000);
        List<CandidateUserVo> jsonToList = operatorUtil.getUserModel(userIds, pagination);
        if (jsonToList.isEmpty()) {
            throw new WorkFlowException(MsgCode.WF125.get());
        }
        ListVO<CandidateUserVo> vo = new ListVO<>();
        vo.setList(jsonToList);
        return vo;
    }
 
    public List<String> getDelegateUser(String userId, TemplateEntity template) throws WorkFlowException {
        List<String> ids = new ArrayList<>();
        List<DelegateEntity> delegateList = this.getByToUserId(userId, 0);
        if (CollectionUtil.isNotEmpty(delegateList)) {
            int permissionCount = 0;
            for (DelegateEntity delegateEntity : delegateList) {
                String flowId = delegateEntity.getFlowId();
                if (StringUtil.isNotBlank(flowId)) {
                    if (flowId.contains(template.getId())) {
                        if (ObjectUtil.equals(template.getVisibleType(), FlowNature.All)) {
                            ids.add(delegateEntity.getUserId());
                            continue;
                        }
                        List<String> launchPermission = serviceUtil.getPermission(delegateEntity.getUserId());
                        if (launchPermission.contains(template.getId())) {
                            ids.add(delegateEntity.getUserId());
                        } else {
                            permissionCount++;
                        }
                    }
                } else {
                    // 全部流程
                    if (ObjectUtil.equals(template.getVisibleType(), FlowNature.All)) {
                        ids.add(delegateEntity.getUserId());
                        continue;
                    }
                    List<String> launchPermission = serviceUtil.getPermission(delegateEntity.getUserId());
                    if (launchPermission.contains(template.getId())) {
                        ids.add(delegateEntity.getUserId());
                    } else {
                        permissionCount++;
                    }
                }
            }
            if (CollectionUtil.isEmpty(ids) && delegateList.size() == permissionCount) {
                throw new WorkFlowException(MsgCode.WF129.get());
            }
        }
        return ids;
    }
 
 
    @Override
    public List<DelegateEntity> selectSameParamAboutDelaget(DelegateCrForm fo) {
        List<DelegateInfoEntity> infoList = delegateInfoMapper.getByToUserIds(fo.getToUserId());
        if (CollectionUtil.isEmpty(infoList)) {
            return new ArrayList<>();
        }
        List<String> ids = infoList.stream().map(DelegateInfoEntity::getDelegateId).distinct().collect(Collectors.toList());
        QueryWrapper<DelegateEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DelegateEntity::getUserId, fo.getUserId()).in(DelegateEntity::getId, ids)
                .eq(DelegateEntity::getType, Integer.valueOf(fo.getType()))
                .gt(DelegateEntity::getEndTime, new Date());
        return this.list(queryWrapper);
    }
 
    @Override
    public List<DelegateEntity> getList() {
        String userId = UserProvider.getLoginUserId();
        QueryWrapper<DelegateEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().and(t ->
                t.eq(DelegateEntity::getCreatorUserId, userId).or().eq(DelegateEntity::getUserId, userId));
        return this.baseMapper.selectList(queryWrapper);
    }
 
    @Override
    public void notarize(DelegateInfoEntity delegateInfo) {
        delegateInfoMapper.updateById(delegateInfo);
        // 判断全部拒绝,更新时间
        DelegateEntity entity = this.getById(delegateInfo.getDelegateId());
        List<DelegateInfoEntity> infoList = delegateInfoMapper.getList(entity.getId());
        boolean isAllReject = infoList.stream().allMatch(e -> ObjectUtil.equals(e.getStatus(), 2));
        if (isAllReject) {
            Date date = new Date();
            entity.setStartTime(date);
            entity.setEndTime(date);
            this.updateById(entity);
        }
        if (!ObjectUtil.equals(delegateInfo.getStatus(), 0)) {
            UserInfo userInfo = UserProvider.getUser();
            List<String> toUserIds = ImmutableList.of(entity.getUserId());
 
            SentMessageForm flowMsgModel = new SentMessageForm();
            flowMsgModel.setToUserIds(toUserIds);
            flowMsgModel.setUserInfo(userInfo);
            flowMsgModel.setTemplateId("PZXTLG022");
            Map<String, Object> parameterMap = new HashMap<>();
            parameterMap.put(FlowConstant.MANDATOR, userInfo.getUserName());
            parameterMap.put(FlowConstant.TITLE, ObjectUtil.equals(entity.getType(), 0) ? "委托" : "代理");
            parameterMap.put(FlowConstant.ACTION, ObjectUtil.equals(delegateInfo.getStatus(), 1) ? "接受" : "拒绝");
            flowMsgModel.setParameterMap(parameterMap);
 
            Integer delegateType = FlowNature.EndMsg.equals(entity.getType()) ? 0 : ObjectUtil.equals(entity.getType(), 0) ? 1 : 3;
            Map<String, String> contentMsg = new HashMap<>();
            contentMsg.put("type", delegateType + "");
            flowMsgModel.setContentMsg(contentMsg);
            flowMsgModel.setFlowType(2);
            flowMsgModel.setType(2);
 
            List<SentMessageForm> messageListAll = new ArrayList<>();
            messageListAll.add(flowMsgModel);
            serviceUtil.sendDelegateMsg(messageListAll);
        }
    }
}