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.

296 lines
12 KiB
C#

using DS.Module.Core;
using DS.Module.Core.Constants;
using DS.Module.Core.Extensions;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.Map.Dtos;
using DS.WMS.Core.Map.Interface;
using DS.WMS.Core.Op.Dtos;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Op.Interface;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using NLog;
using SqlSugar;
namespace DS.WMS.Core.Op.Method
{
public class BookingContractNoManageService : IBookingContractNoManageService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ISaasDbService saasService;
private readonly IMappingCarrierService _mappingCarrierService;
private static readonly NLog.Logger Logger = LogManager.GetCurrentClassLogger();
//船公司基础映射模块
const string CONST_MAPPING_CARRIER_MODULE = "CarrierBaseMapping";
public BookingContractNoManageService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
saasService = _serviceProvider.GetRequiredService<ISaasDbService>();
_mappingCarrierService = _serviceProvider.GetRequiredService<IMappingCarrierService>();
}
#region 保存
/// <summary>
/// 保存
/// </summary>
/// <param name="model">合约号详情</param>
/// <returns>返回回执</returns>
public async Task<DataResult<long>> Save(BookingContractNoManageDto model)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
BookingContractNoManage info = null;
try
{
List<MappingCarrierRes> mapCarrierList = new List<MappingCarrierRes>();
var cacheRlt = await _mappingCarrierService.GetAllList();
if (cacheRlt.Succeeded)
{
mapCarrierList = cacheRlt.Data;
if (model.CarrierId.HasValue && model.CarrierId.Value > 0)
{
var mapInfo = mapCarrierList.FirstOrDefault(a => a.Module == CONST_MAPPING_CARRIER_MODULE && a.LinkId == model.CarrierId.Value);
if (mapInfo != null)
{
model.CarrierCode = mapInfo.MapCode;
}
}
}
4 months ago
//校验约号不能重复
if (model.Id > 0)
{
info = await tenantDb.Queryable<BookingContractNoManage>().FirstAsync(t => t.Id == model.Id);
//合约数据不存在或已作废
if (info == null)
throw new Exception(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.BookingContractRecordDeletedOrNoExists)));
info = model.Adapt<BookingContractNoManage>();
info.UpdateTime = DateTime.Now;
info.UpdateBy = long.Parse(user.UserId);
info.UpdateUserName = user.UserName;
await tenantDb.UpdateNav(info)
.Include(it => it.LaneList)
.ExecuteCommandAsync();
}
else
{
info = model.Adapt<BookingContractNoManage>();
await tenantDb.InsertNav(info)
.Include(t => t.LaneList)
.ExecuteCommandAsync();
}
}
catch (Exception ex)
{
return DataResult<long>.FailedData(info.Id, ex.Message);
}
return DataResult<long>.Success(info.Id);
}
#endregion
#region 主键获取合约号详情
/// <summary>
/// 主键获取合约号详情
/// </summary>
/// <param name="Id">合约号主键</param>
/// <returns>返回合约号详情</returns>
public async Task<DataResult<BookingContractNoManageDto>> GetInfo(long Id)
{
BookingContractNoManageDto model = null;
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
try
{
var info = await tenantDb.Queryable<BookingContractNoManage>()
.Includes(t => t.LaneList)
.FirstAsync(t => t.Id == Id);
//合约数据不存在或已作废
if (info == null)
throw new Exception(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.BookingContractRecordDeletedOrNoExists)));
model = info.Adapt<BookingContractNoManageDto>();
}
catch (Exception ex)
{
return DataResult<BookingContractNoManageDto>.FailedData(model, ex.Message);
}
return DataResult<BookingContractNoManageDto>.Success(model);
}
#endregion
#region 检索合约列表
/// <summary>
/// 检索合约列表
/// </summary>
/// <param name="queryItem">检索值可(模糊查询)</param>
/// <param name="top">返回记录最大行数</param>
/// <param name="carrier">船公司代码</param>
/// <param name="lane">航线代码</param>
/// <param name="lanecname">航线中文</param>
/// <param name="pod">目的港代码</param>
/// <returns>返回合约号详情列表</returns>
public async Task<DataResult<List<BookingContractNoManageDto>>> QuerytContractNoInfo(string queryItem, int top = 10, string carrier = "", string lane = "", string lanecname = "", string pod = "")
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var query = tenantDb.Queryable<BookingContractNoManage>();
query.Includes(t => t.LaneList);
if (!string.IsNullOrWhiteSpace(queryItem))
{
query = query.Where(t =>
t.ContractNo.Contains(queryItem.Trim(), StringComparison.OrdinalIgnoreCase) ||
t.ContractName.Contains(queryItem.Trim(), StringComparison.OrdinalIgnoreCase) ||
t.ContractNote.Contains(queryItem.Trim(), StringComparison.OrdinalIgnoreCase) ||
t.ContractPartyCode.Contains(queryItem.Trim(), StringComparison.OrdinalIgnoreCase) ||
t.ContractPartyName.Contains(queryItem.Trim(), StringComparison.OrdinalIgnoreCase) ||
t.ContractLinkName.Contains(queryItem.Trim(), StringComparison.OrdinalIgnoreCase) ||
t.ContractLinkEmail.Contains(queryItem.Trim(), StringComparison.OrdinalIgnoreCase)
);
}
if (!string.IsNullOrWhiteSpace(carrier))
{
query = query.Where(t => t.CarrierCode.Equals(carrier));
}
if (!string.IsNullOrWhiteSpace(lane))
{
query.Where(t => t.LaneList!.Any(z => string.IsNullOrWhiteSpace(z.LaneCode) || (z.LaneCode != null && z.LaneCode.Contains(lane))));
}
if (!string.IsNullOrWhiteSpace(lanecname))
{
query.Where(t => t.LaneList!.Any(z => string.IsNullOrWhiteSpace(z.LaneCName) || (z.LaneCName != null && z.LaneCName.Contains(lanecname))));
}
if (!string.IsNullOrWhiteSpace(pod))
{
query = query.Where(t => string.IsNullOrWhiteSpace(t.PodCode) || t.PodCode.Contains(pod));
}
var data = await query.OrderBy(a => a.ContractNo).Take(top).ToListAsync();
var result = data.Adapt<List<BookingContractNoManageDto>>();
if (result.Count == 0)
return DataResult<List<BookingContractNoManageDto>>.FailedData(result);
return DataResult<List<BookingContractNoManageDto>>.Success(result);
}
#endregion
#region 作废(可批量删除)
/// <summary>
/// 作废(可批量删除)
/// </summary>
/// <param name="Ids">合约号主键组</param>
/// <returns>返回回执</returns>
public async Task<DataResult<string>> Delete(long[] Ids)
{
try
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
//没有提供需要作废的主键信息
if (Ids.Length == 0)
throw new Exception(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.BookingContractDeleteIdsNull)));
var list = tenantDb.Queryable<BookingContractNoManage>().Where(t => Ids.Contains(t.Id)).ToList();
if (list.Count != Ids.Length)
{
var noRecord = string.Join(",", Ids.GroupJoin(list, l => l, r => r.Id, (l, r) =>
{
var currList = r.ToList();
if (r.Count() > 0)
return string.Empty;
return l.ToString();
}).Where(t => !string.IsNullOrWhiteSpace(t)).ToArray());
throw new Exception(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.BookingContractRecordDeletedOrNoExists)));
}
list.ForEach(async entity =>
{
entity.Deleted = true;
entity.DeleteTime = DateTime.Now;
entity.DeleteBy = long.Parse(user.UserId);
//entity.UpdatedUserName = UserManager.Name;
await tenantDb.Updateable<BookingContractNoManage>().RemoveDataCache($"{SqlSugarCacheConst.ContractNo}{user.TenantId}").IgnoreColumns(it => new
{
it.CreateTime,
it.CreateBy,
//it.CreatedUserName
}).ExecuteCommandAsync();
});
}
catch (Exception ex)
{
//throw Oops.Bah($"作废约号参数异常,{ex.Message}");
return DataResult<string>.FailedData(string.Empty, ex.Message);
}
return DataResult<string>.Success(string.Empty);
}
#endregion
#region 合约号管理台账
/// <summary>
/// 合约号管理台账
/// </summary>
/// <param name="querySearch">查询条件</param>
/// <returns>返回台账列表</returns>
public async Task<DataResult<List<BookingContractNoManageDto>>> GetPageAsync(PageRequest<BookingContractNoManageQueryDto> querySearch)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(querySearch.QueryCondition);
var list = tenantDb.Queryable<BookingContractNoManage>()
.Includes(x => x.LaneList)
.Where(whereList)
.WhereIF(!string.IsNullOrEmpty(querySearch.OtherQueryCondition?.LaneCode) || querySearch.OtherQueryCondition?.LaneId != null,
x => x.LaneList!.Any(z => z.LaneCode == querySearch.OtherQueryCondition!.LaneCode || z.LaneId == querySearch.OtherQueryCondition!.LaneId))
.ToQueryPage(querySearch.PageCondition);
var result = list.Data.Adapt<List<BookingContractNoManageDto>>();
return DataResult<List<BookingContractNoManageDto>>.PageList(list.Count, result, MultiLanguageConst.DataQuerySuccess);
}
#endregion
}
}