HelpProfitHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using MySystem.Models;
  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. // var orders = db.Orders.Select(m => new { m.UserId, m.Status, m.ProductId }).Where(m => m.Status > 0 && m.ProductId == 34).ToList();
  45. // int StartId = 0;
  46. // bool op = true;
  47. // while(op)
  48. // {
  49. // List<HelpProfitMerTradeSummay> list = db.HelpProfitMerTradeSummay.Where(m => m.Id > StartId && m.TradeMonth == Month).OrderBy(m => m.Id).Take(50).ToList();
  50. // if(list.Count > 0)
  51. // {
  52. // LogHelper.Instance.WriteLog("开始:" + DateTime.Now.ToString(), "助利宝分润日志");
  53. // LogHelper.Instance.WriteLog("总数:" + list.Count(), "助利宝分润日志");
  54. // int index = 0;
  55. // foreach(HelpProfitMerTradeSummay sub in list)
  56. // {
  57. // index += 1;
  58. // int MerchantId = sub.MerchantId;
  59. // decimal TradeAmount = sub.TradeAmount;
  60. // string TradeDate = sub.TradeDate;
  61. // HelpProfitMerchantForUser merchant = db.HelpProfitMerchantForUser.FirstOrDefault(m => m.MerchantId == MerchantId);
  62. // if(merchant != null)
  63. // {
  64. // int UserId = merchant.UserId;
  65. // PosMerchantInfo mer = db.PosMerchantInfo.FirstOrDefault(m => m.Id == MerchantId) ?? new PosMerchantInfo();
  66. // MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == mer.KqMerNo) ?? new MachineForMerNo();
  67. // PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == merFor.SnId) ?? new PosMachinesTwo();
  68. // Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  69. // decimal money = TradeAmount * 0.0005M;
  70. // decimal money2 = TradeAmount * 0.00005M;
  71. // AddProfitRecord(db, user, pos, mer, TradeAmount, money, TradeDate, 1);
  72. // db.SaveChanges();
  73. // int ParentUserId = user.ParentUserId;
  74. // while(ParentUserId > 0)
  75. // {
  76. // Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId);
  77. // if(puser != null)
  78. // {
  79. // bool checkorder = orders.Any(m => m.UserId == ParentUserId);
  80. // if(checkorder)
  81. // {
  82. // AddProfitRecord(db, puser, pos, mer, TradeAmount, money2, TradeDate, 112);
  83. // db.SaveChanges();
  84. // ParentUserId = 0;
  85. // }
  86. // else
  87. // {
  88. // ParentUserId = puser.ParentUserId;
  89. // }
  90. // }
  91. // else
  92. // {
  93. // ParentUserId = 0;
  94. // }
  95. // }
  96. // }
  97. // LogHelper.Instance.WriteLog(index + "--Id:" + sub.Id + ";MerchantId:" + sub.MerchantId + ";" + DateTime.Now.ToString(), "助利宝分润日志");
  98. // }
  99. // }
  100. // else
  101. // {
  102. // op = false;
  103. // }
  104. // }
  105. // db.Dispose();
  106. // LogHelper.Instance.WriteLog("结束:" + DateTime.Now.ToString(), "助利宝分润日志");
  107. // }
  108. // private void AddProfitRecord(WebCMSEntities db, Users puser, PosMachinesTwo pos, PosMerchantInfo merchant, decimal TradeAmount, decimal Prize, string TradeDate, int RewardType)
  109. // {
  110. // if(Prize == 0)
  111. // {
  112. // return;
  113. // }
  114. // string RewardDesc = "助利宝分润";
  115. // if(RewardType == 112)
  116. // {
  117. // RewardDesc = "助利宝推荐分润";
  118. // }
  119. // string TradeMonth = TradeDate.Substring(0, 6);
  120. // HelpProfitRewardDetail detail = db.HelpProfitRewardDetail.Add(new HelpProfitRewardDetail()
  121. // {
  122. // CreateDate = DateTime.Now,
  123. // UpdateDate = DateTime.Now,
  124. // TradeMonth = TradeMonth, //交易月
  125. // SeoTitle = TradeDate,
  126. // UserId = puser.Id, //创客
  127. // BrandId = pos.BrandId, //品牌
  128. // ProductName = RelationClass.GetKqProductsInfo(pos.BrandId), //产品名称
  129. // MerchantId = pos.BindMerchantId, //商户
  130. // DirectUserId = merchant.UserId, //商户直属人
  131. // SnNo = pos.PosSn, //SN号
  132. // MerNo = merchant.KqMerNo, //渠道商户号
  133. // SnType = pos.PosSnType, //机具类型
  134. // StandardDate = pos.ActivationTime, //商户的激活日期
  135. // SnStoreId = pos.StoreId, //SN仓库
  136. // MerBuddyType = puser.MerchantType, //商户创客类型
  137. // RewardType = RewardType, //奖励类型
  138. // RewardTips = RewardDesc, //奖励描述
  139. // CreditTradeAmt = TradeAmount, //贷记卡交易总金额
  140. // CreditRewardAmount = Prize, //贷记卡交易奖励金额
  141. // RewardDesc = RewardDesc, //奖励描述
  142. // }).Entity;
  143. // db.HelpProfitReward.Add(new HelpProfitReward()
  144. // {
  145. // CreateDate = DateTime.Now,
  146. // UpdateDate = DateTime.Now,
  147. // TradeMonth = TradeMonth, //交易月
  148. // SeoTitle = TradeDate,
  149. // UserId = puser.Id, //创客
  150. // BrandId = pos.BrandId, //品牌
  151. // RewardType = RewardType, //奖励类型
  152. // CreditTradeAmt = TradeAmount, //贷记卡交易总金额
  153. // CreditRewardAmount = Prize, //贷记卡交易奖励金额
  154. // RewardDesc = RewardDesc, //奖励描述
  155. // });
  156. // }
  157. public void StatProfitEveryDay(string Month)
  158. {
  159. string check = function.ReadInstance("/HelpProfitStat/" + Month + ".txt");
  160. if (!string.IsNullOrEmpty(check))
  161. {
  162. return;
  163. }
  164. function.WritePage("/HelpProfitStat/", Month + ".txt", DateTime.Now.ToString());
  165. WebCMSEntities db = new WebCMSEntities();
  166. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  167. var orders = db.Orders.Select(m => new { m.UserId, m.Status, m.ProductId }).Where(m => m.Status > 0 && m.ProductId == 34).ToList();
  168. DataTable list = OtherMySqlConn.dtable("select UserId,sum(TradeAmount) from HelpProfitMerchantForUser u,HelpProfitMerTradeSummay s where u.MerchantId=s.MerchantId and TradeMonth='" + Month + "' group by UserId");
  169. LogHelper.Instance.WriteLog("开始:" + DateTime.Now.ToString(), "助利宝分润日志");
  170. LogHelper.Instance.WriteLog("总数:" + list.Rows.Count, "助利宝分润日志");
  171. int index = 0;
  172. foreach (DataRow sub in list.Rows)
  173. {
  174. index += 1;
  175. int UserId = int.Parse(sub["UserId"].ToString());
  176. decimal TradeAmount = decimal.Parse(sub[1].ToString());
  177. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  178. decimal money = TradeAmount * 0.0005M;
  179. decimal money2 = TradeAmount * 0.00005M;
  180. AddProfitRecord(db, user, TradeAmount, money, Month, 1);
  181. db.SaveChanges();
  182. int ParentUserId = user.ParentUserId;
  183. while (ParentUserId > 0)
  184. {
  185. Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId);
  186. if (puser != null)
  187. {
  188. bool checkorder = orders.Any(m => m.UserId == ParentUserId);
  189. if (checkorder)
  190. {
  191. AddProfitRecord(db, puser, TradeAmount, money2, Month, 112);
  192. db.SaveChanges();
  193. ParentUserId = 0;
  194. }
  195. else
  196. {
  197. ParentUserId = puser.ParentUserId;
  198. }
  199. }
  200. else
  201. {
  202. ParentUserId = 0;
  203. }
  204. }
  205. LogHelper.Instance.WriteLog(index + "--UserId:" + UserId + ";TradeAmount:" + TradeAmount + ";" + DateTime.Now.ToString(), "助利宝分润日志");
  206. }
  207. db.Dispose();
  208. LogHelper.Instance.WriteLog("结束:" + DateTime.Now.ToString(), "助利宝分润日志");
  209. }
  210. private void AddProfitRecord(WebCMSEntities db, Users puser, decimal TradeAmount, decimal Prize, string TradeMonth, int RewardType)
  211. {
  212. if(Prize == 0)
  213. {
  214. return;
  215. }
  216. string RewardDesc = "助利宝分润";
  217. if(RewardType == 112)
  218. {
  219. RewardDesc = "助利宝推荐分润";
  220. }
  221. db.HelpProfitReward.Add(new HelpProfitReward()
  222. {
  223. CreateDate = DateTime.Now,
  224. UpdateDate = DateTime.Now,
  225. TradeMonth = TradeMonth, //交易月
  226. UserId = puser.Id, //创客
  227. RewardType = RewardType, //奖励类型
  228. CreditTradeAmt = TradeAmount, //贷记卡交易总金额
  229. CreditRewardAmount = Prize, //贷记卡交易奖励金额
  230. RewardDesc = RewardDesc, //奖励描述
  231. });
  232. }
  233. }
  234. }