HelpProfitPreMerchantHelper.cs 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 HelpProfitPreMerchantHelper
  13. {
  14. public readonly static HelpProfitPreMerchantHelper Instance = new HelpProfitPreMerchantHelper();
  15. private HelpProfitPreMerchantHelper()
  16. {
  17. }
  18. // A:前一天剩余数量
  19. // B:第二天释放助利宝商户数量
  20. // C(20):每次递减数量
  21. // D(100):释放助利宝商户最大数量
  22. // B = A - C < C ? D : A - C;
  23. public void Start()
  24. {
  25. Thread th = new Thread(StartFor);
  26. th.IsBackground = true;
  27. th.Start();
  28. }
  29. public void StartFor()
  30. {
  31. while (true)
  32. {
  33. if(DateTime.Now.Hour >= 15 && DateTime.Now.Hour <= 16)
  34. {
  35. StatEveryDay(DateTime.Now.ToString("yyyyMMdd"));
  36. }
  37. Thread.Sleep(600000);
  38. }
  39. }
  40. public void StatEveryDay(string Date)
  41. {
  42. string check = function.ReadInstance("/HelpProfitResetMerchant/" + Date + ".txt");
  43. if (!string.IsNullOrEmpty(check))
  44. {
  45. return;
  46. }
  47. function.WritePage("/HelpProfitResetMerchant/", Date + ".txt", DateTime.Now.ToString());
  48. DateTime start = DateTime.Parse(Date.Substring(0, 4) + "-" + Date.Substring(4, 2) + "-" + Date.Substring(6, 2) + " 00:00:00");
  49. string Month1 = start.AddMonths(-1).ToString("yyyyMM");
  50. string Month2 = start.AddMonths(-2).ToString("yyyyMM");
  51. string Month3 = start.AddMonths(-3).ToString("yyyyMM");
  52. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  53. HelpProfitRelease obj = new HelpProfitRelease();
  54. string JsonParam = function.ReadInstance("/PublicParam/HelpProfitRelease.txt"); //json参数文件
  55. if(string.IsNullOrEmpty(JsonParam))
  56. {
  57. obj.MerBrandId = "1";
  58. obj.GetCount = 100;
  59. obj.ExpandCount = 20;
  60. obj.SourceCount = 100;
  61. function.WritePage("/PublicParam/", "HelpProfitRelease.txt", Newtonsoft.Json.JsonConvert.SerializeObject(obj));
  62. }
  63. else
  64. {
  65. obj = Newtonsoft.Json.JsonConvert.DeserializeObject<HelpProfitRelease>(JsonParam);
  66. }
  67. int ExpandCount = obj.ExpandCount; //膨胀数量
  68. int SourceCount = obj.SourceCount; //原始数量
  69. int GetCount = obj.GetCount; //当前实际发放数量
  70. string BrandId = obj.MerBrandId; //允许释放的品牌
  71. List<int> HelpProfitMerchantIds = RedisDbconn.Instance.GetList<int>("HelpProfitMerchantIds", 1, SourceCount);
  72. int CurrentLastCount = HelpProfitMerchantIds.Count; //前一天剩余数量
  73. //计算第二天释放助利宝商户数量
  74. GetCount = CurrentLastCount - ExpandCount < ExpandCount ? SourceCount : CurrentLastCount - ExpandCount;
  75. obj.GetCount = GetCount;
  76. function.WritePage("/PublicParam/", "HelpProfitRelease.txt", Newtonsoft.Json.JsonConvert.SerializeObject(obj));
  77. RedisDbconn.Instance.Clear("HelpProfitMerchantIds");
  78. DataTable dt = OtherMySqlConn.dtable("SELECT MerchantId,sum FROM( SELECT MerchantId,SUM(TradeAmount) sum FROM PosMerchantTradeSummay WHERE Id>=3809083 and MerchantId not in (select MerchantId from HelpProfitMerIds) AND BrandId in (" + BrandId + ") AND TradeMonth in ('" + Month1 + "','" + Month2 + "','" + Month3 + "') GROUP BY MerchantId)a WHERE a.sum/3 >= 20000 and a.sum/3 <= 60000 limit " + GetCount);
  79. foreach(DataRow dr in dt.Rows)
  80. {
  81. RedisDbconn.Instance.AddList("HelpProfitMerchantIds", int.Parse(function.CheckInt(dr["MerchantId"].ToString())));
  82. }
  83. }
  84. }
  85. }