赵士淞
2023-05-08 ac8c918f8081f21eafa981a35ee431a6233aba91
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
package me.zhengjie.modules.attendance.service.impl;
 
import com.alibaba.fastjson.JSONArray;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.api.Api;
import me.zhengjie.modules.attendance.service.AttendanceLeaveFlowableService;
import me.zhengjie.modules.attendance.service.AttendanceLeaveService;
import me.zhengjie.modules.attendance.service.dto.AttendanceLeaveDto;
import me.zhengjie.modules.constant.Constant;
import me.zhengjie.modules.system.domain.Role;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.repository.RoleRepository;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.modules.system.service.RoleService;
import me.zhengjie.modules.system.service.dto.RoleSmallDto;
import me.zhengjie.utils.ObjectUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Service
@RequiredArgsConstructor
public class AttendanceLeaveFlowableServiceImpl implements AttendanceLeaveFlowableService {
 
    @Value("${flowable.url}")
    private String flowableUrl;
 
    private final Api api;
    private final UserRepository userRepository;
    private final RoleRepository roleRepository;
    private final RoleService roleService;
    private final AttendanceLeaveService attendanceLeaveService;
 
    /**
     * 我发起的
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> report() throws Exception {
        // 发起通信
        Object returnObject = api.post(flowableUrl + "/api/common/report?key=" + "attendanceLeaveApply" + "&userId=" + SecurityUtils.getCurrentUserId(), new HashMap<>());
        // 查询处理
        return queryHandle(returnObject);
    }
 
    /**
     * 待审批的
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> approve() throws Exception {
        // 发起通信
        Object returnObject = api.post(flowableUrl + "/api/common/approve?key=" + "attendanceLeaveApply" + "&userId=" + SecurityUtils.getCurrentUserId(), new HashMap<>());
        // 查询处理
        return queryHandle(returnObject);
    }
 
    /**
     * 查询处理
     * @param returnObject
     * @return
     */
    private Map<String, Object> queryHandle(Object returnObject) {
        // 返回结果
        Map<String, Object> returnMap = new HashMap<>();
        // 通信结果转DTO
        List<AttendanceLeaveDto> attendanceLeaveDtoList = ((JSONArray) returnObject).toJavaList(AttendanceLeaveDto.class);
        // 循环DTO
        for (AttendanceLeaveDto attendanceLeaveDto : attendanceLeaveDtoList) {
            // 查询用户信息
            User user = userRepository.findById(attendanceLeaveDto.getUserId()).orElseGet(User::new);
            attendanceLeaveDto.setUserName(user.getNickName());
            // 查询审批用户信息
            User appUser = userRepository.findById(attendanceLeaveDto.getApprovalUserId()).orElseGet(User::new);
            attendanceLeaveDto.setAppUserName(appUser.getNickName());
        }
        // 返回结果
        returnMap.put("content", attendanceLeaveDtoList);
        returnMap.put("totalElements", attendanceLeaveDtoList.size());
        return returnMap;
    }
 
    /**
     * 创建
     * @param resources /
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(AttendanceLeaveDto resources) throws Exception {
        // 新增校验(工作流)
        attendanceLeaveService.flowableCreateValidation(resources, reportAppoint(resources.getUserId()));
        // 获取用户最大角色等级
        List<Integer> levels = roleService.findByUsersId(resources.getUserId()).stream().map(RoleSmallDto::getLevel).collect(Collectors.toList());
        int min = Collections.min(levels);
        // 请假时长
        Integer leaveDuration = 0;
        // 判断请假时长单位
        if (Constant.LEAVE_TIME_TYPE_HOUR.equals(resources.getLeaveDurationUnit())) {
            Double tempLeaveDuration = Math.floor(resources.getLeaveDuration() / 8);
            // 请假时长
            leaveDuration = tempLeaveDuration.intValue();
        } else {
            // 请假时长
            leaveDuration = resources.getLeaveDuration().intValue();
        }
        // 一级审批人
        String approver1 = "";
        // 二级审批人
        String approver2 = "";
        // 三级审批人
        String approver3 = "";
        // 判断用户最大角色等级
        if (min == Constant.ROLE_LEVEL_ORDINARY) {
            // 一级审批人
            approver1 = resources.getApprovalUserId().toString();
            // 二级审批人
            approver2 = findUserByRoleLevel(Constant.ROLE_LEVEL_MANAGER);
            // 三级审批人
            approver3 = findUserByRoleLevel(Constant.ROLE_LEVEL_GENERAL);
        } else if (min == Constant.ROLE_LEVEL_LEADER) {
            // 二级审批人
            approver2 = resources.getApprovalUserId().toString();
            // 三级审批人
            approver3 = findUserByRoleLevel(Constant.ROLE_LEVEL_GENERAL);
        } else if (min == Constant.ROLE_LEVEL_MANAGER) {
            // 三级审批人
            approver3 = resources.getApprovalUserId().toString();
        } else {
 
        }
        // 参数
        Map<String, Object> params = ObjectUtil.toObjectMap(resources);
        params.put("role", min);
        params.put("time", leaveDuration);
        params.put("approver1", approver1);
        params.put("approver2", approver2);
        params.put("approver3", approver3);
        // 发起通信
        api.post(flowableUrl + "/api/common/run?key=" + "attendanceLeaveApply" + "&userId=" + resources.getUserId(), params);
    }
 
    /**
     * 根据角色等级查询用户
     * @param level
     * @return
     */
    private String findUserByRoleLevel(Long level) {
        // 查询指定角色
        Role role = roleRepository.findByLevel(level.intValue());
        // 查询指定角色用户
        List<User> userList = userRepository.findByRoleId(role.getId());
        // 审批人字符串
        String approver = "";
        // 循环指定角色用户
        for (int i = 0; i < userList.size(); i ++) {
            if (i == 0) {
                // 审批人字符串
                approver = userList.get(i).getId().toString();
            } else {
                // 审批人字符串
                approver += "," + userList.get(i).getId().toString();
            }
        }
        // 返回
        return approver;
    }
 
    /**
     * 同意
     * @param processInstanceId /
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void agree(String processInstanceId) throws Exception {
        // 参数
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("approve", "true");
        // 发起通信
        api.post(flowableUrl + "/api/common/next?processInstanceId=" + processInstanceId, paramsMap);
    }
 
    /**
     * 拒绝
     * @param processInstanceId /
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void reject(String processInstanceId) throws Exception {
        // 参数
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("approve", "false");
        // 发起通信
        api.post(flowableUrl + "/api/common/next?processInstanceId=" + processInstanceId, paramsMap);
    }
 
    /**
     * 我发起的(指定用户)
     * @return
     */
    private List<AttendanceLeaveDto> reportAppoint(Long userId) {
        // 发起通信
        Object returnObject = api.post(flowableUrl + "/api/common/report?key=" + "attendanceLeaveApply" + "&userId=" + userId, new HashMap<>());
        // 通信结果转DTO
        List<AttendanceLeaveDto> attendanceLeaveDtoList = ((JSONArray) returnObject).toJavaList(AttendanceLeaveDto.class);
        return attendanceLeaveDtoList;
    }
}