Browse Source

同步组织提交

“luojun” 7 months ago
parent
commit
40d6acdd87

+ 112 - 0
websrc/com/kingdee/eas/custom/synctask/entry/AdminOrg.java

@@ -0,0 +1,112 @@
+package com.kingdee.eas.custom.synctask.entry;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+/**
+ * @author qingwu
+ * @date 2024/10/21
+ * @apiNote
+ */
+public class AdminOrg {
+    @JsonProperty("@action")
+    private String action;//add 新增,edit 编辑 delete 删除(只会处理canceled字段)
+    private String code;//编号
+    private String shortname;//简称
+    private String fullname;//全称
+    private String org_code;// 分部编号
+    private String parent_code;//上级部门编号
+    private String canceled;//封存标志 ,(@action 为 delete 有效, 0 表示封存该分
+    private String order;//排序
+
+    public AdminOrg() {
+    }
+
+    public AdminOrg(String action, String code, String shortname, String fullname, String org_code, String parent_code, String canceled, String order) {
+        this.action = action;
+        this.code = code;
+        this.shortname = shortname;
+        this.fullname = fullname;
+        this.org_code = org_code;
+        this.parent_code = parent_code;
+        this.canceled = canceled;
+        this.order = order;
+    }
+
+    public void setAction(String action) {
+        this.action = action;
+    }
+
+    public void setCode(String code) {
+        this.code = code;
+    }
+
+    public void setShortname(String shortname) {
+        this.shortname = shortname;
+    }
+
+    public void setFullname(String fullname) {
+        this.fullname = fullname;
+    }
+
+    public void setOrg_code(String org_code) {
+        this.org_code = org_code;
+    }
+
+    public void setParent_code(String parent_code) {
+        this.parent_code = parent_code;
+    }
+
+    public void setCanceled(String canceled) {
+        this.canceled = canceled;
+    }
+
+    public void setOrder(String order) {
+        this.order = order;
+    }
+
+    public String getAction() {
+        return action;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public String getShortname() {
+        return shortname;
+    }
+
+    public String getFullname() {
+        return fullname;
+    }
+
+    public String getOrg_code() {
+        return org_code;
+    }
+
+    public String getParent_code() {
+        return parent_code;
+    }
+
+    public String getCanceled() {
+        return canceled;
+    }
+
+    public String getOrder() {
+        return order;
+    }
+
+    @Override
+    public String toString() {
+        return "AdminOrg{" +
+                "action='" + action + '\'' +
+                ", code='" + code + '\'' +
+                ", shortname='" + shortname + '\'' +
+                ", fullname='" + fullname + '\'' +
+                ", org_code='" + org_code + '\'' +
+                ", parent_code='" + parent_code + '\'' +
+                ", canceled='" + canceled + '\'' +
+                ", order='" + order + '\'' +
+                '}';
+    }
+}

+ 60 - 0
websrc/com/kingdee/eas/custom/synctask/handler/OrgUnitEditHandlerEx.java

@@ -0,0 +1,60 @@
+package com.kingdee.eas.custom.synctask.handler;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.kingdee.bos.BOSException;
+import com.kingdee.bos.Context;
+import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
+import com.kingdee.eas.custom.synctask.ActionTypeEnum;
+import com.kingdee.eas.custom.synctask.utils.SynctaskUtils;
+import com.kingdee.eas.framework.CoreBaseInfo;
+import com.kingdee.eas.hr.org.web.handler.OrgUnitEditHandler;
+import com.kingdee.shr.base.syssetting.context.SHRContext;
+import com.kingdee.shr.base.syssetting.exception.SHRWebException;
+import org.apache.log4j.Logger;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * @author qingwu
+ * @date 2024/10/21
+ * @apiNote
+ */
+public class OrgUnitEditHandlerEx extends OrgUnitEditHandler {
+    Logger logger = Logger.getLogger("com.kingdee.eas.custom.synctask.handler.OrgUnitEditHandlerEx");
+    private Context ctx = SHRContext.getInstance().getContext();
+
+    @Override
+    protected void afterSave(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo model) throws SHRWebException {
+        super.afterSave(request, response, model);
+        AdminOrgUnitInfo adminOrgUnitInfo = (AdminOrgUnitInfo) model;
+        //获取操作状态
+        String operateState = request.getParameter("operateState");
+        SynctaskUtils synctaskUtils = new SynctaskUtils();
+        try {
+            //ADDNEW  EDIT
+            //新增方法
+            if ("ADDNEW".equals(operateState)) {
+                synctaskUtils._syncOrgUnitToOA(ctx, adminOrgUnitInfo.getId().toString(), ActionTypeEnum.ADD);
+            }
+            //编辑方法
+            if ("EDIT".equals(operateState)) {
+                synctaskUtils._syncOrgUnitToOA(ctx, adminOrgUnitInfo.getId().toString(), ActionTypeEnum.EDIT);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new RuntimeException(e);
+        }
+        //Enumeration paramNames = request.getParameterNames();
+        //while (paramNames.hasMoreElements()) {
+        //    String paramName = (String) paramNames.nextElement();
+        //    String[] paramValues = request.getParameterValues(paramName);
+        //    if (paramValues.length == 1) {
+        //        String paramValue = paramValues[0];
+        //        if (paramValue.length() != 0) {
+        //            logger.error("参数:" + paramName + "=" + paramValue);
+        //        }
+        //    }
+        //}
+    }
+}

+ 36 - 0
websrc/com/kingdee/eas/custom/synctask/handler/OrgUnitListHandlerEx.java

@@ -0,0 +1,36 @@
+package com.kingdee.eas.custom.synctask.handler;
+
+import com.kingdee.bos.BOSException;
+import com.kingdee.bos.Context;
+import com.kingdee.eas.custom.synctask.utils.SynctaskUtils;
+import com.kingdee.eas.hr.org.web.handler.OrgUnitListHandler;
+import com.kingdee.shr.base.syssetting.context.SHRContext;
+import com.kingdee.shr.base.syssetting.exception.SHRWebException;
+import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
+import com.kingdee.util.StringUtils;
+import org.apache.log4j.Logger;
+import org.springframework.ui.ModelMap;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * @author qingwu
+ * @date 2024/10/21
+ * @apiNote
+ */
+public class OrgUnitListHandlerEx extends OrgUnitListHandler {
+    Logger logger = Logger.getLogger("com.kingdee.eas.custom.synctask.handler.OrgUnitListHandlerEx");
+    private Context ctx = SHRContext.getInstance().getContext();
+
+    public void syncOrgUnitToOAAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
+        String billIds = request.getParameter("billIds");
+        try {
+            SynctaskUtils synctaskUtils = new SynctaskUtils();
+            synctaskUtils._syncOrgUnitToOA(ctx, billIds == "" ? null : billIds, null);
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new RuntimeException(e);
+        }
+    }
+}

+ 221 - 0
websrc/com/kingdee/eas/custom/synctask/utils/SynctaskUtils.java

@@ -0,0 +1,221 @@
+package com.kingdee.eas.custom.synctask.utils;
+
+import com.alibaba.fastjson.JSONObject;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.kingdee.bos.BOSException;
+import com.kingdee.bos.Context;
+import com.kingdee.bos.metadata.entity.EntityViewInfo;
+import com.kingdee.bos.metadata.entity.FilterInfo;
+import com.kingdee.bos.metadata.entity.FilterItemInfo;
+import com.kingdee.bos.metadata.entity.SelectorItemCollection;
+import com.kingdee.bos.metadata.query.util.CompareType;
+import com.kingdee.eas.basedata.org.AdminOrgUnitCollection;
+import com.kingdee.eas.basedata.org.AdminOrgUnitFactory;
+import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
+import com.kingdee.eas.basedata.org.IAdminOrgUnit;
+import com.kingdee.eas.custom.synctask.ActionTypeEnum;
+import com.kingdee.eas.custom.synctask.entry.AdminOrg;
+import com.kingdee.util.StringUtils;
+import okhttp3.*;
+import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.log4j.Logger;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.*;
+
+/**
+ * @author qingwu
+ * @date 2024/10/21
+ * @apiNote
+ */
+public class SynctaskUtils {
+    public static Logger logger = Logger.getLogger("com.kingdee.eas.custom.synctask.utils.SynctaskUtils");
+    private Properties propt = new Properties();
+
+    public SynctaskUtils() {
+        try {
+            this.propt.load(new FileInputStream("E:\\Kingdee\\eas\\server\\properties\\scy\\synOAConfig.properties"));
+            //propt.load(new FileInputStream(System.getProperty("EAS_HOME") + "/server/properties/scy/synOAConfig.properties"));
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 同步组织
+     * 1、当参数billIds不为空值时,增量同步参数中的部门数据到OA
+     * 2、当参数billIds为空时,全量同步所有组织数据到OA
+     * 3、当参数action不为空时,根据操作类型同步数据
+     * 4、当参数action为空时,判断部门状态是否为失效,如果失效就执行删除同步,否则就执行编辑同步
+     * 5、部门新增,调用部门同步方法增量同步,操作类型为新增
+     *
+     * @param billds
+     * @param action
+     * @throws BOSException
+     */
+    public void _syncOrgUnitToOA(Context ctx, String billds, ActionTypeEnum action) throws BOSException, IOException {
+        Set adminOrgIds = new HashSet();
+        IAdminOrgUnit iAdminOrgUnit = AdminOrgUnitFactory.getLocalInstance(ctx);
+        String actionType = action == null ? "" : action.getValue().toLowerCase();
+        //当参数billIds不为空值时,增量同步参数中的部门数据到OA
+        if (!StringUtils.isEmpty(billds)) {
+            String[] split = billds.split(",");
+            for (String adminOrgId : split) {
+                adminOrgIds.add(adminOrgId);
+            }
+            ////当参数action为空时,判断部门状态是否为失效,如果失效就执行删除同步,否则就执行编辑同步
+            //if (!StringUtils.isEmpty(billds) && action == null) {
+            //    logger.error("_syncOrgUnitToOA---------incrementalQuery");
+            //    AdminOrgUnitCollection adminOrgColl = getAdminOrgColl(ctx, adminOrgIds);
+            //    saveSynOrgUnit(adminOrgColl, actionType);
+            //
+            //} else if (!StringUtils.isEmpty(billds) && action != null) {//当参数action不为空时,根据操作类型同步数据
+            //    logger.error("_syncOrgUnitToOA---------saveQuery");
+            //    //新增同步
+            //    if (ActionTypeEnum.ADD.getValue().equals(action.getValue())) {
+            //        logger.error("_syncOrgUnitToOA---------add");
+            //        AdminOrgUnitCollection adminOrgColl = getAdminOrgColl(ctx, adminOrgIds);
+            //        saveSynOrgUnit(adminOrgColl, actionType);
+            //    }
+            //    //编辑同步
+            //    if (ActionTypeEnum.EDIT.getValue().equals(action.getValue())) {
+            //        logger.error("_syncOrgUnitToOA---------edit");
+            //        AdminOrgUnitCollection adminOrgColl = getAdminOrgColl(ctx, adminOrgIds);
+            //        saveSynOrgUnit(adminOrgColl, actionType);
+            //    }
+            //}
+            AdminOrgUnitCollection adminOrgColl = getAdminOrgColl(ctx, adminOrgIds);
+            saveSynOrgUnit(adminOrgColl, actionType);
+        } else {
+            if (StringUtils.isEmpty(billds) && action == null) { //当参数billIds为空时,全量同步所有组织数据到OA
+                logger.error("_syncOrgUnitToOA---------batchQuery");
+                SelectorItemCollection sic = new SelectorItemCollection();
+                sic.add("*");
+                sic.add("parent.*");
+                AdminOrgUnitCollection adminOrgUnitCollection = iAdminOrgUnit.getAdminOrgUnitCollection(EntityViewInfo.getInstance(null, sic, null));
+                if (adminOrgUnitCollection.size() > 0) {
+                    saveSynOrgUnit(adminOrgUnitCollection, actionType);
+                }
+            }
+        }
+    }
+
+    /**
+     * 批量查询行政组织数据
+     *
+     * @param ctx
+     * @param adminOrgIds
+     * @return
+     * @throws BOSException
+     */
+    public AdminOrgUnitCollection getAdminOrgColl(Context ctx, Set adminOrgIds) throws BOSException {
+        IAdminOrgUnit iAdminOrgUnit = AdminOrgUnitFactory.getLocalInstance(ctx);
+        SelectorItemCollection sic = new SelectorItemCollection();
+        sic.add("*");
+        sic.add("parent.*");
+        FilterInfo filterInfo = new FilterInfo();
+        filterInfo.getFilterItems().add(new FilterItemInfo("id", adminOrgIds, CompareType.INCLUDE));
+        EntityViewInfo entityViewInfo = EntityViewInfo.getInstance(filterInfo, sic, null);
+        return iAdminOrgUnit.getAdminOrgUnitCollection(entityViewInfo);
+    }
+
+    /**
+     * 新增方法
+     *
+     * @param adminOrgUnitCollection
+     */
+    public void saveSynOrgUnit(AdminOrgUnitCollection adminOrgUnitCollection, String action) throws IOException {
+        ObjectMapper mapper = new ObjectMapper();
+        //mapper.writeValueAsString();
+        //mapper.readValue(paramData, Map.class)
+        logger.error("saveSynOrgUnit----------------------start");
+        List saveList = new ArrayList();
+        for (int i = 0; i < adminOrgUnitCollection.size(); i++) {
+            AdminOrgUnitInfo adminOrgUnitInfo = adminOrgUnitCollection.get(i);
+            AdminOrg adminOrg = new AdminOrg();
+            boolean isSealUp = adminOrgUnitInfo.isIsSealUp();
+            if (isSealUp) {
+                adminOrg.setAction("delete");
+            } else {
+                adminOrg.setAction("add");
+            }
+            adminOrg.setCode(adminOrgUnitInfo.getNumber());
+            adminOrg.setShortname(adminOrgUnitInfo.getName());
+            adminOrg.setFullname(adminOrgUnitInfo.getName());
+            adminOrg.setParent_code("ITCS");
+            adminOrg.setOrg_code("0");
+            //adminOrg.setParent_code(adminOrgUnitInfo.getParent().getNumber());
+            adminOrg.setCanceled(adminOrgUnitInfo.isIsSealUp() == true ? "1" : "0");
+            //adminOrg.setOrder((String.valueOf(adminOrgUnitInfo.getIndex())));
+            saveList.add(adminOrg);
+        }
+        //封装请求参数
+        String data = mapper.writeValueAsString(saveList);
+        logger.error("saveSynOrgUnit----------------------data" + data);
+        String adminOrgApiUrl = "/api/hrm/resful/synDepartment";
+        Response response = synScyRequest(data, adminOrgApiUrl);
+        logger.error("response----------------------data" + mapper.writeValueAsString(response));
+    }
+
+
+    //public static void main(String[] args) throws JsonProcessingException {
+    //    ObjectMapper mapper = new ObjectMapper();
+    //    List saveList = new ArrayList();
+    //    for (int i = 0; i < 2; i++) {
+    //        AdminOrg adminOrg = new AdminOrg();
+    //        adminOrg.setAction("add");
+    //        saveList.add(adminOrg);
+    //    }
+    //    String s = mapper.writeValueAsString(saveList);
+    //    System.out.println(s);
+    //    System.out.println(ActionTypeEnum.EDIT.getValue().toLowerCase());
+    //
+    //}
+
+    /**
+     * 同步请求方法
+     *
+     * @param param
+     * @param aipUrl
+     * @return
+     * @throws IOException
+     */
+    public Response synScyRequest(String param, String aipUrl) throws IOException {
+        String url = this.propt.getProperty("url");
+        OkHttpClient client = new OkHttpClient();
+        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
+        String data = String.format("token = %s &data = %s", getToken(), param);
+        logger.error("synAdminOrgRequest----------------------date" + data);
+        RequestBody body = RequestBody.create(mediaType, data);
+        Request request = new Request.Builder()
+                .url(url + aipUrl)
+                .post(body)
+                .addHeader("Content-Type", "application/json")
+                .addHeader("Accept", "*/*")
+                .addHeader("Accept-Encoding", "gzip, deflate, br")
+                .addHeader("User-Agent", "PostmanRuntime-ApipostRuntime/1.1.0")
+                .addHeader("Connection", "keep-alive")
+                .build();
+        return client.newCall(request).execute();
+    }
+
+    /**
+     * 获取token
+     *
+     * @return
+     */
+    public String getToken() {
+        String key = this.propt.getProperty("key");
+        long l = System.currentTimeMillis();//时间戳毫秒数
+        String code = key.concat(Long.toString(l));
+        //String s = DigestUtils.md5Hex(code).toUpperCase();
+        String s = DigestUtils.md5Hex(code).toUpperCase();
+        System.out.println(s);
+        Map<String, String> map = new HashMap<>();
+        map.put("key", s);
+        map.put("ts", Long.toString(l));
+        logger.error("getToken" + JSONObject.toJSONString(map));
+        return JSONObject.toJSONString(map);
+    }
+}