AgentProfitHelper.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using Library;
  6. using LitJson;
  7. using System.Threading;
  8. using Microsoft.Extensions.Hosting;
  9. using System.Threading.Tasks;
  10. using MySystem.Models.Main;
  11. namespace MySystem
  12. {
  13. public class AgentProfitHelper
  14. {
  15. public readonly static AgentProfitHelper Instance = new AgentProfitHelper();
  16. private AgentProfitHelper()
  17. {
  18. }
  19. public void StartListenProfit()
  20. {
  21. Thread th = new Thread(StartListenProfitDo);
  22. th.IsBackground = true;
  23. th.Start();
  24. }
  25. public void StartListenProfitDo()
  26. {
  27. while(true)
  28. {
  29. if(DateTime.Now.Day < 10 && DateTime.Now.Hour > 2 && DateTime.Now.Hour < 23)
  30. {
  31. try
  32. {
  33. DoProfit();
  34. }
  35. catch(Exception ex)
  36. {
  37. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "来客吧区域代理分润异常");
  38. }
  39. Thread.Sleep(600000);
  40. }
  41. else
  42. {
  43. Thread.Sleep(3600000);
  44. }
  45. }
  46. }
  47. //分润算法
  48. public void DoProfit()
  49. {
  50. string TradeMonth = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
  51. string check = function.ReadInstance("/AgentProfitFlag/" + TradeMonth + ".txt");
  52. if(!string.IsNullOrEmpty(check))
  53. {
  54. return;
  55. }
  56. function.WritePage("/AgentProfitFlag/", TradeMonth + ".txt", DateTime.Now.ToString());
  57. MySystem.Models.Main1.WebCMSEntities dbnew1 = new MySystem.Models.Main1.WebCMSEntities();
  58. WebCMSEntities dbnew = new WebCMSEntities();
  59. List<AgentProfit> ProfitList = new List<AgentProfit>();
  60. DataTable dt = CustomerSqlConn.dtable("select IsAct,ManageAreas,AgentLevel,TotalAmount,(case when IsAct=1 then l.Percent else l.UnActPercent end) Percent,(case when IsAct=1 then l.Percent*TotalAmount else l.UnActPercent*TotalAmount end) Profit from (select IsAct,ManageAreas,(LENGTH(ManageAreas)-LENGTH(REPLACE(ManageAreas,',',''))+1) AgentLevel,sum(TotalAmount) TotalAmount from AgentTradeStatSummary where TradeMonth='" + TradeMonth + "' group by IsAct,ManageAreas) tb left join AgentLevels l on tb.AgentLevel=l.Id", AppConfig.Base.SqlConnStr);
  61. foreach(DataRow dr in dt.Rows)
  62. {
  63. ProfitList.Add(new AgentProfit()
  64. {
  65. IsAct = int.Parse(dr["IsAct"].ToString()),
  66. ManageAreas = dr["ManageAreas"].ToString(),
  67. AgentLevel = int.Parse(dr["IsAct"].ToString()),
  68. TotalAmount = decimal.Parse(dr["TotalAmount"].ToString()),
  69. Percent = decimal.Parse(dr["Percent"].ToString()),
  70. Profit = decimal.Parse(dr["Profit"].ToString()),
  71. });
  72. }
  73. foreach(AgentProfit SubProfit in ProfitList)
  74. {
  75. int SubLevel = SubProfit.AgentLevel + 1;
  76. decimal SubProfitAmt = 0;
  77. if(ProfitList.Any(m => m.ManageAreas.StartsWith(SubProfit.ManageAreas) && m.AgentLevel == SubLevel && m.IsAct == SubProfit.IsAct))
  78. {
  79. SubProfitAmt = ProfitList.Where(m => m.ManageAreas.StartsWith(SubProfit.ManageAreas) && m.AgentLevel == SubLevel && m.IsAct == SubProfit.IsAct).Sum(m => m.Profit);
  80. }
  81. if(SubProfitAmt > 0) SubProfit.ProfitResult -= SubProfitAmt;
  82. }
  83. foreach(AgentProfit SubProfit in ProfitList)
  84. {
  85. MySystem.Models.Main1.UserAgent user = dbnew1.UserAgent.FirstOrDefault(m => m.ManageAreas == SubProfit.ManageAreas);
  86. if(user != null)
  87. {
  88. dbnew.AgentProfitRecord.Add(new AgentProfitRecord()
  89. {
  90. CreateDate = DateTime.Now,
  91. UpdateDate = DateTime.Now,
  92. TradeMonth = TradeMonth,
  93. Remark = "来客吧区域代理奖励\n" + SubProfit.ManageAreas,
  94. TradeProfit = SubProfit.ProfitResult,
  95. CreditTradeAmt = SubProfit.TotalAmount,
  96. UserId = user.UserId,
  97. });
  98. dbnew.Dispose();
  99. }
  100. }
  101. dbnew1.Dispose();
  102. dbnew.Dispose();
  103. }
  104. }
  105. }