Ejemplo n.º 1
0
        public async Task Execute(IJobExecutionContext context)
        {
            JobKey     key     = context.JobDetail.Key;
            JobDataMap dataMap = context.JobDetail.JobDataMap;
            string     jobSays = dataMap.GetString("guid");

            var taskPlan = await _SqlLiteContext.TaskPlan.AsNoTracking().SingleOrDefaultAsync(o => o.GUID == jobSays);

            _logger.LogDebug("{TaskName}({EventId}):开始执行!", taskPlan.Name, jobSays);
            var TaskPlan = await _SqlLiteContext.TaskPlan.AsNoTracking().Where(o => o.GUID == jobSays).FirstOrDefaultAsync();

            string orgCode    = TaskPlan.OrgCode;
            var    OrgSetting = await _SqlLiteContext.OrgSetting.AsNoTracking().Where(o => o.CODE == orgCode).FirstOrDefaultAsync();

            string connectString = OrgSetting.ConnectingString;
            string orgType       = string.Empty;
            var    client        = _httpClientFactory.CreateClient();

            try
            {
                switch (OrgSetting.DBType)
                {
                //Oracle 0
                //SqlServer  1
                //MySql  2
                //Sqlite  3
                case "0":
                    orgType = DBTypeEnum.Oracle.ToString();
                    break;

                case "1":
                    orgType = DBTypeEnum.SqlServer.ToString();
                    break;

                case "2":
                    orgType = DBTypeEnum.MySql.ToString();
                    break;

                case "3":
                    orgType = DBTypeEnum.Sqlite.ToString();
                    break;

                default:
                    break;
                }
                _SqlHelper = new ExecSqlHelper(connectString, orgType);

                //List<DataSource> dataSources = new List<DataSource>();
                var taskPlanList = await _SqlLiteContext.TaskPlanRelation.AsNoTracking().Where(o => o.TaskPlanGuid == jobSays).FirstOrDefaultAsync();

                string dataJsonData       = string.Empty;
                string dataJsonDataDetail = string.Empty;
                //string taskApiUrl = _Configuration.GetSection("TaskApiUrls").GetSection("TaskApiUrl").Value;
                string         paramJson      = string.Empty;
                ResponseCommon responseCommon = new ResponseCommon();
                string         groupSql       = string.Empty;
                string[]       sqlStrings;
                string         sqlString    = string.Empty;
                string         afterSuccess = string.Empty;
                string         afterFalse   = string.Empty;
                string         MainKey      = string.Empty;
                string         MainKeyValue = string.Empty;
                var            systemKes    = await _SqlLiteContext.SystemKeys.AsNoTracking().Where(o => o.KeyName != "").ToListAsync();

                //计划中的数据源
                var dataSource = await _SqlLiteContext.OpenSql.AsNoTracking().Where(o => o.GUID == taskPlanList.OpenSqlGuid && o.IsStart == "1").ToListAsync();

                for (int j = 0; j < dataSource.Count; j++)
                {
                    MainKey   = dataSource[j].MainKey;
                    groupSql  = dataSource[j].GroupSqlString;
                    sqlString = dataSource[j].SqlString;

                    foreach (var item in systemKes)
                    {
                        groupSql  = groupSql.Replace($"[{item.KeyName}]", item.KeyValue);
                        sqlString = sqlString.Replace($"[{item.KeyName}]", item.KeyValue);
                    }

                    List <Datas> datas = new List <Datas>();

                    if (!groupSql.Contains(MainKey))
                    {
                        //主数据源中不包含关键字MainKey
                        _logger.LogError("{TaskName}({EventId}):\r\n主数据源中不包含关键字{MainKey}", taskPlan.Name, jobSays, MainKey);
                    }
                    else
                    {
                        //获取当前任务中分组数据
                        var dataMaindt = await _SqlHelper.GetDataTableAsync(groupSql);

                        //MainKeyValue = dataMaindt.Rows[]
                        for (int h = 0; h < dataMaindt.Rows.Count; h++)
                        {
                            List <DataTable> dataTables = new List <DataTable>();
                            DataTable        maindetail = dataMaindt.Copy();
                            MainKeyValue = string.Empty;
                            MainKeyValue = dataMaindt.Rows[h][MainKey].ToString();
                            afterSuccess = dataSource[j].AfterSqlString.Replace($"#{MainKey}#", MainKeyValue);
                            afterFalse   = dataSource[j].AfterSqlstring2.Replace($"#{MainKey}#", MainKeyValue);
                            sqlStrings   = sqlString.Replace($"#{MainKey}#", MainKeyValue).Split(";");
                            //_logger.LogInformation("{TaskName}{EventId}:\r\n数据源:{dataSource[j].Name },\r\n分组sql:{groupSql },\r\n选择sql:{ sqlString },\r\n成功sql:{ afterSuccess },\r\n失败sql:{ afterFalse}", taskPlan.Name, jobSays, dataSource[j].Name, groupSql, sqlString, afterSuccess, afterFalse);
                            dataTables.Clear();
                            for (int k = 0; k < sqlStrings.Length; k++)
                            {
                                dataTables.Add(await _SqlHelper.GetDataTableAsync(sqlStrings[k]));
                            }

                            // var dataDetaildt = await _SqlHelper.GetDataTableAsync(sqlString);
                            var sss = dataMaindt.Rows[h];
                            maindetail.Rows.Clear();
                            maindetail.ImportRow(dataMaindt.Rows[h]);
                            datas.Add(new Datas {
                                DataMain = maindetail, DataDetail = dataTables
                            });
                        }
                        paramJson = JsonConvert.SerializeObject(new DoTaskJson
                        {
                            OpenSqlGuid = dataSource[j].GUID,
                            Data        = datas
                        });

                        string result = await CommonHelper.HttpPostAsync(TaskPlan.DllOrUrl, paramJson, client);

                        _logger.LogInformation("{TaskName}({EventId}):\r\n接口地址:{TaskPlan.TaskUrl},\r\n入参Json{paramJson},\r\n返回:{result}", taskPlan.Name, jobSays, TaskPlan.DllOrUrl, paramJson, result);
                        responseCommon = (ResponseCommon)System.Text.Json.JsonSerializer.Deserialize(result, typeof(ResponseCommon));
                        //记录日志
                        if (responseCommon.code == "0")
                        {
                            var afterS = await _SqlHelper.ExecSqlAsync(afterSuccess);

                            //记录日志
                            _logger.LogInformation("{TaskName}({EventId}):\r\n数据源:{dataSource[j].Name },\r\n成功后执行语句为:{afterSuccess}\r\n,返回:{afterS}", taskPlan.Name, jobSays, dataSource[j].Name, afterSuccess, afterS);
                        }
                        else
                        {
                            var afterF = await _SqlHelper.ExecSqlAsync(afterFalse);

                            //记录日志
                            _logger.LogInformation("{TaskName}({EventId}):\r\n数据源:{dataSource[j].Name },\r\n失败后执行语句为:{afterFalse}\r\n,返回:{afterF}", taskPlan.Name, jobSays, dataSource[j].Name, afterFalse, afterF);
                        }
                    }
                }
            }

            catch (Exception ex)
            {
                _logger.LogError("{TaskName}({EventId})错误信息:{ex.Message},{ex.StackTrace}", taskPlan.Name, jobSays, ex.Message, ex.StackTrace);
                return;
            }
            finally
            {
                await _SqlHelper.DisposeAsync();
            }
        }
Ejemplo n.º 2
0
        public async Task Execute(IJobExecutionContext context)
        {
            var        _SqlLiteContext = _freeSqlFactory.GetBaseSqlLite();
            JobKey     key             = context.JobDetail.Key;
            JobDataMap dataMap         = context.JobDetail.JobDataMap;
            string     jobSays         = dataMap.GetString("guid");
            var        TaskPlan        = await _SqlLiteContext.Select <TaskPlan>().Where(o => o.GUID == jobSays).FirstAsync();

            _logger.LogInformation("{EventId}:开始执行!", TaskPlan.Name);
            string orgCode    = TaskPlan.OrgCode;
            var    OrgSetting = await _SqlLiteContext.Select <Organization>().Where(o => o.CODE == orgCode).FirstAsync();

            string connectString = OrgSetting.ConnectingString;
            string orgType       = string.Empty;

            try
            {
                switch (OrgSetting.DBType)
                {
                case "0":
                    orgType = DBTypeEnum.Oracle.ToString();
                    break;

                case "1":
                    orgType = DBTypeEnum.SqlServer.ToString();
                    break;

                case "2":
                    orgType = DBTypeEnum.MySql.ToString();
                    break;

                case "3":
                    orgType = DBTypeEnum.Sqlite.ToString();
                    break;

                default:
                    break;
                }
                var _SqlHelper   = new ExecSqlHelper(connectString, orgType);
                var taskPlanList = await _SqlLiteContext.Select <TaskPlanDetail>().Where(o => o.TaskPlanGuid == jobSays).ToListAsync();

                string[] openSqls = new string[taskPlanList.Count];
                for (int i = 0; i < openSqls.Length; i++)
                {
                    openSqls[i] = taskPlanList[i].OpenSqlGuid;
                }
                string   dataJsonData       = string.Empty;
                string   dataJsonDataDetail = string.Empty;
                string   paramJson          = string.Empty;
                string   groupSql           = string.Empty;
                string[] sqlStrings;
                string   sqlString    = string.Empty;
                string   afterSuccess = string.Empty;
                string   afterFalse   = string.Empty;
                string   MainKey      = string.Empty;
                string   MainKeyValue = string.Empty;
                var      systemKes    = await _SqlLiteContext.Select <SystemKey>().Where(o => o.KeyName != "").ToListAsync();

                //计划中的数据源
                var dataSource = await _SqlLiteContext.Select <DataSource>().Where(o => openSqls.Contains(o.GUID) && o.IsStart == "1").ToListAsync();

                string startupPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string dllPath     = string.Empty;
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    dllPath = "TaskDlls/" + TaskPlan.DllOrUrl.Split(',')[0];
                }
                else
                {
                    dllPath = "TaskDlls\\" + TaskPlan.DllOrUrl.Split(',')[0];
                }
                object obj = ReflectorDllHelper.ReturnObjType(dllPath, TaskPlan.DllOrUrl, _jobLogger);
                using (IUpJob upJob = obj as IUpJob)
                {
                    for (int j = 0; j < dataSource.Count; j++)
                    {
                        MainKey   = dataSource[j].MainKey;
                        groupSql  = dataSource[j].GroupSqlString;
                        sqlString = dataSource[j].SqlString;
                        foreach (var item in systemKes)
                        {
                            groupSql  = groupSql.Replace($"[{item.KeyName}]", item.KeyValue);
                            sqlString = sqlString.Replace($"[{item.KeyName}]", item.KeyValue);
                        }
                        List <Datas> datas = new List <Datas>();
                        if (!groupSql.Contains(MainKey))
                        {
                            //主数据源中不包含关键字MainKey
                            _logger.LogError("{EventId}:\r\n主数据源中不包含关键字{MainKey}", TaskPlan.Name, MainKey);
                        }
                        else
                        {
                            //获取当前任务中分组数据 ,主表
                            var dataMaindt = await _SqlHelper.GetDataTableAsync(groupSql);

                            if (dataMaindt.Rows.Count == 0)
                            {
                                _logger.LogError("{EventId}:\r\n查询无数据:{groupSql}", TaskPlan.Name, groupSql);
                            }
                            else
                            {
                                for (int h = 0; h < dataMaindt.Rows.Count; h++)
                                {
                                    List <DataTable> dataTables = new List <DataTable>();
                                    DataTable        maindetail = dataMaindt.Copy();
                                    MainKeyValue = string.Empty;
                                    MainKeyValue = dataMaindt.Rows[h][MainKey].ToString();
                                    afterSuccess = dataSource[j].AfterSqlString.Replace($"#{MainKey}#", MainKeyValue);
                                    afterFalse   = dataSource[j].AfterSqlstring2.Replace($"#{MainKey}#", MainKeyValue);
                                    sqlStrings   = sqlString.Replace($"#{MainKey}#", MainKeyValue).Split(";");
                                    dataTables.Clear();
                                    for (int k = 0; k < sqlStrings.Length; k++)
                                    {
                                        dataTables.Add(await _SqlHelper.GetDataTableAsync(sqlStrings[k]));
                                    }
                                    maindetail.Rows.Clear();
                                    maindetail.ImportRow(dataMaindt.Rows[h]);
                                    datas.Add(new Datas {
                                        DataMain = maindetail, DataDetail = dataTables
                                    });
                                }

                                var allResult = upJob.ExecJob(new JobPara {
                                    connString = connectString, dbType = orgType, jobCode = dataSource[j].GUID
                                }, datas);

                                if (await allResult == 0)
                                {
                                    await _SqlHelper.ExecSqlAsync(afterSuccess);

                                    _logger.LogInformation("{EventId}:\r\n数据源:{dataSource[j].Name },\r\n成功后执行语句为:{afterSuccess}\r\n", TaskPlan.Name, dataSource[j].Name, afterSuccess);
                                }
                                else
                                {
                                    await _SqlHelper.ExecSqlAsync(afterFalse);

                                    _logger.LogError("{EventId}:\r\n数据源:{dataSource[j].Name },\r\n失败后执行语句为:{afterFalse}\r\n", TaskPlan.Name, dataSource[j].Name, afterFalse);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("{EventId}错误信息:{ex.Message},{ex.StackTrace}", TaskPlan.Name, ex.Message, ex.StackTrace);
                return;
            }
        }