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; using Myshipping.Application.Enum; using Myshipping.Application.Service.BookingOrder.Dto; using Newtonsoft.Json.Linq; using System.Text; using System.Web; namespace Myshipping.Application { /// /// 舱单服务 /// [ApiDescriptionSettings("Application", Name = "BookingOrderSeaeEdi", Order = 1)] public class BookingOrderSeaeEdiService : IBookingOrderSeaeEdiService, IDynamicApiController, ITransient { private readonly ILogger _logger; private readonly SqlSugarRepository _seaeedi; private readonly SqlSugarRepository _seaeedictn; private readonly ISysCacheService _cache; private readonly IBookingOrderService _rep; private readonly SqlSugarRepository _bookingOrder; private readonly SqlSugarRepository _repStatuslog; private readonly SqlSugarRepository _repTemplate; private readonly IDjyWebsiteAccountConfigService _webAccountConfig; private readonly SqlSugarRepository _goodsStatusConfig; private readonly SqlSugarRepository _goodsStatus; public BookingOrderSeaeEdiService(ILogger logger, SqlSugarRepository seaeedi, SqlSugarRepository seaeedictn, ISysCacheService cache, SqlSugarRepository repStatuslog, SqlSugarRepository repTemplate, SqlSugarRepository goodsStatusConfig, SqlSugarRepository bookingOrder, SqlSugarRepository goodsStatus, IBookingOrderService rep, IDjyWebsiteAccountConfigService webAccountConfig) { this._logger = logger; this._seaeedi = seaeedi; this._seaeedictn = seaeedictn; this._cache = cache; this._repStatuslog = repStatuslog; this._webAccountConfig = webAccountConfig; this._repTemplate = repTemplate; this._goodsStatusConfig = goodsStatusConfig; this._goodsStatus = goodsStatus; this._rep = rep; this._bookingOrder =bookingOrder; } /// /// 获取数据 /// /// /// [HttpGet("/BookingOrderSeaeEdi/PageESeaeEdi")] public async Task> PageESeaeEdi(long bookingId) { var entities = await _seaeedi.AsQueryable().Filter(null, true).Where(x => x.BookingId == bookingId && x.IsDeleted == false).ToListAsync(); var list = entities.Adapt>(); foreach (var item in list) { var ctn = await _seaeedictn.AsQueryable().Filter(null, true).Where(x => x.PId == item.Id && x.IsDeleted == false).ToListAsync(); item.EdiCtn = ctn.Adapt>(); } return list; } /// /// 立即返回保存信息 /// /// /// [HttpPost("/BookingOrderSeaeEdi/Save")] public async Task 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(); entity.State = "已录入"; if (input.Id == 0) { if (!string.IsNullOrEmpty(entity.CARRIERID)) { entity.CARRIER = _cache.GetAllCodeCarrier().Result.Where(x => x.Code == entity.CARRIERID).Select(x => x.EdiCode).FirstOrDefault(); } if (entity.CARGOID!="D") { entity.DCLASS = string.Empty; entity.DUNNO = string.Empty; } await _seaeedi.InsertAsync(entity); if (input.EdiCtn != null) { foreach (var item in input.EdiCtn) { var ctn = item.Adapt(); ctn.PId = entity.Id; if (ctn.CTNALL.Length == 4) { ctn.SIZE = ctn.CTNALL != null ? ctn.CTNALL.Substring(0, 2) : ""; ctn.CTN = ctn.CTNALL != null ? ctn.CTNALL.Substring(2, 2) : ""; } if (ctn.CTNALL.Contains("'")) { ctn.SIZE = ctn.CTNALL.Split("'")[0].ToString(); ctn.CTN = ctn.CTNALL.Split("'")[1].ToString(); } await _seaeedictn.InsertAsync(ctn); } } } else { if (!string.IsNullOrEmpty(entity.CARRIERID)) { entity.CARRIER = _cache.GetAllCodeCarrier().Result.Where(x => x.Code == entity.CARRIERID).Select(x => x.EdiCode).FirstOrDefault(); } if (entity.CARGOID != "D") { entity.DCLASS = string.Empty; entity.DUNNO = string.Empty; } 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(); ctn.PId = entity.Id; if (ctn.CTNALL.Length == 4) { ctn.SIZE = ctn.CTNALL != null ? ctn.CTNALL.Substring(0, 2) : ""; ctn.CTN = ctn.CTNALL != null ? ctn.CTNALL.Substring(2, 2) : ""; } if (ctn.CTNALL.Contains("'")) { ctn.SIZE = ctn.CTNALL.Split("'")[0].ToString(); ctn.CTN = ctn.CTNALL.Split("'")[1].ToString(); } await _seaeedictn.InsertAsync(ctn); } } } var entities = await _seaeedi.AsQueryable().Filter(null, true).Where(x => x.Id == entity.Id).ToListAsync(); var list = entities.Adapt>(); foreach (var item in list) { var ctn = await _seaeedictn.AsQueryable().Filter(null, true).Where(x => x.PId == item.Id).ToListAsync(); item.EdiCtn = ctn.Adapt>(); } return list; } /// /// 删除舱单 /// /// /// [SqlSugarUnitOfWork] [HttpPost("/BookingOrderSeaeEdi/Delete")] public async Task Delete(string Ids) { 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 }); await _seaeedictn.UpdateAsync(x => x.PId == Id, x => new BookingOrderSeaeEdiCtn { IsDeleted = true }); _logger.LogInformation(Id + "删除成功!"); } } } #region 舱单 /// /// 舱单 /// /// [HttpPost("/BookingOrderSeaeEdi/CustEDI")] public async Task CustEDI(string Ids, string type, string SENDREMARK) { var arr = Ids.Split(","); if (arr.Count() > 0) { foreach (var ar in arr) { long Id = Convert.ToInt64(ar); var order = await _seaeedi.AsQueryable().Filter(null, true).Where(x => x.Id == Id).FirstAsync(); var ctns = await _seaeedictn.AsQueryable().Filter(null, true).Where(x => x.PId == order.Id).ToListAsync(); //船公司 if (string.IsNullOrEmpty(order.CARRIER)) { 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; if (key == null) { throw Oops.Bah("当前用户未配置key,请联系管理员"); } List custEDIDtos = new List(); MDATA mDATA = new MDATA(); List CTNDATA = new List(); mDATA = order.Adapt(); if (string.IsNullOrEmpty(mDATA.FORWARDER)) { throw Oops.Bah("船代不能为空"); } if (string.IsNullOrEmpty(mDATA.YARDID)) { throw Oops.Bah("场站不能为空"); } mDATA.SENDREMARK = SENDREMARK; var FORWARDER = mDATA.FORWARDER; var YardCode = mDATA.YARDID; if (_cache.GetAllMappingForwarder().Result.Where(x => x.Code == FORWARDER && x.Module == "cangdan").Select(x => x.MapCode).FirstOrDefault() == null) { throw Oops.Bah("暂不支持此船代"); } mDATA.FORWARDER = _cache.GetAllMappingForwarder().Result.Where(x => x.Code == FORWARDER && x.Module == "cangdan").Select(x => x.MapCode).FirstOrDefault(); mDATA.YARDID = _cache.GetAllCodeYard().Result.Where(x => x.Code == YardCode).Select(x => x.ShowCode).FirstOrDefault(); CTNDATA = ctns.Adapt>(); foreach (var item in CTNDATA) { item.KINDPKGS = _cache.GetAllCodePackage().Result.Where(x => x.Name == item.KINDPKGS).Select(x => x.EdiCode).FirstOrDefault(); } custEDIDtos.Add( new CustEDIDto { MDATA = mDATA, CTNDATA = CTNDATA } ); string strPostObj = custEDIDtos.ToJsonString(); var sendObj = new { ac = "emf", uid = UserManager.DjyUserId, skey = key.Password, optype = type, data = strPostObj }; _logger.LogInformation($"调用舱单接口 {dicUrl.Value} 传递数据:{strPostObj}"); var strResp = await dicUrl.Value.SetContentType("multipart/form-data").SetBody(sendObj).PostAsStringAsync(); _logger.LogInformation($"调用舱单接口返回:{strResp}"); var jobjResp = JObject.Parse(strResp); bool respCode = jobjResp.GetBooleanValue("Success"); if (respCode == false) { throw Oops.Bah(jobjResp.GetStringValue("Message").Replace("
", "\r\n").Replace("
","")); } //货运动态 var bsl = new BookingStatusLog(); bsl.BookingId = order.BookingId; if (type == "3") { bsl.Status = $"保存舱单"; } if (type == "0") { bsl.Status = $"直发舱单"; } if (type == "1") { bsl.Status = $"修改舱单"; } if (type == "2") { bsl.Status = $"删除舱单"; } if (type == "4") { bsl.Status = $"作废舱单"; } bsl.OpTime = DateTime.Now; bsl.Category = "ship"; bsl.MBLNO = order.MBLNO; await _repStatuslog.InsertAsync(bsl); if (type == "3") { await _seaeedi.UpdateAsync(x => x.Id == Id, x => new BookingOrderSeaeEdi { State = "已发送", SENDREMARK = SENDREMARK }); } if (type == "0"|| type == "1") { await _seaeedi.UpdateAsync(x => x.Id == Id, x => new BookingOrderSeaeEdi { State = "已直发", SENDREMARK = SENDREMARK }); #region 插入货运动态 //配置中所有的货物状态 var config = _goodsStatusConfig.AsQueryable().Where(config => config.CreatedUserId == order.CreatedUserId).ToList().DistinctBy(x => x.StatusName).Select(config => new GoodsStatusQuery { ConfigId = config.Id, SystemCode = config.SystemCode, StatusName = config.StatusName, FinishTime = null, FinishUser = null, FinishUserId = null, IsPublic = false, ExtData = null, Remark = null, Sort = config.Sort }).ToList(); var ConfigId = config.Where(x => x.SystemCode == "YFCD").Select(x => x.ConfigId).First(); await _goodsStatus.InsertAsync(new BookingGoodsStatus { bookingId = Id, ConfigId = ConfigId, FinishTime = DateTime.Now, FinishUser = order.CreatedUserName, FinishUserId = order.CreatedUserId, IsPublic = false }); var bookingorder = _bookingOrder.AsQueryable().Filter(null, true).First(x => x.Id == Id); bookingorder.BSSTATUSNAME = config.Where(x => x.SystemCode == "YFCD").Select(x => x.StatusName).First(); await _bookingOrder.AsUpdateable(bookingorder).IgnoreColumns(it => new { it.ParentId, it.TenantId, it.CreatedTime, it.CreatedUserId, it.CreatedUserName, it.TenantName, it.IsDeleted, it.BOOKINGNO }).ExecuteCommandAsync(); List tslist = new List(); tslist.Add((long)Id); var itemcode = _cache.GetAllTenantParam().Result.Where(x => x.ParaCode == "BOOKING_DATA_PUSH" && x.TenantId == UserManager.TENANT_ID).Select(x => x.ItemCode).FirstOrDefault(); if (!string.IsNullOrEmpty(itemcode)) { if (itemcode == "true") await _rep.SendBookingOrder(tslist.ToArray()); _logger.LogInformation(Id + "自动订舱货物状态推送成功!"); } #endregion } if (type == "2") { await _seaeedi.UpdateAsync(x => x.Id == Id, x => new BookingOrderSeaeEdi { State = "已删除", SENDREMARK = SENDREMARK }); } if (type == "4") { await _seaeedi.UpdateAsync(x => x.Id == Id, x => new BookingOrderSeaeEdi { State = "已作废", SENDREMARK = SENDREMARK }); } } } } #endregion #region 模板 /// /// 获取模板数据 /// /// [HttpGet("/BookingOrderSeaeEdi/GetBookingOrderSeaeEdiTemplateList")] public async Task> GetBookingOrderSeaeEdiTemplateList(string type, string templatename = null) { var entities = await _repTemplate.AsQueryable().Where(x => x.Type == type). WhereIF(!string.IsNullOrEmpty(templatename), x => x.TemplateName.Contains(templatename)). ToListAsync(); var list = entities.Adapt>(); return list; } /// /// 保存信息 /// /// /// [HttpPost("/BookingOrderSeaeEdi/SaveBookingOrderSeaeEdiTemplate")] public async Task SaveBookingOrderSeaeEdiTemplate(BookingOrderSeaeEdiTemplateDto input) { if (input == null) { throw Oops.Bah("请传入正常数据!"); } if (!string.IsNullOrWhiteSpace(input.TemplateNAME)) { var et = await _repTemplate.Where(x => x.TemplateName == input.TemplateNAME && x.Id != input.Id).FirstAsync(); if (et != null) { throw Oops.Bah("当前模板名称已存在,请勿重复录入!"); } } var entity = input.Adapt(); if (input.Id == 0) { await _repTemplate.InsertAsync(entity); } else { await _repTemplate.AsUpdateable(entity).IgnoreColumns(it => new { it.TenantId, it.CreatedTime, it.CreatedUserId, it.CreatedUserName, it.IsDeleted }).ExecuteCommandAsync(); } } /// /// 删除舱单 /// /// /// [SqlSugarUnitOfWork] [HttpPost("/BookingOrderSeaeEdi/DeleteBookingOrderSeaeEdiTemplate")] public async Task DeleteBookingOrderSeaeEdiTemplate(string Ids) { var arr = Ids.Split(","); if (arr.Count() > 0) { foreach (var ar in arr) { long Id = Convert.ToInt64(ar); await _repTemplate.UpdateAsync(x => x.Id == Id, x => new BookingOrderSeaeEdiTemplate { IsDeleted = true }); } } } #endregion } }