package com.hx.platform.oa.modules.pad.service;
import com.hx.platform.oa.commons.utils.DateUtils;
import com.hx.platform.oa.commons.utils.StrUtils;
import com.hx.platform.oa.modules.doc.signsync.api.UnitService;
import com.hx.platform.oa.modules.doc.signsync.model.Unit;
import com.hx.platform.sys.api.OrgService;
import com.hx.platform.sys.model.Org;
import com.hx.platform.sys.util.SysUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class CreateSql {
//文件生成路径前缀
static private String pathPrefix = "C:\Users\Administrator\Desktop\PAD登录选项\";
//sql前缀
static String sqlPrefix = "INSERT INTO PAD_LOGIN_OPEN ("ID","ORG_ID","ORG_NAME","ORG_TYPE","OPEN_STATE","CREATE_ACCOUNT","CREATE_TIME","DEL_FLAG","OLD_ORG_ID") VALUES (";
@Autowired
private OrgService orgService;
@Autowired
private UnitService unitService;
//@Scheduled(cron = "0/10 * * * * ?")
public void test() throws IOException {
String unitId = "1f9c902a50c047fd897dffadf838215d";
String type = "1";//0:只创建当前单位sql;1:创建当前单位及其子单位
List unitStatusSql = this.createUnitStatusSql(unitId, type);
this.writeUnitStatusSql(unitStatusSql);
}
public void writeUnitStatusSql(List sqlList) throws IOException {
String fileName = "open_pad_sql_" + System.currentTimeMillis() + ".sql";
String absolutePath = pathPrefix + fileName;
File file = new File(absolutePath);
if (!file.exists()) {//文件不存在创建文件
file.createNewFile();
}
FileWriter writer = new FileWriter(file);
BufferedWriter buffered = new BufferedWriter(writer);
sqlList.forEach(sql -> {
try {
buffered.write(sql, 0, sql.length());
buffered.newline();
} catch (IOException e) {
e.printStackTrace();
}
});
buffered.close();
}
public List createUnitStatusSql(String unitId, String type) {
List strSql = new ArrayList<>();
List orgList = orgService.getOrgListById(unitId, type);//获取同步不单位信息
orgList.forEach(org -> {
String id = StrUtils.uuid();
String orgId = org.getId();
String orgName = org.getName();//单位,部门名称
String orgType = "1";//组织类型,1:单位;2:部门
String delFlag = "0";//虚拟删除标记(0:未删(默认),1:已删)
String openState = "1";//pad开通权限标识
String dateTime = DateUtils.getDateTime();
String protocolId = org.getProtocolId();
String createAccount = SysUtil.getCurAccount().getUserId();
Unit unit = unitService.queryUnitByStrIdNew(protocolId);
String oldOrgId = "";
if (unit == null) {
oldOrgId = "null";
} else {
oldOrgId = unit.getStrId();
}
String sql = sqlPrefix + "'" + id + "'" + "," + "'" + orgId + "'" + "," + "'" + orgName + "'" + "," + "'" + orgType + "'" + "," + "'" + openState + "'" + "," + "'" + createAccount + "'" + "," + "'" + dateTime + "'" + "," + "'" + delFlag + "'" + "," + "'" + oldOrgId + "'" + ");";
strSql.add(sql);
});
return strSql;
}
}