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
{
///
/// 发送租户和用户数据到运营端往来单位订阅器
///
public class SyncTenantUserToDjyCustomerSubscriber : IEventSubscriber
{
private readonly ILogger _logger;
public SyncTenantUserToDjyCustomerSubscriber(IServiceProvider services, ILogger 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>();
var repoUser = App.GetService>();
var cache = App.GetService();
//租户信息
var tenant = await repoTenant.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == tenantId);
//用户信息
var listSyncToDjy = new List();
var dbUsers = await repoUser.AsQueryable().Filter(null, true).Where(x => x.TenantId == tenant.Id && x.AdminType == AdminType.None && x.IsDeleted == false).ToListAsync();
foreach (var user in dbUsers)
{
if (user.Name.EndsWith("(离职)"))
{
continue;
}
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 {
{ CommonConst.API_USER_HEADER_KEY, userId.Value},
{ CommonConst.API_USER_HEADER_SECRET, userSecret.Value}
})
.SetBody(sendDto)
.PostAsStringAsync();
_logger.LogInformation($"返回数据:{rtn}");
}
}
}