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; /** * @Package: com.hx.platform.oa.modules.doc.signsync.createsql * @ClassName: CreateSqlDemo * @Author: guxiang * @Description: 公文同步, 根据单位id创建单位状态表插入语句 * @Date: 2021-4-30 15:19 * @Version: 1.0 */ @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<String> unitStatusSql = this.createUnitStatusSql(unitId, type); this.writeUnitStatusSql(unitStatusSql); } /** * @Author guxiang * @Version 1.0 * @Description 创建待同步单位sql * @Param sqlList sql集合 * @Date 2021-4-30 15:34 */ public void writeUnitStatusSql(List<String> 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(); } /** * @Author guxiang * @Version 1.0 * @Description 创建sql * @Param type 0:只创建当前单位sql;1:创建当前单位及其子单位 * @Date 2021-4-30 15:46 */ public List<String> createUnitStatusSql(String unitId, String type) { List<String> strSql = new ArrayList<>(); List<Org> 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; } }
批量创建sql写脚本工具类
最新推荐文章于 2022-07-03 18:34:48 发布