栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

批量创建sql写脚本工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

批量创建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;


@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;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/786095.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号