personSync.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /*
  2. * resources.getEntityManagerContainer() // 实体管理容器.
  3. * resources.getContext() //上下文根.
  4. * resources.getOrganization() //组织访问接口.
  5. * requestText //请求内容.
  6. * request //请求对象.
  7. */
  8. /*
  9. {
  10. "action": "add",
  11. "genderType": "m",
  12. "signature": "",
  13. "description": "",
  14. "name": "",
  15. "employee": "",
  16. "unique": "",
  17. "distinguishedName": "",
  18. "orderNumber": "",
  19. "controllerList": "",
  20. "superior": "",
  21. "mail": "",
  22. "weixin": "",
  23. "qq": "",
  24. "mobile": "",
  25. "officePhone": "",
  26. "boardDate": "",
  27. "birthday": "",
  28. "age": "",
  29. "dingdingId": "",
  30. "dingdingHash": "",
  31. "attributeList": [
  32. {
  33. "name": "",
  34. "value": "",
  35. "description": "",
  36. "orderNumber": ""
  37. }
  38. ],
  39. "unitList": [
  40. {
  41. "flag": "",
  42. "orderNumber": "",
  43. "description": "",
  44. "duty": "",
  45. "position": ""
  46. }
  47. ]
  48. }
  49. */
  50. print("运行人员同步接口");
  51. var File = Java.type('java.io.File');
  52. var Config = {
  53. localPath : File.separator + "data" + File.separator + "OrganizationSyncRequest" + File.separator + "person" + File.separator,
  54. ignoreDuty : ["部门积分管理员","公司积分管理员","市、县公司普通员工"],
  55. ignorAttribute : ["市、县公司普通员工"]
  56. };
  57. var applications = resources.getContext().applications();
  58. var archiveFlag = false;
  59. var Utils = {
  60. getUserFlag : function( json ){
  61. return json.flag || json.unique || json.distinguishedName || json.employee || json.mobile || json.id;
  62. },
  63. getKeyEqualObjFromArray : function( sourceArray, sourceKey, value ){
  64. for( var i=0; i<sourceArray.length; i++ ){
  65. if( sourceArray[i][sourceKey] === value ){
  66. return sourceArray[i];
  67. }
  68. }
  69. return null;
  70. },
  71. parseResp : function( resp ){
  72. if( !resp || resp === null ){
  73. return {
  74. "type": "error",
  75. message : "服务响应是null,需要管理员查看后台日志"
  76. }
  77. }else{
  78. var json = JSON.parse( resp.toString() );
  79. return json;
  80. }
  81. },
  82. getFailText : function( json ){
  83. //{
  84. // "type": "error",
  85. // "message": "手机号错误:15268803358, 手机号已有值重复.",
  86. // "date": "2018-08-05 02:51:35",
  87. // "spent": 5,
  88. // "size": -1,
  89. // "count": 0,
  90. // "position": 0,
  91. // "prompt": "com.x.organization.assemble.control.jaxrs.person.ExceptionMobileDuplicate"
  92. //}
  93. var text;
  94. if( json.message ){
  95. text = json.message + ( json.prompt ? "("+json.prompt + ")" : "" );
  96. }else if( json.prompt ){
  97. text = json.prompt;
  98. }else{
  99. text = "未知异常";
  100. }
  101. print(text);
  102. return text;
  103. },
  104. processError : function( e, text ){
  105. e.printStackTrace();
  106. var errorText = text + " " + e.name + ": " + e.message;
  107. print(errorText);
  108. return errorText;
  109. },
  110. arrayIndexOf : function( array, target ){
  111. for( var i=0; i<array.length; i++ ){
  112. if( array[i] == target )return i;
  113. }
  114. return -1;
  115. },
  116. arrayErase : function(array, target){
  117. for (var i = array.length; i--;){
  118. if (array[i] === target) array.splice(i, 1);
  119. }
  120. return array;
  121. },
  122. objectClone : function (obj) {
  123. if (null == obj || "object" != typeof obj) return obj;
  124. if ( typeof obj.length==='number'){ //数组
  125. var copy = [];
  126. for (var i = 0, len = obj.length; i < len; ++i) {
  127. copy[i] = Utils.objectClone(obj[i]);
  128. }
  129. return copy;
  130. }else{
  131. var copy = {};
  132. for (var attr in obj) {
  133. copy[attr] = Utils.objectClone(obj[attr]);
  134. }
  135. return copy;
  136. }
  137. },
  138. typeOf : function( item ){
  139. if (item === null) return 'null';
  140. if( !item ){
  141. return typeof item;
  142. }
  143. if (item.$family != null) return item.$family();
  144. if (item.constructor == Array) return 'array';
  145. if (item.nodeName){
  146. if (item.nodeType == 1) return 'element';
  147. if (item.nodeType == 3) return (/\S/).test(item.nodeValue) ? 'textnode' : 'whitespace';
  148. } else if (typeof item.length == 'number'){
  149. if (item.callee) return 'arguments';
  150. //if ('item' in item) return 'collection';
  151. }
  152. return typeof item;
  153. //if( obj === null )return "null";
  154. //if( "object" !== typeof obj )return typeof obj;
  155. //return typeof obj.length==='number' ? 'array' : 'object';
  156. },
  157. saveToLocal : function( json ){
  158. if( json.saveFlag && json.saveFlag == "no" ){
  159. print( "不保存文件" );
  160. return;
  161. }
  162. print( "保存文件开始" );
  163. if( File == null )File = Java.type('java.io.File');
  164. var dir = new File( Config.localPath );
  165. if (!dir.exists()) {
  166. if(!dir.mkdirs()){
  167. print( "创建文件夹失败:"+ recordPath );
  168. return null;
  169. }
  170. }
  171. //var Date = Java.type( "java.util.Date" );
  172. //var now = new Date();
  173. //var nowStr = new java.text.SimpleDateFormat("yyyyMMddHHmmss").format(now);
  174. var name = json.name ? json.name : Utils.getUserFlag(json);
  175. var unique = json.unique ? json.unique : "";
  176. var path = Config.localPath + name + '_' + unique + '.json';
  177. var file = new File(path);
  178. if (file.exists()) { // 如果已存在,删除旧文件
  179. file.delete();
  180. }
  181. if(!file.createNewFile()){
  182. print("不能创建文件:"+path);
  183. return null;
  184. }
  185. var pw = new java.io.PrintWriter(file, "GBK");
  186. pw.write( JSON.stringify(json) );
  187. pw.close();
  188. print( "保存文件结束 path="+path );
  189. }
  190. };
  191. var AttributeAction = function( json ){
  192. var attribute_new = json.attributeList; //传入的用户属性
  193. this.errorText = "";
  194. var flag = Utils.getUserFlag( json );
  195. if( flag ){
  196. try{
  197. var resp = applications.getQuery('x_organization_assemble_control', 'personattribute/list/person/'+flag );
  198. var json_attribute_old = Utils.parseResp( resp );
  199. var attribute_old;
  200. if( json_attribute_old.type && json_attribute_old.type == "success" ){
  201. attribute_old = json_attribute_old.data;
  202. }else{
  203. attribute_old = [];
  204. //return Utils.getFailText(json_attribute_old);
  205. }
  206. for( var i=0; i<attribute_old.length; i++ ){
  207. if( Utils.arrayIndexOf( Config.ignorAttribute, attribute_old[i].name ) == -1 ){
  208. var obj_new = Utils.getKeyEqualObjFromArray( attribute_new, "name", attribute_old[i].name );
  209. if( obj_new == null ){ //老的不在了,要删除
  210. this.errorText = this.remove( attribute_old[i] )
  211. }else{ //已经存在,要修改
  212. this.errorText = this.update( flag, obj_new, attribute_old[i] );
  213. }
  214. }
  215. }
  216. for( var i=0; i<attribute_new.length; i++ ){
  217. if( Utils.arrayIndexOf( Config.ignorAttribute, attribute_new[i].name ) == -1 ) {
  218. var obj_old = Utils.getKeyEqualObjFromArray(attribute_old, "name", attribute_new[i].name);
  219. if (obj_old == null) { //需要新增
  220. this.errorText = this.add(flag, attribute_new[i]);
  221. }
  222. }
  223. }
  224. }catch(e){
  225. this.errorText = Utils.processError( e, "处理用户属性AttributeAction.new() 出错: " );
  226. }
  227. }
  228. };
  229. AttributeAction.prototype.add = function( flag, data ){
  230. //name string single 属性名称 级别
  231. //description string single 属性描述 级别描述
  232. //value string/array multi 属性值 1 / [ "1" ]
  233. //orderNumber string single 排序号,升序排列,为空在最后 18315158
  234. data.person = flag;
  235. var valueList = typeof( data.value ) == "string" ? [data.value] : data.value;
  236. for( var i=0; i<valueList.length; i++ ){
  237. valueList[i] = valueList[i].replace("@","-");
  238. }
  239. data.attributeList = valueList;
  240. try {
  241. var resp = applications.postQuery('x_organization_assemble_control', 'personattribute', JSON.stringify( data ));
  242. var json = Utils.parseResp( resp );
  243. if( json.type && json.type == "success" ){
  244. return "";
  245. }else{
  246. return Utils.getFailText( json );
  247. }
  248. }catch(e){
  249. return Utils.processError( e, "新增用户属性AttributeAction.add() 出错: " );
  250. }
  251. };
  252. AttributeAction.prototype.remove = function( data ){
  253. try {
  254. var resp = applications.deleteQuery('x_organization_assemble_control', 'personattribute/'+data.id );
  255. var json = Utils.parseResp( resp );
  256. if( json.type && json.type == "success" ){
  257. return "";
  258. }else{
  259. return Utils.getFailText( json );
  260. }
  261. }catch(e){
  262. return Utils.processError( e, "删除用户属性AttributeAction.remove() 出错: " );
  263. }
  264. };
  265. AttributeAction.prototype.update = function( flag, data_new, data_old ){
  266. for( var key in data_new ){
  267. if( key != "distinguishedName" ){
  268. data_old[key] = data_new[key];
  269. }
  270. }
  271. data_old.person = flag;
  272. data_old.attributeList = typeof( data_new.value ) == "string" ? [data_new.value] : data_new.value;
  273. try {
  274. var resp = applications.putQuery('x_organization_assemble_control', 'personattribute/'+data_old.id, JSON.stringify(data_old) );
  275. var json = Utils.parseResp( resp );
  276. if( json.type && json.type == "success" ){
  277. return "";
  278. }else{
  279. return Utils.getFailText( json );
  280. }
  281. }catch(e){
  282. return Utils.processError( e, "修改用户属性AttributeAction.update() 出错: " );
  283. }
  284. };
  285. var DutyAction = function( identity, unit, dutyNameList ){
  286. try {
  287. this.errorText = "";
  288. this.identity = identity;
  289. this.unit = unit;
  290. this.dutyNameList = dutyNameList;
  291. var resp_old = applications.getQuery('x_organization_assemble_control', 'unitduty/list/unit/'+unit);
  292. var json_old = Utils.parseResp( resp_old );
  293. if( json_old.type === "success" && json_old.data ) {
  294. var existDutyList = [];
  295. var dutyObject = {};
  296. for( var i=0; i<json_old.data.length; i++ ){
  297. var oldDuty = json_old.data[i];
  298. dutyObject[ oldDuty.name ] = oldDuty;
  299. if( this.identityEquals( oldDuty.woIdentityList, identity ) ){
  300. existDutyList.push( oldDuty.name );
  301. }
  302. }
  303. for( var i=0; i<dutyNameList.length; i++ ){
  304. var dutyName = dutyNameList[i];
  305. if( Utils.arrayIndexOf( existDutyList, dutyName ) == -1 && Utils.arrayIndexOf( Config.ignoreDuty, dutyName ) == -1 ){ //需要添加职务
  306. if( dutyObject[ dutyName ] ){
  307. this.errorText = this.update( dutyObject[dutyName] );
  308. }else{
  309. this.errorText = this.add( dutyName )
  310. }
  311. }
  312. }
  313. for( var i=0; i<existDutyList.length; i++ ){
  314. var existDutyName = existDutyList[i];
  315. if( Utils.arrayIndexOf( dutyNameList, existDutyName ) == -1 && Utils.arrayIndexOf( Config.ignoreDuty, existDutyName ) == -1){ //需要删除
  316. this.errorText = this.remove( dutyObject[ existDutyName ] )
  317. }
  318. }
  319. }
  320. }catch(e){
  321. this.errorText = Utils.processError( e, "处理用户职位new() 出错: " );
  322. }
  323. };
  324. DutyAction.prototype.add = function( dutyName ){
  325. try {
  326. var data = {
  327. name : dutyName,
  328. unit : this.unit,
  329. identityList : [this.identity]
  330. }
  331. var resp = applications.postQuery('x_organization_assemble_control', 'unitduty', JSON.stringify( data ));
  332. var json = Utils.parseResp( resp );
  333. return json.type === "success" ? "" : Utils.getFailText( json );
  334. }catch(e){
  335. return Utils.processError( e, "新增用户职位add() 出错: " );
  336. }
  337. };
  338. DutyAction.prototype.update = function( dutyData ){
  339. try {
  340. if( dutyData.identityList ){
  341. dutyData.identityList.push( this.identity )
  342. }else{
  343. dutyData.identityList = [this.identity];
  344. }
  345. var resp = applications.putQuery('x_organization_assemble_control', 'unitduty/'+dutyData.id, JSON.stringify( dutyData ));
  346. var json = Utils.parseResp( resp );
  347. return json.type === "success" ? "" : Utils.getFailText( json );
  348. }catch(e){
  349. return Utils.processError( e, "修改用户职位add() 出错: " );
  350. }
  351. };
  352. DutyAction.prototype.remove = function( dutyData ){
  353. try {
  354. if( !dutyData.identityList || !dutyData.identityList.length )return;
  355. dutyData.identityList = Utils.arrayErase( dutyData.identityList, this.identityId );
  356. var resp = applications.putQuery('x_organization_assemble_control', 'unitduty/'+dutyData.id, JSON.stringify( dutyData ));
  357. var json = Utils.parseResp( resp );
  358. return json.type === "success" ? "" : Utils.getFailText( json );
  359. }catch(e){
  360. return Utils.processError( e, "修改用户职位remove() 出错: " );
  361. }
  362. };
  363. DutyAction.prototype.identityEquals = function( source, target ) {
  364. var sourceList = Utils.typeOf( source ) === "array" ? source : [source];
  365. for( var i=0; i<sourceList.length; i++ ){
  366. var s = sourceList[i];
  367. if( Utils.arrayIndexOf([s.unique, s.distinguishedName, s.id], target) > -1 ) {
  368. this.identityId = s.id;
  369. return true;
  370. }
  371. }
  372. return false;
  373. };
  374. //identityEquals: function( source, target ) {
  375. // var flag = false;
  376. // var sourceList = Utils.typeOf( source ) === "array" ? source : [source];
  377. // var targetList = Utils.typeOf( target ) === "array" ? target : [target];
  378. // for( var i=0; i<sourceList.length; i++ ){
  379. // var s = sourceList[i];
  380. // var s_type = Utils.typeOf( s );
  381. // for( var j=0; j<targetList.length;j++ ){
  382. // var t = targetList[j];
  383. // var t_type = Utils.typeOf( t );
  384. // if( s_type === "string" && t_type === "string" ){
  385. // if( s == t )return true;
  386. // }else if( s_type === "string" && t_type === "object" ){
  387. // if( Utils.arrayIndexOf([ t.unique, t.distinguishedName, t.id], s) > -1 )return true;
  388. // }else if( s_type === "object" && t_type === "string" ){
  389. // if( Utils.arrayIndexOf([s.unique, s.distinguishedName, s.id], t) > -1 )return true;
  390. // }else if( s_type === "object" && t_type === "object" ){
  391. // if (t.id && t.id == s.id)return true;
  392. // if (t.unique && t.unique == s.unique)return true;
  393. // if (t.distinguishedName && t.distinguishedName == s.distinguishedName)return true;
  394. // }
  395. // }
  396. // }
  397. // return false;
  398. //}
  399. var IdentityAction = function( json ){
  400. this.errorText = "";
  401. var unit_new = []; //传入的用户所在部门
  402. var unit_new_obj = this.unit_new_obj = {};
  403. for( var i=0; i<json.unitList.length; i++ ){
  404. var unit = json.unitList[i];
  405. if( !unit_new_obj[ unit.flag ] ){
  406. var obj = unit_new_obj[ unit.flag ] = Utils.objectClone(unit);
  407. obj.dutyList = unit.duty ? [ unit.duty ] : [];
  408. }else{
  409. if( unit.duty )unit_new_obj[ unit.flag].dutyList.push( unit.duty )
  410. }
  411. }
  412. for( var flag in unit_new_obj ){
  413. unit_new.push( unit_new_obj[flag] );
  414. }
  415. var flag = Utils.getUserFlag( json );
  416. if( !flag ){
  417. this.errorText = "处理用户身份IdentityAction.new() 出错: 未能找到用户标志";
  418. return;
  419. }
  420. try{
  421. var resp = applications.getQuery('x_organization_assemble_control', 'identity/list/person/'+flag );
  422. var json_identityList = Utils.parseResp( resp );
  423. var identityList;
  424. if( json_identityList.type == "success" ){
  425. identityList = json_identityList.data || [];
  426. }else{
  427. identityList = [];
  428. //return Utils.getFailText( json_identityList );
  429. }
  430. var req = {"personList":[flag]};
  431. var unitResq = applications.postQuery('x_organization_assemble_express', 'unit/list/person/object', JSON.stringify( req ) );
  432. var json_unit_old = Utils.parseResp( unitResq );
  433. var unit_old;
  434. if( json_unit_old.type == "success" ){
  435. unit_old = json_unit_old.data || [];
  436. }else{
  437. this.errorText = Utils.getFailText( json_unit_old );
  438. return;
  439. }
  440. for( var i=0; i<unit_old.length; i++ ){
  441. var obj_new = this.getEqualUnitFromArray( unit_new, unit_old[i] );
  442. if( obj_new == null ){ //老的不在了,要删除
  443. print("删除身份");
  444. var identity = this.getIdentityByUnit( identityList, unit_old[i] );
  445. this.errorText = this.remove( identity )
  446. }else{ //已经存在,要修改
  447. print("修改身份");
  448. var identity = this.getIdentityByUnit( identityList, unit_old[i] );
  449. this.errorText = this.update( identity, obj_new, unit_old[i] );
  450. }
  451. }
  452. for( var i=0; i<unit_new.length; i++ ){
  453. if( !unit_new[i].flag || unit_new[i].flag == "" )continue;
  454. var obj_old = this.getEqualUnitFromArray( unit_old, unit_new[i] );
  455. if( obj_old == null ){ //需要新增
  456. print("新增身份");
  457. this.errorText = this.add( flag, unit_new[i], json.name, json.ignoreFlag );
  458. }
  459. }
  460. if( !this.errorText ){
  461. for( var flag in unit_new_obj ){
  462. var unit = unit_new_obj[flag];
  463. if(unit.identity){
  464. var dutyAction = new DutyAction( unit.identity, unit.flag, unit.dutyList );
  465. this.errorText = dutyAction.errorText;
  466. }
  467. }
  468. }
  469. }catch(e){
  470. this.errorText = Utils.processError( e, "处理用户身份IdentityAction.new() 出错: " );
  471. }
  472. };
  473. IdentityAction.prototype.add = function( flag, unit, personName, ignoreFlag ){
  474. //flag: "", //组织唯一编码unique/组织的distinguishedName/组织id
  475. // orderNumber: "", //在组织里的排序号,升序排列,为空在最后
  476. // description: "", //描述
  477. // duty : "", //用户在该组织的职务
  478. // position : "" //用户在该组织的岗位
  479. var data = {
  480. name : personName,
  481. person : flag,
  482. unit : unit.flag,
  483. description : unit.description
  484. };
  485. try {
  486. var resp = applications.postQuery('x_organization_assemble_control', 'identity', JSON.stringify( data ));
  487. var json = Utils.parseResp( resp );
  488. if( json.type === "success" ){
  489. this.unit_new_obj[ unit.flag].identity = json.data.id;
  490. return "";
  491. }else{
  492. if( ignoreFlag != "no" ){
  493. archiveFlag = true;
  494. }else{
  495. return Utils.getFailText( json );
  496. }
  497. //if( json.prompt == "com.x.organization.assemble.control.jaxrs.identity.ExceptionUnitNotExist" ){ //组织不存在,创建组织
  498. //var u_data = { name : unit.flag, unique : unit.flag };
  499. //var json = Utils.parseResp( applications.postQuery('x_organization_assemble_control', 'unit', JSON.stringify( u_data )));
  500. //if( json.type == "success" ){
  501. // return IdentityAction.add( flag, unit );
  502. //}
  503. //}else{
  504. // return Utils.getFailText( json );
  505. //}
  506. }
  507. return "";
  508. }catch(e){
  509. return Utils.processError( e, "新增用户身份IdentityAction.add() 出错: " );
  510. }
  511. };
  512. IdentityAction.prototype.remove = function( data ){
  513. try {
  514. var resp = applications.deleteQuery('x_organization_assemble_control', 'identity/'+data.id );
  515. var json = Utils.parseResp( resp );
  516. if( json.type && json.type == "success" ){
  517. return "";
  518. }else{
  519. return Utils.getFailText( json );
  520. }
  521. }catch(e){
  522. return Utils.processError( e, "删除用户身份IdentityAction.remove() 出错: " );
  523. }
  524. };
  525. IdentityAction.prototype.update = function( identity, unit_new, unit_old ){
  526. //flag: "", //组织唯一编码unique/组织的distinguishedName/组织id
  527. // orderNumber: "", //在组织里的排序号,升序排列,为空在最后
  528. // description: "", //描述
  529. // duty : "", //用户在该组织的职务
  530. // position : "" //用户在该组织的岗位
  531. if( !identity )return;
  532. this.unit_new_obj[ unit_new.flag].identity = identity.id || identity.unique || identity.distinguishedName;
  533. if( identity.orderNumber != unit_new.orderNumber || identity.description != unit_new.description ){
  534. identity.orderNumber = unit_new.orderNumber;
  535. identity.description = unit_new.description;
  536. try {
  537. var resp = applications.putQuery('x_organization_assemble_control', 'identity/'+identity.id, JSON.stringify(identity) );
  538. var json = Utils.parseResp( resp );
  539. if( json.type && json.type == "success" ){
  540. return "";
  541. }else{
  542. return Utils.getFailText( json );
  543. }
  544. }catch(e){
  545. return Utils.processError( e, "修改用户身份IdentityAction.update() 出错: " );
  546. }
  547. }else{
  548. return "";
  549. }
  550. };
  551. IdentityAction.prototype.getIdentityByUnit= function(identityList, unit){
  552. for( var i = 0; i<identityList.length; i++ ){
  553. var identity = identityList[i];
  554. if( identity.unitLevelName === unit.levelName ){
  555. return identity;
  556. }
  557. }
  558. };
  559. IdentityAction.prototype.getEqualUnitFromArray = function( sourceUnitArray, targetUnit ){
  560. for( var i=0; i<sourceUnitArray.length; i++ ){
  561. if( this.unitEquals( sourceUnitArray[i], targetUnit ) ){
  562. return sourceUnitArray[i];
  563. }
  564. }
  565. return null;
  566. };
  567. IdentityAction.prototype.unitEquals= function( source, target ) {
  568. if( !source || !target )return false;
  569. if (target.flag && (Utils.arrayIndexOf([source.flag, source.unique, source.distinguishedName, source.levelName, source.id], target.flag) > -1))return true;
  570. if (source.flag && (Utils.arrayIndexOf([target.flag, target.unique, target.distinguishedName, target.levelName, target.id], source.flag) > -1))return true;
  571. if (target.id && target.id == source.id)return true;
  572. if (target.unique && target.unique == source.unique)return true;
  573. if (target.distinguishedName && target.distinguishedName == source.distinguishedName)return true;
  574. if (target.levelName && target.levelName == source.levelName)return true;
  575. return false;
  576. };
  577. function get( json ){
  578. var errorText = "";
  579. var flag = Utils.getUserFlag(json);
  580. var resp;
  581. var person_old;
  582. if( flag ){
  583. try{
  584. resp = applications.getQuery('x_organization_assemble_control', "person/"+flag ); //先获取人员信息
  585. var json_old = Utils.parseResp( resp );
  586. if( json_old.type && json_old.type == "success" ){
  587. person_old = json_old.data;
  588. }else{
  589. return Utils.getFailText( json_old );
  590. }
  591. }catch(e){
  592. return Utils.processError( e, "get() 出错: " );
  593. }
  594. delete person_old.woIdentityList;
  595. delete person_old.woRoleList;
  596. delete person_old.woGroupList;
  597. delete person_old.woPersonAttributeList;
  598. //delete person_old.control;
  599. //delete person_old.controllerList;
  600. return person_old;
  601. //applications.putQuery('x_organization_assemble_control', "person/"+json.unique, json );
  602. }else{
  603. errorText = "参数中没有个人标志:distinguishedName , unique, employee, mobile 或 id, 不能获取用户";
  604. print(errorText);
  605. return errorText;
  606. }
  607. }
  608. function add( requestJson ){
  609. print("添加个人");
  610. var json = Utils.objectClone(requestJson);
  611. var errorText;
  612. var response;
  613. var resp;
  614. try{
  615. if( json.orderNumber ){
  616. try{ json.orderNumber = parseInt( json.orderNumber).toString() }catch(e){}
  617. }
  618. if( json.superior && json.superior != "" && json.ignoreFlag != "no" ){ //判断汇报对象在不在
  619. var superiorData = get({ flag : json.superior });
  620. if( typeof( superiorData ) != "object" ){ //不在则新建
  621. archiveFlag = true;
  622. delete json.superior;
  623. }
  624. }
  625. var personData = get(json);
  626. if( typeof personData == "object" ){
  627. if( json.forceFlag && json.forceFlag == "yes" ){
  628. for( var key in json){
  629. if( key !== "action" && key !== "attributeList" && key !== "unitList" ){
  630. if( key == "orderNumber" && (!json[key] || json[key]=="") ){
  631. }else if( key == "id" || key == "unique" || key == "distinguishedName" || key == "changePasswordTime"){
  632. }else{
  633. personData[key] = json[key];
  634. }
  635. }
  636. }
  637. if( !personData.controllerList || personData.controllerList == null )personData.controllerList = [];
  638. resp = applications.putQuery('x_organization_assemble_control', "person/"+personData.id, JSON.stringify(personData) )
  639. }else{
  640. errorText = "人员“"+ Utils.getUserFlag(json) +"”已经在系统内存在";
  641. }
  642. }else{
  643. if( json.controllerList === null )json.controllerList = [];
  644. var data = Utils.objectClone(json);
  645. if(data.attributeList)delete data.attributeList;
  646. if(data.unitList)delete data.unitList;
  647. data.controllerList = [];
  648. resp = applications.postQuery( "x_organization_assemble_control", 'person', JSON.stringify(data));
  649. }
  650. if( resp && !errorText ){
  651. response = Utils.parseResp( resp );
  652. if( response.type && response.type == "success" ){
  653. if( !json.attributeList )json.attributeList = [];
  654. var attributeAction = new AttributeAction( json );
  655. errorText = attributeAction.errorText;
  656. if( !json.unitList )json.unitList = [];
  657. var identityAction = new IdentityAction( json );
  658. errorText = identityAction.errorText;
  659. }else{
  660. errorText = Utils.getFailText( response );
  661. }
  662. }
  663. if( archiveFlag && !errorText ){
  664. Utils.saveToLocal( requestJson ); //保存到本地
  665. }
  666. }catch(e){
  667. errorText = Utils.processError( e, "添加个人 add() 出错:" );
  668. }finally{
  669. var result = {
  670. "result" : errorText ? "error" : "success",
  671. "description" : errorText || ""
  672. };
  673. if( response && response.data ){
  674. result.id = response.data.id;
  675. }
  676. return result;
  677. }
  678. }
  679. function update(requestJson){
  680. print("修改个人");
  681. var json = Utils.objectClone(requestJson);
  682. var errorText;
  683. var response;
  684. try{
  685. if( json.orderNumber ){
  686. try{ json.orderNumber = parseInt( json.orderNumber ).toString() }catch(e){}
  687. }
  688. if( json.superior && json.superior != "" && json.ignoreFlag != "no" ){ //判断汇报对象在不在
  689. var superiorData = get({ flag : json.superior });
  690. if( typeof( superiorData ) != "object" ){ //不在则新建
  691. archiveFlag = true;
  692. delete json.superior;
  693. }
  694. }
  695. var personData = get(json);
  696. if( typeof personData == "object" ){
  697. for( var key in json){
  698. if( key !== "action" && key !== "attributeList" && key !== "unitList" ){
  699. if( key == "orderNumber" && (!json[key] || json[key]=="") ){
  700. }else if( key == "id" || key == "unique" || key == "distinguishedName" || key == "changePasswordTime"){
  701. }else{
  702. personData[key] = json[key];
  703. }
  704. }
  705. }
  706. if( !personData.controllerList || personData.controllerList == null )personData.controllerList = [];
  707. var resp = applications.putQuery('x_organization_assemble_control', "person/"+personData.id, JSON.stringify(personData) );
  708. var response = Utils.parseResp( resp );
  709. if( response.type && response.type == "success" ){
  710. if( !json.attributeList )json.attributeList = [];
  711. var attributeAction = new AttributeAction( json );
  712. errorText = attributeAction.errorText;
  713. if( !json.unitList )json.unitList = [];
  714. var identityAction = new IdentityAction( json );
  715. errorText = identityAction.errorText;
  716. }else{
  717. errorText = Utils.getFailText( response );
  718. }
  719. }else{
  720. errorText = personData;
  721. }
  722. if( archiveFlag && !errorText){
  723. Utils.saveToLocal( requestJson ); //保存到本地
  724. }
  725. }catch(e){
  726. errorText = Utils.processError( e, "修改个人 update() 出错:" );
  727. }finally{
  728. var result = {
  729. "result" : errorText ? "error" : "success",
  730. "description" : errorText || ""
  731. };
  732. if( response && response.data ){
  733. result.id = response.data.id;
  734. }
  735. return result;
  736. }
  737. }
  738. function updatePassword(json){
  739. print("修改用户密码");
  740. var errorText = "";
  741. var response;
  742. var flag = Utils.getUserFlag(json);
  743. if( flag ){
  744. try{
  745. var data = { "value": json.password };
  746. var resp = applications.putQuery('x_organization_assemble_control', "person/"+flag+"/set/password", JSON.stringify(data) ); //修改密码
  747. response = Utils.parseResp( resp );
  748. if( response.type && response.type == "success" ){
  749. }else{
  750. errorText = Utils.getFailText( response );
  751. }
  752. }catch(e){
  753. errorText = Utils.processError( e, "修改用户密码出错 " + flag + " updatePassword() 出错:" );
  754. }
  755. }else{
  756. errorText = "参数中没有个人标志:distinguishedName , unique, employee, mobile 或 id, 不能获取用户";
  757. print(errorText);
  758. }
  759. var result = {
  760. "result" : errorText ? "error" : "success",
  761. "description" : errorText || ""
  762. };
  763. //if( response && response.data ){
  764. // result.id = response.data.id;
  765. //}
  766. return result;
  767. }
  768. function updateSuperior(json){
  769. print("修改个人汇报对象");
  770. var errorText;
  771. var response;
  772. try{
  773. var personData = get(json);
  774. if( typeof personData == "object" ){
  775. personData.superior = json.superior;
  776. if( !personData.controllerList )personData.controllerList = [];
  777. var resp = applications.putQuery('x_organization_assemble_control', "person/"+personData.id, JSON.stringify(personData) );
  778. response = Utils.parseResp( resp );
  779. if( response.type && response.type == "success" ){
  780. }else{
  781. errorText = Utils.getFailText( response );
  782. }
  783. }else{
  784. errorText = personData;
  785. }
  786. }catch(e){
  787. errorText = Utils.processError( e, "修改个人 updateSuperior() 出错:" );
  788. }finally{
  789. var result = {
  790. "result" : errorText ? "error" : "success",
  791. "description" : errorText || ""
  792. };
  793. if( response && response.data ){
  794. result.id = response.data.id;
  795. }
  796. return result;
  797. }
  798. }
  799. function remove(json){
  800. print("删除个人");
  801. var errorText;
  802. var response;
  803. try{
  804. var person = get(json);
  805. if( typeof person == "object" ){
  806. var resp = applications.deleteQuery('x_organization_assemble_control', "person/"+person.id ); //s人员信息
  807. response = Utils.parseResp( resp );
  808. if( response.type && response.type == "success" ){
  809. }else{
  810. errorText = Utils.getFailText( response );
  811. }
  812. }else{
  813. errorText = person;
  814. }
  815. }catch(e){
  816. errorText = Utils.processError( e, "删除个人 remove() 出错:" );
  817. }finally{
  818. var result = {
  819. "result" : errorText ? "error" : "success",
  820. "description" : errorText || ""
  821. };
  822. if( response && response.data ){
  823. result.id = response.data.id;
  824. }
  825. return result;
  826. }
  827. }
  828. function init(){
  829. var result ="";
  830. var responseText = "";
  831. try{
  832. //print( "requestText="+requestText );
  833. var requestJson = JSON.parse(requestText);
  834. //print( "type of requestJson = " + typeof( requestJson ));
  835. if( typeof(requestJson) === "string" ){
  836. requestJson = JSON.parse(requestJson);
  837. }
  838. var action = requestJson.action;
  839. print("action="+action);
  840. switch( action ){
  841. case "add":
  842. result = add( requestJson );
  843. break;
  844. case "update":
  845. result = update( requestJson );
  846. break;
  847. case "updatepwd":
  848. result = updatePassword( requestJson );
  849. break;
  850. case "updateSuperior":
  851. result = updateSuperior( requestJson );
  852. break;
  853. case "delete" :
  854. result = remove( requestJson );
  855. break;
  856. default :
  857. result = {
  858. "result" : "error",
  859. "description" : "requestText未设置action,不执行操作"
  860. };
  861. break;
  862. }
  863. }catch(e){
  864. e.printStackTrace();
  865. result = {
  866. "result" : "error",
  867. "description" : e.name + ": " + e.message
  868. };
  869. }finally{
  870. print("responseText="+JSON.stringify(result));
  871. return result;
  872. }
  873. }
  874. init();