From b376ec238703cfab4da0b3176ed5861839f64674 Mon Sep 17 00:00:00 2001 From: liguoyang Date: Wed, 3 Jul 2024 16:24:46 +0800 Subject: [PATCH] =?UTF-8?q?feat=20sap=E5=87=BA=E5=B7=AE=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E8=8E=B7=E5=8F=96,=E6=B5=B7=E5=AE=81=E4=BD=99=E9=A2=9D?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0tenantId=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/Implements/BalanceService.cs | 13 +-- .../Visitor/Entity/BusinessTripEntity.cs | 5 +- .../Response/SapBusinessTripInfoResponse.cs | 87 ++++++++++++++++++ .../Common/Response/SapTravelInfoResponse.cs | 88 ------------------- .../Common/Services/IJinkoJIPService.cs | 14 ++- .../Common/Services/IJinkoSapService.cs | 8 -- .../Services/Implements/JinkoJIPService.cs | 43 +++++++-- .../Services/Implements/JinkoSapService.cs | 28 +----- .../Jobs/SyncBusinessTripJob.cs | 63 +++++++++++++ 9 files changed, 212 insertions(+), 137 deletions(-) create mode 100644 Src/Dlhis.ScheduledTask/Common/Response/SapBusinessTripInfoResponse.cs delete mode 100644 Src/Dlhis.ScheduledTask/Common/Response/SapTravelInfoResponse.cs create mode 100644 Src/Dlhis.ScheduledTask/Jobs/SyncBusinessTripJob.cs diff --git a/Src/Dlhis.Canteen/Order/Services/Implements/BalanceService.cs b/Src/Dlhis.Canteen/Order/Services/Implements/BalanceService.cs index cd4f24fd..74382ee9 100644 --- a/Src/Dlhis.Canteen/Order/Services/Implements/BalanceService.cs +++ b/Src/Dlhis.Canteen/Order/Services/Implements/BalanceService.cs @@ -523,20 +523,21 @@ public class BalanceService : IBalanceService await _dbContext.InsertAsync(gainfoMeal); } - var businessPlace = await _dbContext.Queryable() + var businessPlaceInfo = await _dbContext.Queryable() .LeftJoin((a, b) => a.Code == b.PlaceCode) .LeftJoin((a, b, c) => b.ShiftCode == c.Code) .Where((a, b, c) => a.CustomCode == request.BusinessPlaceCode) .Select((a, b, c) => new { a.Code, - ShiftCode = c.Code + ShiftCode = c.Code, + a.TenantId }) .SingleAsync(); if (request.Type == BalanceRecordTypeEnumType.Out) { - businessPlace.ThrowIfNull($"营业点{request.BusinessPlaceCode}不存在"); + businessPlaceInfo.ThrowIfNull($"营业点{request.BusinessPlaceCode}不存在"); } await _dbContext.InsertAsync(new BalanceRecordEntity @@ -544,8 +545,8 @@ public class BalanceService : IBalanceService Amount = request.Amount, ApplicationType = request.ApplicationType, Type = request.Type, - BusinessPlaceCode = businessPlace?.Code, - BusinessShiftCode = businessPlace?.ShiftCode, + BusinessPlaceCode = businessPlaceInfo?.Code, + BusinessShiftCode = businessPlaceInfo?.ShiftCode, ShiftType = request.ShiftType, CostDate = request.CostDate, CostDateTime = request.CostDateTime, @@ -557,7 +558,7 @@ public class BalanceService : IBalanceService Remark = request.Remark, CostId = gainfo.CostId, SubsidiesFromMonth = request.CostDate.ToString("yyyyMM"), - TenantId = request.Factory, + TenantId = businessPlaceInfo?.TenantId ?? request.Factory, OriginTenantId = gainfo.Factory, Werks = gainfo.Werks, Name = gainfo.Name, diff --git a/Src/Dlhis.Entity/Visitor/Entity/BusinessTripEntity.cs b/Src/Dlhis.Entity/Visitor/Entity/BusinessTripEntity.cs index 00e93578..00d59918 100644 --- a/Src/Dlhis.Entity/Visitor/Entity/BusinessTripEntity.cs +++ b/Src/Dlhis.Entity/Visitor/Entity/BusinessTripEntity.cs @@ -9,6 +9,7 @@ public class BusinessTripEntity : SugarBaseEntity /// /// 工号 /// + [SugarColumn(IsNullable = false, UniqueGroupNameList = new[] { "unique_BusinessTrip_code_begin_end" })] public string Code { get; set; } /// @@ -19,11 +20,13 @@ public class BusinessTripEntity : SugarBaseEntity /// /// 开始日期 /// + [SugarColumn(IsNullable = false, UniqueGroupNameList = new[] { "unique_BusinessTrip_code_begin_end" })] public string BeginDate { get; set; } /// /// 结束日期 /// + [SugarColumn(IsNullable = false, UniqueGroupNameList = new[] { "unique_BusinessTrip_code_begin_end" })] public string EndDate { get; set; } /// @@ -55,7 +58,7 @@ public class BusinessTripEntity : SugarBaseEntity /// 出差天数 /// public double Day { get; set; } - + /// /// 出差目的地 /// diff --git a/Src/Dlhis.ScheduledTask/Common/Response/SapBusinessTripInfoResponse.cs b/Src/Dlhis.ScheduledTask/Common/Response/SapBusinessTripInfoResponse.cs new file mode 100644 index 00000000..4d1495d2 --- /dev/null +++ b/Src/Dlhis.ScheduledTask/Common/Response/SapBusinessTripInfoResponse.cs @@ -0,0 +1,87 @@ +using Newtonsoft.Json; + +namespace Dlhis.ScheduledTask.Common.Response; + +public class SapBusinessTripInfoResponse +{ + /// + /// 明细数据 + /// + [JsonProperty("OT_2002")] + public List Items { get; set; } +} + +public class SapBusinessTripInfoItem +{ + /// + /// 工号 + /// + [JsonProperty("OV_GONGH")] + public string Code { get; set; } + + /// + /// 姓名 + /// + [JsonProperty("OV_ENAME")] + public string Name { get; set; } + + /// + /// 开始日期 + /// + [JsonProperty("OV_BEGDA")] + public string BeginDate { get; set; } + + /// + /// 结束日期 + /// + [JsonProperty("OV_ENDDA")] + public string EndDate { get; set; } + + /// + /// 开始时间 + /// + [JsonProperty("OV_BEGUZ")] + public DateTime BeginDateTime { get; set; } + + /// + /// 结束时间 + /// + [JsonProperty("OV_ENDUZ")] + public DateTime EndDateTime { get; set; } + + /// + /// 出差类型 + /// + [JsonProperty("OV_AWART")] + public string Type { get; set; } + + /// + /// 出差类型描述 + /// + [JsonProperty("OV_ATEXT")] + public string TypeDesc { get; set; } + + /// + /// 出差时数 + /// + [JsonProperty("OV_STDAZ")] + public int Hour { get; set; } + + /// + /// 出差天数 + /// + [JsonProperty("OV_ABWTG")] + public double Day { get; set; } + + /// + /// 出差目的地 + /// + [JsonProperty("OV_CCMDD")] + public string Destination { get; set; } + + /// + /// 出差目的地描述 + /// + [JsonProperty("OV_CCMDDM")] + public string DestinationDesc { get; set; } +} \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/Common/Response/SapTravelInfoResponse.cs b/Src/Dlhis.ScheduledTask/Common/Response/SapTravelInfoResponse.cs deleted file mode 100644 index c706f9bd..00000000 --- a/Src/Dlhis.ScheduledTask/Common/Response/SapTravelInfoResponse.cs +++ /dev/null @@ -1,88 +0,0 @@ -using Newtonsoft.Json; - -namespace Dlhis.ScheduledTask.Common.Response -{ - public class SapTravelInfoResponse - { - /// - /// 明细数据 - /// - [JsonProperty("OT_2002")] - public List Items { get; set; } - } - - public class SapTravelInfoItem - { - /// - /// 工号 - /// - [JsonProperty("OV_GONGH")] - public string Code { get; set; } - - /// - /// 姓名 - /// - [JsonProperty("OV_ENAME")] - public string Name { get; set; } - - /// - /// 开始日期 - /// - [JsonProperty("OV_BEGDA")] - public string BeginDate { get; set; } - - /// - /// 结束日期 - /// - [JsonProperty("OV_ENDDA")] - public string EndDate { get; set; } - - /// - /// 开始时间 - /// - [JsonProperty("OV_BEGUZ")] - public DateTime BeginDateTime { get; set; } - - /// - /// 结束时间 - /// - [JsonProperty("OV_ENDUZ")] - public DateTime EndDateTime { get; set; } - - /// - /// 出差类型 - /// - [JsonProperty("OV_AWART")] - public string Type { get; set; } - - /// - /// 出差类型描述 - /// - [JsonProperty("OV_ATEXT")] - public string TypeDesc { get; set; } - - /// - /// 出差时数 - /// - [JsonProperty("OV_STDAZ")] - public int Hour { get; set; } - - /// - /// 出差天数 - /// - [JsonProperty("OV_ABWTG")] - public double Day { get; set; } - - /// - /// 出差目的地 - /// - [JsonProperty("OV_CCMDD")] - public string Destination { get; set; } - - /// - /// 出差目的地描述 - /// - [JsonProperty("OV_CCMDDM")] - public string DestinationDesc { get; set; } - } -} \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/Common/Services/IJinkoJIPService.cs b/Src/Dlhis.ScheduledTask/Common/Services/IJinkoJIPService.cs index b80442e0..038e5ec0 100644 --- a/Src/Dlhis.ScheduledTask/Common/Services/IJinkoJIPService.cs +++ b/Src/Dlhis.ScheduledTask/Common/Services/IJinkoJIPService.cs @@ -9,5 +9,15 @@ public interface IJinkoJIPService : ISingletonDependency Task SyncMealSupplementRecordsAsync(List requests); - public string IssueToken(); -} + + /// + /// 获取出差信息 + /// + /// + /// + /// + /// + Task GetTravelInfosAsync(DateTime begin, DateTime end, string departmentNo); + + string IssueToken(); +} \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/Common/Services/IJinkoSapService.cs b/Src/Dlhis.ScheduledTask/Common/Services/IJinkoSapService.cs index 9ed75d5e..badd62b3 100644 --- a/Src/Dlhis.ScheduledTask/Common/Services/IJinkoSapService.cs +++ b/Src/Dlhis.ScheduledTask/Common/Services/IJinkoSapService.cs @@ -11,12 +11,4 @@ public interface IJinkoSapService : IScopeDependency Task GetPersonsByWerkAsync(string werk); Task GetPersonsByCodeAsync(string code); - - /// - /// 获取出差信息 - /// - /// - /// - /// - Task GetTravelInfosAsync(DateTime begin, DateTime end); } \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoJIPService.cs b/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoJIPService.cs index c0f7e21b..b6722bc9 100644 --- a/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoJIPService.cs +++ b/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoJIPService.cs @@ -4,7 +4,6 @@ using System.Security.Claims; using System.Text; using Dlhis.ScheduledTask.Common.Request; using Dlhis.ScheduledTask.Common.Response; -using Dlhis.ScheduledTask.Common.Services; using Kurisu.AspNetCore.Utils.Extensions; using Microsoft.IdentityModel.Tokens; using Newtonsoft.Json.Linq; @@ -85,6 +84,7 @@ public class JinkoJIPService : IJinkoJIPService { true.ThrowIfTrue(exp.Value()); } + if (jobj.TryGetValue("message", out var message) && !jobj.ContainsKey("code")) { true.ThrowIfTrue(message.Value()); @@ -95,6 +95,39 @@ public class JinkoJIPService : IJinkoJIPService return responseJson; } + + public async Task GetTravelInfosAsync(DateTime begin, DateTime end, string departmentNo) + { + const string apiUrl = "/JIP/HCM/HCM_TRAVELINFO"; + + var beginDay = begin.ToString("yyyyMMdd"); + var endDay = end.ToString("yyyyMMdd"); + + var obj = new + { + IV_BEGDA = beginDay, + IV_ENDDA = endDay, + IT_2002GH = new List + { + new + { + //IV_GONGH = "" 工号, + IV_ORGID = departmentNo, + } + } + }; + + var body = obj.ToJson(); + + var content = new StringContent(body, Encoding.UTF8, "application/json"); + var response = await GetHttpClient().PostAsync(apiUrl, content); + response.EnsureSuccessStatusCode(); + + var jsonResponse = await response.Content.ReadAsStringAsync(); + return jsonResponse.ToObject(); + } + + public string IssueToken() { var jwtKey = _configuration.GetSection("JinkoOptions:Jip:Key").Value; @@ -106,9 +139,9 @@ public class JinkoJIPService : IJinkoJIPService var claims = new List { - new (JwtRegisteredClaimNames.Nbf,$"{new DateTimeOffset(now).ToUnixTimeSeconds()}"), - new (JwtRegisteredClaimNames.Exp,$"{new DateTimeOffset(expires).ToUnixTimeSeconds()}"), - new (JwtRegisteredClaimNames.Iss,jwtKey), + new(JwtRegisteredClaimNames.Nbf, $"{new DateTimeOffset(now).ToUnixTimeSeconds()}"), + new(JwtRegisteredClaimNames.Exp, $"{new DateTimeOffset(expires).ToUnixTimeSeconds()}"), + new(JwtRegisteredClaimNames.Iss, jwtKey), }; var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSecret)); @@ -126,4 +159,4 @@ public class JinkoJIPService : IJinkoJIPService var securityToken = jwtHandler.CreateToken(descriptor); return jwtHandler.WriteToken(securityToken); } -} +} \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoSapService.cs b/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoSapService.cs index ab9959fa..a808172b 100644 --- a/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoSapService.cs +++ b/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoSapService.cs @@ -5,7 +5,6 @@ using Dlhis.Entity.Basic.Entity; using Dlhis.ScheduledTask.Common.Dto; using Dlhis.ScheduledTask.Common.Response; using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; -using Kurisu.AspNetCore.Utils.Extensions; using Newtonsoft.Json; namespace Dlhis.ScheduledTask.Common.Services.Implements; @@ -153,7 +152,7 @@ public class JinkoSapService : IJinkoSapService persons.AddRange(personResp.Person.Items); } - return new SapPersonResponse() + return new SapPersonResponse { Person = new SapPerson { @@ -161,29 +160,4 @@ public class JinkoSapService : IJinkoSapService } }; } - - - public async Task GetTravelInfosAsync(DateTime begin, DateTime end) - { - const string apiUrl = "/JIP/HCM/HCM_TRAVELINFO"; - - var beginDay = begin.ToString("yyyyMMdd"); - var endDay = end.ToString("yyyyMMdd"); - - var obj = new - { - IV_BEGDA = beginDay, - IV_ENDDA = endDay - }; - - var body = obj.ToJson(); - - var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"); - var response = await GetHttpClient().PostAsync(apiUrl, content); - response.EnsureSuccessStatusCode(); - - var jsonResponse = await response.Content.ReadAsStringAsync(); - - return JsonConvert.DeserializeObject(jsonResponse); - } } \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/Jobs/SyncBusinessTripJob.cs b/Src/Dlhis.ScheduledTask/Jobs/SyncBusinessTripJob.cs new file mode 100644 index 00000000..8930a986 --- /dev/null +++ b/Src/Dlhis.ScheduledTask/Jobs/SyncBusinessTripJob.cs @@ -0,0 +1,63 @@ +using Dlhis.Entity.Basic.Entity; +using Dlhis.Entity.Visitor.Entity; +using Dlhis.ScheduledTask.Attributes; +using Dlhis.ScheduledTask.Common.Services; +using Mapster; + +namespace Dlhis.ScheduledTask.Jobs; + +[RecurringJob(nameof(SyncBusinessTripJob), "0 0 0/1 * * ? ", Name = "SAP同步出差信息")] +public class SyncBusinessTripJob : BaseJob +{ + /// + public SyncBusinessTripJob(IServiceProvider serviceProvider) : base(serviceProvider) + { + } + + /// + protected override async Task RunAsync(IServiceProvider serviceProvider) + { + var jipService = serviceProvider.GetService(); + var today = DateTime.Today; + + var departments = await DbContext.Queryable().ToListAsync(); + var tops = departments.Where(x => x.PNo == "00001000").ToList(); + var validDepartments = tops.ToList(); + tops.ForEach(x => RecursionGetNext(x, departments, validDepartments)); + + foreach (var department in validDepartments) + { + try + { + var tripResponse = await jipService.GetTravelInfosAsync(today, today, department.No); + foreach (var tripInfoItem in tripResponse.Items) + { + if (await DbContext.Queryable().Where(x => x.Code == tripInfoItem.Code && x.BeginDate == tripInfoItem.BeginDate && x.EndDate == tripInfoItem.EndDate) + .AnyAsync()) + { + continue; + } + + var entity = tripInfoItem.Adapt(); + await DbContext.InsertAsync(entity); + } + } + catch (Exception e) + { + Logger.LogError(e, "获取部门{departmentNo}{departmentName}出差数据执行失败:{message}", department.No, department.Name, e.Message); + } + } + } + + //************************************************************************************************************************************************ + + private static void RecursionGetNext(DepartmentEntity current, List data, List result) + { + var next = data.Where(x => x.PNo == current.No).ToList(); + foreach (var item in next) + { + result.Add(item); + RecursionGetNext(item, data, result); + } + } +} \ No newline at end of file