package com.kingdee.eas.custom.beisen.synchronouspos.osf; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Maps; import com.kingdee.bos.BOSException; import com.kingdee.bos.Context; import com.kingdee.bos.bsf.service.app.IHRMsfService; import com.kingdee.bos.rabbitmq.guava.Lists; import com.kingdee.eas.common.EASBizException; import com.kingdee.eas.custom.beisen.utils.BeiSenUtils; import com.kingdee.eas.custom.beisen.utils.BeisenParamByPropertiesUtil; import com.kingdee.eas.custom.beisen.utils.Helper; import com.kingdee.eas.util.app.DbUtil; import com.kingdee.jdbc.rowset.IRowSet; import java.sql.SQLException; import java.util.HashMap; import java.util.List; import java.util.Map; public class SyncStaffManageOSFService implements IHRMsfService { @Override public Object process(Context context, Map map) throws EASBizException, BOSException { Map>> result = Maps.newHashMap(); List> list = Lists.newArrayList(); List> listMap = Lists.newArrayList(); result.put("list",list); result.put("listMap",listMap); String posPath = (String) map.get("posPath"); String orgPath = (String) map.get("orgPath"); StringBuilder sql = new StringBuilder(); sql.append(" "); sql.append(" select fid,FYear,FTableName from T_HR_StaffManageProject "); sql.append(" where FRule in ( "); sql.append(" select fid from T_HR_StaffManageRule where fid in ( "); sql.append(" select FStaffManageRuleId from T_HR_STAFFMANAGERULEFIELD where FColumn in ('FpositionID', 'FAdminOrgID') group by FStaffManageRuleId HAVING count(fid)>1 "); sql.append(" ) and FState = 1 "); sql.append(" ) "); sql.append(" and FState = 1 "); sql.append(" and FChannel = 1 "); IRowSet rs = DbUtil.executeQuery(context, sql.toString()); try{ BeiSenUtils b = new BeiSenUtils(context); Helper helper = b.helper; BeisenParamByPropertiesUtil posUtil = new BeisenParamByPropertiesUtil(posPath); Map poskeyMap = posUtil.getConfig(); BeisenParamByPropertiesUtil orgUtil = new BeisenParamByPropertiesUtil(orgPath); Map orgkeyMap = orgUtil.getConfig(); while (rs.next()) { String fid = rs.getString("fid"); String FTableName = rs.getString("FTableName"); String FYear = rs.getString("FYear"); StringBuilder selSql = new StringBuilder(); selSql.append(" select 'org' orgpos,org.fid posId,org.fname_l2 orgPosName,org.FDisplayName_L2 staffName, "); selSql.append(" org.CFOriginalId,(case when staff.FControlWay=0 then 3 when staff.FControlWay=1 then 2 else 1 end) FControlWay, "); selSql.append(" staff.FPersonCount,staff.FStaffYear,staff.FYearActualStart, "); selSql.append(" staff.FOnWayAddCount,staff.FOnWaySubCount, "); selSql.append(" (staff.FStaffYear-staff.FOnWayAddCount+staff.FOnWaySubCount-staff.FPersonCount) FStaffYearUsable, "); selSql.append(" (staff.FState+1) FState "); selSql.append(" from "); selSql.append(" "+FTableName+" staff "); selSql.append(" left join t_org_admin org on org.fid=staff.FAdminOrgID "); selSql.append(" left join t_org_position pos on pos.fid = staff.FpositionID "); selSql.append(" where pos.fid is null and org.CFOriginalId is not null "); selSql.append(" UNION all "); selSql.append(" select 'pos' orgpos,org.fid posId,org.fname_l2 orgPosName,org.FDisplayName_L2 staffName,pos.CFOriginalId, "); selSql.append(" (case when staff.FControlWay=0 then 3 when staff.FControlWay=1 then 2 else 1 end) FControlWay, "); selSql.append(" staff.FPersonCount,staff.FStaffYear,staff.FYearActualStart, "); selSql.append(" staff.FOnWayAddCount,staff.FOnWaySubCount, "); selSql.append(" (staff.FStaffYear-staff.FOnWayAddCount+staff.FOnWaySubCount-staff.FPersonCount) FStaffYearUsable, "); selSql.append(" (staff.FState+1) FState "); selSql.append(" from "); selSql.append(" "+FTableName+" staff "); selSql.append(" left join t_org_admin org on org.fid=staff.FAdminOrgID "); selSql.append(" left join t_org_position pos on pos.fid = staff.FpositionID "); selSql.append(" where pos.fid is not null and pos.CFOriginalId is not null "); IRowSet rsSel = DbUtil.executeQuery(context, selSql.toString()); // 获取访问令牌 String token = b.getAccessToken(); while (rsSel.next()){ String orgpos= rsSel.getString("orgpos"); if("org".equals(orgpos)){ Map pos = this.getPostData(orgkeyMap,rsSel); if(null!=pos){ // 若访问令牌不为空且开始时间和结束时间不为空 if (!com.kingdee.util.StringUtils.isEmpty(token)) { // 创建请求头的 Map Map header = new HashMap(); // 设置请求头的 Content-Type header.put("Content-Type", "application/json"); // 设置请求头的 Authorization header.put("Authorization", "Bearer " + token); String url = "https://openapi.italent.cn/UserFrameworkApiV3/api/v1/departments/Put"; JSONObject requestBody = new JSONObject(pos); System.out.println("url:"+url); System.out.println("requestBody:"+requestBody); JSONObject responseJson = helper.getURL(url,header, requestBody, "PUT",requestBody.getString("posId"),"更新","北森"); System.out.println("responseJson:"+responseJson); listMap.add(responseJson); list.add(pos); } } }else if ("pos".equals(orgpos)){ Map pos = this.getPostData(poskeyMap,rsSel); if(null!=pos){ // 若访问令牌不为空且开始时间和结束时间不为空 if (!com.kingdee.util.StringUtils.isEmpty(token)) { // 创建请求头的 Map Map header = new HashMap(); // 设置请求头的 Content-Type header.put("Content-Type", "application/json"); // 设置请求头的 Authorization header.put("Authorization", "Bearer " + token); String url = "https://openapi.italent.cn/RecruitV6/api/v1/RecruitOnBoarding/UpdatePost"; JSONObject requestBody = new JSONObject(pos); System.out.println("url:"+url); System.out.println("requestBody:"+requestBody); JSONObject responseJson = helper.getURL(url,header, requestBody, "PUT",requestBody.getString("posId"),"更新","北森"); System.out.println("responseJson:"+responseJson); listMap.add(responseJson); list.add(pos); } } } } } }catch (Exception e){ e.printStackTrace(); }finally { } return result; } /** * shr系统数据组装北森请求参数 * * @param keyMap * @param rowSet * @return * @throws SQLException */ protected Map getPostData(Map keyMap, IRowSet rowSet) throws SQLException { Map pos = Maps.newHashMap(); if (null!=rowSet){ for(Map.Entry entry : keyMap.entrySet()){ String shrKey = entry.getKey(); String beisenKey = entry.getValue(); if(shrKey.indexOf(".")>0){ String[] keys=shrKey.split("\\."); if(2==keys.length){ Map map1 = (Map) pos.get(keys[0]); if(null==map1){ map1=Maps.newHashMap(); pos.put(keys[0],map1); } String valueStr = rowSet.getString(keys[1]); if(beisenKey.indexOf(",")>0) { String[] beisenKeys = beisenKey.split(","); Object value = null; if("integer".equals(beisenKeys[1])){ if(null!=valueStr){ value=Integer.parseInt(valueStr); } }else { value = valueStr; } if(null!=value) { map1.put(beisenKeys[0], value); } }else { if(null!=valueStr) { map1.put(beisenKey, valueStr); } } }else if (3==keys.length){ Map map1 = (Map) pos.get(keys[0]); if(null==map1){ map1=Maps.newHashMap(); pos.put(keys[0],map1); } Map map2 = (Map) map1.get(keys[1]); if(null==map2){ map2=Maps.newHashMap(); map1.put(keys[1],map1); } String valueStr = rowSet.getString(keys[2]); if(beisenKey.indexOf(",")>0) { String[] beisenKeys = beisenKey.split(","); Object value = null; if("integer".equals(beisenKeys[1])){ if(null!=valueStr){ value=Integer.parseInt(valueStr); } }else { value = valueStr; } if(null!=value) { map2.put(beisenKeys[0], value); } }else { if(null!=valueStr) { map2.put(beisenKey,valueStr); } } } }else { String valueStr = rowSet.getString(shrKey); if(beisenKey.indexOf(",")>0) { String[] beisenKeys = beisenKey.split(","); Object value = null; if("integer".equals(beisenKeys[1])){ if(null!=valueStr){ value=Integer.parseInt(valueStr); } }else { value = valueStr; } if(null!=value) { pos.put(beisenKeys[0],value); } }else { if(null!=valueStr) { pos.put(beisenKey,valueStr); } } } } } return pos; } }