ComplianceServer/code/Hg.Core.Domain/SysUserProtocolDomain.cs

51 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hg.Core.Domain
{
internal class SysUserProtocolDomain : ISysUserProtocolDomain
{
private readonly IBaseRepository<ZxdDbContext> _zxdRepositrory;
public SysUserProtocolDomain(IBaseRepository<ZxdDbContext> zxdRepositrory)
{
_zxdRepositrory = zxdRepositrory;
}
public async Task<bool> CheckResAge(string? resid)
{
return await _zxdRepositrory.GetRepository<SysUserProtocol>().Query()
.AnyAsync(x => x.Resid == resid && x.Protocoltype == 6 && x.Status == 9);
}
public async Task SyncUserProtocolName()
{
var now = DateTime.Now.AddDays(-7);
var data = await _zxdRepositrory.GetRepository<SysUserProtocol>().Query()
.Where(x => x.Protocoltype == 6 && x.Ctime > now).ToListAsync();
if (!data.Any()) return;
var resids = data.Select(x => x.Resid?.Trim()).Distinct().Where(x => !string.IsNullOrEmpty(x)).ToList();
var orders = await _zxdRepositrory.GetRepository<WX_SZZYORDER>().Query()
.Where(x => new string[] { "180", "190", "200", "220" }.Contains(x.ORDERSTATUS) && resids.Contains(x.RESID)).Select(x => new { x.RESID, x.CNAME, x.CTIME }).ToListAsync();
if (!orders.Any()) return;
var newData = new List<SysUserProtocol>();
foreach (var item in data)
{
var username = orders.Where(x => x.RESID == item.Resid?.Trim()).OrderByDescending(x => x.CTIME).Select(x => x.CNAME).FirstOrDefault();
if (username != item.Username)
item.Username = username;
newData.Add(item);
}
await _zxdRepositrory.GetRepository<SysUserProtocol>().BatchUpdateAsync(newData, x => new
{
x.Username
});
}
}
}