unitSycn.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /*
  2. * resources.getEntityManagerContainer() // 实体管理容器.
  3. * resources.getContext() //上下文根.
  4. * resources.getOrganization() //组织访问接口.
  5. * requestText //请求内容.
  6. * request //请求对象.
  7. */
  8. /*
  9. {
  10. "action": "add",
  11. "forceFlag" : "yes", //如果add的时候,人员已存在,是否强制更新
  12. "genderType": "m",
  13. "signature": "",
  14. "description": "",
  15. "name": "",
  16. "employee": "",
  17. "unique": "",
  18. "distinguishedName": "",
  19. "orderNumber": "",
  20. "controllerList": "",
  21. "superior": "",
  22. "mail": "",
  23. "weixin": "",
  24. "qq": "",
  25. "mobile": "",
  26. "officePhone": "",
  27. "boardDate": "",
  28. "birthday": "",
  29. "age": "",
  30. "dingdingId": "",
  31. "dingdingHash": "",
  32. "attributeList": [
  33. {
  34. "name": "",
  35. "value": "",
  36. "description": "",
  37. "orderNumber": ""
  38. }
  39. ],
  40. "unitList": [
  41. {
  42. "flag": "",
  43. "orderNumber": "",
  44. "description": "",
  45. "duty": "",
  46. "position": ""
  47. }
  48. ]
  49. }
  50. */
  51. print("运行组织同步接口");
  52. var File = Java.type('java.io.File');
  53. var Config = {
  54. localPath : File.separator + "data" + File.separator + "OrganizationSyncRequest" + File.separator + "unit" + File.separator
  55. };
  56. var applications = resources.getContext().applications();
  57. //var archiveFlag = false; //如果系统内相关的人(上级组织,职务人员等)不在,保存在本地
  58. var Utils = {
  59. getUnitFlag : function( json ){
  60. return json.flag || json.unique || json.distinguishedName || json.id || json.name;
  61. },
  62. getKeyEqualObjFromArray : function( sourceArray, sourceKey, value ){
  63. for( var i=0; i<sourceArray.length; i++ ){
  64. if( sourceArray[i][sourceKey] === value ){
  65. return sourceArray[i];
  66. }
  67. }
  68. return null;
  69. },
  70. parseResp : function( resp ){
  71. if( !resp || resp === null ){
  72. return {
  73. "type": "error",
  74. message : "服务响应是null,需要管理员查看后台日志"
  75. }
  76. }else{
  77. var json = JSON.parse( resp.toString() );
  78. return json;
  79. }
  80. },
  81. getFailText : function( json ){
  82. //{
  83. // "type": "error",
  84. // "message": "手机号错误:15268803358, 手机号已有值重复.",
  85. // "date": "2018-08-05 02:51:35",
  86. // "spent": 5,
  87. // "size": -1,
  88. // "count": 0,
  89. // "position": 0,
  90. // "prompt": "com.x.organization.assemble.control.jaxrs.person.ExceptionMobileDuplicate"
  91. //}
  92. var text;
  93. if( json.message ){
  94. text = json.message + ( json.prompt ? "("+json.prompt + ")" : "" );
  95. }else if( json.prompt ){
  96. text = json.prompt;
  97. }else{
  98. text = "未知异常";
  99. }
  100. print(text);
  101. return text;
  102. },
  103. processError : function( e, text ){
  104. e.printStackTrace();
  105. var errorText = text + " " + e.name + ": " + e.message;
  106. print(errorText);
  107. return errorText;
  108. },
  109. arrayIndexOf : function( array, target ){
  110. for( var i=0; i<array.length; i++ ){
  111. if( array[i] == target )return i;
  112. }
  113. return -1;
  114. },
  115. objectClone : function (obj) {
  116. if (null == obj || "object" != typeof obj) return obj;
  117. if ( typeof obj.length==='number'){ //数组
  118. var copy = [];
  119. for (var i = 0, len = obj.length; i < len; ++i) {
  120. copy[i] = Utils.objectClone(obj[i]);
  121. }
  122. return copy;
  123. }else{
  124. var copy = {};
  125. for (var attr in obj) {
  126. copy[attr] = Utils.objectClone(obj[attr]);
  127. }
  128. return copy;
  129. }
  130. },
  131. saveToLocal : function( json ){
  132. if( json.saveFlag && json.saveFlag == "no" ){
  133. print( "不保存文件" );
  134. return;
  135. }
  136. print( "保存文件开始" );
  137. if( File == null )File = Java.type('java.io.File');
  138. var dir = new File( Config.localPath );
  139. if (!dir.exists()) {
  140. if(!dir.mkdirs()){
  141. print( "创建文件夹失败:"+ recordPath );
  142. return null;
  143. }
  144. }
  145. //var Date = Java.type( "java.util.Date" );
  146. //var now = new Date();
  147. //var nowStr = new java.text.SimpleDateFormat("yyyyMMddHHmmss").format(now);
  148. var name = json.name ? json.name : Utils.getUnitFlag(json);
  149. var unique = json.unique ? json.unique : "";
  150. var path = Config.localPath + name + "_" + unique + '.json';
  151. print( "path="+path );
  152. var file = new File(path);
  153. if (file.exists()) { // 如果已存在,删除旧文件
  154. file.delete();
  155. }
  156. if(!file.createNewFile()){
  157. print("不能创建文件:"+path);
  158. return null;
  159. }
  160. var pw = new java.io.PrintWriter(file, "GBK");
  161. pw.write( JSON.stringify(json) );
  162. pw.close();
  163. print( "保存文件结束 path="+path );
  164. }
  165. };
  166. var AttributeAction = {
  167. add :function( flag, data ){
  168. //"description": "组织属性描述",
  169. // "name": "组织属性",
  170. // "unique": "",
  171. // "distinguishedName": "",
  172. // "orderNumber": "12345",
  173. // "value": "值1"
  174. data.unit = flag;
  175. data.attributeList = typeof( data.value ) == "string" ? [data.value] : data.value;
  176. try {
  177. var resp = applications.postQuery('x_organization_assemble_control', 'unitattribute', JSON.stringify( data ));
  178. var json = Utils.parseResp( resp );
  179. if( json.type && json.type == "success" ){
  180. return "";
  181. }else{
  182. return Utils.getFailText( json );
  183. }
  184. }catch(e){
  185. return Utils.processError( e, "新增组织属性AttributeAction.add() 出错: " );
  186. }
  187. },
  188. remove : function( data ){
  189. try {
  190. var resp = applications.deleteQuery('x_organization_assemble_control', 'unitattribute/'+data.id );
  191. var json = Utils.parseResp( resp );
  192. if( json.type && json.type == "success" ){
  193. return "";
  194. }else{
  195. return Utils.getFailText( json );
  196. }
  197. }catch(e){
  198. return Utils.processError( e, "删除组织属性AttributeAction.remove() 出错: " );
  199. }
  200. },
  201. update : function( flag, data_new, data_old ){
  202. for( var key in data_new ){
  203. if( key != "distinguishedName" ){
  204. data_old[key] = data_new[key];
  205. }
  206. }
  207. data_old.unit = flag;
  208. data_old.attributeList = typeof( data_new.value ) == "string" ? [data_new.value] : data_new.value;
  209. try {
  210. var resp = applications.putQuery('x_organization_assemble_control', 'unitattribute/'+data_old.id, JSON.stringify(data_old) );
  211. var json = Utils.parseResp( resp );
  212. if( json.type && json.type == "success" ){
  213. return "";
  214. }else{
  215. return Utils.getFailText( json );
  216. }
  217. }catch(e){
  218. return Utils.processError( e, "修改组织属性AttributeAction.update() 出错: " );
  219. }
  220. },
  221. compare : function( json ){
  222. var attribute_new = json.attributeList; //传入的组织属性
  223. var errorText = "";
  224. var flag = Utils.getUnitFlag( json );
  225. if( flag ){
  226. try{
  227. var resp = applications.getQuery('x_organization_assemble_control', 'unitattribute/list/unit/'+flag );
  228. var json_attribute_old = Utils.parseResp( resp );
  229. var attribute_old;
  230. if( json_attribute_old.type && json_attribute_old.type == "success" ){
  231. attribute_old = json_attribute_old.data;
  232. }else{
  233. attribute_old = [];
  234. //return Utils.getFailText(json_attribute_old);
  235. }
  236. for( var i=0; i<attribute_old.length; i++ ){
  237. var obj_new = Utils.getKeyEqualObjFromArray( attribute_new, "name", attribute_old[i].name );
  238. if( obj_new == null ){ //老的不在了,要删除
  239. errorText = AttributeAction.remove( attribute_old[i] )
  240. }else{ //已经存在,要修改
  241. errorText = AttributeAction.update( flag, obj_new, attribute_old[i] );
  242. }
  243. }
  244. for( var i=0; i<attribute_new.length; i++ ){
  245. var obj_old = Utils.getKeyEqualObjFromArray( attribute_old, "name", attribute_new[i].name );
  246. if( obj_old == null ){ //需要新增
  247. errorText = AttributeAction.add( flag, attribute_new[i] );
  248. }
  249. }
  250. return errorText;
  251. }catch(e){
  252. return Utils.processError( e, "修改组织属性AttributeAction.compare() 出错: " );
  253. }
  254. }
  255. }
  256. };
  257. //var DutyAction = {
  258. // getIndntityByPerson: function( person, unit ){
  259. // try{
  260. // var resp = applications.getQuery('x_organization_assemble_control', 'identity/list/person/'+person);
  261. // var json = Utils.parseResp( resp );
  262. // var identity;
  263. // if( json.type && json.type == "success" ){
  264. // if( json.data && json.data.length > 0 ){
  265. // var identityList = json.data;
  266. // for( var i=0; i<identityList.length; i++ ){
  267. //
  268. // }
  269. // }
  270. // if( !identity ){
  271. // return "根据人员"+person+"不能获取身份DutyAction.getIndntityByPerson()"
  272. // }
  273. // }else{
  274. // return Utils.getFailText( json );
  275. // }
  276. // }catch(e){
  277. // return Utils.processError( e, "根据人员获取身份DutyAction.getIndntityByPerson() 出错: " );
  278. // }
  279. // },
  280. // add :function( flag, data ){
  281. // //description:"", //描述.
  282. // // name:"", //名称,同一组织下不可重名.
  283. // // unique:"", //唯一标识,不可重复,为空则使用自动填充值
  284. // // distinguishedName:"", //识别名,自动填充,@UD结尾.
  285. // // orderNumber:"", //排序号,升序排列,为空在最后
  286. // // value:"", //组织职务身份成员,多值.
  287. // data.unit = flag;
  288. // data.identityList = typeof( data.value ) == "string" ? [data.value] : data.value;
  289. // try {
  290. // var resp = applications.postQuery('x_organization_assemble_control', 'unitduty', JSON.stringify( data ));
  291. // var json = Utils.parseResp( resp );
  292. // if( json.type && json.type == "success" ){
  293. // return "";
  294. // }else{
  295. // return Utils.getFailText( json );
  296. // }
  297. // }catch(e){
  298. // return Utils.processError( e, "新增组织职务Duty.add() 出错: " );
  299. // }
  300. // },
  301. // remove : function( data ){
  302. // try {
  303. // var resp = applications.deleteQuery('x_organization_assemble_control', 'unitduty/'+data.id );
  304. // var json = Utils.parseResp( resp );
  305. // if( json.type && json.type == "success" ){
  306. // return "";
  307. // }else{
  308. // return Utils.getFailText( json );
  309. // }
  310. // }catch(e){
  311. // return Utils.processError( e, "删除组织职务Duty.remove() 出错: " );
  312. // }
  313. // },
  314. // update : function( flag, data_new, data_old ){
  315. // for( var key in data_new ){
  316. // if( key != "distinguishedName" ){
  317. // data_old[key] = data_new[key];
  318. // }
  319. // }
  320. // data_old.unit = flag;
  321. // data_old.attributeList = typeof( data_new.value ) == "string" ? [data_new.value] : data_new.value;
  322. // try {
  323. // var resp = applications.putQuery('x_organization_assemble_control', 'unitduty/'+data_old.id, JSON.stringify(data_old) );
  324. // var json = Utils.parseResp( resp );
  325. // if( json.type && json.type == "success" ){
  326. // return "";
  327. // }else{
  328. // return Utils.getFailText( json );
  329. // }
  330. // }catch(e){
  331. // return Utils.processError( e, "修改组织职务Duty.update() 出错: " );
  332. // }
  333. // },
  334. // compare : function( json ){
  335. // var attribute_new = json.attributeList; //传入的组织属性
  336. // var errorText = "";
  337. // var flag = Utils.getUnitFlag( json );
  338. // if( flag ){
  339. // try{
  340. // var resp = applications.getQuery('x_organization_assemble_control', 'unitduty/list/unit/'+flag );
  341. // var json_attribute_old = Utils.parseResp( resp );
  342. // var attribute_old;
  343. // if( json_attribute_old.type && json_attribute_old.type == "success" ){
  344. // attribute_old = json_attribute_old.data;
  345. // }else{
  346. // attribute_old = [];
  347. // //return Utils.getFailText(json_attribute_old);
  348. // }
  349. // for( var i=0; i<attribute_old.length; i++ ){
  350. // var obj_new = Utils.getKeyEqualObjFromArray( attribute_new, "name", attribute_old[i].name );
  351. // if( obj_new == null ){ //老的不在了,要删除
  352. // errorText = AttributeAction.remove( attribute_old[i] )
  353. // }else{ //已经存在,要修改
  354. // errorText = AttributeAction.update( flag, obj_new, attribute_old[i] );
  355. // }
  356. // }
  357. // for( var i=0; i<attribute_new.length; i++ ){
  358. // var obj_old = Utils.getKeyEqualObjFromArray( attribute_old, "name", attribute_new[i].name );
  359. // if( obj_old == null ){ //需要新增
  360. // errorText = AttributeAction.add( flag, attribute_new[i] );
  361. // }
  362. // }
  363. // return errorText;
  364. // }catch(e){
  365. // return Utils.processError( e, "修改组织职务Duty.compare() 出错: " );
  366. // }
  367. // }
  368. // }
  369. //};
  370. function get( json ){
  371. var errorText = "";
  372. var flag = Utils.getUnitFlag(json);
  373. var resp;
  374. var unit_old;
  375. if( flag ){
  376. try{
  377. resp = applications.getQuery('x_organization_assemble_control', "unit/"+flag ); //先获取人员信息
  378. var json_old = Utils.parseResp( resp );
  379. if( json_old.type && json_old.type == "success" ){
  380. unit_old = json_old.data;
  381. }else{
  382. return Utils.getFailText( json );
  383. }
  384. }catch(e){
  385. return Utils.processError( e, "get() 出错: " );
  386. }
  387. delete unit_old.woSupDirectUnit;
  388. delete unit_old.woSubDirectIdentityList;
  389. delete unit_old.woUnitAttributeList;
  390. delete unit_old.woUnitDutyList;
  391. //delete person_old.control;
  392. //delete person_old.controllerList;
  393. return unit_old;
  394. //applications.putQuery('x_organization_assemble_control', "person/"+json.unique, json );
  395. }else{
  396. errorText = "参数中没有组织标志:distinguishedName , unique, name 或 id, 不能获取用户";
  397. print(errorText);
  398. return errorText;
  399. }
  400. }
  401. function add( requestJson ){
  402. print("添加组织");
  403. var json = Utils.objectClone(requestJson);
  404. var errorText;
  405. var response;
  406. var resp;
  407. try{
  408. if( json.superior && json.superior != "" ){ //判断上层组织在不在
  409. var superiorData = get({ flag : json.superior });
  410. if( typeof( superiorData ) != "object" ){ //不存在,需要新建
  411. //archiveFlag = true;
  412. //delete json.superior;
  413. var superiorUnitData = { name : json.superior, unique : json.superior };
  414. resp = applications.postQuery( "x_organization_assemble_control", 'unit', JSON.stringify(superiorUnitData));
  415. }
  416. }
  417. var unitData = get(json);
  418. if( typeof(unitData) == "object" ){
  419. // if( json.forceFlag && json.forceFlag == "yes" ){
  420. print( "组织已存在,强制保存" );
  421. for( var key in json){
  422. if( key !== "action" && key !== "attributeList" && key !== "dutyList" ){
  423. if( key == "orderNumber" && (!json[key] || json[key]=="") ) {
  424. }else if( key == "typeList" && (!json[key] || json[key]=="" || json[key].length==0) ){
  425. }else if( key == "id" || key == "unique" || key == "distinguishedName" ){
  426. }else{
  427. unitData[key] = json[key];
  428. }
  429. }
  430. }
  431. if( !unitData.controllerList || unitData.controllerList == null )unitData.controllerList = [];
  432. if( !unitData.typeList ){
  433. unitData.typeList = [];
  434. }else{
  435. if( typeof( unitData.typeList ) == "string" )unitData.typeList = [ unitData.typeList ];
  436. }
  437. var id = unitData.id;
  438. //delete unitData.id;
  439. print("unitData="+JSON.stringify(unitData));
  440. resp = applications.putQuery('x_organization_assemble_control', "unit/"+id, JSON.stringify(unitData) );
  441. // }else{
  442. // errorText = "组织“"+ Utils.getUnitFlag(json) +"”已经在系统内存在";
  443. // }
  444. }else{
  445. if( !json.controllerList ){
  446. json.controllerList = [];
  447. }else{
  448. if( typeof( json.controllerList ) == "string" )json.controllerList = [ json.controllerList ];
  449. }
  450. if( !json.typeList ){
  451. json.typeList = [];
  452. }else{
  453. if( typeof( json.typeList ) == "string" )json.typeList = [ json.typeList ];
  454. }
  455. var data = Utils.objectClone(json);
  456. if(data.attributeList)delete data.attributeList;
  457. if(data.dutyList)delete data.dutyList;
  458. resp = applications.postQuery( "x_organization_assemble_control", 'unit', JSON.stringify(data));
  459. }
  460. if( resp ){
  461. var response = Utils.parseResp( resp );
  462. if( response.type && response.type == "success" ){
  463. if( json.attributeList ){
  464. errorText = AttributeAction.compare( json );
  465. }
  466. //if( json.dutyList ){
  467. // errorText = DutyAction.compare( json );
  468. //}
  469. }else{
  470. errorText = Utils.getFailText( response );
  471. }
  472. }
  473. // if( archiveFlag ){
  474. // Utils.saveToLocal( requestJson ); //保存到本地
  475. // }
  476. }catch(e){
  477. errorText = Utils.processError( e, "添加组织 add() 出错:" );
  478. }finally{
  479. var result = {
  480. "result" : errorText ? "error" : "success",
  481. "description" : errorText || ""
  482. };
  483. if( response && response.data ){
  484. result.id = response.data.id;
  485. }
  486. return result;
  487. }
  488. }
  489. function update(requestJson){
  490. print("修改组织");
  491. var json = Utils.objectClone( requestJson );
  492. var errorText;
  493. var response;
  494. try{
  495. if( json.superior && json.superior != "" ){ //判断上层组织在不在
  496. var superiorData = get({ flag : json.superior });
  497. if( typeof( superiorData ) != "object" ){ //不存在则新建
  498. //archiveFlag = true;
  499. //delete json.superior;
  500. var superiorUnitData = { name : json.superior, unique : json.superior };
  501. resp = applications.postQuery( "x_organization_assemble_control", 'unit', JSON.stringify(superiorUnitData));
  502. }
  503. }
  504. var unitData = get(json);
  505. if( typeof unitData == "object" ){
  506. for( var key in json){
  507. if( key !== "action" && key !== "attributeList" && key !== "dutyList" ){
  508. if( key == "orderNumber" && (!json[key] || json[key]=="") ) {
  509. }else if( key == "typeList" && (!json[key] || json[key]=="" || json[key].length==0) ){
  510. }else if( key == "id" || key == "unique" || key == "distinguishedName" ){
  511. }else{
  512. unitData[key] = json[key];
  513. }
  514. }
  515. }
  516. if( !unitData.controllerList || unitData.controllerList == null )unitData.controllerList = [];
  517. if( !unitData.typeList ){
  518. unitData.typeList = [];
  519. }else{
  520. if( typeof( unitData.typeList ) == "string" )unitData.typeList = [ unitData.typeList ];
  521. }
  522. var resp = applications.putQuery('x_organization_assemble_control', "unit/"+unitData.id, JSON.stringify(unitData) );
  523. var response = Utils.parseResp( resp );
  524. if( response.type && response.type == "success" ){
  525. if( !json.attributeList )json.attributeList = [];
  526. errorText = AttributeAction.compare( json );
  527. //if( !json.dutyList )json.dutyList = [];
  528. //errorText = DutyAction.compare( json );
  529. }else{
  530. errorText = Utils.getFailText( response );
  531. }
  532. }else{
  533. errorText = unitData;
  534. }
  535. }catch(e){
  536. errorText = Utils.processError( e, "修改组织 update() 出错:" );
  537. }finally{
  538. // if( archiveFlag ){
  539. // Utils.saveToLocal( requestJson ); //保存到本地
  540. // }
  541. var result = {
  542. "result" : errorText ? "error" : "success",
  543. "description" : errorText || ""
  544. };
  545. if( response && response.data ){
  546. result.id = response.data.id;
  547. }
  548. return result;
  549. }
  550. }
  551. function remove(json){
  552. print("删除组织");
  553. var errorText;
  554. var response;
  555. try{
  556. var unit = get(json);
  557. if( typeof(unit) == "object" ){
  558. var resp = applications.deleteQuery('x_organization_assemble_control', "unit/"+unit.id ); //s人员信息
  559. response = Utils.parseResp( resp );
  560. if( response.type && response.type == "success" ){
  561. }else{
  562. errorText = Utils.getFailText( response );
  563. }
  564. }else{
  565. errorText = unit;
  566. }
  567. }catch(e){
  568. errorText = Utils.processError( e, "删除组织 remove() 出错:" );
  569. }finally{
  570. var result = {
  571. "result" : errorText ? "error" : "success",
  572. "description" : errorText || ""
  573. };
  574. if( response && response.data ){
  575. result.id = response.data.id;
  576. }
  577. return result;
  578. }
  579. }
  580. function init(){
  581. var result ="";
  582. var responseText = "";
  583. try{
  584. print( "requestText="+requestText );
  585. var requestJson = JSON.parse(requestText);
  586. print( "type of requestJson = " + typeof( requestJson ));
  587. if( typeof(requestJson) === "string" ){
  588. requestJson = JSON.parse(requestJson);
  589. }
  590. var action = requestJson.action;
  591. print("action="+action);
  592. switch( action ){
  593. case "add":
  594. result = add( requestJson );
  595. break;
  596. case "update":
  597. result = update( requestJson );
  598. break;
  599. case "delete" :
  600. result = remove( requestJson );
  601. break;
  602. default :
  603. result = {
  604. "result" : "error",
  605. "description" : "requestText未设置action,不执行操作"
  606. };
  607. break;
  608. }
  609. }catch(e){
  610. e.printStackTrace();
  611. result = {
  612. "result" : "error",
  613. "description" : e.name + ": " + e.message
  614. };
  615. }finally{
  616. print("responseText="+JSON.stringify(result));
  617. return result;
  618. }
  619. }
  620. init();