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
package jnpf.flowable.controller;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.ImmutableList;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.controller.SuperController;
import jnpf.base.vo.ListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.constant.MsgCode;
import jnpf.exception.WorkFlowException;
import jnpf.flowable.entity.DelegateEntity;
import jnpf.flowable.entity.DelegateInfoEntity;
import jnpf.flowable.model.candidates.CandidateUserVo;
import jnpf.flowable.model.delegate.*;
import jnpf.flowable.service.DelegateInfoService;
import jnpf.flowable.service.DelegateService;
import jnpf.flowable.util.ServiceUtil;
import jnpf.util.JsonUtil;
import jnpf.util.JsonUtilEx;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 类的描述
 *
 * @author JNPF@YinMai Info. Co., Ltd
 * @version 5.0.x
 * @since 2024/5/13 17:27
 */
@Tag(name = "流程委托", description = "DelegateController")
@RestController
@RequestMapping("/delegate")
public class DelegateController extends SuperController<DelegateService, DelegateEntity> {
    @Autowired
    private DelegateService delegateService;
    @Autowired
    private ServiceUtil serviceUtil;
    @Autowired
    private DelegateInfoService delegateInfoService;
 
    /**
     * 获取流程委托列表
     *
     * @param pagination 分页参数
     */
    @Operation(summary = "获取流程委托列表")
    @GetMapping
    public ActionResult list(DelegatePagination pagination) {
        List<DelegateListVO> voList;
        if (ObjectUtil.equals(pagination.getType(), 2) || ObjectUtil.equals(pagination.getType(), 4)) {
            voList = delegateInfoService.getList(pagination);
        } else {
            voList = delegateService.getList(pagination);
        }
        PaginationVO paginationVO = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
        return ActionResult.page(voList, paginationVO);
    }
 
    /**
     * 委托信息列表
     *
     * @param delegateId 委托主键
     */
    @Operation(summary = "委托信息列表")
    @GetMapping("/Info/{delegateId}")
    public ActionResult getDelegateInfo(@PathVariable("delegateId") String delegateId) {
        List<DelegateInfoEntity> list = delegateInfoService.getList(delegateId);
        return ActionResult.success(JsonUtil.getJsonToList(list, DelegateListVO.class));
    }
 
    /**
     * 获取流程委托信息
     *
     * @param id 主键
     */
    @Operation(summary = "获取流程委托信息")
    @GetMapping("/{id}")
    public ActionResult info(@PathVariable("id") String id) throws WorkFlowException {
        DelegateEntity entity = delegateService.getInfo(id);
        if (null == entity) {
            throw new WorkFlowException(MsgCode.FA001.get());
        }
        DelegateInfoVO vo = JsonUtilEx.getJsonToBeanEx(entity, DelegateInfoVO.class);
        List<DelegateInfoEntity> infoList = delegateInfoService.getList(entity.getId());
        if (CollectionUtil.isNotEmpty(infoList)) {
            List<String> toUserNameList = infoList.stream().map(DelegateInfoEntity::getToUserName).collect(Collectors.toList());
            vo.setToUserName(String.join(",", toUserNameList));
            List<String> toUserIdList = infoList.stream().map(DelegateInfoEntity::getToUserId).collect(Collectors.toList());
            vo.setToUserId(toUserIdList);
        }
        return ActionResult.success(vo);
    }
 
    /**
     * 新建流程委托
     *
     * @param fo 参数
     */
    @Operation(summary = "新建流程委托")
    @PostMapping
    public ActionResult create(@RequestBody @Valid DelegateCrForm fo) {
        // 超管和管理员不能新增委托/代理(即:只有普通用户身份才能新建委托/代理,提示:“管理员不能新建委托/代理”)
        if (!serviceUtil.isCommonUser(UserProvider.getLoginUserId())) {
            return ActionResult.fail(MsgCode.WF130.get());
        }
        if (StringUtil.isBlank(fo.getUserId())) {
            UserInfo userInfo = UserProvider.getUser();
            fo.setUserId(userInfo.getUserId());
            fo.setUserName(userInfo.getUserName() + "/" + userInfo.getUserAccount());
        }
        boolean isDelegate = ObjectUtil.equals(fo.getType(), "0");
        if (fo.getToUserId().contains(fo.getUserId())) {
            return ActionResult.fail(isDelegate ? MsgCode.WF017.get() : MsgCode.WF137.get());
        }
        // 受托人/代理人不能选择admin和本人
        List<String> toUserList = fo.getToUserId();
        String admin = serviceUtil.getAdmin();
        for (String toUser : toUserList) {
            if (ObjectUtil.equals(toUser, admin)) {
                return ActionResult.fail(MsgCode.WF131.get());
            }
        }
        if (this.alreadyDelegate(fo, null)) {
            return ActionResult.fail(isDelegate ? MsgCode.WF018.get() : MsgCode.WF144.get());
        }
        DelegateCrForm reverse = new DelegateCrForm();
        BeanUtils.copyProperties(fo, reverse);
        reverse.setUserIdList(fo.getToUserId());
        reverse.setToUserId(ImmutableList.of(fo.getUserId()));
        if (this.alreadyDelegate(reverse, null)) {
            return ActionResult.fail(isDelegate ? MsgCode.WF019.get() : MsgCode.WF145.get());
        }
        delegateService.create(fo);
        return ActionResult.success(MsgCode.SU001.get());
    }
 
    /**
     * 更新流程委托
     *
     * @param id 主键
     * @param fo 参数
     */
    @Operation(summary = "更新流程委托")
    @PutMapping("/{id}")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid DelegateUpForm fo) throws WorkFlowException {
        DelegateEntity entity = delegateService.getInfo(id);
        if (null == entity) {
            throw new WorkFlowException(MsgCode.FA001.get());
        }
        fo.setUserId(entity.getUserId());
        boolean isDelegate = ObjectUtil.equals(fo.getType(), "0");
        if (fo.getToUserId().contains(fo.getUserId())) {
            return ActionResult.fail(isDelegate ? MsgCode.WF017.get() : MsgCode.WF137.get());
        }
        if (this.alreadyDelegate(fo, id)) {
            return ActionResult.fail(isDelegate ? MsgCode.WF018.get() : MsgCode.WF144.get());
        }
        // 判断是否有人接受
        List<DelegateInfoEntity> infoList = delegateInfoService.getList(id);
        if (CollectionUtil.isNotEmpty(infoList)) {
            DelegateInfoEntity delegateInfoEntity = infoList.stream().filter(e -> ObjectUtil.equals(e.getStatus(), 1)).findFirst().orElse(null);
            if (null != delegateInfoEntity) {
                return ActionResult.fail(MsgCode.WF132.get());
            }
        }
        DelegateCrForm reverse = new DelegateCrForm();
        BeanUtils.copyProperties(fo, reverse);
        reverse.setUserIdList(fo.getToUserId());
        reverse.setToUserId(ImmutableList.of(fo.getUserId()));
        if (this.alreadyDelegate(reverse, id)) {
            return ActionResult.fail(isDelegate ? MsgCode.WF019.get() : MsgCode.WF145.get());
        }
        if (delegateService.update(entity, fo)) {
            return ActionResult.success(MsgCode.SU004.get());
        }
        return ActionResult.success(MsgCode.FA002.get());
    }
 
    // 判断是否已有委托
    private boolean alreadyDelegate(DelegateCrForm form, String id) {
        List<DelegateEntity> delegateEntities = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(form.getUserIdList())) {
            for (String userId : form.getUserIdList()) {
                form.setUserId(userId);
                List<DelegateEntity> list = delegateService.selectSameParamAboutDelaget(form);
                delegateEntities.addAll(list);
            }
        } else {
            delegateEntities = delegateService.selectSameParamAboutDelaget(form);
        }
        for (DelegateEntity delegate : delegateEntities) {
            if (delegate.getId().equals(id)) {
                continue;
            }
            //时间交叉
            if ((form.getStartTime() <= delegate.getStartTime().getTime() && form.getEndTime() >= delegate.getStartTime().getTime()) ||
                    (form.getStartTime() >= delegate.getStartTime().getTime() && form.getStartTime() <= delegate.getEndTime().getTime())) {
                if (StringUtil.isEmpty(form.getFlowId())) {
                    return true;
                } else {
                    if (StringUtil.isEmpty(delegate.getFlowId())) {
                        return true;
                    } else {
                        List<String> split = Arrays.asList(delegate.getFlowId().split(","));
                        List<String> split1 = Arrays.asList(form.getFlowId().split(","));
                        for (String srt : split) {
                            if (split1.contains(srt)) {
                                return true;
                            }
                        }
                    }
                }
            }
        }
        return false;
    }
 
    /**
     * 结束委托
     *
     * @param id 主键
     */
    @Operation(summary = "结束委托")
    @PutMapping("/Stop/{id}")
    public ActionResult stop(@PathVariable("id") String id) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.SECOND, -1);
        Date date = calendar.getTime();
        DelegateEntity entity = delegateService.getInfo(id);
        if (null != entity) {
            entity.setStartTime(date);
            entity.setEndTime(date);
            delegateService.updateStop(id, entity);
            return ActionResult.success(MsgCode.SU008.get());
        }
        return ActionResult.fail(MsgCode.FA002.get());
    }
 
    /**
     * 删除流程委托
     *
     * @param id 主键
     */
    @Operation(summary = "删除流程委托")
    @DeleteMapping("/{id}")
    public ActionResult delete(@PathVariable("id") String id) {
        DelegateEntity entity = delegateService.getInfo(id);
        if (null != entity) {
            delegateService.delete(entity);
            delegateInfoService.delete(entity.getId());
            return ActionResult.success(MsgCode.SU003.get());
        }
        return ActionResult.fail(MsgCode.FA003.get());
    }
 
 
    /**
     * 获取委托人
     * 根据被委托人查询可发起的流程列表
     */
    @Operation(summary = "获取委托人")
    @GetMapping("/UserList")
    public ActionResult getUserListByFlowId(@RequestParam("templateId") String templateId) throws WorkFlowException {
        ListVO<CandidateUserVo> userList = delegateService.getUserList(templateId);
        return ActionResult.success(userList);
    }
 
    /**
     * 确认
     *
     * @param id   委托信息主键
     * @param type 类型,1.接受  2.拒绝
     */
    @Operation(summary = "确认")
    @PostMapping("/Notarize/{id}")
    public ActionResult accept(@PathVariable("id") String id, @RequestParam("type") Integer type) throws WorkFlowException {
        DelegateInfoEntity delegateInfo = delegateInfoService.getById(id == null ? "" : id);
        if (null == delegateInfo) {
            throw new WorkFlowException(MsgCode.FA001.get());
        }
        delegateInfo.setStatus(type);
        delegateService.notarize(delegateInfo);
        return ActionResult.success(MsgCode.SU004.get());
    }
 
}