fix 生日券发放细节日志

This commit is contained in:
2026-01-06 10:35:27 +08:00
parent 0bcb80f101
commit d7a34c6e10
7 changed files with 247 additions and 93 deletions

View File

@@ -11,6 +11,7 @@ using Dlhis.Entity.Basic.Enums;
using Dlhis.Entity.Canteen.Entities;
using Dlhis.Entity.Canteen.Entities.Voucher;
using Dlhis.Entity.Canteen.Enums;
using Dlhis.Entity.Common.Entities;
using Kurisu.AspNetCore.CustomClass;
using Kurisu.AspNetCore.DataAccess.Dto;
using Kurisu.AspNetCore.DataAccess.Entity;
@@ -45,12 +46,13 @@ public class BirthdayVoucherService : BaseVoucherService, IVoucherService
//忽略tenant操作
await DbContext.IgnoreAsync<ITenantId>(async () =>
{
var gainfos = await GetGainfosAsync(userCodes, p.begin, p.end);
var (gainfos, events) = await GetGainfosAsync(userCodes, p.begin, p.end);
var departmentNos = gainfos.Select(g => g.DepartmentNo).Distinct().ToList();
var departmentInfos = await DbContext.Queryable<DepartmentEntity>()
.Where(x => departmentNos.Contains(x.No))
.ToListAsync();
foreach (var gainfo in gainfos.Where(x => x.Birthday.HasValue))
{
await DbContext.Client.Ado.BeginTranAsync();
@@ -64,11 +66,17 @@ public class BirthdayVoucherService : BaseVoucherService, IVoucherService
await DbContext.Client.Ado.CommitTranAsync();
}
catch (Exception)
catch (Exception ex)
{
await DbContext.Client.Ado.RollbackTranAsync();
events.Add(new UserBusinessEventEntity { UserCode = gainfo.Code, EventType = "GenerateBirthdayVoucher", Content = $"出现异常:{ex.Message}" });
}
}
if (events.Any())
{
await DbContext.InsertAsync(events);
}
});
}
@@ -86,11 +94,16 @@ public class BirthdayVoucherService : BaseVoucherService, IVoucherService
.Where(x => x.Type == VoucherEnumType.Birthday)
.AnyAsync())
{
// 使用安全日期来处理 2/29 情况:非闰年会退到当年该月最大天数(即 2/28)
var claimDate = SafeDateForYear(year, gainfo.Birthday.Value);
await DbContext.InsertAsync(new VoucherEntity
{
No = no,
Type = VoucherEnumType.Birthday,
ClaimDateTime = new DateTime(year, gainfo.Birthday.Value.Month, gainfo.Birthday.Value.Day),
ClaimDateTime = claimDate,
OriginalBalance = amount,
Balance = amount,
@@ -144,8 +157,17 @@ public class BirthdayVoucherService : BaseVoucherService, IVoucherService
}
}
/// <summary>
/// 返回在指定年份内与原生日相匹配的安全日期。
/// - 对 2/29 在非闰年情况下,会退到当年该月最大天数(即 2/28)。
/// </summary>
private static DateTime SafeDateForYear(int year, DateTime birthday)
{
var day = Math.Min(birthday.Day, DateTime.DaysInMonth(year, birthday.Month));
return new DateTime(year, birthday.Month, day);
}
private (DateTime begin, DateTime end) GetDateRange(Dictionary<string, object> pairs)
private static (DateTime begin, DateTime end) GetDateRange(Dictionary<string, object> pairs)
{
var begin = DateTime.Today;
var end = begin;
@@ -172,7 +194,7 @@ public class BirthdayVoucherService : BaseVoucherService, IVoucherService
return (begin, end);
}
private async Task<List<GainfoEntity>> GetGainfosAsync(List<string> userCodes, DateTime begin, DateTime end)
private async Task<(List<GainfoEntity>, List<UserBusinessEventEntity>)> GetGainfosAsync(List<string> userCodes, DateTime begin, DateTime end)
{
var factries = await _factorySettingApi.GetEnableBirthdayVoucherFactoriesAsync();
@@ -195,17 +217,42 @@ public class BirthdayVoucherService : BaseVoucherService, IVoucherService
"49990004"
};
return await DbContext.Queryable<GainfoEntity>()
var gainfos = await DbContext.Queryable<GainfoEntity>()
.WhereIF(userCodes.IsPresent(), x => userCodes.Contains(x.Code))
.Where(x => !SqlFunc.IsNullOrEmpty(x.DepartmentNo))
.Where(x => !excludePositionNos.Contains(x.PositionNo))//排除特殊岗位
//.Where(x => !SqlFunc.IsNullOrEmpty(x.DepartmentNo))
//.Where(x => !excludePositionNos.Contains(x.PositionNo))//排除特殊岗位
.Where(x => x.Type == GainfoUserTypeEnumType.Normal || x.Type == GainfoUserTypeEnumType.SecurityWhite)
.Where(x => x.Birthday.HasValue && !string.IsNullOrEmpty(x.CostId))
.Where(x => x.Birthday.HasValue)
//.Where(x => !string.IsNullOrEmpty(x.CostId))
.WhereIF(isToday, x => SqlFunc.DateValue(x.Birthday.Value, DateType.Month) == begin.Month && SqlFunc.DateValue(x.Birthday.Value, DateType.Day) == begin.Day)
.WhereIF(!isToday && !overYear, x => SqlFunc.DateValue(x.Birthday.Value, DateType.Month) >= beginMonth && SqlFunc.DateValue(x.Birthday.Value, DateType.Month) <= endMonth)
.WhereIF(!isToday && overYear, x => SqlFunc.DateValue(x.Birthday.Value, DateType.Month) >= beginMonth || SqlFunc.DateValue(x.Birthday.Value, DateType.Month) <= endMonth)
.Where(x => factries.Contains(x.Factory))
.ToListAsync();
//UserBusinessEventEntity
var events = new List<UserBusinessEventEntity>();
foreach (var gainfo in gainfos)
{
if (gainfo.DepartmentNo.IsEmpty())
{
events.Add(new UserBusinessEventEntity { UserCode = gainfo.Code, EventType = "GenerateBirthdayVoucher", Content = "部门编号为空" });
}
else if (excludePositionNos.Contains(gainfo.PositionNo))
{
events.Add(new UserBusinessEventEntity { UserCode = gainfo.Code, EventType = "GenerateBirthdayVoucher", Content = $"排除岗位编码{gainfo.PositionNo}" });
}
else if (gainfo.CostId.IsEmpty())
{
events.Add(new UserBusinessEventEntity { UserCode = gainfo.Code, EventType = "GenerateBirthdayVoucher", Content = "成本中心为空" });
}
}
return (gainfos
.Where(x => x.DepartmentNo.IsPresent())
.Where(x => !excludePositionNos.Contains(x.PositionNo))
.Where(x => x.CostId.IsPresent())
.ToList(), events);
}
private static string GenerateUniqueCode(string data)