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 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 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 roleIdList = new HashSet<>(); roleIdList.add(role.getId()); //为用户添加默认角色 roleIdList.add(2L); sysUser.setRoleIdList(roleIdList); adminUserApi.saveOrUpdateUserInfo(sysUser); } } } }