using System; using System.Collections.Generic; using System.Linq; using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; using System.Web; using Hg.Core.Domain.Config; using Hg.Core.Entity; using StackExchange.Redis; namespace Hg.Core.Domain { internal class TraceUserDomain : ITraceUserDomain { private readonly IConfiguration _configuration; private readonly IHttpClient _httpClient; private readonly IBaseRepository _crmRepository; private readonly IBaseRepository _zxdRepository; private readonly ICacheDomain _cacheDomain; private readonly SystemConfig _systemConfig; public TraceUserDomain(IConfiguration configuration, IHttpClient httpClient, IBaseRepository crmRepository, IBaseRepository zxdRepository, ICacheDomain cacheDomain) { _systemConfig = configuration.GetSection("SystemConfig").Get(); _configuration = configuration; _httpClient = httpClient; _crmRepository = crmRepository; _zxdRepository = zxdRepository; _cacheDomain = cacheDomain; } public async Task> GetTraceUsers(TraceUserSearchDto search) { var total = 0; var usernames = new List(); var softUsers = new List(); var traceUsers = new List(); var systemConfig = _configuration.GetSection("SystemConfig").Get(); var accessKey = systemConfig.GetAccessKey("UPWEBSITE"); var resCustomer = _zxdRepository.GetRepository().Query(); //替换resid if (!string.IsNullOrEmpty(search.UMID) && string.IsNullOrEmpty(search.Resid)) { search.Resid = await QueryResidByUMID(search.UMID); } if (!string.IsNullOrEmpty(search.Resid) || !string.IsNullOrEmpty(search.Username)) { softUsers = await _zxdRepository.GetRepository().Query() .If(!string.IsNullOrEmpty(search.Resid), x => x.Where(y => y.RESID == search.Resid)) .If(!string.IsNullOrEmpty(search.Username), x => x.Where(y => y.USERNAME == search.Username)) .ToListAsync(); if (softUsers != null && softUsers.Any()) { usernames = softUsers.Select(x => x.USERNAME).Distinct().ToList(); var query = _crmRepository.GetRepository().Query() .Where(x => x.Status == 0) .Where(x => usernames.Contains(x.Uid)) .Select(x => new TraceUserDto { Bindmob = x.Bindmob, Idcardno = x.Idcardno, Status = x.Status, Uid = x.Uid, Username = x.Username, Idcardback = x.Idcardback, Idcardfront = x.Idcardfront, VerifyType = x.VerifyType }); total = await query.CountAsync(); traceUsers = await query .Skip((search.PageIndex - 1) * search.PageSize) .Take(search.PageSize) .ToListAsync(); var resids = softUsers.Select(m => m.RESID); var ruc = resCustomer.Where(m => resids.Contains(m.RESID)).ToList(); if (traceUsers != null && traceUsers.Any()) { traceUsers.ForEach(x => { var content = SecurityHelper.EncyptData(x.Uid, accessKey); var sign = SecurityHelper.SignData(content, accessKey); var resid= softUsers.First(y => y.USERNAME == x.Uid).RESID; x.Idcardback = $"{systemConfig.UserCenterRiaServiceUrl}{systemConfig.PreviewUrl}{x.Idcardback}?content={HttpUtility.UrlEncode(content)}&sign={HttpUtility.UrlEncode(sign)}&clientId=UPWEBSITE&employeeId={search.EmployeeId}&employeeName={search.EmployeeName}&viewSource=千拓后台管理系统&pageSource=Audit:订单列表.风控"; x.Idcardfront = $"{systemConfig.UserCenterRiaServiceUrl}{systemConfig.PreviewUrl}{x.Idcardfront}?content={HttpUtility.UrlEncode(content)}&sign={HttpUtility.UrlEncode(sign)}&clientId=UPWEBSITE&employeeId={search.EmployeeId}&employeeName={search.EmployeeName}&viewSource=千拓后台管理系统&pageSource=Audit:订单列表.风控"; x.ResId = resid; x.UMID = ruc.FirstOrDefault(m => m.RESID == resid)?.UMID; }); } } } else { var query = _crmRepository.GetRepository().Query() .Where(x => x.Status == 0) .Select(x => new TraceUserDto { Bindmob = x.Bindmob, Idcardno = x.Idcardno, Status = x.Status, Uid = x.Uid, Username = x.Username, Idcardback = x.Idcardback, Idcardfront = x.Idcardfront, AuditRemark = x.AuditRemark, VerifyType = x.VerifyType }); total = await query.CountAsync(); traceUsers = await query .Skip((search.PageIndex - 1) * search.PageSize) .Take(search.PageSize) .ToListAsync(); usernames = traceUsers.Select(x => x.Uid).Distinct().ToList(); softUsers = await _zxdRepository.GetRepository().Query() .Where(x => usernames.Contains(x.USERNAME)) .ToListAsync(); var resids = softUsers.Select(m => m.RESID); var ruc = resCustomer.Where(m => resids.Contains(m.RESID)).ToList(); if (traceUsers != null && traceUsers.Any()) { traceUsers.ForEach(x => { var content = SecurityHelper.EncyptData(x.Uid, accessKey); var sign = SecurityHelper.SignData(content, accessKey); var resid = softUsers.First(y => y.USERNAME == x.Uid).RESID; x.Idcardback = $"{systemConfig.UserCenterRiaServiceUrl}{systemConfig.PreviewUrl}{x.Idcardback}?content={HttpUtility.UrlEncode(content)}&sign={HttpUtility.UrlEncode(sign)}&clientId=UPWEBSITE&employeeId={search.EmployeeId}&employeeName={search.EmployeeName}&viewSource=千拓后台管理系统&pageSource=Audit:订单列表.风控"; x.Idcardfront = $"{systemConfig.UserCenterRiaServiceUrl}{systemConfig.PreviewUrl}{x.Idcardfront}?content={HttpUtility.UrlEncode(content)}&sign={HttpUtility.UrlEncode(sign)}&clientId=UPWEBSITE&employeeId={search.EmployeeId}&employeeName={search.EmployeeName}&viewSource=千拓后台管理系统&pageSource=Audit:订单列表.风控"; x.ResId = resid; x.UMID = ruc.FirstOrDefault(m => m.RESID == resid)?.UMID; }); } } return new PageResult(search.PageIndex, search.PageSize, total, traceUsers); } /// /// 审核 /// /// /// public async Task Audit(TraceUserAuditDto dto) { try { var systemConfig = _configuration.GetSection("SystemConfig").Get(); var accessKey = systemConfig.GetAccessKey("UPWEBSITE"); var traceUser = await _crmRepository.GetRepository().FirstOrDefaultAsync(x => x.Uid == dto.Uid); if (traceUser == null) { throw new ApiException("用户上传信息不存或已删除!"); } traceUser.Status = dto.Status; traceUser.AuditRemark = dto.Remark; var data = new { username = dto.Uid, auditStatus = dto.Status }; var content = SecurityHelper.EncyptData(data.ToJson(), accessKey); var sign = SecurityHelper.SignData(content, accessKey); var url = $"{systemConfig.UserCenterRiaServiceUrl}{systemConfig.AuditUrl}?content={HttpUtility.UrlEncode(content)}&sign={HttpUtility.UrlEncode(sign)}&clientId=UPWEBSITE"; var result = await _httpClient.PostAsync(url); if (result.Code == 0) { await _crmRepository.GetRepository().UpdateAsync(traceUser, x => new { x.Status, x.AuditRemark }); } return true; } catch (Exception ex) { Log.Error(ex, $"{dto.Uid}审核报错!"); return false; } } public async Task GetTraceUser(string? resid, int? eid, string? ename) { var systemConfig = _configuration.GetSection("SystemConfig").Get(); var accessKey = systemConfig.GetAccessKey("UPWEBSITE"); var usernames = await _zxdRepository.GetRepository().Query() .Where(y => y.RESID == resid) .Select(x => x.SOFTUSERNAME).Distinct() .ToListAsync(); if (usernames == null || !usernames.Any()) { throw new ApiException("用户未上传实名信息!"); } var data = await _crmRepository.GetRepository().Query() .Where(x => usernames.Contains(x.Uid)) .OrderByDescending(x => x.Ctime) .Select(x => new TraceUserDto { Bindmob = x.Bindmob, Idcardno = x.Idcardno, Status = x.Status, Uid = x.Uid, Username = x.Username, Idcardback = x.Idcardback, Idcardfront = x.Idcardfront, AuditRemark = x.AuditRemark, VerifyType = x.VerifyType }).FirstOrDefaultAsync(); if (data != null) { var content = SecurityHelper.EncyptData(data.Uid, accessKey); var sign = SecurityHelper.SignData(content, accessKey); data.Idcardback = $"{systemConfig.UserCenterRiaServiceUrl}{systemConfig.PreviewUrl}{data.Idcardback}?content={HttpUtility.UrlEncode(content)}&sign={HttpUtility.UrlEncode(sign)}&clientId=UPWEBSITE&employeeId={eid}&employeeName={HttpUtility.UrlEncode(ename)}&viewSource={HttpUtility.UrlEncode("千拓后台管理系统")}&pageSource={HttpUtility.UrlEncode("Audit:订单列表.风控")}"; data.Idcardfront = $"{systemConfig.UserCenterRiaServiceUrl}{systemConfig.PreviewUrl}{data.Idcardfront}?content={HttpUtility.UrlEncode(content)}&sign={HttpUtility.UrlEncode(sign)}&clientId=UPWEBSITE&employeeId={eid}&employeeName={HttpUtility.UrlEncode(ename)}&viewSource={HttpUtility.UrlEncode("千拓后台管理系统")}&pageSource={HttpUtility.UrlEncode("Audit:订单列表.风控")}"; } else { data = new TraceUserDto(); } data.ResId = resid; return data; } public async Task GetTraceContractUser(int? orderid, int? eid, string? ename) { var systemConfig = _configuration.GetSection("SystemConfig").Get(); var order = await _zxdRepository.GetRepository().Query() .FirstOrDefaultAsync(x => x.ORDERID == orderid); if (order == null) { throw new ApiException("订单不存在或已删除!"); } var accessKey = systemConfig.GetAccessKey("UPWEBSITE"); //var usernames = await _zxdRepository.GetRepository().Query() // .Where(y => y.RESID == order.RESID) // .Select(x => x.SOFTUSERNAME).Distinct() // .ToListAsync(); //if (usernames == null || !usernames.Any()) //{ // throw new ApiException("用户未上传实名信息!"); //} var data = await _crmRepository.GetRepository().Query() .Where(x => order.SOFTUSERNAME == x.Uid) .OrderByDescending(x => x.Ctime) .Select(x => new TraceUserDto { Bindmob = x.Bindmob, Idcardno = x.Idcardno, Status = x.Status, Uid = x.Uid, Username = x.Username, Idcardback = x.Idcardback, Idcardfront = x.Idcardfront, AuditRemark = x.AuditRemark, VerifyType = x.VerifyType, Type = x.VerifyType == 2 ? TraceUserType.三要素 : string.IsNullOrEmpty(x.Bindmob) ? TraceUserType.二要素 : TraceUserType.三要素 }).FirstOrDefaultAsync(); if (data != null) { var content = SecurityHelper.EncyptData(data.Uid, accessKey); var sign = SecurityHelper.SignData(content, accessKey); data.Idcardback = $"{systemConfig.UserCenterRiaServiceUrl}{systemConfig.PreviewUrl}{data.Idcardback}?content={HttpUtility.UrlEncode(content)}&sign={HttpUtility.UrlEncode(sign)}&clientId=UPWEBSITE&employeeId={eid}&employeeName={ename}&viewSource=千拓后台管理系统&pageSource=Audit:订单列表.风控"; data.Idcardfront = $"{systemConfig.UserCenterRiaServiceUrl}{systemConfig.PreviewUrl}{data.Idcardfront}?content={HttpUtility.UrlEncode(content)}&sign={HttpUtility.UrlEncode(sign)}&clientId=UPWEBSITE&employeeId={eid}&employeeName={ename}&viewSource=千拓后台管理系统&pageSource=Audit:订单列表.风控"; } else { data = new TraceUserDto(); data.Type = TraceUserType.二要素; } //data.Username = string.IsNullOrEmpty(data.Username) ? order.CNAME : data.Username; //data.Idcardno = string.IsNullOrEmpty(data.Idcardno) ? order.idcard : data.Idcardno; if (string.IsNullOrEmpty(data.Idcardno)) { var riskinfoUrl = systemConfig.GetRiskinfo(); var bf = "{\"uid\": \"" + order.SOFTUSERNAME + "\",\"htid\":\"DN" + order.SZZYORDERID.Value.ToString() + "\"}"; var hqr = BlowFish.Encode(bf); var para = new { hqr }; var res = await _httpClient.PostAsync(riskinfoUrl, para); if (res.Ret == 0 && res.Businesstype != "smallAmount") { data.Idcardno = res.IdCard; data.Username = res.Name; if (!string.IsNullOrEmpty(data.Idcardno) && !string.IsNullOrEmpty(data.Username)) { data.Status = 1; } } } data.ResId = order.RESID; data.ContractStatus = order.ContractStatus; data.ContractRemark = order.ContractRemark; data.RejectRemark = order.rejectremark; data.RiskCtrlStatus = order.REQUESTSTATUS; return data; } private async Task QueryResidByUMID(string UMID) { var UMIDMain = await _zxdRepository.GetRepository().Query().FirstOrDefaultAsync(m => m.UMID == UMID); if (UMIDMain != null) { return UMIDMain.RESID; } else { return "NULL_RESID";//这里输入一个不可能匹配的值,空值会被视为无传参返回所有数据 } } } }