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.

382 lines
17 KiB
C#

using DS.Module.Core;
using DS.Module.Core.Enums;
using DS.Module.Core.Extensions;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.Fee.Dtos;
using DS.WMS.Core.Fee.Entity;
using DS.WMS.Core.Flow.Dtos;
using DS.WMS.Core.Info.Entity;
using DS.WMS.Core.Invoice.Dtos;
using DS.WMS.Core.Op.Dtos;
using DS.WMS.Core.Op.Dtos.TaskInteraction;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Op.Entity.TaskInteraction;
using DS.WMS.Core.Op.Interface;
using DS.WMS.Core.Op.Interface.TaskInteraction;
using DS.WMS.Core.Op.Method.TaskInteraction;
using DS.WMS.Core.TaskPlat.Dtos;
using DS.WMS.Core.TaskPlat.Entity;
using LanguageExt;
using LanguageExt.Common;
using LanguageExt.Pipes;
using Mapster;
using Masuit.Tools.Systems;
using Microsoft.Extensions.DependencyInjection;
using NPOI.SS.Formula.Functions;
using Org.BouncyCastle.Ocsp;
using SqlSugar;
using System.Collections.Generic;
using System.Drawing;
using static AnyDiff.DifferenceLines;
namespace DS.WMS.Core.Op.Method
{
/// <summary>
/// 海运出口退舱接口
/// </summary>
public class SeaExportRefundService: ISeaExportRefundService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ISaasDbService saasService;
readonly ITaskService taskService;
private readonly ISeaExportCommonService seaComService;
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public SeaExportRefundService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
saasService = _serviceProvider.GetRequiredService<ISaasDbService>();
seaComService = _serviceProvider.GetRequiredService<ISeaExportCommonService>();
taskService = serviceProvider.GetRequiredService<ITaskService>();
}
#region 发起改配
/// <summary>
/// 获取改配订单状态
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<DataResult<string>> GetChangeOrderStatus(long id)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var info = await tenantDb.Queryable<SeaExport>().Where(x => x.Id == id).FirstAsync();
if (info.IsNull())
return await Task.FromResult(DataResult<string>.Failed("不存在的海运出口信息!", MultiLanguageConst.SeaExportExist));
var task = await tenantDb.Queryable<BusinessTask>().Where(x => x.BusinessId == id && x.TaskType == TaskBaseTypeEnum.WAIT_ORDER_AUDIT).FirstAsync();
if (task.TaskStatus == TaskStatusEnum.Complete && string.IsNullOrEmpty(info.MBLNO))
{
return await Task.FromResult(DataResult<string>.Success("获取成功", "已审未出号"));
}
else if (!string.IsNullOrEmpty(info.MBLNO) && (info.IsBooking == false && info.IsVGM == false))
{
return await Task.FromResult(DataResult<string>.Success("获取成功", "已出号未申报"));
}
else if (info.IsBooking == true || info.IsVGM == true)
{
return await Task.FromResult(DataResult<string>.Success("获取成功", "已申报"));
}
else {
return await Task.FromResult(DataResult<string>.Success("获取成功", "未知状态"));
}
}
/// <summary>
///
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public async Task<DataResult> CreateChangeTaskAsync(ChangeTaskReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var info = await tenantDb.Queryable<SeaExport>().Where(x => x.Id == req.Id).FirstAsync();
if (info.IsNull())
return await Task.FromResult(DataResult.Failed("不存在的海运出口信息!", MultiLanguageConst.SeaExportExist));
var task = await tenantDb.Queryable<BusinessTask>().Where(x => x.BusinessId == req.Id && x.TaskType == TaskBaseTypeEnum.WAIT_ORDER_AUDIT).FirstAsync();
if (task.TaskStatus == TaskStatusEnum.Create)
{
return await Task.FromResult(DataResult.Failed("此单未审,可直接编辑调整!"));
}
if (info.ETD.IsNotNull() && info.ETD < DateTime.Now)
{
return await Task.FromResult(DataResult.Failed("此单已开船,无法改配!"));
}
try
{
await tenantDb.Ado.BeginTranAsync();
#region 复制单据
var data = info.Adapt<SeaExport>();
data.Id = 0;
data.ParentId = 0;
//data.CustomerNo = data.CustomerNo +"-GP";
data.BusinessStatus = "YSTC";
data.BusinessStatusName = "退舱已审";
var entity = await tenantDb.Insertable(data).ExecuteReturnEntityAsync();
var newKey = entity.Id;
var edi = await tenantDb.Queryable<SeaExportEdi>().FirstAsync(x => x.BusinessId == req.Id);
if (edi.IsNotNull())
{
var ediEntity = edi.Adapt<SeaExportEdi>();
ediEntity.Id = 0;
ediEntity.BusinessId = newKey;
await tenantDb.Insertable(ediEntity).ExecuteCommandAsync();
}
var ctnList = await tenantDb.Queryable<OpCtn>().Where(x => x.BSNO == req.Id.ToString()).ToListAsync();
if (ctnList.IsNotNull() && ctnList.Count>0)
{
var list = new List<OpCtn>();
foreach (var item in ctnList)
{
var ctn = item.Adapt<OpCtn>();
ctn.Id = 0;
ctn.BSNO = newKey.ToString();
list.Add(ctn);
}
if (list.Count > 0)
await tenantDb.Insertable(list).ExecuteCommandAsync();
}
var priceList = await tenantDb.Queryable<BusinessCtnPrice>().Where(x => x.BusinessId == req.Id).ToListAsync();
if (priceList.IsNotNull() && priceList.Count > 0)
{
var list = new List<BusinessCtnPrice>();
foreach (var item in priceList)
{
var price = item.Adapt<BusinessCtnPrice>();
price.Id = 0;
price.BusinessId = newKey;
list.Add(price);
}
if (list.Count>0)
await tenantDb.Insertable(list).ExecuteCommandAsync();
}
var contactList = await tenantDb.Queryable<BusinessOrderContact>().Where(x => x.BusinessId == req.Id).ToListAsync();
if (contactList.IsNotNull() && contactList.Count > 0)
{
var list = new List<BusinessOrderContact>();
foreach (var item in contactList)
{
var contact = item.Adapt<BusinessOrderContact>();
contact.Id = 0;
contact.BusinessId = newKey;
list.Add(contact);
}
if (list.Count > 0)
await tenantDb.Insertable(list).ExecuteCommandAsync();
}
//任务交互表
var taskList = await tenantDb.Queryable<BusinessTask>().Where(x => x.BusinessId == req.Id).ToListAsync();
if (taskList.IsNotNull() && taskList.Count > 0)
{
foreach (var item in taskList)
{
item.BusinessId = newKey;
item.TaskStatus = TaskStatusEnum.Pending;
}
if (taskList.Count > 0)
await tenantDb.Updateable(taskList).ExecuteCommandAsync();
}
//任务台主表
var taskbaseList = await tenantDb.Queryable<TaskBaseInfo>().Where(x => x.OUT_BS_NO == req.Id).ToListAsync();
if (taskbaseList.IsNotNull() && taskbaseList.Count > 0)
{
foreach (var item in taskbaseList)
{
item.OUT_BS_NO = newKey;
}
if (taskbaseList.Count > 0)
await tenantDb.Updateable(taskbaseList).ExecuteCommandAsync();
}
//任务台任务分配表
var taskAllocationList = await tenantDb.Queryable<TaskBaseAllocation>().Where(x => x.BusinessId == req.Id).ToListAsync();
if (taskAllocationList.IsNotNull() && taskAllocationList.Count > 0)
{
foreach (var item in taskAllocationList)
{
item.BusinessId = newKey;
}
if (taskAllocationList.Count > 0)
await tenantDb.Updateable(taskAllocationList).ExecuteCommandAsync();
}
#endregion
//更新原单信息
info.CustomerNo = info.CustomerNo + "-GP";
info.IsBooking = false;
info.IsCustoms = false;
info.IsLand = false;
info.IsVGM = false;
await tenantDb.Updateable(info).UpdateColumns(x => new { x.CustomerNo,x.IsBooking,x.IsCustoms,x.IsLand,x.IsVGM }).ExecuteCommandAsync();
var oldOrder = entity.Adapt<SeaExport>();
entity.IsChangeETD = true;
entity.ChangeReason = req.ChangeReason;
entity.ChangeRemark = req.ChangeRemark;
entity.ChangeOrderId = req.Id;
int rows = await tenantDb.Updateable(entity).UpdateColumns(x => new
{
x.IsChangeETD,
x.ChangeReason,
x.ChangeRemark,
x.ChangeOrderId
}).ExecuteCommandAsync();
await seaComService.SaveSeaExportLogAsync(new SeaExportSaveLog()
{
OperateType = "Update",
OldOrder = oldOrder,
NewOrder = entity,
SourceCode = "CreateChangeTaskAsync",
SourceName = "发起改配",
}, tenantDb);
#region 判断是否退舱 发起退舱
if (req.IsRefund)
{
var taskReq = new TaskCreationRequest()
{
BusinessId = newKey,
BusinessType = BusinessType.OceanShippingExport,
TaskTypeName = TaskBaseTypeEnum.RETURN_CABIN.ToString(),
TaskTitle = $"【{TaskBaseTypeEnum.RETURN_CABIN.GetDescription()}】{info?.CustomerNo}",
TaskDescription = $"【{TaskBaseTypeEnum.RETURN_CABIN.GetDescription()}】{info?.CustomerNo}",
};
var result = await taskService.CreateTaskAsync(taskReq, false);
if (!result.Succeeded)
return await Task.FromResult(DataResult.Failed(result.Message));
//await seaComService.SetGoodsStatus("YFTC", req.Id, tenantDb);
}
#endregion
await tenantDb.Ado.CommitTranAsync();
return await Task.FromResult(DataResult.Successed("改配成功!"));
}
catch (Exception ex)
{
await tenantDb.Ado.RollbackTranAsync();
await ex.LogAsync(db);
return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed));
}
}
#endregion
#region 发起退舱
public async Task<DataResult> CreateRefundAuditTaskAsync(RefundTaskReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var info = await tenantDb.Queryable<SeaExport>().Where(x => x.Id == req.Id).FirstAsync();
if (info.IsNull())
return await Task.FromResult(DataResult.Failed("不存在的海运出口信息!", MultiLanguageConst.SeaExportExist));
if (!req.IsReserveFee && tenantDb.Queryable<FeeRecord>().Where(x => x.BusinessId == req.Id).Any())
{
return await Task.FromResult(DataResult.Failed("存在业务相关费用,是否保留费用?"));
}
if (req.IsReserveFee && tenantDb.Queryable<FeeRecord>().Where(x => x.BusinessId == req.Id && x.FeeStatus == FeeStatus.Entering).Any())
{
return await Task.FromResult(DataResult.Failed("存在录入状态的费用!"));
}
var taskReq = new TaskCreationRequest()
{
BusinessId = req.Id,
BusinessType = BusinessType.OceanShippingExport,
TaskTypeName = TaskBaseTypeEnum.RETURN_CABIN_AUDIT.ToString(),
TaskTitle = $"【{TaskBaseTypeEnum.RETURN_CABIN_AUDIT.GetDescription()}】{info?.CustomerNo}",
TaskDescription = $"【{TaskBaseTypeEnum.RETURN_CABIN_AUDIT.GetDescription()}】{info?.CustomerNo}",
};
var result = await taskService.CreateTaskAsync(taskReq, false);
if (!result.Succeeded)
{
return await Task.FromResult(DataResult.Failed(result.Message));
}
else {
var oldOrder = info.Adapt<SeaExport>();
info.RefundTag = RefundTagEnum.Normal;
info.RefundReason = req.RefundReason;
info.RefundRemark = req.RefundRemark;
int rows = await tenantDb.Updateable(info).UpdateColumns(x => new
{
x.RefundTag,x.RefundReason,x.RefundRemark
}).ExecuteCommandAsync();
await seaComService.SetGoodsStatus("YFTC", req.Id, tenantDb);
await seaComService.SaveSeaExportLogAsync(new SeaExportSaveLog()
{
OperateType = "Update",
OldOrder = oldOrder,
NewOrder = info,
SourceCode = "CreateRefundAuditTaskAsync",
SourceName = "发起退仓审核",
}, tenantDb);
return await Task.FromResult(DataResult.Successed(result.Message));
}
}
#endregion
/// <summary>
/// 退舱审核完成回调
/// </summary>
/// <param name="callback">回调信息</param>
/// <returns></returns>
public async Task<DataResult> RefundAuditCallbackAsync(FlowCallback callback)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (callback.AuditType != TaskBaseTypeEnum.RETURN_CABIN_AUDIT)
return DataResult.FailedWithDesc(nameof(MultiLanguageConst.NoAuditItems));
var info = await tenantDb.Queryable<SeaExport>().Where(x => x.Id == callback.BusinessId).FirstAsync();
if (info.IsNull())
return await Task.FromResult(DataResult.Failed("不存在的海运出口信息!", MultiLanguageConst.SeaExportExist));
if (callback.FlowStatus == FlowStatusEnum.Approve)
{
await seaComService.SetGoodsStatus("YSTC", callback.BusinessId, tenantDb);
//发起退舱确认任务
var taskReq = new TaskCreationRequest()
{
BusinessId = info.Id,
BusinessType = BusinessType.OceanShippingExport,
TaskTypeName = TaskBaseTypeEnum.RETURN_CABIN.ToString(),
TaskTitle = $"【{TaskBaseTypeEnum.RETURN_CABIN.GetDescription()}】{info?.CustomerNo}",
TaskDescription = $"【{TaskBaseTypeEnum.RETURN_CABIN.GetDescription()}】{info?.CustomerNo}",
};
var result = await taskService.CreateTaskAsync(taskReq, false);
if (!result.Succeeded)
{
return await Task.FromResult(DataResult.Failed(result.Message));
}
}
else
{
await seaComService.SetGoodsStatus("TCBH", callback.BusinessId, tenantDb);
}
return DataResult.Success;
//return rows > 0 ? DataResult.Success : DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed));
}
}
}