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.

391 lines
18 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using DS.Module.Core;
using DS.Module.Core.Data;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.Op.Dtos;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Op.Interface;
using DS.WMS.Core.Sys.Entity;
using DS.WMS.Core.Sys.Interface;
using DS.WMS.Core.TaskInteraction.Dtos;
using DS.WMS.Core.TaskInteraction.Entity;
using DS.WMS.Core.TaskInteraction.Interface;
using DS.WMS.Core.TaskInteraction.Method;
using DS.WMS.Core.TaskPlat.Dtos;
using DS.WMS.Core.TaskPlat.Entity;
using DS.WMS.Core.TaskPlat.Interface;
using LanguageExt.Pipes;
using Mapster;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SqlSugar;
namespace DS.WMS.Core.TaskPlat.Method
{
public class TaskPOLContainerNotPickUpService : TaskManageBaseService<TaskPOLContainerNotPickUpService>, ITaskPOLContainerNotPickUpService
{
private readonly ISeaExportService _seaExportService;
private readonly IConfigService _configService;
private readonly IUserService _userService;
private readonly ITaskLogService _logService;
private readonly ITaskMailService _taskMailService;
private readonly ITaskAllocationService _taskAllocationService;
public TaskPOLContainerNotPickUpService(IUser user, ILogger<TaskPOLContainerNotPickUpService> logger,
ISaasDbService saasDbService,
IServiceProvider serviceProvider,
IWebHostEnvironment environment) : base(user, logger, saasDbService, serviceProvider, environment)
{
//_serviceProvider = serviceProvider;
//db = serviceProvider.GetRequiredService<ISqlSugarClient>();
user = serviceProvider.GetRequiredService<IUser>();
//saasService = serviceProvider.GetRequiredService<ISaasDbService>();
_seaExportService = serviceProvider.GetRequiredService<ISeaExportService>();
_configService = serviceProvider.GetRequiredService<IConfigService>();
_userService = serviceProvider.GetRequiredService<IUserService>();
_taskMailService = serviceProvider.GetRequiredService<ITaskMailService>();
_taskAllocationService = serviceProvider.GetRequiredService<ITaskAllocationService>();
_logService = serviceProvider.GetRequiredService<ITaskLogService>();
}
#region 通过任务主键获取起运港未提箱详情
/// <summary>
/// 通过任务主键获取起运港未提箱详情
/// </summary>
/// <param name="taskPKId">起运港未提箱任务主键</param>
/// <returns>返回回执</returns>
public async Task<DataResult<List<TaskPOLContainerNotPickUpShowDto>>> GetInfoByTaskId(long taskPKId)
{
//await SetTaskStatus(11, TaskBaseTypeEnum.CUT_MODIFY, TaskStatusEnum.Create, DateTime.Now, null);
List<TaskPOLContainerNotPickUpShowDto> list = new List<TaskPOLContainerNotPickUpShowDto>();
var tenantDb = saasDbService.GetBizDbScopeById(user.TenantId);
//任务不考虑OrgId,这里去掉
tenantDb.QueryFilter.Clear<IOrgId>();
var queryList = await tenantDb.Queryable<TaskBaseInfo>()
.InnerJoin<TaskPolContainerNotPickup>((a, b) => a.Id == b.TASK_ID)
.Where((a, b) => a.Id == taskPKId)
.Select((a, b) => new { Base = a, Pol = b })
.ToListAsync();
//任务主键{taskPkId}无法获取业务信息
if (queryList.Count == 0)
throw new Exception(string.Format(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.TaskBaseInfoFromTaskIdNull)), taskPKId));
if (queryList.Count > 0)
{
list = queryList.Select(a => a.Pol.Adapt<TaskPOLContainerNotPickUpShowDto>()).ToList();
}
return DataResult<List<TaskPOLContainerNotPickUpShowDto>>.Success(list);
}
#endregion
#region 检索对应的订舱订单并保存订舱ID
/// <summary>
/// 检索对应的订舱订单并保存订舱ID
/// </summary>
/// <param name="taskPKId">起运港未提箱任务主键</param>
/// <returns>返回回执</returns>
public async Task<DataResult<SeaExportOrderExtension>> SearchAndMarkBookingOrder(long taskPKId)
{
var tenantDb = saasDbService.GetBizDbScopeById(user.TenantId);
var entity = tenantDb.Queryable<TaskPolContainerNotPickup>().Filter(null, true).First(a => a.TASK_ID == taskPKId);
return await _seaExportService.SearchOrderInfo(entity.MBL_NO);
}
#endregion
#region 发送邮件通知给客户
/// <summary>
/// 发送邮件通知给客户
/// </summary>
/// <param name="taskPKId">起运港未提箱主键</param>
/// <param name="businessTaskMailId">邮件模板主键</param>
/// <returns>返回回执</returns>
public async Task<DataResult<TaskTransferMsgDto>> InnerSendEmailToCustomer(long taskPKId, long businessTaskMailId)
{
/*
1、先匹配订单。这里可能关联的是拆票订单如果是拆票订单需要所有拆票记录都要发邮件通知
2、有订单调取任务规则补全相关人(如果当票已经有了人员表记录 task_base_allocation则不再处理人员)
3、调取发送模板。
4、填充模板数据。
5、发送邮件。
6、成功后置任务为完成状态。
*/
var tenantDb = saasDbService.GetBizDbScopeById(user.TenantId);
var entity = tenantDb.Queryable<TaskPolContainerNotPickup>().Filter(null, true).First(a => a.TASK_ID == taskPKId);
var baseInfo = tenantDb.Queryable<TaskBaseInfo>().Filter(null, true).First(a => a.Id == taskPKId);
var queryRlt = await _seaExportService.SearchOrderInfo(entity.MBL_NO);
if (!queryRlt.Succeeded)
{
logger.LogInformation($"匹配订单信息失败 mblno={entity.MBL_NO},原因:{queryRlt.Message}");
return DataResult<TaskTransferMsgDto>.Failed($"匹配订单信息失败 mblno={entity.MBL_NO},原因:{queryRlt.Message}");
}
var taskInfo = entity.Adapt<TaskPOLContainerNotPickUpShowDto>();
BusinessTaskMail? mailConfig = _taskMailService.GetAsync(businessTaskMailId).GetAwaiter().GetResult().Data;
if (mailConfig == null)
{
await _logService.WriteLogAsync(new TaskUpdateRequest
{
BusinessId = taskPKId,
BusinessType = BusinessType.OceanShippingExport,
AutoCreateNext = true,
TaskTypeName = TaskBaseTypeEnum.POL_CTN_NOT_PICKUP.ToString(),
}, $"未能根据任务配置值获取邮件模板设置");
return DataResult<TaskTransferMsgDto>.Failed("未能根据任务配置值获取邮件模板设置");
}
var orderInfo = queryRlt.Data;
logger.LogInformation($"获取订单详情成功 bookid={orderInfo.currOrder.Id}");
DateTime nowDate = DateTime.Now;
List<long> orderIdList = new List<long> { orderInfo.currOrder.Id };
if (orderInfo.otherOrderList != null && orderInfo.otherOrderList.Count > 0)
orderIdList.AddRange(orderInfo.otherOrderList.Select(t => t.Id).ToList());
//如果是拆票需要处理,处理所以分票记录
if (orderInfo.currOrder.SplitOrMergeFlag == 1)
{
bool isHasAlloc = false;
foreach (var id in orderIdList)
{
var taskAllocList = tenantDb.Queryable<TaskBaseAllocation>().Where(a => a.TaskId == taskPKId).ToList();
var searchAllotUserRlt = _taskAllocationService.GetAllotUserBySeaExportId(TaskBaseTypeEnum.POL_CTN_NOT_PICKUP, id, new TaskFlowDataContext()).GetAwaiter().GetResult();
if (searchAllotUserRlt.Succeeded && searchAllotUserRlt.Data?.Count > 0)
{
isHasAlloc = true;
var addUserList = searchAllotUserRlt.Data.GroupJoin(taskAllocList, l => l.RecvUserId, r => r.UserId, (l, r) =>
{
if (r.ToList().Count == 0)
return new { add = true, obl = l};
return new { add = false, obl = l };
}).Where(a=>a.add).Select(a=>a.obl).ToList();
if (addUserList.Count > 0)
{
var userIdList = addUserList.Select(x => x.RecvUserId).Distinct().ToList();
var userWithOrgMap = await db.Queryable<SysUser>().Where(x => userIdList.Contains(x.Id)).Select(x => new { x.Id, x.DefaultOrgId }).ToListAsync();
//写入
var allocList = new List<TaskBaseAllocation>();
addUserList.ForEach(b =>
{
allocList.Add(new TaskBaseAllocation
{
Status = baseInfo.STATUS,
StatusName = baseInfo.STATUS_NAME,
TaskId = taskPKId,
StatusTime = nowDate,
UserId = b.RecvUserId,
UserName = b.RecvUserName,
OrgId = userWithOrgMap.FirstOrDefault(m => m.Id == b.RecvUserId)?.DefaultOrgId ?? 0
});
});
await tenantDb.Insertable<TaskBaseAllocation>(allocList).ExecuteCommandAsync();
}
}
}
if (isHasAlloc && baseInfo.IS_PUBLIC == 1)
{
await tenantDb.Updateable<TaskBaseInfo>(baseInfo).UpdateColumns(x => new
{
x.IS_PUBLIC
}).ExecuteCommandAsync();
}
}
else
{
var taskAllocList = tenantDb.Queryable<TaskBaseAllocation>().Where(a => a.TaskId == taskPKId).ToList();
if (taskAllocList.Count == 0)
{
var searchAllotUserRlt = _taskAllocationService.GetAllotUserBySeaExportId(TaskBaseTypeEnum.POL_CTN_NOT_PICKUP, orderInfo.currOrder.Id,new TaskFlowDataContext()).GetAwaiter().GetResult();
if (searchAllotUserRlt.Succeeded && searchAllotUserRlt.Data?.Count > 0)
{
var userIdList = searchAllotUserRlt.Data.Select(x => x.RecvUserId).Distinct().ToList();
var userWithOrgMap = await db.Queryable<SysUser>().Where(x => userIdList.Contains(x.Id)).Select(x => new { x.Id, x.DefaultOrgId }).ToListAsync();
//写入
var allocList = new List<TaskBaseAllocation>();
searchAllotUserRlt.Data.ForEach(b =>
{
allocList.Add(new TaskBaseAllocation
{
Status = baseInfo.STATUS,
StatusName = baseInfo.STATUS_NAME,
TaskId = taskPKId,
StatusTime = nowDate,
UserId = b.RecvUserId,
UserName = b.RecvUserName,
OrgId = userWithOrgMap.FirstOrDefault(m => m.Id == b.RecvUserId)?.DefaultOrgId ?? 0
});
});
await tenantDb.Insertable<TaskBaseAllocation>(allocList).ExecuteCommandAsync();
if (baseInfo.IS_PUBLIC == 1)
{
baseInfo.IS_PUBLIC = 0;
await tenantDb.Updateable<TaskBaseInfo>(baseInfo).UpdateColumns(x => new
{
x.IS_PUBLIC
}).ExecuteCommandAsync();
}
}
}
}
TaskTransferMsgDto resultDto = new TaskTransferMsgDto {
ExcuteDate = nowDate,
Detail = new List<TaskTransferMsgDataDto>()
};
Dictionary<long, Tuple<bool, string, string>> dict = new Dictionary<long, Tuple<bool, string, string>>();
foreach (var id in orderIdList)
{
TaskTransferMsgDataDto detail = new TaskTransferMsgDataDto {
BusinessId = id,
};
var model = new MailTemplateModel<TaskPOLContainerNotPickUpShowDto>(taskInfo)
{
BusinessId = id,
BusinessType = BusinessType.OceanShippingExport,
};
MailGenerator mailService = new MailGenerator(serviceProvider);
var result = await mailService.SendAsync(mailConfig, model);
if (!result.Succeeded)
{
await _logService.WriteLogAsync(new TaskUpdateRequest
{
BusinessId = taskPKId,
BusinessType = BusinessType.OceanShippingExport,
AutoCreateNext = true,
TaskTypeName = TaskBaseTypeEnum.POL_CTN_NOT_PICKUP.ToString(),
}, result.Message);
//return DataResult.Failed(result.Message);
}
detail.Status = result.Succeeded ? "SUCC" : "FAILURE";
detail.Message = result.Message;
detail.MBlNo = entity.MBL_NO;
resultDto.Detail.Add(detail);
dict.Add(id, new Tuple<bool, string,string>(result.Succeeded, result.Message, entity.MBL_NO));
}
//如果存在失败记录,不能置完成状态
if (!dict.Any(t => !t.Value.Item1))
{
entity.IS_TRANSFER_USER = true;
entity.LST_TRANSFER_USER_DATE = DateTime.Now;
entity.LST_STATUS = "SUCC";
entity.LST_STATUS_NAME = "发送成功";
await tenantDb.Updateable<TaskPolContainerNotPickup>(entity).UpdateColumns(x => new
{
x.IS_TRANSFER_USER,
x.LST_TRANSFER_USER_DATE,
x.LST_STATUS,
x.LST_STATUS_NAME
}).ExecuteCommandAsync();
//发送完邮件,自动标记任务状态为完成
await SetTaskStatus(taskPKId, TaskStatusEnum.Complete, DateTime.Now, null);
}
return DataResult<TaskTransferMsgDto>.Success(resultDto);
}
#endregion
#region 发送邮件通知给客户(任务自动机调取)
/// <summary>
/// 发送邮件通知给客户(任务自动机调取)
/// </summary>
/// <param name="dataContext">数据上下文</param>
/// <returns>返回回执</returns>
public async Task<DataResult<TaskTransferMsgDto>> SendEmailToCustomerTask(TaskFlowDataContext dataContext)
{
var taskPKId = dataContext.Get<long>(TaskFlowDataNameConst.TaskPKId);
if (taskPKId == 0)
throw new ArgumentException($"缺少参数:{nameof(TaskFlowDataNameConst.TaskPKId)}");
var businessTaskMailId = dataContext.Get<long>($"{nameof(BusinessTaskMail)}.{nameof(BusinessTaskMail.Id)}");
if (businessTaskMailId == 0)
throw new ArgumentException($"缺少参数:{nameof(BusinessTaskMail)}.{nameof(BusinessTaskMail.Id)}");
if (businessTaskMailId == 0)
{
await _logService.WriteLogAsync(new TaskUpdateRequest {
BusinessId = taskPKId,
BusinessType = BusinessType.OceanShippingExport,
AutoCreateNext = true,
TaskTypeName = TaskBaseTypeEnum.POL_CTN_NOT_PICKUP.ToString(),
}, $"缺少参数:{nameof(BusinessTaskMail)}.{nameof(BusinessTaskMail.Id)}");
return DataResult<TaskTransferMsgDto>.Failed($"缺少参数:{nameof(BusinessTaskMail)}.{nameof(BusinessTaskMail.Id)}");
}
return await InnerSendEmailToCustomer(taskPKId, businessTaskMailId);
}
#endregion
#region 手工发送邮件通知给客户
/// <summary>
/// 手工发送邮件通知给客户
/// </summary>
/// <param name="taskPKId">起运港未提箱任务主键</param>
/// <returns>返回回执</returns>
public async Task<DataResult<TaskTransferMsgDto>> ManualSendEmailToCustomer(long taskPKId)
{
var paramConfig = _configService.GetConfig("POLContainerNotPickUpEmailTemplateID", long.Parse(user.TenantId), false).GetAwaiter().GetResult()?.Data?.Value;
long businessTaskMailId = 0;
if (!string.IsNullOrWhiteSpace(paramConfig))
{
businessTaskMailId = long.Parse(paramConfig);
}
else
{
return DataResult<TaskTransferMsgDto>.Failed($"缺少系统参数参数:起运港未提箱邮件模板ID-POLContainerNotPickUpEmailTemplateID");
}
return await InnerSendEmailToCustomer(taskPKId, businessTaskMailId);
}
#endregion
}
}