package com.ruoyi.config;


import org.activiti.engine.*;
import org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.activiti.engine.repository.DeploymentBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.io.IOException;

/**
 * 使用Java类完成配置文件
 */
@Configuration
public class ActivitiConfig {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private ResourcePatternResolver resourceLoader;

    /**
     * 初始化配置，将创建28张表
     *
     * @return
     */
    @Bean
    public StandaloneProcessEngineConfiguration processEngineConfiguration() {

        /*ProcessEngineConfiguration pec = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();
        pec.setJdbcDriver("oracle.jdbc.driver.OracleDriver");
        pec.setJdbcUrl("jdbc:oracle:thin:@47.114.101.16:1521/xe");
        pec.setJdbcUsername("newcpms");
        pec.setJdbcPassword("ssy123456");

        pec.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);

        //pec.setDatabaseSchema("FLOWABLE");
        pec.setDatabaseTablePrefix("flow");
        ProcessEngine processEngine = pec.buildProcessEngine();*/

        StandaloneProcessEngineConfiguration configuration = new StandaloneProcessEngineConfiguration();
        configuration.setDataSource(dataSource);
        configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        configuration.setDbIdentityUsed(false);
        configuration.setAsyncExecutorActivate(true);
        configuration.setActivityFontName("宋体");
        configuration.setAnnotationFontName("宋体");
        configuration.setLabelFontName("宋体");
        return configuration;
    }

    @Bean
    public ProcessEngine processEngine() {
        return processEngineConfiguration().buildProcessEngine();
    }

    @Bean
    public RepositoryService repositoryService() {
        return processEngine().getRepositoryService();
    }

    @Bean
    public RuntimeService runtimeService() {
        return processEngine().getRuntimeService();
    }

    @Bean
    public TaskService taskService() {
        return processEngine().getTaskService();
    }

    @Bean
    public HistoryService historyService() {
        return processEngine().getHistoryService();
    }

    /**
     * 部署流程
     *
     * @throws IOException
     */
    @PostConstruct
    public void initProcess() throws IOException {
//        DeploymentBuilder deploymentBuilder = repositoryService().createDeployment();
//        Resource common = resourceLoader.getResource("classpath:/processes/common.bpmn"); //加载流程图资源文件
//        deploymentBuilder.enableDuplicateFiltering().addInputStream(common.getFilename(), common.getInputStream()).name("普通隐患审批流程").deploy(); //按流程id部署
//
//        DeploymentBuilder deploymentBuilder1 = repositoryService().createDeployment();
//        Resource major= resourceLoader.getResource("classpath:/processes/major.bpmn");
//        deploymentBuilder1.enableDuplicateFiltering().addInputStream(major.getFilename(), major.getInputStream()).name("重大隐患审批流程").deploy();
//
//        DeploymentBuilder deploymentBuilder2 = repositoryService().createDeployment();
//        Resource accident = resourceLoader.getResource("classpath:/processes/accident.bpmn"); //加载流程图资源文件
//        deploymentBuilder2.enableDuplicateFiltering().addInputStream(accident.getFilename(), accident.getInputStream()).name("事件审批流程").deploy();//按流程id部署
//
//        DeploymentBuilder deploymentBuilder3 = repositoryService().createDeployment();
//        Resource spec = resourceLoader.getResource("classpath:/processes/MyProcess.bpmn"); //加载流程图资源文件
//        deploymentBuilder3.enableDuplicateFiltering().addInputStream(spec.getFilename(), spec.getInputStream()).name("特种设备审批流程").deploy();//按流程id部署
//
//        DeploymentBuilder deploymentBuilder4 = repositoryService().createDeployment();
//        Resource spec2 = resourceLoader.getResource("classpath:/processes/his/semsAddDelProcess.bpmn"); //加载流程图资源文件
//        deploymentBuilder4.enableDuplicateFiltering().addInputStream(spec2.getFilename(), spec2.getInputStream()).name("特种设备新增修改流程").deploy();//按流程id部署
//
//        DeploymentBuilder deploymentBuilder5 = repositoryService().createDeployment();
//        Resource spec3 = resourceLoader.getResource("classpath:/processes/sems/yearProcess.bpmn"); //加载流程图资源文件
//        deploymentBuilder5.enableDuplicateFiltering().addInputStream(spec3.getFilename(), spec3.getInputStream()).name("特种设备年检报告流程").deploy();//按流程id部署
//
//        DeploymentBuilder deploymentBuilder6 = repositoryService().createDeployment();
//        Resource spec4 = resourceLoader.getResource("classpath:/processes/sems/monthProcess.bpmn"); //加载流程图资源文件
//        deploymentBuilder6.enableDuplicateFiltering().addInputStream(spec4.getFilename(), spec4.getInputStream()).name("特种设备月度检验流程").deploy();//按流程id部署

//        DeploymentBuilder deploymentBuilder7 = repositoryService().createDeployment();
//        Resource spec7 = resourceLoader.getResource("classpath:/processes/specTraining/specTrainingPlanProcess.bpmn"); //加载流程图资源文件
//        deploymentBuilder7.enableDuplicateFiltering().addInputStream(spec7.getFilename(), spec7.getInputStream()).name("专项培训计划审核").deploy();//按流程id部署
//
//        DeploymentBuilder deploymentBuilder8 = repositoryService().createDeployment();
//        Resource spec8 = resourceLoader.getResource("classpath:/processes/maintenance.bpmn"); //加载流程图资源文件
//        deploymentBuilder8.enableDuplicateFiltering().addInputStream(spec8.getFilename(), spec8.getInputStream()).name("设备维修审核").deploy();//按流程id部署
//
//        DeploymentBuilder deploymentBuilder9 = repositoryService().createDeployment();
//        Resource spec9 = resourceLoader.getResource("classpath:/processes/intact/intactChangeApplicationProcess.bpmn"); //加载流程图资源文件
//        deploymentBuilder9.enableDuplicateFiltering().addInputStream(spec9.getFilename(), spec9.getInputStream()).name("设备变更申请流程").deploy();//按流程id部署
//
//        DeploymentBuilder deploymentBuilder10 = repositoryService().createDeployment();
//        Resource spec10 = resourceLoader.getResource("classpath:/processes/kekao/reliability.bpmn"); //加载流程图资源文件
//        deploymentBuilder10.enableDuplicateFiltering().addInputStream(spec10.getFilename(), spec10.getInputStream()).name("可靠性管理申请流程").deploy();//按流程id部署
//
//        DeploymentBuilder deploymentBuilder11 = repositoryService().createDeployment();
//        Resource spec11 = resourceLoader.getResource("classpath:/processes/reserveInvoice.bpmn"); //加载流程图资源文件
//        deploymentBuilder11.enableDuplicateFiltering().addInputStream(spec11.getFilename(), spec11.getInputStream()).name("预约开票审批流程").deploy();//按流程id部署
//
//        DeploymentBuilder deploymentBuilder12 = repositoryService().createDeployment();
//        Resource spec12 = resourceLoader.getResource("classpath:/processes/apply/offlinevalve.bpmn"); //加载流程图资源文件
//        deploymentBuilder12.enableDuplicateFiltering().addInputStream(spec12.getFilename(), spec12.getInputStream()).name("安全阀离线/切出申请流程").deploy();//按流程id部署
//
//        DeploymentBuilder deploymentBuilder13 = repositoryService().createDeployment();
//        Resource spec13 = resourceLoader.getResource("classpath:/processes/apply/safetychange.bpmn"); //加载流程图资源文件
//        deploymentBuilder13.enableDuplicateFiltering().addInputStream(spec13.getFilename(), spec13.getInputStream()).name("组织保护措施状态变更申请流程").deploy();//按流程id部署
//
//        DeploymentBuilder deploymentBuilder14 = repositoryService().createDeployment();
//        Resource spec14 = resourceLoader.getResource("classpath:/processes/sai/saiApply.bpmn"); //加载流程图资源文件
//        deploymentBuilder14.enableDuplicateFiltering().addInputStream(spec14.getFilename(), spec14.getInputStream()).name("SAI开项申请流程").deploy();//按流程id部署
//
//        DeploymentBuilder deploymentBuilder15 = repositoryService().createDeployment();
//        Resource spec15 = resourceLoader.getResource("classpath:/processes/sems/semsWeekcheck.bpmn"); //加载流程图资源文件
//        deploymentBuilder15.enableDuplicateFiltering().addInputStream(spec15.getFilename(), spec15.getInputStream()).name("每周安全排查报告").deploy();//按流程id部署
//
        DeploymentBuilder deploymentBuilder16 = repositoryService().createDeployment();
        Resource spec16 = resourceLoader.getResource("classpath:/processes/bccnew/dsdtTraining.bpmn"); //加载流程图资源文件
        deploymentBuilder16.enableDuplicateFiltering().addInputStream(spec16.getFilename(), spec16.getInputStream()).name("导师带徒经理签字").deploy();//按流程id部署

        DeploymentBuilder deploymentBuilder17 = repositoryService().createDeployment();
        Resource spec17 = resourceLoader.getResource("classpath:/processes/bccnew/dsdtNoPass.bpmn"); //加载流程图资源文件
        deploymentBuilder17.enableDuplicateFiltering().addInputStream(spec17.getFilename(), spec17.getInputStream()).name("导师带徒学员成绩不合格审批").deploy();//按流程id部署

    }
}


