SynADService.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package com.kingdee.shr.customer.gtiit.osf;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.HashSet;
  6. import java.util.Map;
  7. import java.util.Set;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.apache.log4j.Logger;
  10. import com.alibaba.fastjson.JSONArray;
  11. import com.alibaba.fastjson.JSONObject;
  12. import com.kingdee.bos.BOSException;
  13. import com.kingdee.bos.Context;
  14. import com.kingdee.bos.bsf.service.app.IHRMsfService;
  15. import com.kingdee.eas.common.EASBizException;
  16. import com.kingdee.eas.util.app.DbUtil;
  17. import com.kingdee.jdbc.rowset.IRowSet;
  18. /**
  19. * 同步AD域信息接口
  20. *
  21. * @author 何朗
  22. *
  23. */
  24. public class SynADService implements IHRMsfService {
  25. private static Logger logger = Logger.getLogger("com.kingdee.shr.customer.gtiit.osf.SynADService");
  26. @Override
  27. public Object process(Context ctx, Map map) throws EASBizException, BOSException {
  28. logger.error("进入同步AD域信息接口");
  29. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  30. // 开始日期
  31. String startDate = (String) map.get("startDate");
  32. // 截止日期
  33. String endDate = (String) map.get("endDate");
  34. // 员工工号
  35. String personNumber = (String) map.get("personNumber");
  36. logger.error("获取员工信息接口参数-开始日期:" + startDate + ",截止日期:" + endDate + ",人员:" + personNumber);
  37. if (StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)) {
  38. Calendar calendar = Calendar.getInstance();
  39. startDate = sdf.format(calendar.getTime());
  40. calendar.add(Calendar.DAY_OF_MONTH, 1);
  41. endDate = sdf.format(calendar.getTime());
  42. }
  43. JSONArray jsonArray = new JSONArray();
  44. Set<String> personIdSet = new HashSet<String>();
  45. try {
  46. // 入职审核通过、当天入职的员工
  47. String enrollSql = "SELECT b.fpersonid FROM T_HR_EmpEnrollBizBill a left join T_HR_EmpEnrollBizBillentry b on a.fid=b.fbillid where a.fbillstate = '3' and b.fenrolldate='"
  48. + startDate + "'";
  49. IRowSet enrollRow = DbUtil.executeQuery(ctx, enrollSql);
  50. while (enrollRow.next()) {
  51. personIdSet.add(enrollRow.getString("fpersonid"));
  52. }
  53. // 调动单审核通过的员工
  54. String fluctuaSql = "SELECT b.fpersonid FROM T_HR_FluctuationBizBill a left join T_HR_FluctuationBizBillentry b on a.fid=b.fbillid where FBILLSTATE ='3' and FLASTUPDATETIME >='"
  55. + startDate + "' and FLASTUPDATETIME <='" + endDate + "'";
  56. IRowSet fluctuaRow = DbUtil.executeQuery(ctx, fluctuaSql);
  57. while (fluctuaRow.next()) {
  58. personIdSet.add(fluctuaRow.getString("fpersonid"));
  59. }
  60. // 离职单审核通过、前一天离职的员工
  61. Calendar resignCalendar = Calendar.getInstance();
  62. resignCalendar.add(Calendar.DAY_OF_MONTH, -1);
  63. String resignSql = "select b.fpersonid from T_HR_ResignBizBill a left join T_HR_ResignBizBillentry b on a.fid=b.fbillid where a.FBILLSTATE = '3' and b.FBIZDATE = '"
  64. + sdf.format(resignCalendar.getTime()) + "'";
  65. IRowSet resignRow = DbUtil.executeQuery(ctx, resignSql);
  66. while (resignRow.next()) {
  67. personIdSet.add(resignRow.getString("fpersonid"));
  68. }
  69. // 预入职生效的员工
  70. String preSql = "select per.fid from T_HR_PreEntry pre left join T_BD_Person per on per.fnumber = pre.CFEMPNUMBER where FBILLSTATE = '3' and FCHECKINSTATE = '3' and CFEMPNUMBER <> '' and FBIZDATE >='"
  71. + startDate + "' and FBIZDATE <='" + endDate + "'";
  72. IRowSet preRow = DbUtil.executeQuery(ctx, preSql);
  73. while (preRow.next()) {
  74. personIdSet.add(preRow.getString("fid"));
  75. }
  76. // 员工信息变动的员工
  77. String lastDateSql = "SELECT fid FROM T_BD_Person where flastupdatetime>='" + startDate
  78. + "' and flastupdatetime<='" + endDate + "'";
  79. IRowSet lastDateRow = DbUtil.executeQuery(ctx, lastDateSql);
  80. while (lastDateRow.next()) {
  81. personIdSet.add(lastDateRow.getString("fid"));
  82. }
  83. if (StringUtils.isNotBlank(personNumber)) {
  84. personIdSet = new HashSet<String>();
  85. String personSql = "SELECT fid FROM T_BD_Person where fnumber = '" + personNumber + "'";
  86. IRowSet personRow = DbUtil.executeQuery(ctx, personSql);
  87. while (personRow.next()) {
  88. personIdSet.add(personRow.getString("fid"));
  89. }
  90. }
  91. logger.error("员工id集合:" + personIdSet.toString());
  92. StringBuilder sb = new StringBuilder();
  93. if (personIdSet.size() == 0) {
  94. sb.append("''");
  95. } else {
  96. for (String value : personIdSet) {
  97. sb.append("'").append(value).append("',");
  98. }
  99. // 移除最后一个逗号
  100. sb.deleteCharAt(sb.length() - 1);
  101. }
  102. StringBuffer sql = new StringBuffer();
  103. sql.append(
  104. " SELECT DISTINCT p.fname_l2 showname ,sj.cfusername as manager, fullp.fname_l1 AS extensionAttribute6 ,res.extensionAttribute4 ,cmpt.cfexpireddate as extensionAttribute10, emp.departmentname department,emp.positionname title ,p.fnumber personnumber,name personname,p.femail email,hpcc.cfprivateemail AS extensionAttribute9,p.cfusername sAMAccountName,p.cfgivenName givenName,p.cfsurname sn, \r\n"
  105. + " case when p.fgender='1' then '男' when p.fgender='2' then '女' else '' end as gender,ify.fname_l2 extensionAttribute5, \r\n"
  106. + " sj.fnumber personparent,zt.fname_l2 statusname,p.fcell as extensionAttribute11,p.fofficephone as telephoneNumber,la.fenterdate extensionAttribute3,nation.fname_l1 extensionAttribute8,\r\n"
  107. + " us.fpassword password,emp.jobname,emp.jobgradename FROM T_BD_Person p \r\n"
  108. + " left join (select e.fpersonid personid,a.fname_l2 departmentname,g.fname_l2 positionname,job.fname_l2 jobname,gra.fname_l2 jobgradename \r\n"
  109. + " from (SELECT FPERSONID,max(FEFFDT) as maxDate FROM T_HR_EmpPostExperienceHis where fassigntype='1' group by FPERSONID) as h \r\n"
  110. + " left join T_HR_EmpPostExperienceHis e on e.fpersonid= h.FPERSONID and e.feffdt = h.maxDate \r\n"
  111. + " left join T_ORG_Admin a on a.fid=e.fadminorgid \r\n"
  112. + " left join T_ORG_Position g on g.fid=e.fpositionid \r\n"
  113. + " left join T_ORG_Job job on job.fid = g.fjobid \r\n"
  114. + " left join (SELECT * FROM T_HR_EmpPostRank where fislatest='1') r on r.fpersonid = e.fpersonid \r\n"
  115. + " left join T_HR_JobGrade gra on gra.fid=r.fjobgradeid where e.fassigntype='1') emp on p.fid=emp.personid \r\n"
  116. + " left join T_HR_EmployeeClassify ify on ify.fid = p.cfpersontypeid \r\n"
  117. + " LEFT JOIN T_BD_Nationality nation ON nation.fid = p.fnationalityid \r\n"
  118. + " left join CT_MP_Fullorpart fullp on fullp.fid=p.cfftorptid \r\n"
  119. + " left join (select hpca.fpersonid,hpca.cfprivateemail from T_HR_PersonContactMethod hpca inner join (select fpersonid,max(flastupdatetime) maxDate from T_HR_PersonContactMethod group by fpersonid) hpcb on hpca.fpersonid=hpcb.fpersonid and hpca.flastupdatetime=hpcb.maxDate) hpcc on hpcc.fpersonid=p.fid \r\n"
  120. + " left join (select ctpa.fpersonid,ctpa.cfexpireddate from CT_MP_Pcontractinfo ctpa inner join (select fpersonid,max(cfexpireddate) maxDate from CT_MP_Pcontractinfo group by fpersonid ) ctpb on ctpa.fpersonid=ctpb.fpersonid and ctpa.cfexpireddate=ctpb.maxDate) cmpt on cmpt.fpersonid=p.fid \r\n"
  121. + " left join (select p.fname_l2 as parentName, p.cfusername ,re.FPERSONID,p.cfsurname,p.fname_l2 name,p.fnumber from (SELECT FPERSONID,max(flastupdatetime) as maxDate FROM T_HR_EmpOrgRelation group by FPERSONID) as re \r\n"
  122. + " left join T_HR_EmpOrgRelation rl on re.fpersonid = rl.fpersonid and re.maxDate = rl.flastupdatetime \r\n"
  123. + " left join T_BD_Person p on p.fid=rl.cflinemanagernameI where p.fname_l1 is not null\r\n"
  124. + " ) sj on sj.fpersonid=p.fid \r\n"
  125. + " left join T_HR_BDEmployeeType zt on zt.fid=p.femployeetypeid \r\n"
  126. + " left join (select a.fpersonid,b.fenterdate,b.FEFFDT,b.fleffdt from (SELECT FPERSONID,max(fenterdate) as maxDate FROM T_HR_EmpLaborRelationHis group by fpersonid) a \r\n"
  127. + " left join T_HR_EmpLaborRelationHis b on a.fpersonid=b.fpersonid and a.maxdate=b.fenterdate) la on la.fpersonid=p.fid \r\n"
  128. + " left join T_PM_USER us on us.FPERSONID=p.fid \r\n"
  129. + "left join (select a.fid,b.fbillid,b.fpersonid,b.FBIZDATE as extensionAttribute4 from T_HR_ResignBizBill a \r\n"
  130. + " left join T_HR_ResignBizBillentry b on a.fid=b.fbillid where a.FBILLSTATE = '3') as res on res.fpersonid =p.fid ");
  131. sql.append("where p.fid in (" + sb.toString() + ")");
  132. sql.append("and ( p.CFGtiitemail is null or p.CFGtiitemail !='Yes' ) ");
  133. String str = sql.toString();
  134. str = str.replaceAll("(?i)fname_l2", "fname_l1");
  135. logger.info("非预入职流程SQL"+str);
  136. IRowSet iRowSet = DbUtil.executeQuery(ctx, str);
  137. while (iRowSet.next()) {
  138. JSONObject jsonObject = new JSONObject();
  139. // 用户名
  140. jsonObject.put("sAMAccountName", iRowSet.getString("sAMAccountName"));
  141. // 名
  142. jsonObject.put("givenName", iRowSet.getString("givenName"));
  143. // 性
  144. jsonObject.put("sn", iRowSet.getString("sn"));
  145. // 显示名称
  146. jsonObject.put("displayName", iRowSet.getString("showname"));
  147. // 用户名称
  148. jsonObject.put("userPrincipalName", iRowSet.getString("email"));
  149. // 邮箱地址
  150. jsonObject.put("mail", iRowSet.getString("email"));
  151. // 办公电话
  152. if(StringUtils.isNotBlank(iRowSet.getString("telephoneNumber"))) {
  153. jsonObject.put("telephoneNumber", iRowSet.getString("telephoneNumber"));
  154. }
  155. // 职位
  156. jsonObject.put("title", iRowSet.getString("title"));
  157. // 所属部门
  158. jsonObject.put("department", iRowSet.getString("department"));
  159. // 上级负责人
  160. jsonObject.put("manager", iRowSet.getString("manager"));
  161. // 员工工号
  162. jsonObject.put("employeeID", iRowSet.getString("personnumber"));
  163. // 员工类型
  164. jsonObject.put("employeeType", "STAFF");
  165. // 入职日期
  166. jsonObject.put("extensionAttribute3", stringDateFormat(iRowSet.getDate("extensionAttribute3")));
  167. // 离职日期
  168. Date extensionAttribute4 = iRowSet.getDate("extensionAttribute4");
  169. String AccountExpirationDate = "";
  170. if (extensionAttribute4 != null) {
  171. Calendar calendar = Calendar.getInstance();
  172. calendar.setTime(extensionAttribute4);
  173. calendar.add(Calendar.DAY_OF_MONTH, 1);
  174. Date nextDay = (Date) calendar.getTime();
  175. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  176. // 使用format方法将Date对象转换为指定格式的字符串
  177. AccountExpirationDate = formatter.format(nextDay);
  178. }
  179. jsonObject.put("extensionAttribute4", stringDateFormat(iRowSet.getDate("extensionAttribute4")));
  180. // 人员类型
  181. jsonObject.put("extensionAttribute5", iRowSet.getString("extensionAttribute5"));
  182. // 全职/兼职
  183. jsonObject.put("extensionAttribute6", iRowSet.getString("extensionAttribute6"));
  184. //国籍
  185. jsonObject.put("extensionAttribute8", iRowSet.getString("extensionAttribute8"));
  186. //私人邮箱
  187. jsonObject.put("extensionAttribute9", iRowSet.getString("extensionAttribute9"));
  188. //合同结束日期
  189. jsonObject.put("extensionAttribute10", stringDateFormat(iRowSet.getDate("extensionAttribute10")));
  190. //私人手机
  191. jsonObject.put("extensionAttribute11", iRowSet.getString("extensionAttribute11"));
  192. // 失效日期
  193. jsonObject.put("AccountExpirationDate", AccountExpirationDate);
  194. jsonArray.add(jsonObject);
  195. }
  196. // 处理新增预入职
  197. StringBuffer sql2 = new StringBuffer();
  198. sql2.append(
  199. "SELECT pe.FName_l1 as sAMAccountName ,pe.CFGivenName_l1 as givenName,pe.CFSurname_l1 as sn ,pe.FName_l1 as displayName ,pe.CFUserName_l1 as userPrincipalName ,\r\n"
  200. + " pe.cfworkemail as mail ,pe.femail as extensionAttribute9,pe.FCellPhone as telephoneNumber ,pe.FPositionID ,pe.FAdminOrgUnitID ,pe.CFLmanagerID ,pe.CFEmpNumber as employeeID ,pe.FPreEnterDate as extensionAttribute3 ,ec.fname_l1 AS extensionAttribute5 ,pe.CFFtorptID\r\n"
  201. + " ,pep.CFGtiitemail \r\n" + " , post.fname_l1 as title\r\n"
  202. + " ,bu.fname_l1 as department\r\n" + " ,p.cfusername as manager\r\n"
  203. + ",ec.FNAME_l1 as employeeType \r\n" + ",f.fname_l1 as extensionAttribute6 \r\n"
  204. + " FROM T_HR_PreEntry pe\r\n"
  205. + " left join T_HR_PreEntryPerson pep on pep.fid = pe.FTalentID\r\n"
  206. + " left join T_ORG_Position post on pe.FPositionID = post.fid \r\n"
  207. + " left join T_ORG_BaseUnit bu on bu.fid=pe.FAdminOrgUnitID \r\n"
  208. + " left join T_BD_PERSON p on p.fid = pe.CFLmanagerID\r\n"
  209. + " left join T_HR_EmployeeClassify ec on ec.fid = pe.CFPersontypeID\r\n"
  210. + " left join CT_MP_Fullorpart f on f.fid = pe.CFFtorptID\r\n" + "where 1=1\r\n"
  211. + "and pe.FCheckInState='4'\r\n" + "and pe.FBillState ='3' ");
  212. sql2.append(" and ( pep.CFGtiitemail is null or pep.CFGtiitemail !='Yes' ) ");
  213. if (StringUtils.isNotBlank(personNumber)) {
  214. sql2.append(" and pe.CFEmpNumber = '" + personNumber + "' ");
  215. }
  216. if (StringUtils.isNotBlank(startDate)) {
  217. sql2.append(" and pe.FLastUpdateTime >= '" + startDate + "' ");
  218. }
  219. if (StringUtils.isNotBlank(endDate)) {
  220. sql2.append(" and pe.FLastUpdateTime <= '" + endDate + "' ");
  221. }
  222. logger.info("预入职流程SQL"+sql2.toString());
  223. IRowSet iRowSet2 = DbUtil.executeQuery(ctx, sql2.toString());
  224. while (iRowSet2.next()) {
  225. JSONObject jsonObject = new JSONObject();
  226. // 用户名
  227. jsonObject.put("sAMAccountName", iRowSet2.getString("sAMAccountName"));
  228. // 名
  229. jsonObject.put("givenName", iRowSet2.getString("givenName"));
  230. // 性
  231. jsonObject.put("sn", iRowSet2.getString("sn"));
  232. // 显示名称
  233. jsonObject.put("displayName", iRowSet2.getString("displayName"));
  234. // 用户名称
  235. jsonObject.put("userPrincipalName", iRowSet2.getString("userPrincipalName"));
  236. // 邮箱地址
  237. jsonObject.put("mail", iRowSet2.getString("mail"));
  238. // 手机号码
  239. if(StringUtils.isNotBlank(iRowSet.getString("telephoneNumber"))) {
  240. jsonObject.put("telephoneNumber", iRowSet.getString("telephoneNumber"));
  241. }
  242. // 职位
  243. jsonObject.put("title", iRowSet2.getString("title"));
  244. // 所属部门
  245. jsonObject.put("department", iRowSet2.getString("department"));
  246. // 上级负责人
  247. jsonObject.put("manager", iRowSet2.getString("manager"));
  248. // 员工工号
  249. jsonObject.put("employeeID", iRowSet2.getString("employeeID"));
  250. // 员工类型
  251. jsonObject.put("employeeType", "STAFF");
  252. // 入职日期
  253. jsonObject.put("extensionAttribute3", stringDateFormat(iRowSet2.getDate("extensionAttribute3")));
  254. // 离职日期
  255. jsonObject.put("extensionAttribute4", null);
  256. // 人员类型
  257. jsonObject.put("extensionAttribute5", iRowSet2.getString("extensionAttribute5"));
  258. // 全职/兼职
  259. jsonObject.put("extensionAttribute6", iRowSet2.getString("extensionAttribute6"));
  260. jsonObject.put("extensionAttribute8", "");
  261. jsonObject.put("extensionAttribute9", "");
  262. jsonObject.put("extensionAttribute10", "");
  263. // 失效日期
  264. jsonObject.put("AccountExpirationDate", null);
  265. jsonArray.add(jsonObject);
  266. }
  267. } catch (Exception e) {
  268. e.printStackTrace();
  269. JSONObject resultJson = new JSONObject();
  270. resultJson.put("code", "0");
  271. resultJson.put("status", "false");
  272. resultJson.put("message", "获取人员信息失败:" + e.getMessage());
  273. resultJson.put("data", "");
  274. logger.error("获取人员信息失败:" + e.getMessage());
  275. return resultJson.toJSONString();
  276. }
  277. JSONObject resultJson = new JSONObject();
  278. resultJson.put("code", "1");
  279. resultJson.put("status", "true");
  280. resultJson.put("message", "获取人员信息成功");
  281. resultJson.put("data", jsonArray);
  282. jsonArray.forEach(item -> {
  283. System.out.println(item);
  284. });
  285. logger.error("返回员工信息结果集:" + resultJson);
  286. return resultJson;
  287. }
  288. private String stringDateFormat(Date date) {
  289. if (date == null) {
  290. return null;
  291. }
  292. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  293. // 使用format方法将Date对象转换为指定格式的字符串
  294. return formatter.format(date.getTime());
  295. }
  296. }