定时任务同步泛微的人员对应的成本中心

This commit is contained in:
zengweihai 2024-05-31 16:53:15 +08:00
parent b37cbebe5d
commit 0af8d62b71
1 changed files with 136 additions and 0 deletions

View File

@ -0,0 +1,136 @@
package shkd.cosmic.cxkg.task;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.exception.KDException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import shkd.cosmic.cxkg.task.DTO.Query;
import shkd.cosmic.cxkg.task.DTO.QueryDTO;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
public class CostCenterTaskImpl extends AbstractTask {
private static Logger logger = LoggerFactory.getLogger(HrmDepartmentTaskImpl.class);
private static final int PAGESIZE = 20;
private int CURPAGE = 1;
private String base_custom_data = "field0,field1";
@Override
public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
//构造请求体
QueryDTO queryDTO = new QueryDTO();
queryDTO.setPagesize(PAGESIZE);
queryDTO.setCurpage(CURPAGE);
queryDTO.setBase_custom_data(base_custom_data);
Query query = new Query();
query.setQueryDTO(queryDTO);
Object isFull = map.get("isfull");//判断是否全量
if ("0".equals(isFull)) {//判断是否全量不是则设置时间戳参数
Calendar calendar = Calendar.getInstance();// 获取当前日期
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Long time = calendar.getTimeInMillis();
queryDTO.setCreated(time.toString());
}
//创建人员对应成本中心集合
HashMap<String, String> orgMap = new HashMap<>();
JSONObject data = this.getOAUserData(query);
this.importOrgMap(orgMap,data);//填充人员数据人员编码成本中心编码
BigDecimal totalSize = data.getBigDecimal("totalSize");//获取查询到人员总数
BigDecimal size = new BigDecimal(PAGESIZE);//设置页大小为20
BigDecimal pageNumber = totalSize.divide(size, 0, RoundingMode.UP);//计算需要分页数量
//判断分页数量是否大于1是则进行遍历后续数据
if (pageNumber.compareTo(new BigDecimal(1)) > 0) {//当页数为1时不需要再次查询接口直接去同步数据
for (int i = 1; i < pageNumber.intValue(); i++){//查询成功取第一轮数据同步
//修改CURPAGE当前页数
CURPAGE = i + 1;
queryDTO.setCurpage(CURPAGE);
JSONObject dataNext = this.getOAUserData(query);//获取查询的数据
this.importOrgMap(orgMap,dataNext);//填充人员数据人员编码成本中心编码
}
}
OperateOption option = OperateOption.create();
if (!orgMap.isEmpty()){
Set<String> keys = orgMap.keySet();
for (String key : keys) {
QFilter userQF = new QFilter("number", QCP.equals, key);
DynamicObject bos_user = BusinessDataServiceHelper.loadSingle("bos_user", userQF.toArray());//查找星瀚对应人员
if (bos_user != null){//当人员数据不为空时
String costNumber = orgMap.get(key);//对应的成本中心编码
QFilter costQFr = new QFilter("number", QCP.equals, costNumber);
DynamicObject bos_costCenter = BusinessDataServiceHelper.loadSingle("bos_costcenter", costQFr.toArray());
if (bos_costCenter != null){//当成本中心不为空时
//获取用户的成本中心信息分录
DynamicObjectCollection costEntity = bos_user.getDynamicObjectCollection("shkd_costentity");
//首先遍历移除已存在的来源为泛微的数据
for (int i = 0;i < costEntity.size();i++){
DynamicObject dynamicObject = costEntity.get(i);
String shkd_e_source = dynamicObject.getString("shkd_e_source");
if ("B".equals(shkd_e_source)){
int index = costEntity.indexOf(dynamicObject);
costEntity.remove(index);
}
}
DynamicObject newObject = costEntity.addNew();
newObject.set("shkd_e_cost",bos_costCenter);
newObject.set("shkd_e_source","B");
OperationResult operationResult = OperationServiceHelper.executeOperate("save", "bos_user", new DynamicObject[]{bos_user}, option);
}
}
}
}
}
private JSONObject getOAUserData(Query query) {
String requestBody = JSON.toJSONString(query);
//返回参数
String json = HttpRequest.post("http://" + "10.157.226.9:8088" + "/api/hrm/resful/getHrmUserInfoWithPage").body(requestBody).execute().body();
JSONObject resultJson = JSONObject.parseObject(json);;//查询oa接口返回人员数据
String code = resultJson.getString("code");//第一次执行查询判断接口调用是否成功否则不执行后续逻辑
if (!Objects.equals("1",code)){//当code不为1时调用失败
logger.info("调用接口失败"+resultJson.getString("msg"));
return null;
}
JSONObject data = resultJson.getJSONObject("data");
return data ;
}
private void importOrgMap(HashMap<String, String> orgMap,JSONObject data){
JSONArray dataList = data.getJSONArray("dataList");//oa人员数据集合
for (int i = 0; i < dataList.size(); i++) {
JSONObject userData = dataList.getJSONObject(i);
JSONObject base_custom_data = userData.getJSONObject("base_custom_data");
if (base_custom_data != null){
String field0 = base_custom_data.getString("field0");
if (!"".equals(field0)){
orgMap.put(userData.getString("workcode"),field0);
}
}
}
}
}