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.

813 lines
32 KiB
C#

using Amazon.Runtime.Internal.Util;
using AngleSharp.Dom;
using DS.Module.Core;
using DS.Module.Core.Data;
using DS.Module.Core.Extensions;
using DS.Module.Core.Helpers;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.Code.Entity;
using DS.WMS.Core.Fee.Entity;
using DS.WMS.Core.Map.Entity;
using DS.WMS.Core.Op.Dtos;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Op.Interface;
using DS.WMS.Core.System.Entity;
using DS.WMS.Core.System.Interface;
using Mapster;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System.Security.Policy;
namespace DS.WMS.Core.Op.Method;
public class SeaExportService : ISeaExportService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ISaasDbService saasService;
//private readonly ICrawlerDataService crawlerService;
private readonly ICommonService commonService;
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public SeaExportService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
saasService = _serviceProvider.GetRequiredService<ISaasDbService>();
commonService = _serviceProvider.GetRequiredService<ICommonService>();
//crawlerService = _serviceProvider.GetRequiredService<ICrawlerDataService>();
}
/// <summary>
/// 列表
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public DataResult<List<SeaExportRes>> GetListByPage(PageRequest request)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = tenantDb.Queryable<SeaExport>()
.Where(whereList)
.Select<SeaExportRes>().ToQueryPage(request.PageCondition);
return data;
}
/// <summary>
/// 编辑
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public DataResult EditSeaExport(SeaExportReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (req.Id == 0)
{
if (req.BLType != "拼箱分票" && tenantDb.Queryable<SeaExport>().Where(x => x.MBLNO == req.MBLNO.Trim()).Any())
{
return DataResult.Failed("海运出口信息主提单号已存在!", MultiLanguageConst.SeaExportMBLNOExist);
}
if (tenantDb.Queryable<SeaExport>().Where(x => x.HBLNO == req.HBLNO.Trim()).Any())
{
return DataResult.Failed("海运出口信息分提单号已存在!", MultiLanguageConst.SeaExportHBLNOExist);
}
//TODO 会计期间不允许小于已结转期间
if (req.CloseDocDate.IsNotNull() && req.CloseDocDate < req.ETD)
{
return DataResult.Failed("截单日期不允许小于开船日期!", MultiLanguageConst.SeaExportCloseDocDateLimit);
}
if (req.ClosingDate.IsNotNull() && req.ClosingDate < req.ETD)
{
return DataResult.Failed("截港日期不允许小于开船日期!", MultiLanguageConst.SeaExportCloseDateLimit);
}
var sequence = commonService.GetSequenceNext<SeaExport>();
if (!sequence.Succeeded)
{
return DataResult.Failed(sequence.Message, MultiLanguageConst.SequenceSetNotExist);
}
var data = req.Adapt<SeaExport>();
data.CustomerNo = sequence.Data;
var entity = tenantDb.Insertable(data).ExecuteReturnEntity();
if (req.CtnInfo.Count > 0)
{
foreach (var item in req.CtnInfo)
{
var ctn = item.Adapt<OpCtn>();
ctn.BSNO = entity.Id.ToString();
tenantDb.Insertable(ctn).ExecuteCommand();
}
}
return DataResult.Successed("添加成功!", entity.Id, MultiLanguageConst.DataCreateSuccess);
}
else
{
var info = tenantDb.Queryable<SeaExport>().Where(x => x.Id == req.Id).First();
if ((bool)info.IsBusinessLocking)
{
return DataResult.Failed("海运出口信息业务已锁定!", MultiLanguageConst.SeaExportBusinessLock);
}
info = req.Adapt(info);
//TODO 会计期间不允许小于已结转期间
if (info.CloseDocDate.IsNotNull() && info.CloseDocDate < info.ETD)
{
return DataResult.Failed("截单日期不允许小于开船日期!", MultiLanguageConst.SeaExportCloseDocDateLimit);
}
if (info.ClosingDate.IsNotNull() && info.ClosingDate < info.ETD)
{
return DataResult.Failed("截港日期不允许小于开船日期!", MultiLanguageConst.SeaExportCloseDateLimit);
}
tenantDb.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).EnableDiffLogEvent().ExecuteCommand();
if (req.CtnInfo.Count > 0)
{
var ctnList = tenantDb.Queryable<OpCtn>().Where(x => x.BSNO == req.Id.ToString()).ToList();
foreach (var item in req.CtnInfo)
{
if (item.Id == 0)
{
var ctn = item.Adapt<OpCtn>();
ctn.BSNO = info.Id.ToString();
tenantDb.Insertable(ctn).ExecuteCommand();
}
else
{
var ctn = ctnList.First(x => x.Id == item.Id);
ctn = item.Adapt(ctn);
tenantDb.Updateable(ctn).ExecuteCommand();
}
}
}
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
}
}
/// <summary>
/// 详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataResult<SeaExportRes> GetSeaExportInfo(string id)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var data = tenantDb.Queryable<SeaExport>()
.Where(a => a.Id == long.Parse(id))
.Select<SeaExportRes>()
.First();
return DataResult<SeaExportRes>.Success(data, MultiLanguageConst.DataQuerySuccess);
}
/// <summary>
/// 批量更新
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public DataResult SeaExportBatchEdit(SeaExportBatchEditReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (req.Ids.Count == 0)
{
return DataResult.Failed("海运出口批量编辑未勾选!", MultiLanguageConst.SeaExportBatchEditNoSelect);
}
if (tenantDb.Queryable<SeaExport>().Where(x => !req.Ids.Contains(x.Id)).Any().IsNull())
{
return DataResult.Failed("不存在的海运出口信息!", MultiLanguageConst.SeaExportExist);
}
if (tenantDb.Queryable<SeaExport>().Where(x => req.Ids.Contains(x.Id) && x.IsBusinessLocking == true).Any())
{
return DataResult.Failed("海运出口信息业务已锁定!", MultiLanguageConst.SeaExportBusinessLock);
}
if (req.AccountDate.ToString().IsNotNull())
{
if (tenantDb.Queryable<SeaExport>().Where(x => req.Ids.Contains(x.Id) && x.IsFeeLocking == true).Any())
{
return DataResult.Failed("海运出口信息费用已锁定!", MultiLanguageConst.SeaExportFeeLock);
}
}
var orderList = tenantDb.Queryable<SeaExport>().Where(x => req.Ids.Contains(x.Id)).ToList();
var dic = req.GetPropertiesArray();
foreach (var item in orderList)
{
var info = item;
info = req.Adapt(info);
tenantDb.Updateable(info).UpdateColumns(dic).EnableDiffLogEvent().ExecuteCommand();
}
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
}
/// <summary>
/// 业务单据单票复制
/// </summary>
/// <param name="id">业务Id</param>
/// <returns></returns>
public DataResult SeaExportCopy(string id)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var info = tenantDb.Queryable<SeaExport>().Where(x => x.Id == long.Parse(id)).First();
if (info.IsNullOrEmpty())
{
return DataResult.Failed("海运出口信息不存在!", MultiLanguageConst.SeaExportNotExist);
}
//获取表单复制模板
var template = tenantDb.Queryable<CodeFormCopy>().Where(x => x.PermissionId == 1772509201441099776).First();
var sequence = commonService.GetSequenceNext<SeaExport>();
if (!sequence.Succeeded)
{
return DataResult.Failed(sequence.Message, MultiLanguageConst.SequenceSetNotExist);
}
var data = new SeaExport();
if (template.IsNullOrEmpty())
{
data = info.Adapt(data);
data.Id = 0;
data.ParentId = 0;
data.IsBusinessLocking = false;
data.IsFeeLocking = false;
data.CustomerNo = sequence.Data;
var entity = tenantDb.Insertable(data).ExecuteReturnEntity();
return DataResult.Successed("复制成功!", entity.Id, MultiLanguageConst.DataCopySuccess);
}
else
{
data = info.Adapt(data);
data.Id = 0;
data.ParentId = 0;
data.IsBusinessLocking = false;
data.IsFeeLocking = false;
data.CustomerNo = sequence.Data;
var list0 = template.CopyFields.Split(",");
var list1 = new List<string>();
foreach (var item in list0)
{
list1.Add(item.ToUpperCamelCase());
}
list1.Add("IsBusinessLocking");
list1.Add("IsFeeLocking");
list1.Add("CustomerNo");
var insertColumns = list1.ToArray();
//insertColumns.AddRange(["Id", "ParentId", "IsBusinessLocking", "IsFeeLocking", "CustomerNo"]);
var entity = tenantDb.Insertable(data).InsertColumns(insertColumns).ExecuteReturnEntity();
return DataResult.Successed("复制成功!", entity.Id, MultiLanguageConst.DataCopySuccess);
}
}
#region 删除
/// <summary>
/// 业务单据删除
/// </summary>
/// <param name="id">业务Id</param>
/// <returns></returns>
public DataResult SeaExportDel(string id)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var info = tenantDb.Queryable<SeaExport>().Where(x => x.Id == long.Parse(id)).First();
if (info.IsNullOrEmpty())
{
return DataResult.Failed("海运出口信息不存在!", MultiLanguageConst.SeaExportNotExist);
}
if (tenantDb.Queryable<SeaExport>().Where(x => x.ParentId == long.Parse(id)).Any())
{
return DataResult.Failed("海运出口存在分票信息,不能删除!", MultiLanguageConst.SeaExportPartExist);
}
if (tenantDb.Queryable<FeeRecord>().Where(x => x.BusinessId == long.Parse(id)).Any())
{
return DataResult.Failed("海运出口存在费用信息,不能删除!", MultiLanguageConst.SeaExportFeeExist);
}
if ((bool)info.IsBusinessLocking)
{
return DataResult.Failed("海运出口信息业务已锁定!", MultiLanguageConst.SeaExportBusinessLock);
}
if ((bool)info.IsFeeLocking)
{
return DataResult.Failed("海运出口信息费用已锁定!", MultiLanguageConst.SeaExportFeeLock);
}
info.Deleted = true;
info.DeleteTime = DateTime.Now;
info.DeleteBy = long.Parse(user.UserId);
tenantDb.Updateable(info).ExecuteCommand();
//tenantDb.Deleteable(info).IsLogic().ExecuteCommand("Deleted");
return DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess);
}
/// <summary>
/// 业务单据删除
/// </summary>
/// <param name="req">业务Ids</param>
/// <returns></returns>
public DataResult SeaExportBatchDel(IdModel req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (req.Ids.Length == 0)
{
return DataResult.Failed("海运出口批量操作未勾选!", MultiLanguageConst.SeaExportBatchOpNoSelect);
}
if (tenantDb.Queryable<SeaExport>().Where(x => req.Ids.Contains(x.Id)).Any().IsNull())
{
return DataResult.Failed("不存在的海运出口信息!", MultiLanguageConst.SeaExportExist);
}
if (tenantDb.Queryable<SeaExport>().Where(x => req.Ids.Contains(x.Id) && x.IsBusinessLocking == true).Any())
{
return DataResult.Failed("海运出口信息业务已锁定!", MultiLanguageConst.SeaExportBusinessLock);
}
if (tenantDb.Queryable<SeaExport>().Where(x => req.Ids.Contains(x.Id) && x.IsFeeLocking == true).Any())
{
return DataResult.Failed("海运出口信息费用已锁定!", MultiLanguageConst.SeaExportFeeLock);
}
if (tenantDb.Queryable<SeaExport>().Where(x => req.Ids.Contains(x.ParentId)).Any())
{
return DataResult.Failed("海运出口存在分票信息,不能删除!", MultiLanguageConst.SeaExportPartExist);
}
if (tenantDb.Queryable<FeeRecord>().Where(x => req.Ids.Contains(x.BusinessId)).Any())
{
return DataResult.Failed("海运出口存在费用信息,不能删除!", MultiLanguageConst.SeaExportFeeExist);
}
var list = tenantDb.Queryable<SeaExport>().Where(x => req.Ids.Contains(x.Id)).ToList();
//tenantDb.Deleteable(list).IsLogic().ExecuteCommand("Deleted");
//tenantDb.Deleteable<SeaExport>(list).IsLogic().ExecuteCommand("Deleted");
foreach (var item in list)
{
item.Deleted = true;
item.DeleteTime = DateTime.Now;
item.DeleteBy = long.Parse(user.UserId);
tenantDb.Updateable(item).ExecuteCommand();
}
return DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess);
}
#endregion
#region 费用锁定 业务锁定
/// <summary>
/// 业务锁定
/// </summary>
/// <param name="id">业务Id</param>
/// <returns></returns>
public DataResult SeaExportBusinessLook(string id)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var info = tenantDb.Queryable<SeaExport>().Where(x => x.Id == long.Parse(id)).First();
if (info.IsNullOrEmpty())
{
return DataResult.Failed("海运出口信息不存在!", MultiLanguageConst.SeaExportNotExist);
}
if ((bool)info.IsBusinessLocking)
{
return DataResult.Failed("海运出口信息业务已锁定!", MultiLanguageConst.SeaExportBusinessLock);
}
info.IsBusinessLocking = true;
tenantDb.Updateable(info).ExecuteCommand();
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
}
/// <summary>
/// 费用锁定
/// </summary>
/// <param name="id">业务Id</param>
/// <returns></returns>
public DataResult SeaExportFeeLook(string id)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var info = tenantDb.Queryable<SeaExport>().Where(x => x.Id == long.Parse(id)).First();
if (info.IsNullOrEmpty())
{
return DataResult.Failed("海运出口信息不存在!", MultiLanguageConst.SeaExportNotExist);
}
if ((bool)info.IsFeeLocking)
{
return DataResult.Failed("海运出口信息费用已锁定!", MultiLanguageConst.SeaExportFeeLock);
}
info.IsFeeLocking = true;
tenantDb.Updateable(info).ExecuteCommand();
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
}
/// <summary>
/// 业务解锁
/// </summary>
/// <param name="id">业务Id</param>
/// <returns></returns>
public DataResult SeaExportBusinessUnLook(string id)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var info = tenantDb.Queryable<SeaExport>().Where(x => x.Id == long.Parse(id)).First();
if (info.IsNullOrEmpty())
{
return DataResult.Failed("海运出口信息不存在!", MultiLanguageConst.SeaExportNotExist);
}
if (!(bool)info.IsBusinessLocking)
{
return DataResult.Failed("海运出口信息业务锁定已解锁!", MultiLanguageConst.SeaExportBusinessUnLock);
}
info.IsBusinessLocking = false;
tenantDb.Updateable(info).ExecuteCommand();
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
}
/// <summary>
/// 费用解锁
/// </summary>
/// <param name="id">业务Id</param>
/// <returns></returns>
public DataResult SeaExportFeeUnLook(string id)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var info = tenantDb.Queryable<SeaExport>().Where(x => x.Id == long.Parse(id)).First();
if (info.IsNullOrEmpty())
{
return DataResult.Failed("海运出口信息不存在!", MultiLanguageConst.SeaExportNotExist);
}
if (!(bool)info.IsFeeLocking)
{
return DataResult.Failed("海运出口信息费用锁定已解锁!", MultiLanguageConst.SeaExportFeeUnLock);
}
info.IsFeeLocking = false;
tenantDb.Updateable(info).ExecuteCommand();
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
}
#endregion
/// <summary>
/// 获取场站数据
/// </summary>
/// <param name="id">业务Id</param>
/// <param name="isWeb"></param>
/// <returns></returns>
public async Task<DataResult<string>> GetYardData(string id, bool isWeb = false)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var info = tenantDb.Queryable<SeaExport>().Where(x => x.Id == long.Parse(id)).First();
if (info.IsNull())
{
return await Task.FromResult(DataResult<string>.Failed("海运出口信息不存在!", MultiLanguageConst.SeaExportNotExist));
}
if (string.IsNullOrEmpty(info.YardId.ToString()))
{
return await Task.FromResult(DataResult<string>.Failed("请先选择场站!", MultiLanguageConst.SeaExportNotExist));
}
var data = GetYardDataAndMappingSystem(info, isWeb);
return await Task.FromResult(DataResult<string>.Success(""));
}
/// <summary>
/// 从爬虫获取场站数据,并映射为系统中的箱型
/// </summary>
/// <returns></returns>
private async Task<DataResult<List<YardCtnImportRes>>> GetYardDataAndMappingSystem(SeaExport order, bool isWeb = false)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var req = new YardDataReq
{
YardId = order.YardId,
MBLNO = order.MBLNO,
IsWeb = isWeb
};
var rtn = await GetYardData(req);
//if (!rtn.Key)
//{
// throw Oops.Bah(rtn.Value);
//}
//场站引入的数据,转换为订舱箱型,且带上箱型代码
var ctnList = tenantDb.Queryable<CodeCtn>().Where(x => x.Status == StatusEnum.Enable).ToList();
var mapCtn = tenantDb.Queryable<MappingCtn>().Where(x => x.Status == StatusEnum.Enable).ToList();
var listRtn = JsonConvert.DeserializeObject<List<YardCtnImportRes>>(rtn.Data);
foreach (var item in listRtn)
{
var ctnall = item.CTNALL;
var findMap = mapCtn.FirstOrDefault(x => x.Module == "YardData" && x.MapCode == ctnall);
var findCtn = ctnList.FirstOrDefault(x => x.CtnName == ctnall);
if (findMap != null)
{
item.CtnCode = findMap.Code;
findCtn = ctnList.First(c => c.EdiCode == findMap.Code);
item.CTNALL = findCtn.CtnName; //名称显示维护的箱型
}
else if (findCtn != null)
{
item.CtnCode = findCtn.EdiCode;
item.CTNALL = findCtn.CtnName; //名称显示维护的箱型
}
else
{
return await Task.FromResult(DataResult<List<YardCtnImportRes>>.Failed($"未找到箱型{ctnall}的场站引入配置"));
}
}
return await Task.FromResult(DataResult<List<YardCtnImportRes>>.Success(listRtn));
}
/// <summary>
/// 异步调取出口运踪场站详情(按箱明细显示)
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public async Task<DataResult<string>> GetYardData(YardDataReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var account = tenantDb.Queryable<CodeThirdParty>().Where(x => x.CustomerId == req.YardId && x.AccountType == "BillTrace").First();
var userKey = db.Queryable<SysConfig>().Where(x => x.Code == "spiderUserKeyBilltrace").First().Value;
var userSecret = db.Queryable<SysConfig>().Where(x => x.Code == "spiderUserSecretBilltrace").First().Value;
var spiderServerUrl = db.Queryable<SysConfig>().Where(x => x.Code == "spiderServerUrlBillTraceNew").First().Value;
var tenantName = db.Queryable<SysTenant>().Filter(null, true).Where(x => x.Id == long.Parse(user.TenantId)).First().Name;
if (account.IsNull())
{
return await Task.FromResult(DataResult<string>.Failed("未配置出口运踪场站第三方账号信息!"));
}
var yardMap = tenantDb.Queryable<MappingYard>().Where(x => x.LinkId == req.YardId && x.Module == "BillTrace").First();
if (yardMap.IsNull())
{
return await Task.FromResult(DataResult<string>.Failed($"场站代号配置未找到:{req.YardId}"));
}
var objSend = new
{
user_key = userKey,
user_secret = userSecret,
customer_id = user.TenantId,
customer_name = tenantName,
web_code = yardMap.MapCode,
bno = req.MBLNO,
req_type = req.IsWeb ? "1" : "0",
web_user = account.AppKey,
web_psw = account.AppSecret,
};
var urlYard = spiderServerUrl;
if (!urlYard.EndsWith("/"))
{
urlYard += "/";
}
urlYard += "real/query";
var res = RequestHelper.Post(JsonConvert.SerializeObject(objSend), urlYard);
var rtnObj = JObject.Parse(res);
if (rtnObj.GetIntValue("code") != 200)
{
return await Task.FromResult(DataResult<string>.Failed("$\"获取场站数据失败:{rtnObj.GetStringValue(\"msg\")}\"!"));
}
else
{
var rtnData = rtnObj.GetStringValue("data");
if (req.IsWeb)
{
var viewstatehtml = GetDataHtmlList(rtnData, "__VIEWSTATE", "/>");
if (viewstatehtml != "")
{
rtnData = rtnData.Replace(viewstatehtml, "");
}
}
return await Task.FromResult(DataResult<string>.Success(rtnData));
}
}
public static string GetDataHtmlList(string html, string startstr, string endstr)
{
var subhtml = html;
var htmllength = subhtml.Length;
var startindex = subhtml.IndexOf(startstr);
//if (startindex == -1 || startindex == 0) return "";
if (startindex == -1) return "";
subhtml = subhtml.Substring(startindex + startstr.Length, htmllength - startindex - startstr.Length);
var endindex = subhtml.IndexOf(endstr);
if (endindex != -1 && endindex != 0)
subhtml = subhtml.Substring(0, endindex);
return subhtml;
}
#region 运踪相关
/// <summary>
/// 发起运踪订阅或者更新提单号
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public async Task<DataResult<string>> SendOrUpdateTrace(List<BillTraceReq> req)
{
if (req.Count == 0)
{
return await Task.FromResult(DataResult<string>.Failed("未传入正确参数!"));
}
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var userKey = db.Queryable<SysConfig>().Where(x => x.Code == "seae_billtraceurl_userKey").First().Value;
var userPwd = db.Queryable<SysConfig>().Where(x => x.Code == "seae_billtraceurl_userPwd").First().Value;
var userId = db.Queryable<SysConfig>().Where(x => x.Code == "seae_billtraceurl_userId").First().Value;
var reqUrl = db.Queryable<SysConfig>().Where(x => x.Code == "request_seae_billtraceurl").First().Value;
var resUrl = db.Queryable<SysConfig>().Where(x => x.Code == "response_seae_billtraceurl").First().Value;
BillTraceMain billdto = new BillTraceMain();
List<BillTraceReq> billTraceList = new List<BillTraceReq>();
foreach (var item in req)
{
if (!string.IsNullOrWhiteSpace(item.CARRIERID))
{
var et = await tenantDb.Queryable<BookingStatus>().Where(x => x.StatusCode == "status_mudigang" && x.BusinessId == Convert.ToInt64(item.BusinessId)).FirstAsync();
if (et != null)
{
return await Task.FromResult(DataResult<string>.Failed("\"提单号\" + item.MBLNO + \"已订阅!"));
}
}
if (!string.IsNullOrWhiteSpace(item.YardCode))
{
var et = await tenantDb.Queryable<BookingStatus>().Where(x => x.StatusCode == "status_qiyungang" && x.BusinessId == Convert.ToInt64(item.BusinessId)).FirstAsync();
if (et != null)
{
return await Task.FromResult(DataResult<string>.Failed("\"提单号\" + item.MBLNO + \"已订阅!"));
}
}
if (item.isBook && (string.IsNullOrWhiteSpace(item.CARRIERID) || string.IsNullOrWhiteSpace(item.CARRIER) || string.IsNullOrWhiteSpace(item.MBLNO)))
{
return await Task.FromResult(DataResult<string>.Failed("订阅目的港船公司或提单号不能为空!"));
}
if (!item.isBook && (string.IsNullOrWhiteSpace(item.YARD) || string.IsNullOrWhiteSpace(item.YardCode) || string.IsNullOrWhiteSpace(item.MBLNO)))
{
return await Task.FromResult(DataResult<string>.Failed("订阅港前数据提单号或场站不能为空!"));
}
var yardMap = tenantDb.Queryable<MappingYard>().Where(x => x.Code == item.YardCode && x.Module == "BillTrace").First();
if (yardMap.IsNull())
{
return await Task.FromResult(DataResult<string>.Failed($"场站代号配置未找到:{item.YardCode}"));
}
billTraceList.Add(new BillTraceReq
{
BusinessId = item.BusinessId,
MBLNO = item.MBLNO,
YARD = item.YARD == "" ? null : item.YARD,
YardCode = yardMap.MapCode == "" ? null : yardMap.MapCode,
CARRIER = item.CARRIER == "" ? null : item.CARRIER,
CARRIERID = item.CARRIERID == "" ? null : item.CARRIERID,
isBook = item.isBook
});
var status = string.Empty;
var IsBookingYZ = String.Empty;
if (!string.IsNullOrWhiteSpace(item.YardCode) && !string.IsNullOrWhiteSpace(item.CARRIERID))
{
status = "订阅起运港,目的港";
IsBookingYZ = "3";
}
else if (string.IsNullOrWhiteSpace(item.YardCode) && !string.IsNullOrWhiteSpace(item.CARRIERID))
{
status = "订阅目的港";
IsBookingYZ = "2";
}
else if (!string.IsNullOrWhiteSpace(item.YardCode) && string.IsNullOrWhiteSpace(item.CARRIERID))
{
status = "订阅起运港";
IsBookingYZ = "1";
}
else
{
IsBookingYZ = "0";
}
//TODO 添加booking日志
//更新运踪标识
var info = tenantDb.Queryable<SeaExport>().First(x => x.Id == Convert.ToInt64(item.BusinessId));
info.IsBookingYZ = IsBookingYZ;
await tenantDb.Updateable(info).ExecuteCommandAsync();
}
billdto.Children = billTraceList;
billdto.Key = userKey;
billdto.PWD = userPwd;
billdto.url = resUrl;
billdto.Gid = userId;
var json = billdto.ToJsonString();
if (!reqUrl.EndsWith("/"))
{
reqUrl += "/";
}
reqUrl += "BillTrace/UpdateMblno";
var result = RequestHelper.Post(json, reqUrl);
var res = JsonConvert.DeserializeObject<BillTraceRes>(result);
if (!res.Success)
{
return await Task.FromResult(DataResult<string>.Failed($"发起运踪订阅或者更新提单号失败:{res.Message}"));
}
else
{
return await Task.FromResult(DataResult<string>.Success(res.Data));
}
}
/// <summary>
/// 运踪退订
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public async Task<DataResult<string>> UnsubscribeBillTrace(List<BillTraceUnsubscribeList> req)
{
if (req.Count == 0)
{
return await Task.FromResult(DataResult<string>.Failed("未传入正确参数!"));
}
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var userKey = db.Queryable<SysConfig>().Where(x => x.Code == "seae_billtraceurl_userKey").First().Value;
var userPwd = db.Queryable<SysConfig>().Where(x => x.Code == "seae_billtraceurl_userPwd").First().Value;
var userId = db.Queryable<SysConfig>().Where(x => x.Code == "seae_billtraceurl_userId").First().Value;
var reqUrl = db.Queryable<SysConfig>().Where(x => x.Code == "request_seae_billtraceurl").First().Value;
var resUrl = db.Queryable<SysConfig>().Where(x => x.Code == "response_seae_billtraceurl").First().Value;
BillTraceUnsubscribeDto reqDto = new BillTraceUnsubscribeDto()
{
Children = new List<BillTraceUnsubscribeList>(),
Key = userKey,
PWD = userPwd,
Gid = userId
};
foreach (var item in req)
{
if (string.IsNullOrEmpty(item.BusinessId))
{
return await Task.FromResult(DataResult<string>.Failed("订舱记录主键不能为空!!"));
}
reqDto.Children.Add(new BillTraceUnsubscribeList(item.BusinessId, item.MBLNO));
//TODO 添加booking日志
//更新运踪标识
var info = tenantDb.Queryable<SeaExport>().First(x => x.Id == Convert.ToInt64(item.BusinessId));
info.IsBookingYZ = "0";
await tenantDb.Updateable(info).ExecuteCommandAsync();
}
var json = reqDto.ToJsonString();
if (!reqUrl.EndsWith("/"))
{
reqUrl += "/";
}
reqUrl += "BillTrace/UnsubscribeBillTrace";
var result = RequestHelper.Post(json, reqUrl);
var res = JsonConvert.DeserializeObject<BillTraceRes>(result);
if (!res.Success)
{
return await Task.FromResult(DataResult<string>.Failed($"运踪退订失败:{res.Message}"));
}
else
{
return await Task.FromResult(DataResult<string>.Success(res.Data));
}
}
#endregion
}