houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
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
package com.iailab.module.data.http.collector;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iailab.module.data.common.utils.HttpsRequest;
import com.iailab.module.data.http.entity.HttpApiEntity;
import com.iailab.module.data.http.service.HttpApiService;
import com.iailab.module.system.api.permission.RoleApi;
import com.iailab.module.system.api.permission.dto.RoleDTO;
import com.iailab.module.system.api.user.AdminUserApi;
import com.iailab.module.system.api.user.dto.AdminUserRespDTO;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
 
import java.util.*;
 
/**
 * 山大设备数据采集
 *
 * @author lirm
 * @Description
 * @createTime 2024年05月21日
 */
@Slf4j
@Component
public class HttpCollectorForUser {
 
    @Resource
    private HttpApiService httpApiService;
 
    @Resource
    HttpsRequest httpsRequest;
 
    @Resource
    private AdminUserApi adminUserApi;
 
    @Resource
    private RoleApi roleApi;
 
    public void saveJobInfo(Map<String, String> tMap) {
        HttpApiEntity httpApi = httpApiService.getByCode(tMap.get("code"));
        String responseStr = httpsRequest.doGet(httpApi.getUrl(), null, "utf-8", "");
        log.info("JobInfo responseStr:" + responseStr);
        JSONObject responseObj = JSON.parseObject(responseStr);
        if ("true".equals(responseObj.get("sta").toString())) {
            JSONArray jsonArray = responseObj.getJSONArray("res");
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                RoleDTO role = new RoleDTO();
                role.setName(jsonObject.getString("name"));
                role.setUpdateDate(new Date());
                log.info("同步保存的角色名称为:" + role.getName());
                roleApi.saveRoleInfo(role);
            }
        }
 
    }
    public void saveUserInfo(Map<String, String> tMap) {
        HttpApiEntity httpApi = httpApiService.getByCode(tMap.get("code"));
        String responseStr = httpsRequest.doGet(httpApi.getUrl(), null, "utf-8", "");
        log.info("responseStr:" + responseStr);
        JSONObject responseObj = JSON.parseObject(responseStr);
        if ("true".equals(responseObj.get("sta").toString())) {
            JSONArray jsonArray = responseObj.getJSONArray("res");
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                AdminUserRespDTO sysUser = new AdminUserRespDTO();
                sysUser.setUsername(jsonObject.getString("no"));
                sysUser.setNickname(jsonObject.getString("name"));
                sysUser.setMobile(jsonObject.getString("phone"));
                sysUser.setStatus(1);
                //添加用户角色
                RoleDTO role = roleApi.getRoleByName(jsonObject.getString("job"));
                Set<Long> roleIdList = new HashSet<>();
                roleIdList.add(role.getId());
                //为用户添加默认角色
                roleIdList.add(2L);
                sysUser.setRoleIdList(roleIdList);
                adminUserApi.saveOrUpdateUserInfo(sysUser);
            }
        }
    }
}