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.

429 lines
20 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Myshipping.Core;
using Myshipping.Core.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Myshipping.Application.Entity;
using Furion.FriendlyException;
using Newtonsoft.Json.Linq;
using Furion.RemoteRequest.Extensions;
using System.Net.Http;
using Myshipping.Application.Service.BookingYunZong.Dto;
using Myshipping.Core.Entity;
namespace Myshipping.Application
{
/// <summary>
/// 运踪模块
/// </summary>
[ApiDescriptionSettings("Application", Name = "BookingYunZong", Order = 1)]
public class BookingYunZongService : IBookingYunZong, IDynamicApiController, ITransient
{
private readonly ILogger<BookingYunZongService> _logger;
private readonly ISysCacheService _cache;
private readonly IDjyWebsiteAccountConfigService _account;
private readonly SqlSugarRepository<BookingOrder> _rep;
private readonly SqlSugarRepository<BookingStatusLog> _repStatuslog;
private readonly SqlSugarRepository<BookingStatusLogDetail> _statuslogdetail;
private readonly SqlSugarRepository<BookingStatus> _repBookingStatus;
private readonly IBookingOrderService _bookingorderservice;
public BookingYunZongService(ILogger<BookingYunZongService> logger, ISysCacheService cache, IDjyWebsiteAccountConfigService account, SqlSugarRepository<BookingStatus> repBookingStatus,
SqlSugarRepository<BookingOrder> rep, SqlSugarRepository<BookingStatusLog> repStatuslog, SqlSugarRepository<BookingStatusLogDetail> statuslogdetail, IBookingOrderService bookingorderservice)
{
_cache = cache;
_account = account;
this._logger = logger;
_rep = rep;
repStatuslog = _repStatuslog;
_statuslogdetail = statuslogdetail;
_repBookingStatus = repBookingStatus;
_bookingorderservice = bookingorderservice;
}
/// <summary>
/// 刷新运踪
/// </summary>
/// <param name="Ids"></param>
/// <returns></returns>
public async Task RefreshYunZong(long[] Ids)
{
var order = await _rep.AsQueryable().Filter(null, true).Where(x => Ids.Contains(x.Id)).ToListAsync();
var key = _cache.GetAllSysConfig().Result.Where(x => x.Code == "billTraceNewUserKey").Select(x => x.Value).FirstOrDefault();
if (string.IsNullOrEmpty(key))
{
throw Oops.Bah("未获取到相关KEY请联系管理员");
}
var url = _cache.GetAllDictData().Result.Where(x => x.Code == "billTraceYgtUrl").Select(x => x.Value).FirstOrDefault();
var secret = _cache.GetAllSysConfig().Result.Where(x => x.Code == "billTraceNewUserSecret").Select(x => x.Value).FirstOrDefault();
if (string.IsNullOrEmpty(secret))
{
throw Oops.Bah("未获取到相关SECRET请联系管理员");
}
JObject jobjSend = new JObject();
jobjSend.Add("user_key", new JValue(key));
jobjSend.Add("user_secret", new JValue(secret));
jobjSend.Add("customer_id", new JValue(UserManager.TENANT_ID));
jobjSend.Add("customer_name", new JValue(UserManager.TENANT_NAME));
JArray jarrCZTemos = new JArray();
JArray jarrHLWTemos = new JArray();
JArray jarrYGTTemos = new JArray();
foreach (var item in order)
{
if (!string.IsNullOrEmpty(item.YARDID) && !string.IsNullOrEmpty(item.MBLNO))
{
JObject jobjczTemp = new JObject();
jobjczTemp.Add("web_code", new JValue(item.YARDID));
jobjczTemp.Add("bno", new JValue(item.MBLNO.Trim()));
jarrCZTemos.Add(jobjczTemp);
}
JObject jobjhlwTemp = new JObject();
jobjhlwTemp.Add("web_code", "HLW_CD");
jobjhlwTemp.Add("bno", new JValue(item.MBLNO.Trim()));
jarrHLWTemos.Add(jobjhlwTemp);
JObject jobjygtTemp = new JObject();
jobjygtTemp.Add("web_code", "YGT_YZ");
jobjygtTemp.Add("bno", new JValue(item.MBLNO.Trim()));
jarrYGTTemos.Add(jobjygtTemp);
}
if (jarrCZTemos.Count > 0)
{
jobjSend.Add("data_list", "");
jobjSend.Add("data_list", jarrCZTemos);
var json = jobjSend.ToJsonString();
_logger.LogInformation("运踪查询场站参数:" + json);
var rtn = await url.SetHttpMethod(HttpMethod.Post).SetBody(jobjSend.ToJsonString()).PostAsStringAsync();
_logger.LogInformation("运踪查询场站查询结果:" + rtn);
await ExecuteYard(rtn);
}
if (jarrHLWTemos.Count > 0)
{
jobjSend.Add("data_list", "");
jobjSend.Add("data_list", jarrHLWTemos);
var json = jobjSend.ToJsonString();
_logger.LogInformation("运踪查询HLW+参数:" + json);
var rtn = await url.SetHttpMethod(HttpMethod.Post).SetBody(jobjSend.ToJsonString()).PostAsStringAsync();
_logger.LogInformation("运踪查询HLW+查询结果:" + rtn);
await ExecuteHLW(rtn);
}
if (jarrYGTTemos.Count > 0)
{
jobjSend.Add("data_list", "");
jobjSend.Add("data_list", jarrYGTTemos);
var json = jobjSend.ToJsonString();
_logger.LogInformation("运踪查询YGT参数" + json);
var rtn = await url.SetHttpMethod(HttpMethod.Post).SetBody(jobjSend.ToJsonString()).PostAsStringAsync();
_logger.LogInformation("运踪查询YGT查询结果" + rtn);
await ExecuteYGT(rtn);
}
}
[SqlSugarUnitOfWork]
public async Task ExecuteYard(string rtn)
{
JObject jobjRtn = JObject.Parse(rtn);
var code = jobjRtn.GetValue("code").ToString();
var msg = jobjRtn.GetValue("msg").ToString();
if (code != "200")
{
_logger.LogError($"请求爬虫获取【场站数据】出错:{msg}");
return;
}
JArray jarrData = jobjRtn.GetValue("data") as JArray;
foreach (JObject item in jarrData)
{
//每票 货物
var mblno = item.GetValue("bno").ToString();
var bookingId = await _rep.AsQueryable().Filter(null, true).Where(x => x.MBLNO == mblno).Select(x => x.Id).FirstAsync();
var jarrInnerData = item.GetValue("data") as JArray;
List<YardDataDto> yarddto = new List<YardDataDto>();
foreach (JObject dataItem in jarrInnerData)
{
var CNTRNO = dataItem.GetStringValue("CNTRNO"); //箱号
var TiXiangShiJian = dataItem.GetDateTimeValue("TiXiangShiJian"); //提箱时间
var FanChangShiJian = dataItem.GetDateTimeValue("FanChangShiJian"); //返厂时间
var XiangLiang = dataItem.GetIntValue("XiangLiang"); //箱量
yarddto.Add(new YardDataDto
{
CNTRNO = CNTRNO,
TiXiangShiJian = TiXiangShiJian,
FanChangShiJian = FanChangShiJian,
XiangLiang = XiangLiang,
});
}
var xl = yarddto.Select(x => x.XiangLiang).FirstOrDefault();
var tinum = yarddto.Where(x => x.TiXiangShiJian != null).Count();
var fcnum = yarddto.Where(x => x.FanChangShiJian != null).Count();
var old = await _repStatuslog.AsQueryable().Filter(null, true).Where(x => x.BookingId == bookingId && x.Category == "yunzong" && x.IsDeleted == false && (x.Status == "提箱" || x.Status == "返场")).ToListAsync();
await _repStatuslog.DeleteAsync(x => x.BookingId == bookingId && x.Category == "yunzong" && (x.Status == "提箱" || x.Status == "返场"));
foreach (var ot in old)
{
await _statuslogdetail.DeleteAsync(x => x.PId == ot.Id);
}
if (xl > 0 && tinum > 0)
{
//新增数据
var bookingStatusLog = new BookingStatusLog();
bookingStatusLog.BookingId = bookingId;
bookingStatusLog.Category = "yunzong";
bookingStatusLog.CreatedTime = DateTime.Now;
bookingStatusLog.Status = "提箱";
bookingStatusLog.OpTime = xl == tinum ? yarddto.Select(x => x.TiXiangShiJian).Max() : null;
bookingStatusLog.MBLNO = mblno;
await _repStatuslog.InsertAsync(bookingStatusLog);
foreach (var dt in yarddto)
{
var BookingStatusLogDetail = new BookingStatusLogDetail();
BookingStatusLogDetail.PId = bookingStatusLog.Id;
BookingStatusLogDetail.Status = "提箱";
BookingStatusLogDetail.CNTRNO = dt.CNTRNO;
BookingStatusLogDetail.OPTime = dt.TiXiangShiJian;
await _statuslogdetail.InsertAsync(BookingStatusLogDetail);
}
}
if (xl > 0 && fcnum > 0)
{
//新增数据
var bookingStatusLog = new BookingStatusLog();
bookingStatusLog.BookingId = bookingId;
bookingStatusLog.Category = "yunzong";
bookingStatusLog.CreatedTime = DateTime.Now;
bookingStatusLog.Status = "返场";
bookingStatusLog.OpTime = xl == tinum ? yarddto.Select(x => x.FanChangShiJian).Max() : null;
bookingStatusLog.MBLNO = mblno;
await _repStatuslog.InsertAsync(bookingStatusLog);
foreach (var dt in yarddto)
{
var BookingStatusLogDetail = new BookingStatusLogDetail();
BookingStatusLogDetail.PId = bookingStatusLog.Id;
BookingStatusLogDetail.Status = "返场";
BookingStatusLogDetail.CNTRNO = dt.CNTRNO;
BookingStatusLogDetail.OPTime = dt.FanChangShiJian;
await _statuslogdetail.InsertAsync(BookingStatusLogDetail);
}
}
}
}
[SqlSugarUnitOfWork]
public async Task ExecuteHLW(string rtn)
{
JObject jobjRtn = JObject.Parse(rtn);
var code = jobjRtn.GetValue("code").ToString();
var msg = jobjRtn.GetValue("msg").ToString();
if (code != "200")
{
_logger.LogError($"请求爬虫获取【HLW+数据】出错:{msg}");
return;
}
var dicdatalist = _cache.GetAllDictData().Result;
JArray jarrData = jobjRtn.GetValue("data") as JArray;
foreach (JObject item in jarrData)
{
//每票 货物
var mblno = item.GetValue("bno").ToString();
var bookingId = await _rep.AsQueryable().Filter(null, true).Where(x => x.MBLNO == mblno).Select(x => x.Id).FirstAsync();
var list = await _repBookingStatus.AsQueryable().Filter(null, true).InnerJoin<SysDictData>((d, t) => d.StaCode == t.Code && d.StaCate == "book_sta_cate_billtrace" && d.BookingId == bookingId).Select((d, t) => new
{
BookingId = d.BookingId,
StaCode = d.StaCode,
StaName = d.StaName,
StaTime = d.StaTime,
Code = t.Code,
Value = t.Value
}).ToListAsync();
var jarrInnerData = item.GetValue("data") as JArray;
List<YardDataDto> yarddto = new List<YardDataDto>();
DateTime? TimeCangDan = null;
DateTime? FangXingShiJian = null;
foreach (JObject dataItem in jarrInnerData)
{
TimeCangDan = dataItem.GetDateTimeValue("YuPeiShiJian"); //舱单时间
FangXingShiJian = dataItem.GetDateTimeValue("FangXingShiJian"); //放行时间
}
await _repStatuslog.DeleteAsync(x => x.BookingId == bookingId && x.Category == "yunzong" && (x.Status == "舱单" || x.Status == "报关"));
if (TimeCangDan != null)
{
//新增数据
var bookingStatusLog = new BookingStatusLog();
bookingStatusLog.BookingId = bookingId;
bookingStatusLog.Category = "yunzong";
bookingStatusLog.CreatedTime = DateTime.Now;
bookingStatusLog.Status = "舱单";
bookingStatusLog.OpTime = TimeCangDan;
bookingStatusLog.MBLNO = mblno;
await _repStatuslog.InsertAsync(bookingStatusLog);
if (list.Where(x => x.StaCode == "sta_cangdan").FirstOrDefault() == null)
{
BookingStatus bookingStatus = new BookingStatus();
bookingStatus.BookingId = bookingId;
bookingStatus.StaCode = "sta_cangdan";
bookingStatus.StaName = dicdatalist.Where(x => x.Code == "sta_cangdan").Select(x => x.Value).FirstOrDefault();
bookingStatus.StaTime = TimeCangDan;
bookingStatus.StaCate = "book_sta_cate_billtrace";
await _repBookingStatus.InsertAsync(bookingStatus);
}
}
if (FangXingShiJian != null)
{
//新增数据
var bookingStatusLog = new BookingStatusLog();
bookingStatusLog.BookingId = bookingId;
bookingStatusLog.Category = "yunzong";
bookingStatusLog.CreatedTime = DateTime.Now;
bookingStatusLog.Status = "报关";
bookingStatusLog.OpTime = FangXingShiJian;
bookingStatusLog.MBLNO = mblno;
await _repStatuslog.InsertAsync(bookingStatusLog);
if (list.Where(x => x.Code == "sta_haifang").FirstOrDefault() == null)
{
BookingStatus bookingStatus = new BookingStatus();
bookingStatus.BookingId = bookingId;
bookingStatus.StaCode = "sta_haifang";
bookingStatus.StaName = dicdatalist.Where(x => x.Code == "sta_haifang").Select(x => x.Value).FirstOrDefault();
bookingStatus.StaTime = FangXingShiJian;
bookingStatus.StaCate = "book_sta_cate_billtrace";
await _repBookingStatus.InsertAsync(bookingStatus);
await _bookingorderservice.SetGoodsStatus("BG", bookingId);
await _bookingorderservice.SendBookingOrder(new long[] { bookingId });
}
}
}
}
[SqlSugarUnitOfWork]
public async Task ExecuteYGT(string rtn)
{
JObject jobjRtn = JObject.Parse(rtn);
var code = jobjRtn.GetValue("code").ToString();
var msg = jobjRtn.GetValue("msg").ToString();
if (code != "200")
{
_logger.LogError($"请求爬虫获取【YGT数据】出错{msg}");
return;
}
var dicdatalist = _cache.GetAllDictData().Result;
JArray jarrData = jobjRtn.GetValue("data") as JArray;
foreach (JObject item in jarrData)
{
//每票 货物
var mblno = item.GetValue("bno").ToString();
var bookingId = await _rep.AsQueryable().Filter(null, true).Where(x => x.MBLNO == mblno).Select(x => x.Id).FirstAsync();
var list = await _repBookingStatus.AsQueryable().Filter(null, true).InnerJoin<SysDictData>((d, t) => d.StaCode == t.Code && d.StaCate == "book_sta_cate_billtrace" && d.BookingId == bookingId).Select((d, t) => new
{
BookingId = d.BookingId,
StaCode = d.StaCode,
StaName = d.StaName,
StaTime = d.StaTime,
Code = t.Code,
Value = t.Value
}).ToListAsync();
var jarrInnerData = item.GetValue("data") as JArray;
List<YardDataDto> yarddto = new List<YardDataDto>();
DateTime? TimeCangDan = null;
DateTime? FangXingShiJian = null;
foreach (JObject dataItem in jarrInnerData)
{
TimeCangDan = dataItem.GetDateTimeValue("YuPeiShiJian"); //舱单时间
FangXingShiJian = dataItem.GetDateTimeValue("FangXingShiJian"); //放行时间
}
await _repStatuslog.DeleteAsync(x => x.BookingId == bookingId && x.Category == "yunzong" && (x.Status == "舱单" || x.Status == "报关"));
if (TimeCangDan != null)
{
//新增数据
var bookingStatusLog = new BookingStatusLog();
bookingStatusLog.BookingId = bookingId;
bookingStatusLog.Category = "yunzong";
bookingStatusLog.CreatedTime = DateTime.Now;
bookingStatusLog.Status = "舱单";
bookingStatusLog.OpTime = TimeCangDan;
bookingStatusLog.MBLNO = mblno;
await _repStatuslog.InsertAsync(bookingStatusLog);
if (list.Where(x => x.StaCode == "sta_cangdan").FirstOrDefault() == null)
{
BookingStatus bookingStatus = new BookingStatus();
bookingStatus.BookingId = bookingId;
bookingStatus.StaCode = "sta_cangdan";
bookingStatus.StaName = dicdatalist.Where(x => x.Code == "sta_cangdan").Select(x => x.Value).FirstOrDefault();
bookingStatus.StaTime = TimeCangDan;
bookingStatus.StaCate = "book_sta_cate_billtrace";
await _repBookingStatus.InsertAsync(bookingStatus);
}
}
if (FangXingShiJian != null)
{
//新增数据
var bookingStatusLog = new BookingStatusLog();
bookingStatusLog.BookingId = bookingId;
bookingStatusLog.Category = "yunzong";
bookingStatusLog.CreatedTime = DateTime.Now;
bookingStatusLog.Status = "报关";
bookingStatusLog.OpTime = FangXingShiJian;
bookingStatusLog.MBLNO = mblno;
await _repStatuslog.InsertAsync(bookingStatusLog);
if (list.Where(x => x.Code == "sta_haifang").FirstOrDefault() == null)
{
BookingStatus bookingStatus = new BookingStatus();
bookingStatus.BookingId = bookingId;
bookingStatus.StaCode = "sta_haifang";
bookingStatus.StaName = dicdatalist.Where(x => x.Code == "sta_haifang").Select(x => x.Value).FirstOrDefault();
bookingStatus.StaTime = FangXingShiJian;
bookingStatus.StaCate = "book_sta_cate_billtrace";
await _repBookingStatus.InsertAsync(bookingStatus);
await _bookingorderservice.SetGoodsStatus("BG", bookingId);
await _bookingorderservice.SendBookingOrder(new long[] { bookingId });
}
}
}
}
}
}