RepositoryExtension.ApiOrder.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using Aliyun.OSS;
  2. using Elasticsearch.Net;
  3. using JiaZhiQuan.Common.AliOSS;
  4. using JiaZhiQuan.Common.Config;
  5. using JiaZhiQuan.Common.ElasticSearch;
  6. using JiaZhiQuan.Common.ElasticSearch.Models;
  7. using JiaZhiQuan.Common.Response;
  8. using Newtonsoft.Json;
  9. using Newtonsoft.Json.Linq;
  10. using Senparc.Weixin.MP.Containers;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Data;
  14. using System.Diagnostics;
  15. using System.Dynamic;
  16. using System.Linq;
  17. using System.Net.Http;
  18. using System.Reflection;
  19. using System.Security.Cryptography;
  20. using System.Text;
  21. using System.Threading;
  22. using System.Threading.Tasks;
  23. using Wicture.DbRESTFul;
  24. using Wicture.DbRESTFul.Cache;
  25. using Wicture.DbRESTFul.Infrastructure.Repository;
  26. using static JiaZhiQuan.Common.Models.ApiOrderModel;
  27. using static JiaZhiQuan.Common.YCTech.YCTechModel;
  28. namespace JiaZhiQuan.Common
  29. {
  30. public static partial class RepositoryExtension
  31. {
  32. //下单api直充
  33. public static async Task<OperateResult> PurchaseApiOrder(this DbRESTFulRepository repository,
  34. IHttpClientFactory httpClientFactory, ConfigFromDb config, SupplierOrder orderInfo) {
  35. var rst = new OperateResult() { count = 0 };
  36. orderInfo.supplierId = config.YCTechSupplierId;
  37. CheckOrder(orderInfo);
  38. var httpClient = httpClientFactory.CreateClient();
  39. var request = new YCTechSubmitOrderRequest {
  40. OutOrderId = orderInfo.id.ToString(), Account = orderInfo.rechargeAccount,
  41. CallbackUrl = config.YCTechOrderCallBackUrl, ProductId = orderInfo.productId.ToString(),
  42. Timestamp = DateTime.Now.ToString("yyyyMMddHHmmssfff")
  43. };
  44. //下单成功和状态未更新时,不能重复下单。
  45. var count = await repository.QuerySingleOrDefaultAsync<int>(
  46. $@"select count(*) from n_supplier_order
  47. where orderId={orderInfo.orderId} and orderStatus in (0,1)");
  48. if (count > 0) throw new Exception("不能重复下单");
  49. //尝试3次
  50. for (int i = 0; i < 3; i++) {
  51. await request.ExeRequest(httpClient, config);
  52. if (request.Response.Sucess) {
  53. break;
  54. } else {
  55. Thread.Sleep(200);
  56. }
  57. }
  58. if (!request.Response.Sucess|| request.Response.Data == null) {
  59. LoggerManager.Logger.Error($"供应商下单失败:{request.Response.Msg}");
  60. throw new Exception($"供应商下单失败:{request.Response.Msg}");
  61. }
  62. orderInfo.supplierOrderId = request.Response.Data.OrderId;
  63. orderInfo.purchaseTime = DateTime.Now;
  64. orderInfo.purchasePrice = (int)(request.Response.Data.Cost * 100);
  65. orderInfo.orderStatus = 0;
  66. orderInfo.orderMethod = 1;
  67. orderInfo.createAt = DateTime.Now;
  68. using var conn = repository.ConnectionManager.GetConnection();
  69. conn.Open();
  70. var trans = conn.BeginTransaction();
  71. try {
  72. var insert = await repository.SimpleInsert("n_supplier_order", orderInfo, conn, trans);
  73. if (insert.count > 0) {
  74. await repository.QuerySingleOrDefaultAsync<int>(
  75. $@"update n_exchange_order set supplierOrderId=@supplierOrderId where id={orderInfo.orderId}"
  76. , new { orderInfo.supplierOrderId },conn,trans);
  77. }
  78. trans.Commit();
  79. return new OperateResult { count = insert.count };
  80. } catch (Exception ex) {
  81. trans.Rollback();
  82. LoggerManager.Logger.Error(ex,$"未入库供应商订单号:{request.Response.Data.OrderId}");
  83. throw ex;
  84. }
  85. }
  86. private static void CheckOrder(SupplierOrder orderInfo) {
  87. if (orderInfo == null) throw new Exception("订单信息不能为空");
  88. if (orderInfo.id <= 0) throw new Exception("订单id不能为空");
  89. if(orderInfo.orderId<=0) throw new Exception("平台订单id不能为空");
  90. if (orderInfo.productId <= 0) throw new Exception("采购商品id不能为空");
  91. if(string.IsNullOrEmpty(orderInfo.productName)) throw new Exception("采购商品名称不能为空");
  92. if (orderInfo.supplierId <= 0) throw new Exception("供应商id不能为空");
  93. if (string.IsNullOrEmpty(orderInfo.rechargeAccount)) throw new Exception("充值号码不能为空");
  94. if (orderInfo.userId <= 0) throw new Exception("下单用户id不能为空");
  95. }
  96. }
  97. }