zhuo
2025-04-23 07476d90bea3e755e8372c22b5ca3a2ecb86150e
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
package com.ruoyi.web.controller.api;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.config.PersonnelProperties;
import com.ruoyi.common.core.domain.entity.Custom;
import com.ruoyi.common.core.domain.entity.User;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.UserMapper;
import com.ruoyi.system.service.CustomService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
 
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
/**
 * 获取人事系统
 */
@Component
public class PersonnelHeaderApi {
 
    @Resource
    private PersonnelProperties personnelProperties;
    @Resource
    private CustomService customService;
    @Resource
    private UserMapper userMapper;
 
    public String fetchNewAccessToken() {
        HttpRequest request = HttpRequest.post(personnelProperties.getCode())
                .header("Content-Type", "application/x-www-form-urlencoded")
                .form("grant_type", "client_credentials")
                .form("client_id", personnelProperties.getAppId())
                .form("client_secret", personnelProperties.getAppSecret());
        HttpResponse response = request.execute();
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode;
        try {
            jsonNode = objectMapper.readTree(response.body());
            String accessToken = jsonNode.get("access_token").asText();
            return accessToken;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }
 
    //判断是否存在
    public String getAccessToken() {
        String accessToken;
        accessToken = fetchNewAccessToken();
        return accessToken;
    }
 
    //调用
    public List<Company> companyUrl() {
        String accessToken = getAccessToken();
        HttpRequest request = HttpRequest.get(personnelProperties.getCompanies())
                .header("Authorization", "Bearer " + accessToken);
        List<Company> companies;
        try {
            companies = JSON.parseArray(request.execute().body(), Company.class);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return companies.stream().filter(ob -> {
            if (Objects.equals(ob.getStatus(), "enabled")) return true;
            return false;
        }).collect(Collectors.toList());
    }
 
    public List<Person> userUrl(String companyId) {
        String accessToken = getAccessToken();
        HttpRequest request = HttpRequest.get(personnelProperties.getSimple() + companyId)
                .header("Authorization", "Bearer " + accessToken)
                .header("Content-Type", "application/form-data");
        List<Person> person;
        try {
            person = JSON.parseArray(request.execute().body(), Person.class);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        List<JSONObject> department = getDepartment(companyId);
        return person.stream().filter(ob -> {
            if (Objects.equals(ob.getStatus(), "enabled")) {
                ob.setIsLive(userMapper.selectCount(Wrappers.<User>lambdaQuery().eq(User::getAccount, ob.getEmployeeID())));
                ob.setDepartment(getDepartmentStr(department, ob.getDepartmentCode()).replaceFirst("/", ""));
                return true;
            }
            return false;
        }).collect(Collectors.toList());
    }
 
    public String getDepartmentStr(List<JSONObject> department, String code) {
        String str = "";
        Optional<JSONObject> depart = department.stream().filter(a -> code.equals(a.get("departmentCode"))).findFirst();
        str = "/" + depart.get().get("departmentName") + str;
        if (depart.get().get("parentDepartmentCode") != null) {
            str = getDepartmentStr(department, depart.get().get("parentDepartmentCode").toString()) + str;
        }
        return str;
    }
 
    public List<JSONObject> getDepartment(String companyId) {
        String accessToken = getAccessToken();
        HttpRequest request = HttpRequest.get(personnelProperties.getDepartment().replace("companyId", companyId))
                .header("Authorization", "Bearer " + accessToken)
                .header("Content-Type", "application/form-data");
        List<JSONObject> list;
        try {
            list = JSON.parseArray(request.execute().body());
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return list.stream().filter(ob -> {
            if (Objects.equals(ob.get("status"), "enabled")) return true;
            return false;
        }).collect(Collectors.toList());
    }
 
    public Person selectPersonUser(String code) {
        String accessToken = getAccessToken();
        HttpRequest request = HttpRequest.get(personnelProperties.getPerson() + code)
                .header("Authorization", "Bearer " + accessToken)
                .header("Content-Type", "application/form-data");
        Person person;
        try {
            person = JSON.parseObject(request.execute().body(), Person.class);
            if (BeanUtil.isEmpty(person)) return null;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        List<JSONObject> department = getDepartment(person.getCompanyId());
        person.setDepartment(getDepartmentStr(department, person.getDepartmentCode()).replaceFirst("/", ""));
        person.setIsLive(userMapper.selectCount(Wrappers.<User>lambdaQuery().eq(User::getAccount, person.getEmployeeID())));
        return person;
    }
 
    /**
     * 将人事系统勾选的内容转移到本系统
     * @param personDto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Object addPersonUser(PersonDto personDto) {
        personDto.getPerson().forEach(person -> {
            User user = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getAccount, person.getEmployeeID()));
            List<Company> company = personDto.getCompany();
            String companyName = "";
            Custom custom = null;
            // 填充工厂信息
            companyName = company.stream().filter(a -> a.getCompanyId().equals(person.getCompanyId())).findFirst().get().getCompanyName();
            custom = customService.getCustomId(companyName);
 
            if (BeanUtil.isEmpty(user)) {
                user = new User();
                user.setName(person.getName());
                user.setNameEn("not write");
                user.setAccount(person.getEmployeeID());
                user.setPhone(person.getPhoneNumber());
                user.setEmail(person.getCompanyEmail());
                user.setIsCustom(0);
                user.setPassword(SecurityUtils.encryptPassword("zttZTT123!"));
                user.setCompany(BeanUtil.isNotEmpty(custom) ? (custom.getId() + "") : companyName);
                userMapper.insert(user);
            } else {
                user.setName(person.getName());
                user.setPhone(person.getPhoneNumber());
                user.setEmail(person.getCompanyEmail());
                user.setIsCustom(0);
                user.setCompany(BeanUtil.isNotEmpty(custom) ? (custom.getId() + "") : companyName);
                userMapper.updateById(user);
            }
        });
        return 1;
    }
}