批量创建sql写脚本工具类

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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值