RepositoryExtension.PostOrder.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using JiaZhiQuan.Common.Messaging;
  2. using JiaZhiQuan.Common.Messaging.Models;
  3. using JiaZhiQuan.Common.Models;
  4. using JiaZhiQuan.Common.Models.PO;
  5. using JiaZhiQuan.Common.Models.VO.UE;
  6. using JiaZhiQuan.Common.Utils.Extend;
  7. using Newtonsoft.Json;
  8. using NPOI.Util;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Data;
  12. using System.Linq;
  13. using System.Threading.Tasks;
  14. using Wicture.DbRESTFul.Infrastructure.Repository;
  15. using static JiaZhiQuan.Common.Models.MallGoodsModel.RefundModel;
  16. namespace JiaZhiQuan.Common {
  17. public partial class RepositoryExtension {
  18. private static async Task MakeOrderNotification(DbRESTFulRepository repository, Producer producer, string orderId, int orderType, int newState)
  19. {
  20. if (orderType == 2 || orderType == 3)
  21. {
  22. var orderInfo = await repository.QuerySingleOrDefaultAsync<dynamic>(
  23. $"select orderId, buyerId, sellerId, `source`, closeReason, earnestMoneyRefunded, receiveAt, receiveExpireAt from ue_order_hang where orderId={orderId}");
  24. if (orderInfo != null)
  25. {
  26. if (orderInfo.source == 2)
  27. {
  28. if (newState == -1)
  29. {
  30. if (orderInfo.earnestMoneyRefunded == 0 || orderInfo.earnestMoneyRefunded == 3)
  31. await repository.MakeHangSaleOrderPlatformClosedMessage(producer,
  32. (long)orderInfo.buyerId, (long)orderInfo.sellerId, (long)orderInfo.orderId,
  33. (string)orderInfo.closeReason);
  34. else
  35. await repository.MakeHangSaleOrderPlatformClosedMessage(producer,
  36. (long)orderInfo.buyerId, (long)orderInfo.sellerId, (long)orderInfo.orderId, true,
  37. (string)orderInfo.closeReason);
  38. }
  39. else if (newState == -2)
  40. {
  41. await repository.MakeHangSaleOrderPayExpireCloseMessage(producer, (long)orderInfo.buyerId,
  42. (long)orderInfo.orderId);
  43. }
  44. else if (newState == -3)
  45. {
  46. }
  47. else if (newState == -4)
  48. {
  49. await repository.MakeHangSaleOrderRefusePickUpClosedMessage(producer,
  50. (long)orderInfo.buyerId, (long)orderInfo.sellerId, (long)orderInfo.orderId);
  51. }
  52. else if (newState == -5)
  53. {
  54. await repository.MakeHangSaleOrderExpirePickUpClosedMessage(producer,
  55. (long)orderInfo.buyerId, (long)orderInfo.sellerId, (long)orderInfo.orderId);
  56. }
  57. else if (newState == 2)
  58. {
  59. await repository.MakeHangSaleOrderWaitDeliveryMessage(producer, (long)orderInfo.buyerId,
  60. (long)orderInfo.sellerId, (long)orderInfo.orderId);
  61. }
  62. else if (newState == 3)
  63. {
  64. await repository.MakeHangSaleOrderWaitReceiveMessage(producer, (long)orderInfo.buyerId,
  65. (long)orderInfo.orderId);
  66. }
  67. else if (newState == 4)
  68. {
  69. await repository.MakeHangSaleOrderWaitPickUpMessage(producer, (long)orderInfo.buyerId,
  70. (long)orderInfo.sellerId, (long)orderInfo.orderId);
  71. }
  72. else if (newState == 5)
  73. {
  74. await repository.MakeHangSaleOrderBuyerSignForMessage(producer, (long)orderInfo.buyerId,
  75. (long)orderInfo.sellerId, (long)orderInfo.orderId, (DateTime?)orderInfo.receiveAt >= (DateTime?)orderInfo.receiveExpireAt);
  76. }
  77. else if (newState == 6)
  78. {
  79. await repository.MakeHangSaleOrderPickUpMessage(producer, (long)orderInfo.buyerId,
  80. (long)orderInfo.sellerId, (long)orderInfo.orderId);
  81. }
  82. }
  83. }
  84. }
  85. }
  86. /// <summary>
  87. /// orderType 订单类型。1:寄售订单,2:销售订单-寄售 3,销售订单-挂售
  88. /// operateType 操作人类型:0 平台 1 买家 2 卖家 3 运营人员
  89. /// </summary>
  90. /// <param name="repository"></param>
  91. /// <param name="orderId">订单id</param>
  92. /// <param name="newState">新状态</param>
  93. /// <param name="orderType"></param>
  94. /// <param name="conn"></param>
  95. /// <param name="trans"></param>
  96. /// <returns></returns>
  97. public static async Task<InsertResult> RecordOrderLog(this DbRESTFulRepository repository, Producer producer, string orderId,
  98. int newState,int orderType,string eventDesc,string operatorId, int operatorType,
  99. IDbConnection conn = null, IDbTransaction trans = null) {
  100. var now=DateTime.Now;
  101. var eventName = orderType switch {
  102. 1 => BuildPostOrderEventName(newState),
  103. 2 => BuildSaleOrderEventName(newState),
  104. 3 => BuildSaleOrderEventNameForPub(newState),
  105. _ => throw new Exception("不支持的订单类型")
  106. };
  107. await MakeOrderNotification(repository, producer, orderId, orderType, newState);
  108. var eventInfo = new {
  109. orderId,
  110. eventName,
  111. eventDesc,
  112. operatorId,
  113. operatorType,
  114. createAt=now,
  115. };
  116. return await repository.SimpleInsert("mall_order_change_events", eventInfo, conn, trans);
  117. }
  118. public static async Task<InsertResult> RecordOrderLogs(this DbRESTFulRepository repository, Producer producer,
  119. List<string> orderIds,
  120. int newState, int orderType, string eventDesc, string operatorId, int operatorType,
  121. IDbConnection conn = null, IDbTransaction trans = null) {
  122. var now = DateTime.Now;
  123. var eventName = orderType switch {
  124. 1 => BuildPostOrderEventName(newState),
  125. 2 => BuildSaleOrderEventName(newState),
  126. 3 => BuildSaleOrderEventNameForPub(newState),
  127. _ => throw new Exception("不支持的订单类型")
  128. };
  129. var events = orderIds.Select(orderId => new {
  130. orderId,
  131. eventName,
  132. eventDesc,
  133. operatorId,
  134. operatorType,
  135. createAt = now,
  136. });
  137. var inSql = events.BuildBatchInsertsql("mall_order_change_events", "orderId,eventName");
  138. var cnt = await repository.QuerySingleOrDefaultAsync<int>(inSql, null, conn, trans);
  139. foreach (var orderId in orderIds)
  140. {
  141. await MakeOrderNotification(repository, producer, orderId, orderType, newState);
  142. }
  143. return new InsertResult { count = cnt };
  144. }
  145. //添加模拟浏览任务。
  146. public static async Task AddGoodsMockViewTask(this DbRESTFulRepository repository,
  147. List<long> goodsIds, IDbConnection conn = null, IDbTransaction trans = null) {
  148. var rand = new Random();
  149. if(null==goodsIds|| goodsIds.Count() == 0) { return; }
  150. var now = DateTime.Now;
  151. var mockTasks = goodsIds.Select(g => {
  152. var addCount = rand.Next(CommonConst.MinViewCnt, CommonConst.MaxViewCnt);
  153. var hours = rand.Next(CommonConst.MinViewHours, CommonConst.MaxViewHours);
  154. if (hours <= 0 || addCount <= 0) throw new Exception("模拟浏览数计算不正常");
  155. return new {
  156. targetId = g,
  157. //需添加数
  158. addCount,
  159. //需耗时
  160. hours,
  161. type = "GOODS_VIEW",
  162. state = 0,
  163. createAt = now,
  164. lastExecTime=now,
  165. remindCount = addCount,
  166. };
  167. });
  168. var inSql = mockTasks.BuildBatchInsertsql("ue_mock_task", "targetId,addCount,createAt");
  169. await repository.QuerySingleOrDefaultAsync<int>(inSql, null, conn, trans);
  170. }
  171. //发送计算商品推荐分的消息
  172. public static async Task SendCalRecommendScoreMsg(this DbRESTFulRepository repository,
  173. Producer producer, RecommendScoreModel model) {
  174. await producer.ProduceAsync(StatisticActionModel.GetMsgKey(), new StatisticActionModel {
  175. Type = StatisticActionType.GOODS_RECOMMEND_SCORE,
  176. Content = JsonConvert.SerializeObject(model)
  177. });
  178. }
  179. //寄售订单
  180. private static string BuildPostOrderEventName(int orderState) {
  181. return orderState switch {
  182. -1 => "后台关闭订单",
  183. -2 => "卖家已签收退货,系统关闭订单",
  184. -3 => "平台鉴定未通过,不退货,系统关闭订单",
  185. 1 => "卖家已发货,等待平台收货",
  186. 2 => "平台已收货,等待鉴定",
  187. 3 => "平台鉴定通过,等待卖家上架",
  188. 4 => "平台鉴定未通过,等待平台退货",
  189. 5 => "卖家取回商品 (无偿) ,等待平台退货",
  190. 6 => "商品已上架",
  191. 7 => "卖家拒绝上架,等待平台退货",
  192. 8 => "卖家超时未上架,等待平台退货",
  193. 9 => "买家已付款,等待交易完成",
  194. 10 => "卖家取回商品 (有偿) ,等待平台退货",
  195. 11 => "寄售成功",
  196. 12 => "销售订单关闭,重新上架中",
  197. 13 => "平台已退货,等待卖家签收",
  198. _ => throw new Exception("不支持的订单状态")
  199. };
  200. }
  201. //销售订单-寄售
  202. private static string BuildSaleOrderEventName(int orderState) {
  203. return orderState switch {
  204. -1 => "后台关闭订单",
  205. -2 => "买家超时未支付,系统关闭订单",
  206. -3=> "买家关闭订单",
  207. 1 => "等待买家付款",
  208. 2 => "买家已付款,等待平台发货",
  209. 3 => "平台已发货,等待买家签收",
  210. 4 => "待提待",
  211. 5 => "买家已签收,交易成功",
  212. _ => throw new Exception("不支持的订单状态")
  213. };
  214. }
  215. //销售订单-挂售
  216. private static string BuildSaleOrderEventNameForPub(int orderState) {
  217. return orderState switch {
  218. -1 => "后台关闭订单",
  219. -2 => "买家超时未支付,系统关闭订单;",
  220. -3 => "买家关闭订单;",
  221. -4 => "买家拒绝取货,扣除保证金,交易关闭;",
  222. -5=> "买家超时未取货,扣除保证金,交易关闭;",
  223. 1 => "等待买家付款",
  224. 2 => "买家已付款,选择快递收货,等待卖家发货;",
  225. 3 => "卖家已发货,等待买家签收;",
  226. 4 => "买家已付款,选择线下收货,等待买家取货;",
  227. 5 => "买家已签收,交易成功;",
  228. 6=> "买家已取货,交易成功;",
  229. _ => throw new Exception("不支持的订单状态")
  230. };
  231. }
  232. }
  233. }