You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookingHeChuan/Myshipping.Core/EventSubscriber/SyncTenantUserToDjyCustomer...

112 lines
4.2 KiB
C#

using Furion;
using Furion.DataEncryption;
using Furion.EventBus;
using Furion.FriendlyException;
using Furion.RemoteRequest.Extensions;
using Microsoft.Extensions.Logging;
using Myshipping.Core.Entity;
using Myshipping.Core.Service;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;
namespace Myshipping.Core
{
/// <summary>
/// 发送租户和用户数据到运营端往来单位订阅器
/// </summary>
public class SyncTenantUserToDjyCustomerSubscriber : IEventSubscriber
{
private readonly ILogger<CompanyUserSyncSubscriber> _logger;
public SyncTenantUserToDjyCustomerSubscriber(IServiceProvider services, ILogger<CompanyUserSyncSubscriber> logger)
{
Services = services;
_logger = logger;
}
public IServiceProvider Services { get; }
//发送租户和用户数据到运营端往来单位
[EventSubscribe("DjySync:TenantUserToCustomer")]
public async Task CompanyUserSyncCompanyUser(EventHandlerExecutingContext context)
{
_logger.LogInformation($"收到发送租户和用户数据到运营端往来单位订阅消息:{context.Source.Payload}");
var tenantId = Convert.ToInt64(context.Source.Payload);
var repoTenant = App.GetService<SqlSugarRepository<SysTenant>>();
var repoUser = App.GetService<SqlSugarRepository<SysUser>>();
var cache = App.GetService<ISysCacheService>();
//租户信息
var tenant = await repoTenant.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == tenantId);
//用户信息
var listSyncToDjy = new List<DjyCustomerFromCustomerUserDto>();
var dbUsers = await repoUser.AsQueryable().Filter(null, true).Where(x => x.TenantId == tenant.Id && x.IsDeleted == false).ToListAsync();
foreach (var user in dbUsers)
{
listSyncToDjy.Add(new DjyCustomerFromCustomerUserDto()
{
Id = user.Id,
Name = user.Name,
DjyGid = user.DjyUserId,
Email = user.Email,
Phone = user.Phone,
});
}
var recUrl = cache.GetAllSysConfig().Result.FirstOrDefault(x => x.Code == "DjyBookingRequestReceiveUrl");
if (recUrl == null || string.IsNullOrEmpty(recUrl.Value))
{
throw Oops.Bah("大简云运营端URL地址未配置请联系管理员");
}
var userId = cache.GetAllSysConfig().Result.FirstOrDefault(x => x.Code == "DjyBookingReceiveUserId");
var userSecret = cache.GetAllSysConfig().Result.FirstOrDefault(x => x.Code == "DjyBookingReceiveUserSecret");
if (userId == null || string.IsNullOrEmpty(userId.Value) || userSecret == null || string.IsNullOrEmpty(userSecret.Value))
{
throw Oops.Bah("大简云运营端用户key和秘钥未配置请联系管理员");
}
var syncUrl = recUrl.Value;
if (!syncUrl.EndsWith("/"))
{
syncUrl += "/";
}
syncUrl += "DjyCustomer/RecCustomerUserSync";
var sendDto = new DjyCustomerFromCustomerDto()
{
Id = tenant.Id,
Name = tenant.Name,
Email = tenant.Email,
Phone = tenant.Phone,
DjyGid = tenant.CompId,
UserList = listSyncToDjy
};
_logger.LogInformation($"发送租户和用户数据给运营端({syncUrl}{userId.Value}{userSecret.Value}{JsonConvert.SerializeObject(sendDto)}");
var rtn = await syncUrl
.SetHeaders(new Dictionary<string, object> {
{ CommonConst.API_USER_HEADER_KEY, userId.Value},
{ CommonConst.API_USER_HEADER_SECRET, userSecret.Value}
})
.SetBody(sendDto)
.PostAsStringAsync();
_logger.LogInformation($"返回数据:{rtn}");
}
}
}