From fab6df69106bc9d16499450fefa983765fb52c7b Mon Sep 17 00:00:00 2001 From: liguoyang Date: Thu, 4 Jan 2024 15:13:39 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E7=BB=84=E7=BB=87=E6=9E=B6=E6=9E=84?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=BB=A3=E7=A0=81=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Configurations/ServiceSettings.cs | 51 ++++---- .../Dlhis.ScheduledTask.csproj | 4 +- .../Extensions/DbConnectionExtensions.cs | 122 ------------------ .../Extensions/EnumerableExtensions.cs | 13 -- Src/Dlhis.ScheduledTask/Job/JinkoHrJob.cs | 111 +++------------- Src/Dlhis.ScheduledTask/appsettings.json | 3 + 6 files changed, 46 insertions(+), 258 deletions(-) delete mode 100644 Src/Dlhis.ScheduledTask/Extensions/DbConnectionExtensions.cs delete mode 100644 Src/Dlhis.ScheduledTask/Extensions/EnumerableExtensions.cs diff --git a/Src/Dlhis.ScheduledTask/Configurations/ServiceSettings.cs b/Src/Dlhis.ScheduledTask/Configurations/ServiceSettings.cs index a17dceea..cdb1fba0 100644 --- a/Src/Dlhis.ScheduledTask/Configurations/ServiceSettings.cs +++ b/Src/Dlhis.ScheduledTask/Configurations/ServiceSettings.cs @@ -1,10 +1,9 @@ -using System; -using System.Configuration; -using ConfigurationManager = System.Configuration.ConfigurationManager; +using Kurisu.Core.ConfigurableOptions.Attributes; namespace Dlhis.ScheduledTask.Configurations { + [Configuration] public class ServiceSettings { public string ServerUrl { get; private set; } @@ -46,24 +45,24 @@ namespace Dlhis.ScheduledTask.Configurations public string RestPassword { get; set; } public ServiceSettings() { - ServerUrl = ConfigurationManager.AppSettings[nameof(ServerUrl)]; - UserName = ConfigurationManager.AppSettings[nameof(UserName)]; - Password = ConfigurationManager.AppSettings[nameof(Password)]; - SyncTime = ConfigurationManager.AppSettings[nameof(SyncTime)]; - DlhisDatabase = ConfigurationManager.AppSettings[nameof(DlhisDatabase)]; - AttendanceUploadUrl = ConfigurationManager.AppSettings[nameof(AttendanceUploadUrl)]; - AttendanceUploadInterval = SafeParseInt(ConfigurationManager.AppSettings[nameof(AttendanceUploadInterval)]); - Werks = ConfigurationManager.AppSettings[nameof(Werks)]; - BeginSyncDate = SafeParseDate(ConfigurationManager.AppSettings[nameof(BeginSyncDate)], DateTime.Today); - ApiPort = SafeParseInt(ConfigurationManager.AppSettings[nameof(ApiPort)], 8099); - EHRWebServiceUserName = ConfigurationManager.AppSettings[nameof(EHRWebServiceUserName)]; - EHRWebServicePassword = ConfigurationManager.AppSettings[nameof(EHRWebServicePassword)]; - EHRInsertRecordApiUrl = ConfigurationManager.AppSettings[nameof(EHRInsertRecordApiUrl)]; - EHRDeleteRecordApiUrl = ConfigurationManager.AppSettings[nameof(EHRDeleteRecordApiUrl)]; - EHRSavePhotoApiUrl = ConfigurationManager.AppSettings[nameof(EHRSavePhotoApiUrl)]; - EHR_GET_WORK_CARD_INFOApiUrl = ConfigurationManager.AppSettings[nameof(EHR_GET_WORK_CARD_INFOApiUrl)]; - RestUserName = ConfigurationManager.AppSettings[nameof(RestUserName)]; - RestPassword = ConfigurationManager.AppSettings[nameof(RestPassword)]; + //ServerUrl = ConfigurationManager.AppSettings[nameof(ServerUrl)]; + //UserName = ConfigurationManager.AppSettings[nameof(UserName)]; + //Password = ConfigurationManager.AppSettings[nameof(Password)]; + //SyncTime = ConfigurationManager.AppSettings[nameof(SyncTime)]; + //DlhisDatabase = ConfigurationManager.AppSettings[nameof(DlhisDatabase)]; + //AttendanceUploadUrl = ConfigurationManager.AppSettings[nameof(AttendanceUploadUrl)]; + //AttendanceUploadInterval = SafeParseInt(ConfigurationManager.AppSettings[nameof(AttendanceUploadInterval)]); + //Werks = ConfigurationManager.AppSettings[nameof(Werks)]; + //BeginSyncDate = SafeParseDate(ConfigurationManager.AppSettings[nameof(BeginSyncDate)], DateTime.Today); + //ApiPort = SafeParseInt(ConfigurationManager.AppSettings[nameof(ApiPort)], 8099); + //EHRWebServiceUserName = ConfigurationManager.AppSettings[nameof(EHRWebServiceUserName)]; + //EHRWebServicePassword = ConfigurationManager.AppSettings[nameof(EHRWebServicePassword)]; + //EHRInsertRecordApiUrl = ConfigurationManager.AppSettings[nameof(EHRInsertRecordApiUrl)]; + //EHRDeleteRecordApiUrl = ConfigurationManager.AppSettings[nameof(EHRDeleteRecordApiUrl)]; + //EHRSavePhotoApiUrl = ConfigurationManager.AppSettings[nameof(EHRSavePhotoApiUrl)]; + //EHR_GET_WORK_CARD_INFOApiUrl = ConfigurationManager.AppSettings[nameof(EHR_GET_WORK_CARD_INFOApiUrl)]; + //RestUserName = ConfigurationManager.AppSettings[nameof(RestUserName)]; + //RestPassword = ConfigurationManager.AppSettings[nameof(RestPassword)]; } private static int SafeParseInt(string value, int defaultValue = 0) @@ -74,6 +73,7 @@ namespace Dlhis.ScheduledTask.Configurations } return defaultValue; } + private static bool SafeParseBoolean(string value, bool defaultValue = false) { if (bool.TryParse(value, out var b)) @@ -82,6 +82,7 @@ namespace Dlhis.ScheduledTask.Configurations } return defaultValue; } + private static DateTime SafeParseDate(string value, DateTime defaultValue) { if (DateTime.TryParse(value, out var b)) @@ -90,13 +91,5 @@ namespace Dlhis.ScheduledTask.Configurations } return defaultValue; } - - public void SetBeginSyncDate(DateTime beginSyncDate) - { - BeginSyncDate = beginSyncDate; - Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); - config.AppSettings.Settings[nameof(BeginSyncDate)].Value = beginSyncDate.ToString("yyyy-MM-dd"); - config.Save(ConfigurationSaveMode.Modified); - } } } diff --git a/Src/Dlhis.ScheduledTask/Dlhis.ScheduledTask.csproj b/Src/Dlhis.ScheduledTask/Dlhis.ScheduledTask.csproj index 341c1b33..14b363be 100644 --- a/Src/Dlhis.ScheduledTask/Dlhis.ScheduledTask.csproj +++ b/Src/Dlhis.ScheduledTask/Dlhis.ScheduledTask.csproj @@ -2,7 +2,7 @@ net6.0 - enable + disable enable Linux ..\.. @@ -21,8 +21,6 @@ - - diff --git a/Src/Dlhis.ScheduledTask/Extensions/DbConnectionExtensions.cs b/Src/Dlhis.ScheduledTask/Extensions/DbConnectionExtensions.cs deleted file mode 100644 index 30da5522..00000000 --- a/Src/Dlhis.ScheduledTask/Extensions/DbConnectionExtensions.cs +++ /dev/null @@ -1,122 +0,0 @@ -using log4net; -using System.Data.SqlClient; -using System.Data; -using Dapper; -using Dlhis.ScheduledTask.Job; - -namespace Dlhis.ScheduledTask.Extensions -{ - public static class DbConnectionExtensions - { - private static readonly ILog log = LogManager.GetLogger(typeof(JinkoHrJob)); - - public static void BulkcopyInsert(this IDbConnection conn, string tableName, List data) - { - using (var bulkcopy = new SqlBulkCopy(conn as SqlConnection)) - { - bulkcopy.BatchSize = 10000; - bulkcopy.BulkCopyTimeout = 60; - bulkcopy.DestinationTableName = tableName; - var dt = ToDataTable(data); - foreach (DataColumn column in dt.Columns) - { - bulkcopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(column.ColumnName, column.ColumnName)); - } - - bulkcopy.WriteToServer(dt); - } - } - - - private static DataTable ToDataTable(List data) - { - var dt = new DataTable(); - var properties = typeof(T).GetProperties();//.Where(x => x.IsDefined(typeof(CopyAttribute), false)); - - foreach (var property in properties) - { - dt.Columns.Add(property.Name, Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType); - } - - foreach (var item in data) - { - var values = new object[properties.Length]; - for (int i = 0; i < properties.Length; i++) - { - if (properties[i].PropertyType == typeof(DateTime?)) - { - var o = (DateTime?)properties[i].GetValue(item); - if (!o.HasValue || o.Value == DateTime.MinValue) - { - o = new DateTime(1900, 1, 1); - } - values[i] = o; - } - else - { - var o = properties[i].GetValue(item); - values[i] = o; - } - - } - - dt.Rows.Add(values); - } - - return dt; - } - - - - public static void BulkcopyUpdate(this IDbConnection conn, string targetTableName, List data) - { - var tmpTable = "#tmp_" + targetTableName; - try - { - //go 语法错误 ??? - conn.Execute($@" -if object_id('tempdb..{tmpTable}') is not null -drop table {tmpTable}"); - - conn.Execute($@" -select * -into {tmpTable} -from {targetTableName} where 1=2 ;"); - - conn.BulkcopyInsert(tmpTable, data); - - var properties = typeof(T).GetProperties(); - var updates = properties.Where(x => !x.Name.Equals("ename")) - .Where(x => !x.Name.Equals("id")) - .Where(x => !x.Name.Equals("timeid")) - .Select(x => - { - return $"T.{x.Name}=S.{x.Name}"; - }); - - var updateScript = string.Join(",", updates); - - var merge = $@" -Merge into {targetTableName} AS T -Using {tmpTable} AS S -ON T.ename = S.ename -WHEN MATCHED -THEN UPDATE SET {updateScript} ; -"; - - conn.Execute(merge); - } - catch (Exception ex) - { - log.Error($"批量更新失败:{ex.Message}", ex); - } - finally - { - conn.Execute($@" -drop table {tmpTable} -" -); - } - } - } -} diff --git a/Src/Dlhis.ScheduledTask/Extensions/EnumerableExtensions.cs b/Src/Dlhis.ScheduledTask/Extensions/EnumerableExtensions.cs deleted file mode 100644 index caa57d0c..00000000 --- a/Src/Dlhis.ScheduledTask/Extensions/EnumerableExtensions.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace Dlhis.ScheduledTask.Extensions -{ - public static class EnumerableExtensions - { - public static IEnumerable> Chunk(this IEnumerable arr, int size) - { - for (var i = 0; i < arr.Count() / size + 1; i++) - { - yield return arr.Skip(i * size).Take(size); - } - } - } -} diff --git a/Src/Dlhis.ScheduledTask/Job/JinkoHrJob.cs b/Src/Dlhis.ScheduledTask/Job/JinkoHrJob.cs index c4d1407c..4cbcdb01 100644 --- a/Src/Dlhis.ScheduledTask/Job/JinkoHrJob.cs +++ b/Src/Dlhis.ScheduledTask/Job/JinkoHrJob.cs @@ -3,85 +3,30 @@ using Dlhis.ScheduledTask.Dto; using Dlhis.ScheduledTask.Entities; using Dlhis.ScheduledTask.Response; using Hangfire.Logging; -using Microsoft.AspNetCore.SignalR; -using Microsoft.Extensions.DependencyModel; using Newtonsoft.Json; using Serilog; -using System.Data.SqlClient; -using System.Data; -using System.Net; using System.Net.Http.Headers; using System.Text; -using static Dapper.SqlMapper; using Dlhis.ScheduledTask.WsProxy; -using log4net; +using Microsoft.Extensions.Options; namespace Dlhis.ScheduledTask.Job { public class JinkoHrJob { - private IDisposable webApp; - private static readonly log4net.ILog log = LogManager.GetLogger(typeof(JinkoHrJob)); - const string ORG_CACHE_FILE = "org.json"; - const string POSITION_CACHE_FILE = "position.json"; - const string PERSON_CACHE_FILE = "person.json"; - private List lastUpdatedPersonList = new List(); private List lastUpdatedOrgList = new List(); private List lastUpdatedPositionList = new List(); - private readonly ZHRF_SYN_01 orgClient; - private readonly ZHRF_SYN_02 positionClient; - private readonly ZHRF_SYN_03N archivesClient; - private readonly ZHRF_SWIPE_DATA_SAVE_CLIENT dataSaveClient; - private readonly SI_DLDM2EHR_InsertRecord_OutServiceClient insertRecord_OutService; - private readonly SI_DLDM2EHR_DeleteRecord_OutServiceClient deleteRecord_OutService; public ServiceSettings Settings { get; } private readonly ZHRS_WERKS[] werks; - volatile bool started = false; - public JinkoHrJob(ServiceSettings settings) + private readonly ILogger _logger; + private readonly ServiceSettings _serviceSettings; + + public JinkoHrJob(ILogger logger, IOptions options) { - Settings = settings; - var list = new List(); - if (!string.IsNullOrWhiteSpace(Settings.Werks)) - { - var ss = Settings.Werks.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); - foreach (var w in ss) - { - list.Add(new ZHRS_WERKS() { WERKS = w }); - } - werks = list.ToArray(); - } - else - { - werks = Array.Empty(); - } - orgClient = new ZHRF_SYN_01() - { - Url = Settings.ServerUrl + "/sap/bc/srt/rfc/sap/zhrf_syn_01/800/zhrf_syn_01/zhrf_syn_01", - Credentials = new NetworkCredential(Settings.UserName, Settings.Password), - Timeout = 10000000, - }; - positionClient = new ZHRF_SYaN_02() - { - Url = Settings.ServerUrl + "/sap/bc/srt/rfc/sap/zhrf_syn_02/800/zhrf_syn_02/zhrf_syn_02", - Credentials = new NetworkCredential(Settings.UserName, Settings.Password), - Timeout = 10000000 - }; - - archivesClient = new ZHRF_SYN_03N() - { - Url = Settings.ServerUrl + "/sap/bc/srt/rfc/sap/zhrf_syn_03n/800/zhrf_syn_03n/zhrf_syn_03n", - Credentials = new NetworkCredential(Settings.UserName, Settings.Password), - Timeout = 10000000 - }; - - dataSaveClient = new ZHRF_SWIPE_DATA_SAVE_CLIENT() - { - Credentials = new NetworkCredential(Settings.UserName, Settings.Password), - Url = settings.AttendanceUploadUrl, - Timeout = 10000000 - }; + _logger = logger; + _serviceSettings = options.Value; } private PersonResult GetPersons() @@ -124,7 +69,7 @@ namespace Dlhis.ScheduledTask.Job if (personResp?.T_PERSON?.item?.Any() == true) { persons.AddRange(personResp.T_PERSON.item); - log.Info($"人事范围[{item.WERKS}]获取{personResp.T_PERSON.item.Count}条数据"); + //log.Info($"人事范围[{item.WERKS}]获取{personResp.T_PERSON.item.Count}条数据"); } } else @@ -142,7 +87,7 @@ namespace Dlhis.ScheduledTask.Job } catch (Exception ex) { - log.Error($"人事范围[{item.WERKS}]获取失败:{ex.Message}", ex); + // log.Error($"人事范围[{item.WERKS}]获取失败:{ex.Message}", ex); } } @@ -196,7 +141,7 @@ namespace Dlhis.ScheduledTask.Job if (originUpdates.Count + originInserts.Count <= count) { var currentTotal = originUpdates.Count + originInserts.Count; - log.Debug($"正在进行第{index}批次,需要处理{currentTotal}条数据"); + //log.Debug($"正在进行第{index}批次,需要处理{currentTotal}条数据"); var updates = ToGainfoSap(originUpdates); var inserts = ToGainfoSap(originInserts); @@ -209,12 +154,12 @@ namespace Dlhis.ScheduledTask.Job conn.Execute(SqlGeneration.InsertSql, item); // 模拟触发器业务逻辑 SimulateTriggerBusinessLogic(item); - log.Debug($"执行新增{i}/{currentTotal} name:{item.gname},ename:{item.ename}"); + // log.Debug($"执行新增{i}/{currentTotal} name:{item.gname},ename:{item.ename}"); } catch (Exception ex) { failure++; - log.Error($"执行新增{i}/{currentTotal} name:{item.gname},ename:{item.ename}失败 \r\n{ex.Message}"); + //log.Error($"执行新增{i}/{currentTotal} name:{item.gname},ename:{item.ename}失败 \r\n{ex.Message}"); } } @@ -230,17 +175,17 @@ namespace Dlhis.ScheduledTask.Job conn.Execute(SqlGeneration.UpdateSql, item); // 手动执行触发器的业务逻辑 UpdateExecuteTriggerBusinessLogic(originUpdates); - log.Debug($"执行更新{i}/{currentTotal} name:{item.gname},ename:{item.ename}"); + //log.Debug($"执行更新{i}/{currentTotal} name:{item.gname},ename:{item.ename}"); } catch (Exception ex) { failure++; - log.Error($"执行更新{i}/{currentTotal} name:{item.gname},ename:{item.ename}失败 \r\n{ex.Message}"); + //log.Error($"执行更新{i}/{currentTotal} name:{item.gname},ename:{item.ename}失败 \r\n{ex.Message}"); } } - + } @@ -278,8 +223,8 @@ namespace Dlhis.ScheduledTask.Job // 根据业务需求更新相应的字段 // 这里仅为演示,需要替换为实际逻辑 db.Updateable().UpdateColumns(it => new GainfoEntity - { - + { + Gname = item.gname, Ename = item.ename, Web = item.web, @@ -320,11 +265,11 @@ namespace Dlhis.ScheduledTask.Job Phone = item.phone, Log_id = item.log_id, Log_shift = item.log_shift, - Log_date =item.log_date, + Log_date = item.log_date, Log_ip = item.log_ip, Isowner = item.isowner, Remark = item.remark, - + }).ExecuteCommand(); } // 获取 accnt 的方法 @@ -361,6 +306,7 @@ namespace Dlhis.ScheduledTask.Job } private void ExecuteUpdateLogic(GainfoSap updatedItem) { + //todo 下发任务 // 获取 accnt string accnt = GetAccnt(); @@ -454,23 +400,6 @@ namespace Dlhis.ScheduledTask.Job log.Error("同步人员信息失败", ex); } } - - private IDbConnection OpenDlDbConnection() - { - try - { - IDbConnection conn = new SqlConnection(Settings.DlhisDatabase); - if (conn.State != ConnectionState.Open) - { - conn.Open(); - } - return conn; - } - catch (Exception ex) - { - throw new Exception($"打开迪联数据库错误:{ex.Message}", ex); - } - } } } diff --git a/Src/Dlhis.ScheduledTask/appsettings.json b/Src/Dlhis.ScheduledTask/appsettings.json index 19f31111..9c2dcc06 100644 --- a/Src/Dlhis.ScheduledTask/appsettings.json +++ b/Src/Dlhis.ScheduledTask/appsettings.json @@ -8,6 +8,9 @@ "ConnectionStrings": { "HangfireConnection": "server=192.168.199.124;port=3306;userid=root;password=dlhis123;database=Hangfire;charset=utf8mb4;Allow User Variables=true;" }, + "ServiceSettings": { + "ServerUrl": "" + }, "AllowedHosts": "*", "Serilog": { "MinimumLevel": {