首先引入hutool依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.1.2</version>
</dependency>
编写调用外部接口的工具类
package com.donlim.fms.common.utils;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import com.donlim.fms.common.dto.Dto;
import com.donlim.fms.model.ESB;
import com.donlim.fms.model.Org;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName HttpUtil
* @Description TODO
* @Author 李安山
* @DATE 2023/2/15 11:44
* <p>
* Version 1.0
**/
@Slf4j
public class HttpUtil {
// 人员信息同步接口
private static final String url = "http://************:7080/HTTP/DONLIM/ESB/DONLIM_HRMS_QUERYSYNC";
public static List<ESB> getEmployeeList(Dto dto){
//封装请求参数,以下参数是此接口必须的参数
JSONObject params = new JSONObject();
params.put("SOURCEID","FMS");
params.put("DESTINATIONID","ESB");
params.put("TYPE","FMS");
params.put("IPADDRESS","192.168.119.30");
params.put("BO","syncEmployeeInfoToFms");
if (dto==null){
params.put("starttime","2020-01-01");
params.put("endtime","2023-02-16");
}else {
if (ObjectUtils.isEmpty(dto.getDate("startTime")) ||ObjectUtils.isEmpty(dto.getDate("endTime"))){
params.put("starttime","2020-01-01");
params.put("endtime","2023-02-16");
}else {
String date1 = dto.getDate("startTime").toString();
String date2 = dto.getDate("endTime").toString();
params.put("starttime",date1);
params.put("endtime",date2);
}
}
//调用HttpRequest.put方法
String result = HttpRequest.put(url)
.header("Content-Type", "application/json")//头信息,多个头信息多次调用此方法即可
.header("UrlType", "1")//头信息,多个头信息多次调用此方法即可
.body(params.toString())//设置请求参数
.execute().body();
//根据结果获取请求的数据,一般在[]之中
result = result.substring(result.indexOf("["),result.lastIndexOf("]")+1);
// log.info("result==>{}",result);
//下面为hrms系统人员信息转换成fms人员信息
List<ESB> emp = new ArrayList<>();
List<ESB> employeeList = com.alibaba.fastjson.JSONObject.parseArray(result, ESB.class);
for (int j = 0; j < employeeList.size(); j++) {
emp.add(employeeList.get(j));
}
// log.debug("时间段共{}个员工信息",employeeList.size());
String company_id = dto.getString("company_id");
boolean f = StringUtils.isNotBlank(company_id);
emp.stream().forEach(esb -> {
esb.setCard_Number(esb.getID_Number());
try {
esb.setSalt(PBKD2FUtils.getSalt());
esb.setPassword(PBKD2FUtils.getPBKDF2(esb.getCard_Number(), esb.getSalt()));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (DecoderException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
});
return emp;
}
public static List<Org> getOrgList(){
JSONObject params = new JSONObject();
params.put("SOURCEID","FMS");
params.put("DESTINATIONID","ESB");
params.put("TYPE","FMS");
params.put("IPADDRESS","192.168.119.30");
params.put("BO","syncORGInfoToFms");
String result = HttpRequest.put(url)
.header("Content-Type", "application/json")//头信息,多个头信息多次调用此方法即可
.header("UrlType", "2")//头信息,多个头信息多次调用此方法即可
.body(params.toString())
.execute().body();
result = result.substring(result.indexOf("["),result.length()-17);
// System.out.println(result);
List<Org> emp = new ArrayList<>();
List<Org> employeeList = com.alibaba.fastjson.JSONObject.parseArray(result, Org.class);
for (int j = 0; j < employeeList.size(); j++) {
emp.add(employeeList.get(j));
}
System.out.println("共"+emp.size());
// emp.stream().forEach(System.out::println);
return emp;
}
}
根据返回的包含id和父id的集合,自动构建目录树工具类
首先是TreeNodeVo实体
package com.donlim.fms.common.helper;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.List;
/**
* @ClassName TreeNodeVo
* @Description TODO
* @Author 李安山
* @DATE 2023/2/16 11:47
* <p>
* Version 1.0
**/
@Data
@Accessors(chain = true)
public class TreeNodeVO implements Serializable {
private static final long serialVersionUID = 1L;
// 节点ID
private String id;
private String value;
// 父节点ID
private String pId;
// 节点名称
private String label;
// 节点复选框选中状态
// private Boolean checked;
//节点图标样式类
private String icon;
//URL链接
// private String url;
private int sort_no;
// private String is_enable;
private Integer size;
// 子孙节点集合
private List<TreeNodeVO> children;
}
构建目录树的工具类
package com.donlim.fms.common.helper;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
/**
* @ClassName TreeBuilder
* @Description TODO
* @Author 李安山
* @DATE 2023/2/16 11:49
* <p>
* Version 1.0
**/
public class TreeBuilder {
@Getter
@Setter
List<TreeNodeVO> nodeVOs = Lists.newArrayList();
public TreeBuilder() {
}
public TreeBuilder(List<TreeNodeVO> nodeVOs) {
super();
this.nodeVOs = nodeVOs;
}
public String buildJSONTree() {
List<TreeNodeVO> nodeTree = buildTree();
return JSON.toJSONString(nodeTree);
}
public List<TreeNodeVO> buildTree() {
List<TreeNodeVO> treeNodeVOs = Lists.newArrayList();
List<TreeNodeVO> rootTreeNodeVOs = getParentTreeNodes();
for (TreeNodeVO rootTreeNodeVO : rootTreeNodeVOs) {
buildChildTreeNodes(rootTreeNodeVO);
treeNodeVOs.add(rootTreeNodeVO);
}
return treeNodeVOs;
}
private void buildChildTreeNodes(TreeNodeVO node) {
List<TreeNodeVO> children = getChildTreeNodes(node);
if (!children.isEmpty()) {
for (TreeNodeVO child : children) {
buildChildTreeNodes(child);
}
node.setChildren(children);
}
}
public List<TreeNodeVO> getChildTreeNodes(TreeNodeVO pnode) {
List<TreeNodeVO> childTreeNodeVOs = Lists.newArrayList();
for (TreeNodeVO nodeVO : nodeVOs) {
if (pnode.getId().equals(nodeVO.getPId())) {
childTreeNodeVOs.add(nodeVO);
}
}
return childTreeNodeVOs;
}
private boolean isParentNode(TreeNodeVO node) {
boolean isParentNode = true;
for (TreeNodeVO nodeVO : nodeVOs) {
if (node.getPId().equals(nodeVO.getId())) {
isParentNode = false;
break;
}
}
return isParentNode;
}
public List<TreeNodeVO> getParentTreeNodes() {
List<TreeNodeVO> rootTreeNodeVOs = Lists.newArrayList();
for (TreeNodeVO nodeVO : nodeVOs) {
if (isParentNode(nodeVO)) {
rootTreeNodeVOs.add(nodeVO);
}
}
return rootTreeNodeVOs;
}
}
最后根据返回的集合生成目录结构,主要设置treenodevo对象的id,pid,value,lable参数
@Override
public List<TreeNodeVO> getOrgInfo() {
List<Org> orgList = HttpUtil.getOrgList();
List<TreeNodeVO> treeNodeVOS = new ArrayList<>();
for (int i = 0; i < orgList.size(); i++) {
Org o = orgList.get(i);
String orgName = o.getOrgName();
TreeNodeVO treeNodeVO = new TreeNodeVO();
treeNodeVO.setId(o.getOrgId());
treeNodeVO.setValue(o.getOrgId());
treeNodeVO.setPId(o.getSuperId());
treeNodeVO.setLabel(orgName.substring(orgName.lastIndexOf("-")+1));
treeNodeVOS.add(treeNodeVO);
}
treeNodeVOS = new TreeBuilder(treeNodeVOS).buildTree();
return treeNodeVOS;
}
controller类
package com.donlim.fms.controller.hrms;
import com.donlim.fms.common.dto.Dtos;
import com.donlim.fms.common.helper.TreeNodeVO;
import com.donlim.fms.service.EmployeeSyncService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* @ClassName EmployeeSyncController
* @Description TODO
* @Author 李安山
* @DATE 2023/2/16 8:53
* <p>
* Version 1.0
**/
@RestController
@Slf4j
@Api(tags = "总线人员组织同步接口")
@RequestMapping("/api")
public class EmployeeSyncController {
@Autowired
private EmployeeSyncService employeeSyncService;
@PostMapping("/employSync")
@ApiOperation("人员同步接口")
public String employeeSync(HttpServletRequest request) {
return employeeSyncService.employeeSync(Dtos.newDto(request));
}
@GetMapping("/getOrg")
@ApiOperation("人员同步接口")
public List<TreeNodeVO> getOrg(HttpServletRequest request) {
return employeeSyncService.getOrgInfo();
}
@GetMapping("/updateNullPassword")
@ApiOperation("员工密码初始化")
public void updatePassword(HttpServletRequest request) {
employeeSyncService.updatePassword();
}
}
前端vue+elementui,用级联选择器显示目录树
<el-form-item label="选择同步组织">
<div class="block">
<el-cascader
v-model="syncParams.id"
:options="treeNodeVos"
@change="handleChange"></el-cascader>
</div>
</el-form-item>
data中添加treeNodeVos对象
treeNodeVos:[],
methods中添加后台调用方法,此方法可以在点击页面加载时调用,看自己,还有文章来源:https://www.toymoban.com/news/detail-752932.html
handleChange(value) {
console.log(value);
},
getOrgs() {
this.company_id = '';
this.$fetch("/api/getOrg", null).then(re => {
this.treeNodeVos= re;
});
},
对应处理时间较长的接口,使用@Async注解,需要在启动类上加@EnableAsync
@Override
@Async
public String employeeSync(Dto dto){
List<ESB> employeeList = HttpUtil.getEmployeeList(dto);
String id = dto.getString("id").substring(dto.getString("id").lastIndexOf(",")+1,dto.getString("id").lastIndexOf("]"));
log.info("id==>{}",id);
log.info("查询到{}个员工",employeeList.size());
for (int i = 0; i <employeeList.size() ; i++) {
ESB e = employeeList.get(i);
if (ObjectUtils.isNotEmpty(e)){
if (StringUtils.isNotBlank(e.getORGID())){
if(!e.getORGID().equals(id)){
employeeList.remove(e);
}
}
}
}
if (employeeList.size()==0){
return "没有要新增的员工";
}
int sum = 0;
if (employeeList.size()>1000){
for (int i = 0; i < employeeList.size()/300; i++) {
if ((i+1)*300>employeeList.size()){
sum+= esbMapper.insertESB(employeeList.subList(i*300,employeeList.size()));
}
sum+= esbMapper.insertESB(employeeList.subList(i*300,(i+1)*300+1));
}
}else {
sum= esbMapper.insertESB(employeeList);
}
log.info("新增{}个员工",sum);
return "新增"+sum+"个员工";
}
总结:使用第三方工具类,开发简单不少,好用的工具类大大提高开发效率。文章来源地址https://www.toymoban.com/news/detail-752932.html
到了这里,关于记一次使用hutool的http工具调用外部接口同步人员信息的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!