MpsChanelOrderController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. 
  2. using Microsoft.AspNetCore.Mvc;
  3. using XYY.Core.Standard.Mvc;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using XYY.Service.Standard.Order;
  9. using XYY.Core.Standard.ApiClient;
  10. using Microsoft.Extensions.Logging;
  11. using XYY.Model.Standard.Channel;
  12. using XYY.Model.Standard.MpsOrder;
  13. using XYY.Model.Standard.Order;
  14. namespace XYY.Api.Order.Controllers.MoreThanOneTicketMps
  15. {
  16. [Route("api/[controller]/[action]")]
  17. [ApiController]
  18. public class MpsChanelOrderController : ApiControllerBase
  19. {
  20. IMoreThanOneTicketService mpsOrderService;
  21. IOrderService _orderService;
  22. XYY.Core.Standard.AliYun.IAliYunPostFileSerivce _aliYunPostFileSerivce;
  23. ILogger<HttpApiClient> Looger;
  24. public MpsChanelOrderController(IMoreThanOneTicketService orderService,
  25. ILogger<HttpApiClient> looger,
  26. IOrderService OrderService, Core.Standard.AliYun.IAliYunPostFileSerivce aliYunPostFileSerivce)
  27. {
  28. this.mpsOrderService = orderService;
  29. Looger = looger;
  30. this._orderService = OrderService;
  31. _aliYunPostFileSerivce = aliYunPostFileSerivce;
  32. }
  33. public class tempRequest
  34. {
  35. public int[] orderIds { get; set; }
  36. public int channelId { get; set; }
  37. }
  38. public async Task<ApiJsonModel> MergePost(tempRequest tempRequest)
  39. {
  40. Order_MpsOrder mps = new Order_MpsOrder();
  41. mps.Boxs = new List<Order_MpsOrderBox>();
  42. List<Order_MpsOrder> mpsList = new List<Order_MpsOrder>();
  43. int weight = 0;
  44. foreach (var orderId in tempRequest.orderIds)
  45. {
  46. var mpsItem = await mpsOrderService.Get(orderId, 0);
  47. mpsList.Add(mpsItem);
  48. mps.Boxs.AddRange(mpsItem.Boxs);
  49. mps.BoxQty += mpsItem.Boxs.Count;
  50. mps.Weight += mpsItem.Weight;
  51. mps.ChargedWeight += mpsItem.ChargedWeight;
  52. var o = await _orderService.Get(orderId);
  53. weight += o.OPWeight;
  54. }
  55. var gqty = mpsList.GroupBy(x => new
  56. {
  57. x.ReceiverCountry,
  58. x.ReceiverCity,
  59. x.ReceiverState,
  60. x.ReceiverName,
  61. x.ReceiverZipCode,
  62. x.ReceiverStreet1,
  63. x.ReceiverStreet2,
  64. x.ReceiverPhone,
  65. x.ChannelId,
  66. x.Electric,
  67. x.Liquid,
  68. x.DeclarationMethod
  69. }).Count();
  70. if (gqty != 1)
  71. {
  72. //throw new Exception("当前订单无法合并,请检查国家、州名、城市名、收件人姓名、收件人邮编、收件人地址、收件人电话、下单渠道、是否带电、是否带磁、是否危险品、报关方式完全一致。");
  73. }
  74. mps.CustomerOrderNumber = String.Join('_', mpsList.Select(x => x.CustomerOrderNumber));
  75. mps.OrderId = mpsList.First().OrderId;
  76. try
  77. {
  78. if (mps.Boxs.Any(x => x.BoxOutStatus == XYY.Model.Standard.MpsOrder.BoxOutStatus.Default) || mps.Boxs.All(x => x.BoxOutStatus == XYY.Model.Standard.MpsOrder.BoxOutStatus.Back))
  79. {
  80. throw new Exception("当前订单有未操作完成的箱子或全部退回的箱子,不可发货");
  81. }
  82. //增加箱子是否导入确认信息验证
  83. else if (mps.Boxs.Any(x => !x.ChannelWeight.HasValue || !x.ChannelLength.HasValue
  84. || !x.ChannelWidth.HasValue || !x.ChannelHeight.HasValue || x.ChannelWeight <= 0))
  85. {
  86. throw new Exception("当前订单有未导入末端下单信息的箱子,不可发货");
  87. }
  88. else
  89. {
  90. if (mps.OrderId == 0)
  91. throw new Exception($"订单{mps.TrackingNumber}未找到客户订单Id");
  92. var order = await _orderService.Get(mps.OrderId.Value);
  93. mps.Boxs = mps.Boxs.Where(x => x.BoxStatus != XYY.Model.Standard.MpsOrder.BoxStatus.Back).ToList();
  94. //全部操作完成了,我们根据渠道匹配规则标记订单和草稿订单的相关派送渠道
  95. //我们先调用原wms项目的匹配接口,之后再调整
  96. HttpApiClient client =
  97. new HttpApiClient(HttpApiClientHelper.WMSHost, HttpApiClientHelper.AdminToken, this.Looger);
  98. order.CustomerOrderNo = order.SystemNo; //mps.CustomerOrderNumber;//郑曼媛需求:末端下单使用xyy单号
  99. order.ChannelId = tempRequest.channelId;
  100. order.OPWeight = weight;
  101. order.CustomerId = weight;
  102. List<Logistics_Channel> channels = null;
  103. channels = await client.Post<List<Logistics_Channel>>("/api/BaseInfo/CacheGetAllChannel");
  104. var channel = channels.FirstOrDefault(x => x.Id == order.ChannelId);
  105. if (channel == null)
  106. throw new Exception($"在CacheGetAllChannel中找未到渠道Id{order.ChannelId}的渠道");
  107. if (channel.CanMPS)
  108. {
  109. //是否支持一票多件
  110. DataUploadRequest req = new DataUploadRequest
  111. {
  112. order = order,
  113. goods = null,
  114. channel = channel,
  115. arg = new ExtendArg
  116. {
  117. AgentIOSS = false,
  118. Order = mps
  119. }
  120. };
  121. FbaChannelInfoMiddleware(req.order, req.arg);
  122. var result = await _orderService.DataUpload(req.order, null, req.channel, req.arg);
  123. order.TrackingNumber = result.TrackingNumber;
  124. order.ChannelId = req.channel.Id;
  125. var result2 = await _orderService.DrawPdfLabel(result, req.channel, req.order, req.goods);
  126. if (result2 != null && result2.Length > 0)
  127. {
  128. using (System.IO.MemoryStream ms = new System.IO.MemoryStream(Convert.FromBase64String(result2)))
  129. {
  130. order.ServiceLabelPath = string.Empty;
  131. string aliFile = _aliYunPostFileSerivce.PostHttpFile(ms, order.TrackingNumber + ".pdf");
  132. order.ServiceLabelUrl = aliFile;
  133. //更新箱子信息
  134. mps.Boxs.ForEach(x =>
  135. {
  136. if (!result.MPSTrackings.ContainsKey(x.BoxServiceNumber))
  137. {
  138. throw new Exception($"保存一票多件下单箱子运单号失败,渠道API未返回箱子{x.BoxServiceNumber}实际运单号");
  139. }
  140. x.BoxTrackingNumber =
  141. result.MPSTrackings[x.BoxServiceNumber];
  142. });
  143. await mpsOrderService.UpdateBoxNumbser(result.MPSTrackings);
  144. }
  145. }
  146. //更新客户订单的末端订单信息
  147. int num = 1;//主要针对NZ 不返回箱号,还合并下单的处理
  148. foreach (var mo in mpsList)
  149. {
  150. Order_Order o = new Order_Order();
  151. //查找每箱的单号
  152. o.TrackingNumber = !string.IsNullOrEmpty(mo.Boxs.FirstOrDefault().BoxTrackingNumber) ? mo.Boxs.FirstOrDefault().BoxTrackingNumber : result.TrackingNumber + "-" + num.ToString();
  153. o.ChannelId = order.ChannelId;
  154. o.ServiceLabelPath = order.ServiceLabelPath;
  155. o.ServiceLabelUrl = order.ServiceLabelUrl;
  156. o.Id = mo.OrderId.Value;
  157. await _orderService.UpdateLastInfo(o);
  158. num++;
  159. }
  160. //await _orderService.UpdateLastInfo(order);
  161. return ApiJsonModel.OK();
  162. }
  163. else
  164. {
  165. throw new Exception("渠道不支持一票多件,暂示实现逻辑");
  166. }
  167. }
  168. }
  169. catch (Exception ex)
  170. {
  171. await mpsOrderService.UpdateDraftOrderErrorMessage(mps.DraftOrderId, ex.Message);//所有catch 异常信息记录
  172. throw;
  173. }
  174. }
  175. public async Task<ApiJsonModel> Post(int orderId, int channelId)
  176. {
  177. var mps = await mpsOrderService.Get(orderId, 0);
  178. try
  179. {
  180. if (mps.Boxs.Any(x => x.BoxOutStatus == XYY.Model.Standard.MpsOrder.BoxOutStatus.Default) || mps.Boxs.All(x => x.BoxOutStatus == XYY.Model.Standard.MpsOrder.BoxOutStatus.Back))
  181. {
  182. throw new Exception("当前订单有未操作完成的箱子或全部退回的箱子,不可发货");
  183. }
  184. //增加箱子是否导入确认信息验证
  185. else if (mps.Boxs.Any(x => !x.ChannelWeight.HasValue || !x.ChannelLength.HasValue
  186. || !x.ChannelWidth.HasValue || !x.ChannelHeight.HasValue || x.ChannelWeight <= 0))
  187. {
  188. throw new Exception("当前订单有未导入末端下单信息的箱子,不可发货");
  189. }
  190. else
  191. {
  192. if (mps.OrderId == 0)
  193. throw new Exception($"订单{mps.TrackingNumber}未找到客户订单Id");
  194. var order = await _orderService.Get(mps.OrderId.Value);
  195. mps.Boxs = mps.Boxs.Where(x => x.BoxStatus != XYY.Model.Standard.MpsOrder.BoxStatus.Back).ToList();
  196. //全部操作完成了,我们根据渠道匹配规则标记订单和草稿订单的相关派送渠道
  197. //我们先调用原wms项目的匹配接口,之后再调整
  198. HttpApiClient client =
  199. new HttpApiClient(HttpApiClientHelper.WMSHost, HttpApiClientHelper.AdminToken, this.Looger);
  200. order.ChannelId = channelId;
  201. //临时增加单号判断
  202. if (order.CustomerOrderNo == "PO96737890595")
  203. {
  204. order.CustomerOrderNo = "PO#9673789-0595";
  205. }
  206. else if (order.CustomerOrderNo == "PO96737890597")
  207. {
  208. order.CustomerOrderNo = "PO#9673789-0597";
  209. }
  210. else if (order.CustomerOrderNo == "PO96737760789")
  211. {
  212. order.CustomerOrderNo = "PO#9673776-0789";
  213. }
  214. else
  215. {
  216. order.CustomerOrderNo = order.SystemNo; //mps.CustomerOrderNumber;//郑曼媛需求:末端下单使用xyy单号
  217. }
  218. List<Logistics_Channel> channels = null;
  219. channels = await client.Post<List<Logistics_Channel>>("/api/BaseInfo/CacheGetAllChannel");
  220. var channel = channels.FirstOrDefault(x => x.Id == order.ChannelId);
  221. if (channel == null)
  222. throw new Exception($"在CacheGetAllChannel中找未到渠道Id{order.ChannelId}的渠道");
  223. if (channel.CanMPS)
  224. {
  225. //是否支持一票多件
  226. DataUploadRequest req = new DataUploadRequest
  227. {
  228. order = order,
  229. goods = null,
  230. channel = channel,
  231. arg = new ExtendArg
  232. {
  233. AgentIOSS = false,
  234. Order = mps
  235. }
  236. };
  237. FbaChannelInfoMiddleware(req.order, req.arg);
  238. var result = await _orderService.DataUpload(req.order, null, req.channel, req.arg);
  239. order.TrackingNumber = result.TrackingNumber;
  240. order.ChannelId = req.channel.Id;
  241. var result2 = await _orderService.DrawPdfLabel(result, req.channel, req.order, req.goods);
  242. if (result2 != null && result2.Length > 0)
  243. {
  244. using (System.IO.MemoryStream ms = new System.IO.MemoryStream(Convert.FromBase64String(result2)))
  245. {
  246. order.ServiceLabelPath = string.Empty;
  247. string aliFile = _aliYunPostFileSerivce.PostHttpFile(ms, order.TrackingNumber + ".pdf");
  248. order.ServiceLabelUrl = aliFile;
  249. //更新箱子信息
  250. mps.Boxs.ForEach(x =>
  251. {
  252. if (!result.MPSTrackings.ContainsKey(x.BoxServiceNumber))
  253. {
  254. throw new Exception($"保存一票多件下单箱子运单号失败,渠道API未返回箱子{x.BoxServiceNumber}实际运单号");
  255. }
  256. x.BoxTrackingNumber =
  257. result.MPSTrackings[x.BoxServiceNumber];
  258. });
  259. await mpsOrderService.UpdateBoxNumbser(result.MPSTrackings);
  260. }
  261. }
  262. //更新客户订单的末端订单信息
  263. await _orderService.UpdateLastInfo(order);
  264. return ApiJsonModel.OK();
  265. }
  266. else
  267. {
  268. throw new Exception("渠道不支持一票多件,暂示实现逻辑");
  269. }
  270. }
  271. }
  272. catch (Exception ex)
  273. {
  274. await mpsOrderService.UpdateDraftOrderErrorMessage(mps.DraftOrderId, ex.Message);//所有catch 异常信息记录
  275. throw;
  276. }
  277. }
  278. /// <summary>
  279. /// 修改fba的下单信息
  280. /// 在DataUpload前调用
  281. /// </summary>
  282. /// <param name="order"></param>
  283. /// <param name="arg"></param>
  284. private void FbaChannelInfoMiddleware(Order_Order order, ExtendArg arg)
  285. {
  286. order.CustomerWeight = order.OPWeight = Convert.ToInt32(arg.Order.Boxs.Sum(x => x.ChannelWeight) * 1000);
  287. foreach (var item in arg.Order.Boxs)
  288. {
  289. item.OpLength = item.ChannelLength ?? 0;
  290. item.OpWidth = item.ChannelWidth ?? 0;
  291. item.OpHeight = item.ChannelHeight ?? 0;
  292. item.OpInputWeight = item.ChannelWeight ?? 0;
  293. }
  294. }
  295. }
  296. }