ProfitHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using MySystem.Models.Main;
  6. using Library;
  7. using System.Threading;
  8. using Microsoft.Extensions.Hosting;
  9. using System.Threading.Tasks;
  10. namespace MySystem
  11. {
  12. public class ProfitHelper
  13. {
  14. public readonly static ProfitHelper Instance = new ProfitHelper();
  15. private ProfitHelper()
  16. {
  17. }
  18. #region 消费队列(执行消费退款金额的统计)
  19. public void StartListenTrade()
  20. {
  21. Thread th = new Thread(StartListenTradeDo);
  22. th.IsBackground = true;
  23. th.Start();
  24. }
  25. public void StartListenTradeDo()
  26. {
  27. while (true)
  28. {
  29. string orderidstring = RedisDbconn.Instance.RPop<string>("ConsumerOrders:Divi:1:List");
  30. if (!string.IsNullOrEmpty(orderidstring))
  31. {
  32. ReturnStat(orderidstring, 1);
  33. }
  34. else
  35. {
  36. Thread.Sleep(60000);
  37. }
  38. }
  39. }
  40. public void StartListenWxTrade()
  41. {
  42. Thread th = new Thread(StartListenWxTradeDo);
  43. th.IsBackground = true;
  44. th.Start();
  45. }
  46. public void StartListenWxTradeDo()
  47. {
  48. while (true)
  49. {
  50. string orderidstring = RedisDbconn.Instance.RPop<string>("ConsumerOrders:Divi:2:List");
  51. if (!string.IsNullOrEmpty(orderidstring))
  52. {
  53. ReturnStat(orderidstring, 2);
  54. }
  55. else
  56. {
  57. Thread.Sleep(10000);
  58. }
  59. }
  60. }
  61. public void ReturnStat(string orderidstring, int PayMode)
  62. {
  63. int OrderId = int.Parse(function.CheckInt(orderidstring));
  64. WebCMSEntities db = new WebCMSEntities();
  65. using (var tran = db.Database.BeginTransaction())
  66. {
  67. try
  68. {
  69. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == OrderId);
  70. if (order != null)
  71. {
  72. decimal PayMoney = order.PayMoney;
  73. int MerchantId = order.MerchantId;
  74. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == MerchantId);
  75. if (merchant != null)
  76. {
  77. int UserId = merchant.UserId;
  78. List<CustomTagSet> customTagSets = db.CustomTagSet.ToList(); //获取公用配置参数集和
  79. //获取参数
  80. CustomTagSet profitPercentSet = customTagSets.FirstOrDefault(m => m.Tags == "CusumerProfitPercent") ?? new CustomTagSet();
  81. decimal profitPercent = decimal.Parse(function.CheckNum(profitPercentSet.Contents)); //基于收款金额的总分润比例;
  82. CustomTagSet cusumerFeePercentSet = customTagSets.FirstOrDefault(m => m.Tags == "CusumerFeePercent") ?? new CustomTagSet();
  83. decimal cusumerFeePercent = decimal.Parse(function.CheckNum(cusumerFeePercentSet.Contents)); //费率;
  84. CustomTagSet minProfitSet = customTagSets.FirstOrDefault(m => m.Tags == "MinProfit") ?? new CustomTagSet();
  85. decimal minProfit = decimal.Parse(function.CheckNum(minProfitSet.Contents)); //最小分润值;
  86. MerchantParamSet set = Newtonsoft.Json.JsonConvert.DeserializeObject<MerchantParamSet>(order.SeoDescription);
  87. int DiviPersons = set.DiviPersons; //单笔订单分红人数
  88. decimal DiviPercent = set.DiviPercent; //单人最大分红比例
  89. int ProfitDays = set.ProfitDays; //分红期限(天)
  90. decimal GetPercent = set.GetPercent / 100; //商家实收比例
  91. decimal MinPayMoney = set.MinPayMoney; //订单参与门槛
  92. int IsAll = set.IsAll; //是否收全额
  93. // decimal TotalActual = 0;
  94. if (IsAll == 0 && PayMoney >= MinPayMoney && GetPercent < 1)
  95. {
  96. decimal DiviMoney = PayMoney * (1 - GetPercent - cusumerFeePercent - profitPercent);
  97. if (DiviMoney > 0)
  98. {
  99. RedisDbconn.Instance.AddRightList("ConsumerOrders:Divi:" + PayMode + ":" + order.MerchantId, order);
  100. DiviMoney = DiviMoney / DiviPersons;
  101. List<int> deletes = new List<int>();
  102. long CurDiviPersons = DiviPersons;
  103. long QueueCount = RedisDbconn.Instance.Count("ConsumerOrders:Divi:" + PayMode + ":" + MerchantId);
  104. long CurQueueCount = QueueCount;
  105. if (CurDiviPersons >= CurQueueCount)
  106. {
  107. CurDiviPersons = CurQueueCount;
  108. }
  109. if (DiviPersons < QueueCount)
  110. {
  111. QueueCount = DiviPersons;
  112. }
  113. decimal OtherMoney = 0; // 退款金额>最大退款金额-当前退款金额,次金额应归还给商户
  114. // 减去自己付的订单
  115. int OutCount = 0; // 出局人数
  116. while (CurDiviPersons > 0)
  117. {
  118. ConsumerOrders suborder = RedisDbconn.Instance.RPop<ConsumerOrders>("ConsumerOrders:Divi:" + PayMode + ":" + MerchantId);
  119. if (suborder != null)
  120. {
  121. MerchantParamSet subset = Newtonsoft.Json.JsonConvert.DeserializeObject<MerchantParamSet>(order.SeoDescription);
  122. int subProfitDays = subset.ProfitDays; //分红期限(天)
  123. bool IsOut = suborder.UpdateDate.Value.AddDays(subProfitDays) < DateTime.Now ? true : false;
  124. if (suborder.CurDivi < suborder.MaxDivi && !IsOut)
  125. {
  126. //如果没过期并且当前退款金额没超过最大退款金额,就执行
  127. decimal GetMoney = suborder.MaxDivi - suborder.CurDivi;
  128. decimal DiffMoney = 0;
  129. if (GetMoney >= DiviMoney)
  130. {
  131. GetMoney = DiviMoney;
  132. }
  133. else
  134. {
  135. DiffMoney = DiviMoney - GetMoney;
  136. }
  137. if (GetMoney > minProfit)
  138. {
  139. suborder.CurDivi += GetMoney;
  140. if (DiffMoney > 0)
  141. {
  142. OtherMoney += DiffMoney;
  143. }
  144. ConsumerProfit cprofit = db.ConsumerProfit.Add(new ConsumerProfit()
  145. {
  146. CreateDate = DateTime.Now,
  147. ConsumerId = suborder.ConsumerId,
  148. MerchantId = suborder.MerchantId,
  149. OrderId = suborder.Id,
  150. GetMoney = GetMoney,
  151. QueryCount = PayMode,
  152. Sort = OrderId,
  153. SeoTitle = suborder.OrderNo,
  154. SeoKeyword = suborder.PayMoney.ToString(),
  155. }).Entity;
  156. db.SaveChanges();
  157. MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == suborder.MerchantId) ?? new MerchantAddInfo();
  158. //聚合支付退款
  159. var info = HaoDaHelper.Instance.AggregatedPayRefund(merchantAdd.SubMchid, merchantAdd.AlipayPid, "HDTK" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8), merchantAdd.AlipayAuthToken, GetMoney.ToString());
  160. if (suborder.CurDivi < suborder.MaxDivi)
  161. {
  162. RedisDbconn.Instance.AddList("ConsumerOrders:Divi:" + PayMode + ":" + MerchantId, suborder);
  163. }
  164. else
  165. {
  166. OutCount += 1;
  167. }
  168. }
  169. }
  170. else
  171. {
  172. //否者计算人数不计入退款名额
  173. CurDiviPersons += 1;
  174. }
  175. }
  176. CurDiviPersons -= 1;
  177. QueueCount = RedisDbconn.Instance.Count("ConsumerOrders:Divi:" + PayMode + ":" + MerchantId);
  178. if (QueueCount == 0)
  179. {
  180. CurDiviPersons = 0;
  181. }
  182. }
  183. }
  184. }
  185. }
  186. }
  187. tran.Commit();
  188. }
  189. catch (Exception ex)
  190. {
  191. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + ex.ToString(), "自定义分润程序监听队列异常");
  192. tran.Rollback();
  193. }
  194. }
  195. db.Dispose();
  196. }
  197. #endregion
  198. }
  199. }