文件接收及推送

master
zhangxiaofeng 7 months ago
parent 8bc00d6fab
commit 71f5daf382

@ -7,6 +7,7 @@ using EntrustSettle.Model.Models;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json; using Newtonsoft.Json;
using RestSharp;
namespace EntrustSettle.Api.Controllers namespace EntrustSettle.Api.Controllers
{ {
@ -17,6 +18,8 @@ namespace EntrustSettle.Api.Controllers
public class OpenController : BaseApiController public class OpenController : BaseApiController
{ {
private readonly IOrderService orderService; private readonly IOrderService orderService;
private readonly IAnnexService annexService;
private readonly IOrderAnnexService orderAnnexService;
private readonly ILogger<OpenController> logger; private readonly ILogger<OpenController> logger;
private readonly IOrderHistoryService orderHistoryService; private readonly IOrderHistoryService orderHistoryService;
private readonly IQueueService queueService; private readonly IQueueService queueService;
@ -25,13 +28,17 @@ namespace EntrustSettle.Api.Controllers
ILogger<OpenController> logger, ILogger<OpenController> logger,
IOrderHistoryService orderHistoryService, IOrderHistoryService orderHistoryService,
IQueueService queueService, IQueueService queueService,
IOrderFeeService orderFeeService) IOrderFeeService orderFeeService,
IAnnexService annexService,
IOrderAnnexService orderAnnexService)
{ {
this.orderService = orderService; this.orderService = orderService;
this.logger = logger; this.logger = logger;
this.orderHistoryService = orderHistoryService; this.orderHistoryService = orderHistoryService;
this.queueService = queueService; this.queueService = queueService;
this.orderFeeService = orderFeeService; this.orderFeeService = orderFeeService;
this.annexService = annexService;
this.orderAnnexService = orderAnnexService;
} }
/// <summary> /// <summary>
@ -87,41 +94,90 @@ namespace EntrustSettle.Api.Controllers
/// </summary> /// </summary>
[HttpPost] [HttpPost]
[ApiUser(ApiCode = "PushOrderStatus")] [ApiUser(ApiCode = "PushOrderStatus")]
public async Task<MessageModel> PushOrderStatus([FromBody] HydQueryResultDto item) public async Task<MessageModel> PushOrderStatus([FromBody] HydQueryResultDto input)
{ {
var orderList = await orderService.Query(x => x.Bsno == item.id); var orderList = await orderService.Query(x => x.Bsno == input.id);
if (orderList == null || orderList.Count == 0) if (orderList == null || orderList.Count == 0)
{ {
string msg = $"订单Id[{item.id}]接收到回推后未查询到本地订单"; string msg = $"订单Id[{input.id}]接收到回推后未查询到本地订单";
logger.LogInformation(msg); logger.LogInformation(msg);
return FailedMsg(msg); return FailedMsg(msg);
} }
if (orderList.Count > 1) if (orderList.Count > 1)
{ {
string msg = $"订单Id[{item.id}]接收到回推后根据Id查询到多个本地订单{string.Join(',', orderList.Select(x => x.Mblno))}"; string msg = $"订单Id[{input.id}]接收到回推后根据Id查询到多个本地订单{string.Join(',', orderList.Select(x => x.Mblno))}";
logger.LogInformation(msg); logger.LogInformation(msg);
return FailedMsg(msg); return FailedMsg(msg);
} }
var order = orderList[0]; var order = orderList[0];
if (order.Status == item.status)
{
string msg = $"订单Id[{item.id}],提单号[{order.Mblno}]状态不变";
logger.LogInformation(msg);
return SuccessMsg(msg);
}
order.Status = item.status;
var updateSuccess = await orderService.Update(order, x => new { x.Status });
logger.LogInformation($"订单Id[{item.id}],提单号[{order.Mblno}]状态更新{(updateSuccess ? "" : "")}status:{item.status}");
// 记录订单状态变更历史 // 保存往来单据
await orderHistoryService.Add(new OrderHistory() if (input.feedback != null && input.feedback.Count > 0)
{ {
Pid = order.Id, var outerFileIdList = input.feedback.Select(x => x.id).ToList();
Status = item.status,
StatusTime = DateTime.Now, var existsOuterFileIdList = await orderAnnexService.AsQueryable()
CreateBy = "系统", .LeftJoin<Annex>((o, a) => o.AnnexId == a.Id)
Remark = "(状态接收)" .Where((o, a) => o.OrderId == order.Id && outerFileIdList.Contains((long)a.OuterFileId))
}); .Select((o, a) => a.OuterFileId)
.ToListAsync();
foreach (var item in input.feedback)
{
// 如果文件不是外部推送的,不处理
if (item.sendType != 1)
continue;
// 如果文件信息已经存在,不处理
if (existsOuterFileIdList.Contains(item.id))
continue;
if (!string.IsNullOrEmpty(item.fileUrl))
{
// 检查文件保存目录
var dir = Path.Combine(App.WebHostEnvironment.WebRootPath, "files");
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
// 文件名
var newFileName = Guid.NewGuid().ToString("N") + Path.GetExtension(item.fileName);
// 相对路径
var relativePath = Path.Combine("files", newFileName);
// 完整路径
var fullPath = Path.Combine(dir, newFileName);
// 保存附件信息
var annex = new Annex()
{
Type = 5,
OuterFileId = item.id,
Name = item.fileName,
Remark = item.remark,
BusinessTime = item.createTime,
Path = relativePath
};
await annexService.Add(annex);
var orderAnnex = new OrderAnnex()
{
OrderId = order.Id,
AnnexId = annex.Id
};
await orderAnnexService.Add(orderAnnex);
var restClient = new RestClient(item.fileUrl);
var request = new RestRequest();
using var stream = await restClient.DownloadStreamAsync(request);
using FileStream fileStream = new FileStream(fullPath, FileMode.Create);
await stream.CopyToAsync(fileStream);
}
}
}
// 将更新后的状态及费用推送到消息队列 // 将更新后的状态及费用推送到消息队列
if (AppSettings.app("RabbitMQ", "Enabled").ObjToBool()) if (AppSettings.app("RabbitMQ", "Enabled").ObjToBool())
@ -138,8 +194,8 @@ namespace EntrustSettle.Api.Controllers
MessageType = 1, MessageType = 1,
MessageDesc = "状态更新推送", MessageDesc = "状态更新推送",
Remark = "", Remark = "",
Status = item.status, Status = input.status,
StatusDesc = item.status switch StatusDesc = input.status switch
{ {
0 => "已下单", 0 => "已下单",
1 => "已接单", 1 => "已接单",
@ -159,18 +215,55 @@ namespace EntrustSettle.Api.Controllers
FeeAmount = x.Amount FeeAmount = x.Amount
}).ToList(); }).ToList();
} }
var annexList = await orderAnnexService.AsQueryable()
.LeftJoin<Annex>((o, a) => o.AnnexId == a.Id)
.Where((o, a) => o.OrderId == order.Id && a.Type == 5)
.Select((o, a) => a)
.ToListAsync();
if (annexList.Count > 0)
{
pushDto.FeebackAnnexList = annexList.Select(x => new StatusPushDto.FeebackAnnex()
{
Id = x.Id,
BusinessTime = x.BusinessTime,
FileName = x.Name,
Remark = x.Remark
}).ToList();
}
var json = JsonConvert.SerializeObject(pushDto, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); var json = JsonConvert.SerializeObject(pushDto, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
queueService.Push(msg, order.CompanyId, json); queueService.Push(msg, order.CompanyId, json);
} }
catch (Exception ex) catch (Exception ex)
{ {
logger.LogError(ex, $"订单Id[{item.id}],提单号[{order.Mblno}]接收到状态然后推送时发生未知异常:{msg}"); logger.LogError(ex, $"订单Id[{input.id}],提单号[{order.Mblno}]接收到状态然后推送时发生未知异常:{msg}");
} }
}); });
} }
return SuccessMsg($"订单Id[{item.id}],提单号[{order.Mblno}]状态更新成功"); if (order.Status != input.status)
{
order.Status = input.status;
var updateSuccess = await orderService.Update(order, x => new { x.Status });
logger.LogInformation($"订单Id[{input.id}],提单号[{order.Mblno}]状态更新{(updateSuccess ? "" : "")}status:{input.status}");
// 记录订单状态变更历史
await orderHistoryService.Add(new OrderHistory()
{
Pid = order.Id,
Status = input.status,
StatusTime = DateTime.Now,
CreateBy = "系统",
Remark = "(状态接收)"
});
return SuccessMsg($"订单Id[{input.id}],提单号[{order.Mblno}]状态更新成功");
}
else
{
string msg = $"订单Id[{input.id}],提单号[{order.Mblno}]状态不变";
logger.LogInformation(msg);
return SuccessMsg(msg);
}
} }
} }
} }

@ -230,7 +230,7 @@ namespace EntrustSettle.Api.Controllers
// 构建提交数据 // 构建提交数据
string fileNameStr = string.Join(",", annexList.Select(x => x.Name)); string fileNameStr = string.Join(",", annexList.Select(x => x.Name));
string fileUrl = string.Join(",", annexList.Select(x => $"{domainUrl}/api/Annex/get?key={x.Key}")); string fileUrl = string.Join(",", annexList.Select(x => $"{domainUrl}/api/Annex/Download?key={x.Key}"));
string remark = "备注:客户所选服务:"; string remark = "备注:客户所选服务:";
var projects = orderItem.ProjectType.Split(","); var projects = orderItem.ProjectType.Split(",");
@ -492,6 +492,21 @@ namespace EntrustSettle.Api.Controllers
FeeAmount = x.Amount FeeAmount = x.Amount
}).ToList(); }).ToList();
} }
var annexList = await orderAnnexService.AsQueryable()
.LeftJoin<Annex>((o, a) => o.AnnexId == a.Id)
.Where((o, a) => o.OrderId == order.Id && a.Type == 5)
.Select((o, a) => a)
.ToListAsync();
if (annexList.Count > 0)
{
pushDto.FeebackAnnexList = annexList.Select(x => new StatusPushDto.FeebackAnnex()
{
Id = x.Id,
BusinessTime = x.BusinessTime,
FileName = x.Name,
Remark = x.Remark
}).ToList();
}
//if (changeStatusDto.FeeList != null && changeStatusDto.FeeList.Count > 0) //if (changeStatusDto.FeeList != null && changeStatusDto.FeeList.Count > 0)
//{ //{
// pushDto.FeeList = changeStatusDto.FeeList.Select(x => new StatusPushDto.FeeDto() // pushDto.FeeList = changeStatusDto.FeeList.Select(x => new StatusPushDto.FeeDto()
@ -502,7 +517,10 @@ namespace EntrustSettle.Api.Controllers
// }).ToList(); // }).ToList();
//} //}
var json = JsonConvert.SerializeObject(pushDto, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); var json = JsonConvert.SerializeObject(pushDto, new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
});
queueService.Push(msg, order.CompanyId, json); queueService.Push(msg, order.CompanyId, json);
} }
catch (Exception ex) catch (Exception ex)
@ -549,7 +567,7 @@ namespace EntrustSettle.Api.Controllers
var annexList = await annexService.Query(x => bindDto.AnnexIdList.Contains(x.Id)); var annexList = await annexService.Query(x => bindDto.AnnexIdList.Contains(x.Id));
foreach (Annex item in annexList) foreach (Annex item in annexList)
{ {
string fileUrl = string.Join(",", annexList.Select(x => $"{domainUrl}/api/Annex/get?key={x.Key}")); string fileUrl = string.Join(",", annexList.Select(x => $"{domainUrl}/api/Annex/Download?key={x.Key}"));
hydFeedbackDtoList.Add(new HydFeedbackDto() hydFeedbackDtoList.Add(new HydFeedbackDto()
{ {
@ -655,7 +673,10 @@ namespace EntrustSettle.Api.Controllers
pushDto.MailBillNo = bindDto.MailBillNo; pushDto.MailBillNo = bindDto.MailBillNo;
} }
var json = JsonConvert.SerializeObject(pushDto, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); var json = JsonConvert.SerializeObject(pushDto, new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
});
queueService.Push(msg, order.CompanyId, json); queueService.Push(msg, order.CompanyId, json);
} }
catch (Exception ex) catch (Exception ex)
@ -685,7 +706,7 @@ namespace EntrustSettle.Api.Controllers
} }
var annexList = await orderAnnexService.Db.Queryable<OrderAnnex, Annex>((o, a) => o.AnnexId == a.Id) var annexList = await orderAnnexService.Db.Queryable<OrderAnnex, Annex>((o, a) => o.AnnexId == a.Id)
.Where((o, a) => o.OrderId == id) .Where((o, a) => o.OrderId == id)
.Where((o, a) => a.Type == (int)FileTypeEnum. || a.Type == (int)FileTypeEnum.) .Where((o, a) => a.Type == (int)FileTypeEnum. || a.Type == (int)FileTypeEnum. || a.Type == (int)FileTypeEnum.)
.Select((o, a) => new AnnexDto() .Select((o, a) => new AnnexDto()
{ {
Id = a.Id, Id = a.Id,

@ -137,6 +137,18 @@ internal class Program
//options.SerializerSettings.Converters.Add(new NumberConverter(NumberConverterShip.Int64)); //options.SerializerSettings.Converters.Add(new NumberConverter(NumberConverterShip.Int64));
}); });
JsonSerializerSettings setting = new JsonSerializerSettings();
JsonConvert.DefaultSettings = new Func<JsonSerializerSettings>(() =>
{
//日期类型默认格式化处理
setting.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
setting.DateFormatString = "yyyy-MM-dd HH:mm:ss";
//空值处理
//setting.NullValueHandling = NullValueHandling.Ignore;
return setting;
});
//builder.Services.AddRabbitMQSetup(); 未采用注入的方式而是直接在需要的地方new //builder.Services.AddRabbitMQSetup(); 未采用注入的方式而是直接在需要的地方new
builder.Services.AddKafkaSetup(builder.Configuration); builder.Services.AddKafkaSetup(builder.Configuration);
builder.Services.AddEventBusSetup(); builder.Services.AddEventBusSetup();

@ -30,6 +30,7 @@ namespace EntrustSettle.Model
= 1, = 1,
= 2, = 2,
= 3, = 3,
= 4 = 4,
= 5
} }
} }

@ -1,11 +1,40 @@
namespace EntrustSettle.Model.Dtos using System;
using System.Collections.Generic;
namespace EntrustSettle.Model.Dtos
{ {
public class HydQueryResultDto public class HydQueryResultDto
{ {
public long id { get; set; } public long id { get; set; }
public string billNo { get; set; } public string billNo { get; set; }
public int status { get; set; } public int status { get; set; }
//public string fileName { get; set; } public List<Feedback> feedback { get; set; }
//public string fileUrl { get; set; } public class Feedback
{
/// <summary>
/// 1=海运达推送的文件
/// </summary>
public int sendType { get; set; }
/// <summary>
/// 文件id
/// </summary>
public long id { get; set; }
/// <summary>
/// 反馈信息上传附件地址
/// </summary>
public string fileUrl { get; set; }
/// <summary>
/// 反馈信息上传附件文件名
/// </summary>
public string fileName { get; set; }
/// <summary>
/// 反馈信息备注
/// </summary>
public string remark { get; set; }
/// <summary>
/// 反馈信息添加时间
/// </summary>
public DateTime? createTime { get; set; }
}
} }
} }

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
namespace EntrustSettle.Model.Dtos namespace EntrustSettle.Model.Dtos
{ {
@ -46,5 +47,33 @@ namespace EntrustSettle.Model.Dtos
/// </summary> /// </summary>
public decimal FeeAmount { get; set; } public decimal FeeAmount { get; set; }
} }
/// <summary>
/// 海运达往来单据列表
/// </summary>
public List<FeebackAnnex> FeebackAnnexList { get; set; }
/// <summary>
/// 海运达往来单据
/// </summary>
public class FeebackAnnex
{
/// <summary>
/// 附件Id
/// </summary>
public long Id { get; set; }
/// <summary>
/// 附件名称
/// </summary>
public string FileName { get; set; }
/// <summary>
/// 附件备注
/// </summary>
public string Remark { get; set; }
/// <summary>
/// 附件添加时间
/// </summary>
public DateTime? BusinessTime { get; set; }
}
} }
} }

@ -1,4 +1,5 @@
using SqlSugar; using SqlSugar;
using System;
namespace EntrustSettle.Model.Models namespace EntrustSettle.Model.Models
{ {
@ -17,10 +18,22 @@ namespace EntrustSettle.Model.Models
/// </summary> /// </summary>
public string Path { get; set; } public string Path { get; set; }
/// <summary> /// <summary>
/// 附件类型 1:原始附件 2:反馈附件 3:账单 4:发票 /// 附件类型 1:原始附件 2:反馈附件 3:账单 4:发票 5:外部往来单据
/// </summary> /// </summary>
public int Type { get; set; } public int Type { get; set; }
/// <summary> /// <summary>
/// 外部系统的备注(文件类型为外部往来单据时会使用此字段)
/// </summary>
public string Remark { get; set; }
/// <summary>
/// 外部系统的业务发生时间(文件类型为外部往来单据时会使用此字段)
/// </summary>
public DateTime? BusinessTime { get; set; }
/// <summary>
/// 外部系统的文件Id文件类型为外部往来单据时会使用此字段
/// </summary>
public long? OuterFileId { get; set; }
/// <summary>
/// 用于外部文件下载时确认文件及验证 /// 用于外部文件下载时确认文件及验证
/// </summary> /// </summary>
public string Key { get; set; } public string Key { get; set; }

@ -44,7 +44,7 @@ namespace EntrustSettle.Services
conn.Close(); conn.Close();
logger.LogInformation($"{opType},已发送数据到消息队列,数据内容:【{data}】"); logger.LogInformation($"{opType},已发送数据到消息队列,队列名称:【{queueName}】,数据内容:【{data}】");
} }
} }
catch (Exception) catch (Exception)

@ -76,7 +76,7 @@ namespace EntrustSettle.Tasks
// 构建提交数据 // 构建提交数据
string fileNameStr = string.Join(",", annexList.Select(x => x.Name)); string fileNameStr = string.Join(",", annexList.Select(x => x.Name));
string fileUrl = string.Join(",", annexList.Select(x => $"{domainUrl}/api/Annex/get?key={x.Key}")); string fileUrl = string.Join(",", annexList.Select(x => $"{domainUrl}/api/Annex/Download?key={x.Key}"));
string remark = "备注:客户所选服务:"; string remark = "备注:客户所选服务:";
var projects = orderItem.ProjectType.Split(","); var projects = orderItem.ProjectType.Split(",");

Loading…
Cancel
Save