HelpProfitHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using MySystem.PxcModels;
  6. using Library;
  7. using System.Threading;
  8. using Microsoft.Extensions.Hosting;
  9. using System.Threading.Tasks;
  10. namespace MySystem
  11. {
  12. public class HelpProfitHelper
  13. {
  14. public readonly static HelpProfitHelper Instance = new HelpProfitHelper();
  15. private HelpProfitHelper()
  16. {
  17. }
  18. public void Start()
  19. {
  20. Thread th = new Thread(StartFor);
  21. th.IsBackground = true;
  22. th.Start();
  23. }
  24. public void StartFor()
  25. {
  26. while (true)
  27. {
  28. if(DateTime.Now.Day == 1 && DateTime.Now.Hour >= 2 && DateTime.Now.Hour <= 3)
  29. {
  30. StatProfitEveryDay(DateTime.Now.AddMonths(-1).ToString("yyyyMM"));
  31. }
  32. Thread.Sleep(600000);
  33. }
  34. }
  35. public void StatProfitEveryDay(string Month)
  36. {
  37. string check = function.ReadInstance("/HelpProfitStat/" + Month + ".txt");
  38. if (!string.IsNullOrEmpty(check))
  39. {
  40. return;
  41. }
  42. function.WritePage("/HelpProfitStat/", Month + ".txt", DateTime.Now.ToString());
  43. WebCMSEntities db = new WebCMSEntities();
  44. List<HelpProfitMerTradeSummay> list = db.HelpProfitMerTradeSummay.Where(m => m.TradeMonth == Month).ToList();
  45. foreach(HelpProfitMerTradeSummay sub in list)
  46. {
  47. int MerchantId = sub.MerchantId;
  48. decimal TradeAmount = sub.TradeAmount;
  49. string TradeDate = sub.TradeDate;
  50. HelpProfitMerchantForUser merchant = db.HelpProfitMerchantForUser.FirstOrDefault(m => m.MerchantId == MerchantId);
  51. if(merchant != null)
  52. {
  53. int UserId = merchant.UserId;
  54. PosMerchantInfo mer = db.PosMerchantInfo.FirstOrDefault(m => m.Id == MerchantId) ?? new PosMerchantInfo();
  55. MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == mer.KqMerNo) ?? new MachineForMerNo();
  56. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == merFor.SnId) ?? new PosMachinesTwo();
  57. // string Name = mer.MerchantName;
  58. // if (mer.BrandId == 2)
  59. // {
  60. // if (Name.Contains("-"))
  61. // {
  62. // Name = Name.Split('-')[1];
  63. // }
  64. // else if (Name.Contains("_"))
  65. // {
  66. // Name = Name.Split('_')[1];
  67. // }
  68. // }
  69. // Name = Name.Replace("个体户", "");
  70. // Name = Name.Replace("个体商户", "");
  71. // Name = Name.Replace("企业户", "");
  72. // Name = Name.Replace("企业商户", "");
  73. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  74. decimal money = TradeAmount * 0.0005M;
  75. decimal money2 = TradeAmount * 0.00005M;
  76. // UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  77. // if (account == null)
  78. // {
  79. // account = db.UserAccount.Add(new UserAccount()
  80. // {
  81. // Id = UserId,
  82. // UserId = UserId,
  83. // }).Entity;
  84. // db.SaveChanges();
  85. // }
  86. // decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  87. // account.HelpProfitBalanceAmount += money;
  88. // decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  89. // UserAccountRecord accountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  90. // {
  91. // CreateDate = DateTime.Now,
  92. // UpdateDate = DateTime.Now,
  93. // Sort = MerchantId,
  94. // UserId = UserId, //创客
  95. // ProductType = 101, //产品类型
  96. // ChangeType = 1,
  97. // ChangeAmount = money, //变更金额
  98. // BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  99. // AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  100. // Remark = Name.Substring(0, 1) + "**:" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + "交易" + TradeAmount.ToString("f2"),
  101. // }).Entity;
  102. AddProfitRecord(db, user, pos, mer, TradeAmount, money, TradeDate, 1);
  103. db.SaveChanges();
  104. // account = db.UserAccount.FirstOrDefault(m => m.Id == user.ParentUserId);
  105. // if (account == null)
  106. // {
  107. // account = db.UserAccount.Add(new UserAccount()
  108. // {
  109. // Id = user.ParentUserId,
  110. // UserId = user.ParentUserId,
  111. // }).Entity;
  112. // db.SaveChanges();
  113. // }
  114. // BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  115. // account.HelpProfitBalanceAmount += money2;
  116. // AfterBalanceAmount = account.BalanceAmount; //变更后余额
  117. // accountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  118. // {
  119. // CreateDate = DateTime.Now,
  120. // UpdateDate = DateTime.Now,
  121. // Sort = MerchantId,
  122. // UserId = user.ParentUserId, //创客
  123. // ProductType = 101, //产品类型
  124. // ChangeType = 112,
  125. // ChangeAmount = money2, //变更金额
  126. // BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  127. // AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  128. // Remark = Name.Substring(0, 1) + "**:" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + "交易" + TradeAmount.ToString("f2"),
  129. // }).Entity;
  130. int ParentUserId = user.ParentUserId;
  131. while(ParentUserId > 0)
  132. {
  133. Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId);
  134. if(puser != null)
  135. {
  136. bool op = db.Orders.Any(m => m.UserId == ParentUserId && m.Status > 0 && m.ProductId == 34);
  137. if(op)
  138. {
  139. AddProfitRecord(db, puser, pos, mer, TradeAmount, money2, TradeDate, 112);
  140. db.SaveChanges();
  141. ParentUserId = 0;
  142. }
  143. else
  144. {
  145. ParentUserId = puser.ParentUserId;
  146. }
  147. }
  148. else
  149. {
  150. ParentUserId = 0;
  151. }
  152. }
  153. // string Month = Date.Substring(0, 6);
  154. // HelpProfitRebateDetail selfStat = db.HelpProfitRebateDetail.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == Month && m.TradeDate == Date);
  155. // if (selfStat == null)
  156. // {
  157. // selfStat = db.HelpProfitRebateDetail.Add(new HelpProfitRebateDetail()
  158. // {
  159. // TradeMonth = Month,
  160. // TradeDate = Date,
  161. // UserId = UserId,
  162. // }).Entity;
  163. // db.SaveChanges();
  164. // }
  165. // selfStat.CreditRewardAmount += money;
  166. // selfStat = db.HelpProfitRebateDetail.FirstOrDefault(m => m.UserId == user.ParentUserId && m.TradeMonth == Month && m.TradeDate == Date);
  167. // if (selfStat == null)
  168. // {
  169. // selfStat = db.HelpProfitRebateDetail.Add(new HelpProfitRebateDetail()
  170. // {
  171. // TradeMonth = Month,
  172. // TradeDate = Date,
  173. // UserId = user.ParentUserId,
  174. // }).Entity;
  175. // db.SaveChanges();
  176. // }
  177. // selfStat.CreditRewardAmount += money2;
  178. // db.SaveChanges();
  179. }
  180. }
  181. db.Dispose();
  182. }
  183. private void AddProfitRecord(WebCMSEntities db, Users puser, PosMachinesTwo pos, PosMerchantInfo merchant, decimal TradeAmount, decimal Prize, string TradeDate, int RewardType)
  184. {
  185. if(Prize == 0)
  186. {
  187. return;
  188. }
  189. string RewardDesc = "助利宝分润";
  190. if(RewardType == 112)
  191. {
  192. RewardDesc = "助利宝推荐分润";
  193. }
  194. string TradeMonth = TradeDate.Substring(0, 6);
  195. HelpProfitRewardDetail detail = db.HelpProfitRewardDetail.Add(new HelpProfitRewardDetail()
  196. {
  197. CreateDate = DateTime.Now,
  198. UpdateDate = DateTime.Now,
  199. TradeMonth = TradeMonth, //交易月
  200. SeoTitle = TradeDate,
  201. UserId = puser.Id, //创客
  202. BrandId = pos.BrandId, //品牌
  203. ProductName = RelationClass.GetKqProductsInfo(pos.BrandId), //产品名称
  204. MerchantId = pos.BindMerchantId, //商户
  205. DirectUserId = merchant.UserId, //商户直属人
  206. SnNo = pos.PosSn, //SN号
  207. MerNo = merchant.KqMerNo, //渠道商户号
  208. SnType = pos.PosSnType, //机具类型
  209. StandardDate = pos.ActivationTime, //商户的激活日期
  210. SnStoreId = pos.StoreId, //SN仓库
  211. MerBuddyType = puser.MerchantType, //商户创客类型
  212. RewardType = RewardType, //奖励类型
  213. RewardTips = RewardDesc, //奖励描述
  214. CreditTradeAmt = TradeAmount, //贷记卡交易总金额
  215. CreditRewardAmount = Prize, //贷记卡交易奖励金额
  216. RewardDesc = RewardDesc, //奖励描述
  217. }).Entity;
  218. db.HelpProfitReward.Add(new HelpProfitReward()
  219. {
  220. CreateDate = DateTime.Now,
  221. UpdateDate = DateTime.Now,
  222. TradeMonth = TradeMonth, //交易月
  223. SeoTitle = TradeDate,
  224. UserId = puser.Id, //创客
  225. BrandId = pos.BrandId, //品牌
  226. RewardType = RewardType, //奖励类型
  227. CreditTradeAmt = TradeAmount, //贷记卡交易总金额
  228. CreditRewardAmount = Prize, //贷记卡交易奖励金额
  229. RewardDesc = RewardDesc, //奖励描述
  230. });
  231. }
  232. }
  233. }