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.
BookingHeChuan/Myshipping.Application/Service/BookingOrderSeaeEdi/BookingOrderSeaeEdiService.cs

294 lines
11 KiB
C#

2 years ago
using Myshipping.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq;
using System.Threading.Tasks;
using Myshipping.Application.Entity;
using Microsoft.AspNetCore.Authorization;
using Furion;
using Microsoft.AspNetCore.Http;
using Furion.DataEncryption;
using System.Collections.Generic;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Identity;
using Furion.FriendlyException;
using Furion.Logging;
using System;
using Microsoft.Extensions.Logging;
using System.Reflection;
using System.ComponentModel;
using Myshipping.Application.ConfigOption;
using System.IO;
using Yitter.IdGenerator;
using Myshipping.Core.Entity;
using Furion.RemoteRequest.Extensions;
using System.Net.Http;
using Myshipping.Core.Service;
using System.Reflection.Emit;
using Myshipping.Application.Service.BookingOrderSeaeEdi.Dto;
2 years ago
using Myshipping.Application.Enum;
using Myshipping.Application.Service.BookingOrder.Dto;
using Newtonsoft.Json.Linq;
using System.Text;
using System.Web;
2 years ago
namespace Myshipping.Application
{
/// <summary>
/// 舱单服务
/// </summary>
[ApiDescriptionSettings("Application", Name = "BookingOrderSeaeEdi", Order = 1)]
public class BookingOrderSeaeEdiService : IBookingOrderSeaeEdiService, IDynamicApiController, ITransient
{
private readonly ILogger<BookingOrderSeaeEdiService> _logger;
private readonly SqlSugarRepository<BookingOrderSeaeEdi> _seaeedi;
private readonly SqlSugarRepository<BookingOrderSeaeEdiCtn> _seaeedictn;
2 years ago
private readonly ISysCacheService _cache;
private readonly SqlSugarRepository<BookingStatusLog> _repStatuslog;
private readonly IDjyWebsiteAccountConfigService _webAccountConfig;
public BookingOrderSeaeEdiService(ILogger<BookingOrderSeaeEdiService> logger, SqlSugarRepository<BookingOrderSeaeEdi> seaeedi,
SqlSugarRepository<BookingOrderSeaeEdiCtn> seaeedictn, ISysCacheService cache, SqlSugarRepository<BookingStatusLog> repStatuslog,
IDjyWebsiteAccountConfigService webAccountConfig)
2 years ago
{
this._logger = logger;
this._seaeedi = seaeedi;
this._seaeedictn = seaeedictn;
2 years ago
this._cache = cache;
this._repStatuslog = repStatuslog;
this._webAccountConfig = webAccountConfig;
2 years ago
}
/// <summary>
/// 获取数据
/// </summary>
/// <param name="bookingId"></param>
/// <returns></returns>
[HttpGet("/BookingOrderSeaeEdi/PageESeaeEdi")]
public async Task<List<BookingOrderSeaeEdiServiceDto>> PageESeaeEdi(long bookingId)
{
2 years ago
var entities = await _seaeedi.AsQueryable().Filter(null, true).Where(x => x.BookingId == bookingId && x.IsDeleted == false).ToListAsync();
2 years ago
var list = entities.Adapt<List<BookingOrderSeaeEdiServiceDto>>();
foreach (var item in list)
{
2 years ago
var ctn = await _seaeedictn.AsQueryable().Filter(null, true).Where(x => x.PId == item.Id && x.IsDeleted == false).ToListAsync();
2 years ago
item.EdiCtn = ctn.Adapt<List<BookingOrderSeaeEdiCtnDto>>();
}
return list;
}
/// <summary>
/// 立即返回保存信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/BookingOrderSeaeEdi/Save")]
public async Task<dynamic> Save(BookingOrderSeaeEdiServiceDto input)
{
if (input == null)
{
throw Oops.Bah("请传入正常数据!");
}
if (!string.IsNullOrWhiteSpace(input.MBLNO))
{
var et = await _seaeedi.Where(x => x.MBLNO == input.MBLNO && x.TenantId == UserManager.TENANT_ID && x.HBLNO == input.HBLNO && x.Id != input.Id).FirstAsync();
if (et != null)
{
throw Oops.Bah("当前提单号已存在,请勿重复录入!");
}
}
var entity = input.Adapt<BookingOrderSeaeEdi>();
2 years ago
entity.State = "已录入";
2 years ago
if (input.Id == 0)
{
await _seaeedi.InsertAsync(entity);
if (input.EdiCtn != null)
{
foreach (var item in input.EdiCtn)
{
var ctn = item.Adapt<BookingOrderSeaeEdiCtn>();
ctn.PId = entity.Id;
2 years ago
if (ctn.CTNALL.Length==4) {
ctn.SIZE = ctn.CTNALL != null ? ctn.CTNALL.Substring(0, 2) : "";
2 years ago
ctn.CTN = ctn.CTNALL != null ? ctn.CTNALL.Substring(2, 2) : "";
2 years ago
}
2 years ago
await _seaeedictn.InsertAsync(ctn);
}
}
}
else
{
await _seaeedi.AsUpdateable(entity).IgnoreColumns(it => new
{
it.MBLNO,
it.HBLNO,
it.BookingId,
it.TenantId,
it.CreatedTime,
it.CreatedUserId,
it.CreatedUserName,
it.IsDeleted
}).ExecuteCommandAsync();
await _seaeedictn.DeleteAsync(x => x.PId == input.Id);
if (input.EdiCtn != null)
{
foreach (var item in input.EdiCtn)
{
var ctn = item.Adapt<BookingOrderSeaeEdiCtn>();
ctn.PId = entity.Id;
2 years ago
if (ctn.CTNALL.Length == 4)
{
ctn.SIZE = ctn.CTNALL != null ? ctn.CTNALL.Substring(0, 2) : "";
2 years ago
ctn.CTN = ctn.CTNALL != null ? ctn.CTNALL.Substring(2, 2) : "";
2 years ago
}
2 years ago
await _seaeedictn.InsertAsync(ctn);
}
}
}
2 years ago
var entities = await _seaeedi.AsQueryable().Filter(null, true).Where(x => x.Id == entity.Id).ToListAsync();
2 years ago
var list = entities.Adapt<List<BookingOrderSeaeEdiServiceDto>>();
foreach (var item in list)
{
var ctn = await _seaeedictn.AsQueryable().Filter(null, true).Where(x => x.PId == item.Id).ToListAsync();
item.EdiCtn = ctn.Adapt<List<BookingOrderSeaeEdiCtnDto>>();
}
return list;
}
/// <summary>
/// 删除舱单
/// </summary>
2 years ago
/// <param name="Ids"></param>
2 years ago
/// <returns></returns>
[SqlSugarUnitOfWork]
2 years ago
2 years ago
[HttpPost("/BookingOrderSeaeEdi/Delete")]
2 years ago
public async Task Delete(string Ids)
2 years ago
{
2 years ago
var arr = Ids.Split(",");
if (arr.Count() > 0)
{
foreach (var ar in arr)
{
long Id = Convert.ToInt64(ar);
await _seaeedi.UpdateAsync(x => x.Id == Id, x => new BookingOrderSeaeEdi { IsDeleted = true });
2 years ago
2 years ago
await _seaeedictn.UpdateAsync(x => x.PId == Id, x => new BookingOrderSeaeEdiCtn { IsDeleted = true });
_logger.LogInformation(Id + "删除成功!");
}
}
2 years ago
}
#region 舱单
2 years ago
/// <summary>
/// 舱单
/// </summary>
2 years ago
/// <param name="Ids"></param>
2 years ago
/// <returns></returns>
[HttpPost("/BookingOrderSeaeEdi/CustEDI")]
2 years ago
public async Task CustEDI(string Ids)
2 years ago
{
2 years ago
var arr = Ids.Split(",");
if (arr.Count() > 0)
{
foreach (var ar in arr)
2 years ago
{
2 years ago
long Id = Convert.ToInt64(ar);
var order = await _seaeedi.AsQueryable().Filter(null, true).Where(x => x.Id == Id).FirstAsync();
2 years ago
var ctns = await _seaeedictn.AsQueryable().Filter(null, true).Where(x => x.PId == order.Id).ToListAsync();
//船公司
2 years ago
if (string.IsNullOrEmpty(order.CARRIER))
2 years ago
{
throw Oops.Bah(BookingErrorCode.BOOK118);
}
//提单号不能为空
if (string.IsNullOrEmpty(order.MBLNO))
{
throw Oops.Bah(BookingErrorCode.BOOK127);
}
var dicUrl = _cache.GetAllDictData().Result.First(x => x.TypeCode == "url_set" && x.Code == "request_emf");
var key = _webAccountConfig.GetAccountConfig("DjyCangDan", UserManager.UserId).Result;
2 years ago
if (key==null) {
throw Oops.Bah("当前用户未配置key,请联系管理员");
}
2 years ago
List<CustEDIDto> custEDIDtos = new List<CustEDIDto>();
MDATA mDATA = new MDATA();
List<CTNDATAItem> CTNDATA = new List<CTNDATAItem>();
mDATA = order.Adapt<MDATA>();
2 years ago
var FORWARDER = mDATA.FORWARDER;
mDATA.FORWARDER = _cache.GetAllMappingForwarder().Result.Where(x => x.Code == FORWARDER && x.Module == "cangdan").Select(x => x.MapCode).FirstOrDefault() == null ?
_cache.GetAllCodeForwarder().Result.Where(x => x.Code == FORWARDER).Select(x => x.Name).FirstOrDefault() :
_cache.GetAllMappingForwarder().Result.Where(x => x.Code == FORWARDER && x.Module == "cangdan").Select(x => x.MapCode).FirstOrDefault();
2 years ago
CTNDATA = ctns.Adapt<List<CTNDATAItem>>();
custEDIDtos.Add(
new CustEDIDto
{
MDATA = mDATA,
CTNDATA = CTNDATA
}
);
string strPostObj = custEDIDtos.ToJsonString();
2 years ago
2 years ago
var sendObj = new
{
ac = "emf",
uid = UserManager.DjyUserId,
skey = key.Password,
optype = "3",
data = strPostObj
};
_logger.LogInformation($"调用舱单接口 {dicUrl.Value} 传递数据:{strPostObj}");
var strResp = await dicUrl.Value.SetContentType("multipart/form-data").SetBody(sendObj).PostAsStringAsync();
_logger.LogInformation($"调用舱单接口返回:{strResp}");
2 years ago
2 years ago
2 years ago
var jobjResp = JObject.Parse(strResp);
2 years ago
bool respCode = jobjResp.GetBooleanValue("Success");
if (respCode== false)
2 years ago
{
2 years ago
throw Oops.Bah(jobjResp.GetStringValue("Message"));
2 years ago
}
//货运动态
var bsl = new BookingStatusLog();
2 years ago
bsl.BookingId = order.BookingId;
2 years ago
bsl.Status = $"保存舱单";
bsl.OpTime = DateTime.Now;
bsl.Category = "ship";
bsl.MBLNO = order.MBLNO;
await _repStatuslog.InsertAsync(bsl);
2 years ago
await _seaeedi.UpdateAsync(x => x.Id == Id, x => new BookingOrderSeaeEdi { State = "已发送" });
2 years ago
}
2 years ago
2 years ago
}
2 years ago
}
#endregion
2 years ago
}
}