You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
7.9 KiB

package com.eco.plugin.xx.zgydsso.function;
import com.eco.plugin.xx.zgydsso.config.PluginSimpleConfig;
import com.eco.plugin.xx.zgydsso.db.bean.RoleUserLinkEntity;
import com.eco.plugin.xx.zgydsso.db.bean.TimeEntity;
import com.eco.plugin.xx.zgydsso.db.bean.UserEntity;
import com.eco.plugin.xx.zgydsso.db.controller.DBController;
import com.eco.plugin.xx.zgydsso.utils.FRUtils;
import com.eco.plugin.xx.zgydsso.utils.FileUtils;
import com.eco.plugin.xx.zgydsso.utils.Utils;
import com.fr.plugin.context.PluginContexts;
import com.fr.script.AbstractFunction;
import com.fr.stable.fun.Authorize;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Authorize(callSignKey = "com.eco.plugin.xx.zgydsso")
public class AuthFunction extends AbstractFunction {
@Override
public Object run(Object[] objects) {
if(!PluginContexts.currentContext().isAvailable()) {
return "fail";
}
PluginSimpleConfig psc = PluginSimpleConfig.getInstance();
//获取上一次同步的工号文件名
List<TimeEntity> userTimes = DBController.getAllTime("user");
String userTime = "0";
if(userTimes != null && userTimes.size() > 0){
FRUtils.FRLogInfo("userFileCount:"+userTimes.size());
userTime = userTimes.get(0).getFilename();
}
final String userTimeFinal = userTime;
//增量文件文件夹及工号、工号角色文件前缀
String folderPath = psc.getDataurl();
String userPre = psc.getUsernamefile();
String linkPre = psc.getRolelinkfile();
//工号文件过滤器,规则为以工号文件开头且文件名大于上次同步文件的文件名,按文件名的hash值排序(默认排序即是hash值,所以这里不显示指定)
FileFilter userFilter = new FileFilter() {
@Override
public boolean accept(File pathname) {
String filename = pathname.getName();
return filename.startsWith(userPre) && filename.compareTo(userTimeFinal) > 0 ;
}
};
File[] userFiles = FileUtils.getFileInFolderAndSort(folderPath,userFilter,null);
//遍历文件并操作用户,结束后将本次最后的文件名插入数据库中
if(userFiles != null && userFiles.length > 0){
for(int i=0;i<userFiles.length;i++){
File userFile = userFiles[i];
operUser(userFile);
}
String userFilename = userFiles[userFiles.length -1].getName();
TimeEntity te = new TimeEntity();
te.setId(UUID.randomUUID().toString());
te.setFilename(userFilename);
te.setType("user");
DBController.addTime(te);
}
//获取上一次同步的关联文件名
List<TimeEntity> linkTimes = DBController.getAllTime("link");
String linkTime = "0";
if(linkTimes != null && linkTimes.size() > 0){
FRUtils.FRLogInfo("linkFileCount:"+linkTimes.size());
linkTime = linkTimes.get(0).getFilename();
}
final String linkTimeFinal = linkTime;
//关联文件过滤器,规则为以关联文件开头且文件名大于上次同步文件的文件名,按文件名的hash值排序(默认排序即是hash值,所以这里不显示指定)
FileFilter linkFilter = new FileFilter() {
@Override
public boolean accept(File pathname) {
String filename = pathname.getName();
return filename.startsWith(linkPre) && filename.compareTo(linkTimeFinal) > 0;
}
};
File[] linkFiles = FileUtils.getFileInFolderAndSort(folderPath,linkFilter,null);
//遍历文件并操作关联关系,结束后将本次最后的文件名插入数据库中
if(linkFiles != null && linkFiles.length > 0){
for(int i=0;i<linkFiles.length;i++){
File linkFile = linkFiles[i];
operRoleUserLink(linkFile);
}
String userFilename = linkFiles[linkFiles.length -1].getName();
TimeEntity te = new TimeEntity();
te.setId(UUID.randomUUID().toString());
te.setFilename(userFilename);
te.setType("link");
DBController.addTime(te);
}
return "success";
}
/**
* 处理用户同步
*/
private static void operUser(File file){
if(file == null || !file.isFile() || !file.exists()){
return;
}
List<UserEntity> addorupdate = new ArrayList<UserEntity>();
List<UserEntity> delete = new ArrayList<UserEntity>();
BufferedReader br = null;
try{
FileInputStream fis = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(fis,"GBK");
br = new BufferedReader(reader);
String line = "";
while((line = br.readLine()) != null){
String[] userBean = line.split("&&");
String username = userBean[0];
String password = userBean[15];
//如果密码为空则设置为123456
if(Utils.isNullStr(password)){
password = "123456";
}
String city = userBean[7];
String status = userBean[2];
String realname = userBean[1];
UserEntity user = new UserEntity();
user.setId(username);
user.setUsername(username);
user.setPassword(password);
user.setCity(city);
user.setStatus(status);
user.setRealname(realname);
addorupdate.add(user);
}
br.close();
}catch(Exception e){
FRUtils.FRLogError("读取txt文件异常!");
}finally {
if(br != null){
try {
br.close();
} catch (IOException e) {
FRUtils.FRLogError("BufferedReader关闭异常");
}
}
}
DBController.batchUser(addorupdate,delete);
}
private static void operRoleUserLink(File file){
if(file == null || !file.isFile() || !file.exists()){
return;
}
List<RoleUserLinkEntity> addorupdate = new ArrayList<RoleUserLinkEntity>();
List<RoleUserLinkEntity> delete = new ArrayList<RoleUserLinkEntity>();
BufferedReader br = null;
try{
FileInputStream fis = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(fis,"GBK");
br = new BufferedReader(reader);
String line = "";
while((line = br.readLine()) != null){
String[] array = line.split("&&");
String username = array[0];
String roleid = array[1];
String system = array[2];
String status = array[4];
String oprtype = array[8];
if(!"RPT".equals(system)){
continue;
}
RoleUserLinkEntity user = new RoleUserLinkEntity();
user.setId(username);
user.setUsername(username);
user.setRoleid(roleid);
user.setSystem(system);
user.setStatus(status);
if("DEL".equals(oprtype)){
delete.add(user);
}
else{
addorupdate.add(user);
}
}
br.close();
}catch(Exception e){
FRUtils.FRLogError("读取txt文件异常!");
}finally {
if(br != null){
try {
br.close();
} catch (IOException e) {
FRUtils.FRLogError("BufferedReader关闭异常");
}
}
}
DBController.batchLink(addorupdate,delete);
}
}