sendSMS.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * resources.getEntityManagerContainer() // 实体管理容器.
  3. * resources.getContext() //上下文根.
  4. * resources.getOrganization() //组织访问接口.
  5. * requestText //请求内容.
  6. * request //请求对象.
  7. */
  8. var _self = this;
  9. var applications = resources.getContext().applications();
  10. var departmentLevel_1 = 3; //2级部门所在层级
  11. function typeOf( item ){
  12. if (item === null) return 'null';
  13. if( !item ){
  14. return typeof item;
  15. }
  16. if (item.$family != null) return item.$family();
  17. if (item.constructor == Array) return 'array';
  18. if (item.nodeName){
  19. if (item.nodeType == 1) return 'element';
  20. if (item.nodeType == 3) return (/\S/).test(item.nodeValue) ? 'textnode' : 'whitespace';
  21. } else if (typeof item.length == 'number'){
  22. if (item.callee) return 'arguments';
  23. //if ('item' in item) return 'collection';
  24. }
  25. return typeof item;
  26. }
  27. function objectClone(obj) {
  28. if (null == obj || "object" != typeof obj) return obj;
  29. if ( typeof obj.length==='number'){ //数组
  30. var copy = [];
  31. for (var i = 0, len = obj.length; i < len; ++i) {
  32. copy[i] = objectClone(obj[i]);
  33. }
  34. return copy;
  35. }else{
  36. var copy = {};
  37. for (var attr in obj) {
  38. copy[attr] = objectClone(obj[attr]);
  39. }
  40. return copy;
  41. }
  42. }
  43. //返回字符串转json对象
  44. function parseResp(resp) {
  45. if (!resp || resp === null) {
  46. return {
  47. "type": "error",
  48. message: "服务响应是null"
  49. }
  50. } else {
  51. var json = JSON.parse(resp.toString());
  52. return json;
  53. }
  54. }
  55. function getPerson( employee ){
  56. var list = typeOf(employee) === "array" ? employee : [employee];
  57. var filter = {"personList":list}
  58. var resp = applications.postQuery("x_organization_assemble_express","person/list/object", JSON.stringify(filter));
  59. var json = parseResp( resp );
  60. if( json && json.type !== "error" && json.data && json.data.length ){
  61. return json.data[0]
  62. }else{
  63. return ""
  64. }
  65. }
  66. function getPhone( employee ){
  67. var list = typeOf(employee) === "array" ? employee : [employee];
  68. var filter = {"personList":list}
  69. var resp = applications.postQuery("x_organization_assemble_express","person/list/object", JSON.stringify(filter));
  70. var json = parseResp( resp );
  71. var mobile = [];
  72. if( json && json.type !== "error" && json.data && json.data.length ){
  73. for( var k=0; k<json.data.length; k++ ){
  74. var d = json.data[k];
  75. mobile.push( d.mobile );
  76. }
  77. return mobile
  78. }else{
  79. return ""
  80. }
  81. }
  82. function getPhoneByIdentityDn( identityDn ){
  83. var list = typeOf(identityDn) === "array" ? identityDn : [identityDn];
  84. var filter = {"identityList":list};
  85. var resp = applications.postQuery("x_organization_assemble_express","person/list/identity/object", JSON.stringify(filter));
  86. var json = parseResp( resp );
  87. var mobile = [];
  88. if( json && json.type !== "error" && json.data && json.data.length ){
  89. for( var k=0; k<json.data.length; k++ ){
  90. var d = json.data[k];
  91. mobile.push( d.mobile );
  92. }
  93. return mobile
  94. }else{
  95. return ""
  96. }
  97. }
  98. function getDepartmentLevel_1( employee ){
  99. var list = typeOf(employee) === "array" ? employee : [employee];
  100. var filter = {"personList":list}
  101. var resp = applications.postQuery("x_organization_assemble_express","unit/list/person/sup/nested/object", JSON.stringify(filter));
  102. var json = parseResp( resp );
  103. if( json && json.type !== "error" && json.data && json.data.length ){
  104. for( var i=0; i<json.data.length; i++ ){
  105. var d = json.data[i];
  106. if( d.level == 3 ){
  107. return d.distinguishedName;
  108. }
  109. }
  110. }else{
  111. return ""
  112. }
  113. }
  114. function getCompany( employee ){
  115. var list = typeOf(employee) === "array" ? employee : [employee];
  116. var filter = {"personList":list}
  117. var resp = applications.postQuery("x_organization_assemble_express","unit/list/person/sup/nested/object", JSON.stringify(filter));
  118. var json = parseResp( resp );
  119. if( json && json.type !== "error" && json.data && json.data.length ){
  120. for( var i=0; i<json.data.length; i++ ){
  121. var d = json.data[i];
  122. if( d.level == 2 ){
  123. return d.distinguishedName;
  124. }
  125. }
  126. }else{
  127. return ""
  128. }
  129. }
  130. //获取各流程人力资源部处理人
  131. function getActivityOwner( company, processAlias, activityAlias ){
  132. var where = "o.companyDn='"+company+"' and " + "o.processAlias='"+processAlias+"' and o.activityAlias='"+activityAlias+"'";
  133. where = where.replace(/\s/g,"%20");
  134. var resp = applications.getQuery("x_query_assemble_surface","table/list/hrMarketSetting_ActivityOwner/row/select/where/"+where);
  135. var json = parseResp( resp );
  136. if( json && json.type !== "error" && json.data && json.data.length ){
  137. return json.data[0].identityDn
  138. }else{
  139. return null;
  140. }
  141. }
  142. //获取人才市场人力管理员
  143. function getHrManagerIdentityList( company ){
  144. if( !company )return [];
  145. var resp = applications.postQuery("x_organization_assemble_express","unitduty/list/identity/unit/name",JSON.stringify({
  146. "name":"人才市场人力管理员","unit":company
  147. }));
  148. var json = parseResp( resp );
  149. if( json && json.type !== "error" && json.data ){
  150. return json.data.identityList
  151. }else{
  152. return [];
  153. }
  154. }
  155. //获取人才市场部门管理员
  156. function getDepartmentManagerIdentityList( department ){
  157. if( !department )return [];
  158. var resp = applications.postQuery("x_organization_assemble_express","unitduty/list/identity/unit/name",JSON.stringify({
  159. "name":"人才市场部门管理员","unit":department
  160. }));
  161. var json = parseResp( resp );
  162. if( json && json.type !== "error" && json.data ){
  163. return json.data.identityList
  164. }else{
  165. return [];
  166. }
  167. }
  168. // {
  169. // employeeCode : "处理的人员编码,比如合同到期人员、离岗学习到期人员、转岗到期人员",
  170. // person : "处理的人员名称,比如合同到期人员、离岗学习到期人员、转岗到期人员",
  171. // typeName : "类型,合同到期人员、离岗学习到期人员、转岗到期人员",
  172. // typeCode : "类型,contractNotice、levelToLearn、internalChange",
  173. // receiveMobile : "接收人员手机号",
  174. // receivePerson : "接收人员",
  175. // content : "短信内容",
  176. // resultCode : "短信发送结果编码",
  177. // result : "短信发送结果",
  178. // appointTime : "定时发送时间"
  179. // }
  180. var CipherConnectionAction = Java.type('com.x.base.core.project.connection.CipherConnectionAction');
  181. var Config = Java.type('com.x.base.core.project.config.Config');
  182. //调动本系统的接口,发送短信
  183. function sendSMS( json ){
  184. if( !json )return;
  185. if( !json.receiveMobile )return;
  186. if( !json.content )return;
  187. //var resp = CipherConnectionAction.post(false, Config.x_program_centerUrlRoot() + "invoke/sendSMS/execute", JSON.stringify(json));
  188. //print( "mobile=" + mobile + ", content=" + content );
  189. var mobileList = typeOf(json.receiveMobile) === "array" ? json.receiveMobile : [json.receiveMobile];
  190. var personList = typeOf(json.receivePerson) === "array" ? json.receivePerson : [json.receivePerson];
  191. for( var i=0; i<mobileList.length; i++ ){
  192. var d = objectClone( json );
  193. d.receiveMobile = mobileList[i];
  194. d.receivePerson = personList[i] ? personList[i] : "";
  195. var resp = CipherConnectionAction.post(false, Config.x_program_centerUrlRoot() + "invoke/sendSMS/execute", JSON.stringify(d));
  196. //print( "mobile=" + mobile + ", content=" + content );
  197. }
  198. }
  199. //离岗学习期限三个月,需提前一周给员工部门、人力资源部发送考核提醒
  200. //提醒内容:员工张某的离岗学习期还有一周即将结束,请相关部门尽快完成考核。
  201. function sendSMS_levelToLearn(){
  202. print( "发离岗学习到期短信" );
  203. var getTime = function(){
  204. var Calendar = Java.type("java.util.Calendar");
  205. var formatter = new java.text.SimpleDateFormat("yyyy-MM-dd");//格式化为2017-10
  206. var calendar = Calendar.getInstance();//得到Calendar实例
  207. calendar.add(Calendar.MONTH, -3);//把月份减三个月
  208. calendar.add( Calendar.DAY_OF_YEAR, 7 ); //周加1
  209. var starDate = calendar.getTime();//得到时间赋给Data
  210. return formatter.format(starDate);//使用格式化Data
  211. }
  212. //今天往前三个月减一周的日期
  213. var timeLimit = getTime();
  214. var where = "(o.sendedSMSDate is null) and o.status='离岗学习员工' and o.createTime<'" + timeLimit +"'"; //Date.format('%Y-%m-%d %H:%M:%S');
  215. where = where.replace(/\s/g,"%20")
  216. print(where);
  217. var resp = applications.getQuery("x_query_assemble_surface","table/list/hrMarketResourcePool/row/select/where/"+where);
  218. var json = parseResp( resp );
  219. if( json && json.type !== "error" && json.data && json.data.length ){
  220. for( var k=0; k<json.data.length; k++ ){
  221. var d = json.data[k];
  222. var content = "员工"+( d.name || "" ).split("@")[0]+"的离岗学习期还有一周即将结束,请相关部门尽快完成考核。";
  223. var phone;
  224. //获取各流程人力资源部处理人设置中试用考核流程人力资源部起草节点的人员
  225. var identityDn = getActivityOwner( d.company, "probationCheck", "hrManager" );
  226. if( identityDn ){
  227. print( identityDn );
  228. phone = getPhoneByIdentityDn( identityDn );
  229. if( phone )sendSMS({
  230. employeeCode : d.employeeCode,
  231. person : d.name,
  232. typeName : "离岗学习员工",
  233. typeCode : "levelToLearn",
  234. receiveMobile : phone,
  235. receivePerson : identityDn,
  236. content : content
  237. })
  238. }else{ //获取人力资源部管理员
  239. var list = getHrManagerIdentityList( d.company );
  240. if( list.length ){
  241. phone = getPhoneByIdentityDn( list );
  242. if( phone )sendSMS({
  243. employeeCode : d.employeeCode,
  244. person : d.name,
  245. typeName : "离岗学习员工",
  246. typeCode : "levelToLearn",
  247. receiveMobile : phone,
  248. receivePerson : list,
  249. content : content
  250. })
  251. }
  252. }
  253. //获取离岗学习流程的转岗部门,给部门人力管理员发短信
  254. var transferDepartment = d.transferDepartment;
  255. if( transferDepartment ){
  256. var list = getDepartmentManagerIdentityList( transferDepartment );
  257. if( list.length ){
  258. print( list );
  259. phone = getPhoneByIdentityDn( list );
  260. if( phone )sendSMS( {
  261. employeeCode : d.employeeCode,
  262. person : d.name,
  263. typeName : "离岗学习员工",
  264. typeCode : "levelToLearn",
  265. receiveMobile : phone,
  266. receivePerson : list,
  267. content : content
  268. });
  269. }
  270. }
  271. formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化为2017-10
  272. d.sendedSMSDate = formatter.format(new java.util.Date())
  273. //保存会资源池
  274. applications.putQuery("x_query_assemble_surface","table/hrMarketResourcePool/row/"+d.id, JSON.stringify(d));
  275. }
  276. }
  277. }
  278. //sendSMS_levelToLearn();
  279. //内部转岗期不得超过1个月
  280. //内部转岗到期限前,提前一周给人力资源部发送到期提醒。
  281. //提醒内容:员工张某的内部转岗期还有一周即将到期。
  282. function sendSMS_internalChange(){
  283. print( "发送内部转岗短信" );
  284. var getTime = function(){
  285. var Calendar = Java.type("java.util.Calendar");
  286. var formatter = new java.text.SimpleDateFormat("yyyy-MM-dd");//格式化为2017-10
  287. var calendar = Calendar.getInstance();//得到Calendar实例
  288. calendar.add(Calendar.MONTH, -1);//把月份减一个月
  289. calendar.add( Calendar.DAY_OF_YEAR, 7 ); //周加1
  290. var starDate = calendar.getTime();//得到时间赋给Data
  291. return formatter.format(starDate);//使用格式化Data
  292. }
  293. //今天往前三个月减一周的日期
  294. var timeLimit = getTime();
  295. var where = "(o.sendedSMSDate is null) and o.status='内部转岗员工' and o.createTime<'" + timeLimit +"'"; //Date.format('%Y-%m-%d %H:%M:%S');
  296. where = where.replace(/\s/g,"%20")
  297. print(where);
  298. var resp = applications.getQuery("x_query_assemble_surface","table/list/hrMarketResourcePool/row/select/where/"+where);
  299. var json = parseResp( resp );
  300. if( json && json.type !== "error" && json.data && json.data.length ){
  301. for( var k=0; k<json.data.length; k++ ){
  302. var d = json.data[k];
  303. var content = "员工"+( d.name || "" ).split("@")[0]+"的内部转岗期还有一周即将到期。";
  304. var phone;
  305. //获取各流程人力资源部处理人设置中内部转岗人力资源部起草节点的人员
  306. var identityDn = getActivityOwner( d.company, "internalWaiting", "hrManager2" );
  307. if( identityDn ){
  308. print( identityDn );
  309. phone = getPhoneByIdentityDn( identityDn );
  310. if( phone )sendSMS( {
  311. employeeCode : d.employeeCode,
  312. person : d.name,
  313. typeName : "内部转岗员工",
  314. typeCode : "internalChange",
  315. receiveMobile : phone,
  316. receivePerson : identityDn,
  317. content : content
  318. })
  319. }else{ //获取人力资源部管理员
  320. var list = getHrManagerIdentityList( d.company );
  321. if( list.length ){
  322. print( list )
  323. phone = getPhoneByIdentityDn( list );
  324. if( phone )sendSMS({
  325. employeeCode : d.employeeCode,
  326. person : d.name,
  327. typeName : "内部转岗员工",
  328. typeCode : "internalChange",
  329. receiveMobile : phone,
  330. receivePerson : list,
  331. content : content
  332. })
  333. }
  334. }
  335. formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化为2017-10
  336. d.sendedSMSDate = formatter.format(new java.util.Date())
  337. //保存会资源池
  338. applications.putQuery("x_query_assemble_surface","table/hrMarketResourcePool/row/"+d.id, JSON.stringify(d));
  339. }
  340. }
  341. }
  342. //合同到期检查是否已经发送过
  343. function checkSended_contractNotice( EMPLOYEE_NUMBER ){
  344. var where = "o.employeeCode='" + EMPLOYEE_NUMBER + "' and o.typeCode='contractNotice'";
  345. where = where.replace(/\s/g,"%20")
  346. print(where);
  347. var resp = applications.getQuery("x_query_assemble_surface","table/list/hrMarketSMSLog/row/select/where/"+where);
  348. var json = parseResp( resp );
  349. if( json && json.type !== "error" && json.data && json.data.length ){
  350. return true;
  351. }else{
  352. return false;
  353. }
  354. }
  355. //合同到期前两个月要给人力资源部、员工、员工所在部门发送合同到期预警。
  356. function sendSMS_contractNotice(){
  357. print( "发送合同续签短信" );
  358. var getTime = function(){
  359. var Calendar = Java.type("java.util.Calendar");
  360. var formatter = new java.text.SimpleDateFormat("yyyy-MM-dd");//格式化为2017-10
  361. var calendar = Calendar.getInstance();//得到Calendar实例
  362. calendar.add(Calendar.MONTH, -2);//把月份减2个月
  363. var starDate = calendar.getTime();//得到时间赋给Data
  364. return formatter.format(starDate);//使用格式化Data
  365. }
  366. //今天往前三个月减一周的日期
  367. var timeLimit = getTime();
  368. var where = "o.CONTRACT_END='" + timeLimit + "'"; //Date.format('%Y-%m-%d %H:%M:%S');
  369. where = where.replace(/\s/g,"%20")
  370. print(where);
  371. var resp = applications.getQuery("x_query_assemble_surface","table/list/hrSyncPersonalContract/row/select/where/"+where);
  372. var json = parseResp( resp );
  373. if( json && json.type !== "error" && json.data && json.data.length ){
  374. for( var k=0; k<json.data.length; k++ ){
  375. var d = json.data[k];
  376. if( checkSended_contractNotice(d.EMPLOYEE_NUMBER) ){
  377. continue;
  378. }
  379. var person = getPerson(d.EMPLOYEE_NUMBER);
  380. if( !person )return;
  381. //给员工本人发送短信
  382. if( person.mobile ){
  383. print( person.mobile );
  384. sendSMS({
  385. employeeCode : d.EMPLOYEE_NUMBER,
  386. person : person.distinguishedName,
  387. typeName : "合同到期员工",
  388. typeCode : "contractNotice",
  389. receiveMobile : person.mobile,
  390. receivePerson : person.distinguishedName,
  391. content : ( person.name || "" )+"您好,您的合同还有一周即将到期。"
  392. })
  393. }
  394. var phone;
  395. var content = "员工"+( person.name || "" ).split("@")[0]+"的合同还有一周即将到期。";
  396. //给部门人力管理员发短信
  397. var department = getDepartmentLevel_1(d.EMPLOYEE_NUMBER);
  398. if( department ){
  399. var list = getDepartmentManagerIdentityList( department );
  400. if( list && list.length ){
  401. print( list );
  402. phone = getPhoneByIdentityDn( list );
  403. //if( phone )sendSMS( phone, content );
  404. if( phone ){
  405. sendSMS({
  406. employeeCode : d.EMPLOYEE_NUMBER,
  407. person : person.distinguishedName,
  408. typeName : "合同到期员工",
  409. typeCode : "contractNotice",
  410. receiveMobile : phone,
  411. receivePerson : list,
  412. content : content
  413. })
  414. }
  415. }
  416. }
  417. //人力资源部
  418. var company = getCompany( d.EMPLOYEE_NUMBER )
  419. if(company){
  420. //获取各流程人力资源部处理人设置中合同续签流程人力资源部起草节点的人员
  421. var identityDn = getActivityOwner( company, "contractRenewal", "hrManager" );
  422. if( identityDn ){
  423. print( identityDn )
  424. phone = getPhoneByIdentityDn( identityDn );
  425. if( phone )sendSMS( {
  426. employeeCode : d.EMPLOYEE_NUMBER,
  427. person : person.distinguishedName,
  428. typeName : "合同到期员工",
  429. typeCode : "contractNotice",
  430. receiveMobile : phone,
  431. receivePerson : identityDn,
  432. content : content
  433. });
  434. }else{ //获取人力资源部管理员
  435. var list = getHrManagerIdentityList( company );
  436. if( list.length ){
  437. print( list )
  438. phone = getPhoneByIdentityDn( list );
  439. if( phone )sendSMS({
  440. employeeCode : d.EMPLOYEE_NUMBER,
  441. person : person.distinguishedName,
  442. typeName : "合同到期员工",
  443. typeCode : "contractNotice",
  444. receiveMobile : phone,
  445. receivePerson : list,
  446. content : content
  447. })
  448. }
  449. }
  450. }
  451. }
  452. }
  453. }
  454. sendSMS_levelToLearn();
  455. sendSMS_internalChange();
  456. sendSMS_contractNotice();