Test.min.js 3.3 KB

1
  1. print("批量导入组织");var File=Java.type("java.io.File");var Config={interfaceUrl:"http://ahpp.zoneland.net:20030/x_program_center/jaxrs/invoke/unitAsync/execute",localPath:File.separator+"data"+File.separator+"ImportUnitFromExcel"+File.separator,recordLocalPath:File.separator+"data"+File.separator+"ImportUnitFromExcelRecord"+File.separator};var Utils={parseResp:function(e){if(!e||e===null){return{type:"error",message:"服务响应是null,需要管理员查看后台日志"}}else{var a=JSON.parse(e.toString());return a}},getFailText:function(e){var a;if(e.message){a=e.message+(e.prompt?"("+e.prompt+")":"")}else if(e.prompt){a=e.prompt}else{a="未知异常"}print(a);return a}};function getFileList(){var e=Config.localPath;var a=new File(e);var r=Java.type("java.util.ArrayList");var t=new r;if(a.exists()){var i=a.listFiles();for(var s=0;s<i.length;s++){if(i[s].isFile()){print(i[s].getName());var l=i[s].getName().split(".");if(l[l.length-1].equals("xls")){t.add(i[s])}}}}else{print("目录不存在:"+e)}return t}function importExcel(e){var a=Java.type("java.io.FileInputStream");var r="";var t=null;try{t=new a(e);var i=null;try{var s=e.getName();if(s.endsWith(".xls")){var l=Java.type("org.apache.poi.hssf.usermodel.HSSFWorkbook");i=new l(t)}else if(s.endsWith(".xlsx")){var n=Java.type("org.apache.poi.hssf.usermodel.XSSFWorkbook");i=new n(t)}else{r="不支持的文件类型!";print(r)}}catch(e){r="解析Excel文件出错!";print(r);e.printStackTrace()}finally{t.close()}if(r.length===0){var o=null!==i?i.getNumberOfSheets():0;var p=i.getSheetAt(0);var c=p.getPhysicalNumberOfRows();if(c>1){for(var v=1;v<c;v++){print("正在执行导入第"+v+"行");var u=p.getRow(v);var f=u.getLastCellNum();if(f>0){importRow(u,f)}}}else{r="EXCEL没有数据,请确定。"}}if(r.length()>0){print("错误消息:"+r)}else{print(e.getName()+"导入成功!")}}catch(e){e.printStackTrace()}}var Cell=Java.type("org.apache.poi.ss.usermodel.Cell");function importRow(e,a){var r={action:"add",attributeList:[]};for(var t=0;t<a;t++){var i="";var s=e.getCell(t);if(s!==null){s.setCellType(Cell.CELL_TYPE_STRING);i=s.getStringCellValue().trim()}var l=true;switch(t){case 1:r.unique=i;r.shortName=i;break;case 4:r.typeList=[i.toLowerCase()];break;case 5:r.name=i;break;case 6:var n={name:"displayName",value:i};r.attributeList.push(n);break;case 7:r.description=i;break;case 9:r.orderNumber=i;break;case 10:r.superior=i;break;case 12:if(i.toLowerCase()!=="active")l=false;break;case 17:var n={name:"postalcode",value:i};r.attributeList.push(n);break}if(!l)return false;var o=sendRequest(r);return o==""}}function sendRequest(e){var a="";try{if(typeof e=="string")e=JSON.parse(bodyStr);e.saveFlag="no";var r=com.x.base.core.project.connection.CipherConnectionAction.post(false,Config.interfaceUrl,JSON.stringify(e));var t=Utils.parseResp(r);if(t.type&&t.type=="success"&&t.data&&t.data.value&&t.data.value.result&&t.data.value.result=="success"){}else{a=Utils.getFailText(t)}}catch(e){a=e.printStackTrace()}finally{print("resend person errorText="+a);return a}}function init(){var e=getFileList();for(var a=0;a<e.size();a++){try{var r=e.get(a);if(importExcel(r)){print("正在修改文件目录:"+r.getName());var t=Config.recordLocalPath+r.getName();if(r.renameTo(new File(t))){print("文件移动到了:"+t)}else{print(r.getName()+" is failed to move!")}}}catch(e){e.printStackTrace()}}}init();