LeaderCompPrizeHelper.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System;
  2. using System.Threading;
  3. using System.Linq;
  4. using System.Data;
  5. using Library;
  6. using LitJson;
  7. using System.Collections.Generic;
  8. using MySystem.PxcModels;
  9. namespace MySystem
  10. {
  11. public class LeaderCompPrizeHelper
  12. {
  13. public readonly static LeaderCompPrizeHelper Instance = new LeaderCompPrizeHelper();
  14. private LeaderCompPrizeHelper()
  15. {
  16. }
  17. public void Start()//启动
  18. {
  19. Thread thread = new Thread(Listen);
  20. thread.IsBackground = true;
  21. thread.Start();
  22. }
  23. public void Listen()//启动
  24. {
  25. while(true)
  26. {
  27. if(DateTime.Now.Hour > 0 && DateTime.Now.Hour < 23)
  28. {
  29. string check = function.ReadInstance("/LeaderComp/" + DateTime.Now.ToString("yyyyMMdd") + ".txt");
  30. if(string.IsNullOrEmpty(check))
  31. {
  32. function.WritePage("/LeaderComp/", DateTime.Now.ToString("yyyyMMdd") + ".txt", DateTime.Now.ToString());
  33. if(DateTime.Now.Day == 1)
  34. {
  35. Ready(DateTime.Now.AddMonths(-1).ToString("yyyy-MM"));
  36. Thread.Sleep(2000);
  37. CustomerSqlConn.op("insert into LeaderCompTradeStatBak select * from LeaderCompTradeStat;insert into LeaderCompTmpBak select * from LeaderCompTmp;insert into LeaderCompPrizeBak select * from LeaderCompPrize;insert into LeaderCompAddTradeBak select * from LeaderCompAddTrade;", MysqlConn.SqlConnStr);
  38. }
  39. Ready(DateTime.Now.ToString("yyyyMM"));
  40. }
  41. }
  42. Thread.Sleep(60000);
  43. }
  44. }
  45. public void Ready(string curMonth)
  46. {
  47. CustomerSqlConn.op("delete from LeaderCompTradeStat;delete from LeaderCompTmp;delete from LeaderCompPrize;delete from LeaderCompAddTrade;", MysqlConn.SqlConnStr);
  48. doSomething(curMonth, curMonth);
  49. SendPrize(curMonth, curMonth);
  50. }
  51. public void doSomething(string month, string checkMonth)
  52. {
  53. try
  54. {
  55. WebCMSEntities db = new WebCMSEntities();
  56. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  57. MpMainModels.WebCMSEntities mpmaindb = new MpMainModels.WebCMSEntities();
  58. MpMainModels2.WebCMSEntities mpmaindb2 = new MpMainModels2.WebCMSEntities();
  59. string MonthString = month + "-" + checkMonth;
  60. //统计交易增量
  61. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  62. //创客团队交易额
  63. string sql = "";
  64. int num = 0;
  65. DataTable dt = CustomerSqlConn.dtable("select UserId,sum(HelpNonDirectTradeAmt + NotHelpNonDirectTradeAmt + ProfitNonDirectTradeAmt + HelpNonDirectDebitTradeAmt + NotHelpNonDirectDebitTradeAmt + ProfitNonDirectDebitTradeAmt) Amt from TradeDaySummary where Id>=15055757 and TradeMonth='" + month + "' and SeoTitle='team' and UserId>0 group by UserId", MysqlConn.ReadSqlConnStr);
  66. foreach(DataRow dr in dt.Rows)
  67. {
  68. int UserId = int.Parse(dr["UserId"].ToString());
  69. decimal TradeAmount = decimal.Parse(dr["Amt"].ToString());
  70. //码牌团队交易额(直联)
  71. bool check = mpmaindb.UserAmountSummary.Any(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.IsAct == 1);
  72. if (check)
  73. {
  74. TradeAmount += mpmaindb.UserAmountSummary.Where(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.IsAct == 1).Sum(m => m.TotalAmount) * 4;
  75. }
  76. check = mpmaindb.UserAmountSummary.Any(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.IsAct == 1);
  77. if (check)
  78. {
  79. TradeAmount += mpmaindb.UserAmountSummary.Where(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.IsAct == 1).Sum(m => m.TotalAmount);
  80. }
  81. //码牌团队交易额(银联)
  82. check = mpmaindb2.UserAmountSummary.Any(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.IsAct == 1);
  83. if (check)
  84. {
  85. TradeAmount += mpmaindb2.UserAmountSummary.Where(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.IsAct == 1).Sum(m => m.TotalAmount) * 4;
  86. }
  87. check = mpmaindb2.UserAmountSummary.Any(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.IsAct == 1);
  88. if (check)
  89. {
  90. TradeAmount += mpmaindb2.UserAmountSummary.Where(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.IsAct == 1).Sum(m => m.TotalAmount);
  91. }
  92. //广电卡扶持期按1万/张计入职级
  93. check = db.UserTradeMonthSummary.Any(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.BrandId == 14);
  94. if (check)
  95. {
  96. TradeAmount += db.UserTradeMonthSummary.Where(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.BrandId == 14).Sum(m => m.ActiveBuddyMerStatus) * 10000;
  97. }
  98. sql += "insert into LeaderCompTradeStat (CreateDate,UserId,StatMonth,TradeAmount) values (now(), " + UserId + ", '" + month + "-" + checkMonth + "', " + TradeAmount + ");\n";
  99. num += 1;
  100. if(num >= 200)
  101. {
  102. CustomerSqlConn.op(sql, MysqlConn.SqlConnStr);
  103. sql = "";
  104. num = 0;
  105. }
  106. }
  107. if(!string.IsNullOrEmpty(sql))
  108. {
  109. CustomerSqlConn.op(sql, MysqlConn.SqlConnStr);
  110. }
  111. CustomerSqlConn.op("update LeaderCompTradeStat leader set ParentUserId=case when (select ParentUserId from Users where Id=leader.UserId) is null then 0 else (select ParentUserId from Users where Id=leader.UserId) end,ParentNav=(select ParentNav from Users where Id=leader.UserId) where StatMonth='" + MonthString + "' and UserId>1", MysqlConn.SqlConnStr);
  112. function.WriteLog("start-----" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  113. //计算创客名下的达标人数,直推和间接
  114. function.WriteLog("MonthString:" + MonthString, "领导人达标奖励日志");
  115. Thread.Sleep(4000000);
  116. // List<LeaderCompTradeStat> stats = db.LeaderCompTradeStat.Where(m => m.StatMonth == MonthString).ToList();
  117. DataTable stats = CustomerSqlConn.dtable("select UserId,ParentUserId,ParentNav,TradeAmount,(select sum(TradeAmount) from LeaderCompTradeStat where ParentUserId=p.UserId and TradeAmount<30000000) SmallTradeAmount from LeaderCompTradeStat p where StatMonth='" + MonthString + "' and UserId>1", MysqlConn.ReadSqlConnStr);
  118. function.WriteLog(stats.Rows.Count.ToString(), "领导人达标奖励日志");
  119. foreach(DataRow stat in stats.Rows)
  120. {
  121. int UserId = int.Parse(function.CheckInt(stat["UserId"].ToString()));
  122. int ParentUserId = int.Parse(function.CheckInt(stat["ParentUserId"].ToString()));
  123. decimal totalAmount = decimal.Parse(function.CheckNum(stat["TradeAmount"].ToString()));
  124. decimal smallTradeAmount = decimal.Parse(function.CheckNum(stat["SmallTradeAmount"].ToString()));
  125. string ParentNav = stat["ParentNav"].ToString();
  126. function.WriteLog("1-----" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  127. function.WriteLog("UserId:" + UserId, "领导人达标奖励日志");
  128. function.WriteLog("totalAmount:" + totalAmount, "领导人达标奖励日志");
  129. function.WriteLog("3-----" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  130. //直推创客交易额大于等于3000万即成为达标市场
  131. if(totalAmount >= 30000000 && !string.IsNullOrEmpty(ParentNav))
  132. {
  133. function.WriteLog("4-----" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  134. LeaderCompTmp tmp = db.LeaderCompTmp.FirstOrDefault(m => m.Id == UserId);
  135. if(tmp == null)
  136. {
  137. tmp = db.LeaderCompTmp.Add(new LeaderCompTmp()
  138. {
  139. Id = UserId,
  140. ParentUserId = ParentUserId,
  141. ParentNav = ParentNav
  142. }).Entity;
  143. db.SaveChanges();
  144. }
  145. tmp.EveryMonthData += "1";
  146. db.SaveChanges();
  147. function.WriteLog("5-----" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  148. }
  149. if(smallTradeAmount >= 30000000 && !string.IsNullOrEmpty(ParentNav))
  150. {
  151. function.WriteLog("4-----" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  152. LeaderCompTmp tmp = db.LeaderCompTmp.FirstOrDefault(m => m.Id == UserId);
  153. if(tmp == null)
  154. {
  155. tmp = db.LeaderCompTmp.Add(new LeaderCompTmp()
  156. {
  157. Id = UserId,
  158. ParentUserId = ParentUserId,
  159. ParentNav = ParentNav
  160. }).Entity;
  161. db.SaveChanges();
  162. }
  163. tmp.EveryMonthData += "2";
  164. db.SaveChanges();
  165. function.WriteLog("5-----" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  166. }
  167. function.WriteLog("7-----" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  168. function.WriteLog(DateTime.Now.ToString() + "------" + UserId, "领导人达标奖励日志");
  169. }
  170. db.SaveChanges();
  171. opdb.Dispose();
  172. mpmaindb.Dispose();
  173. mpmaindb2.Dispose();
  174. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  175. }
  176. catch(Exception ex)
  177. {
  178. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "领导达标人奖励异常");
  179. }
  180. }
  181. private void SendPrize(string month, string checkDate)
  182. {
  183. string MonthFlag = month + "-" + checkDate;
  184. WebCMSEntities db = new WebCMSEntities();
  185. DataTable dt = CustomerSqlConn.dtable("select ParentUserId,ParentNav,count(1) from LeaderCompTmp where SUBSTR(EveryMonthData,1,1)='1' GROUP BY ParentUserId,ParentNav", MysqlConn.ReadSqlConnStr);
  186. foreach(DataRow dr in dt.Rows)
  187. {
  188. int ParentUserId = int.Parse(function.CheckInt(dr["ParentUserId"].ToString()));
  189. string ParentNav = dr["ParentNav"].ToString();
  190. int Count = int.Parse(function.CheckInt(dr[2].ToString()));
  191. LeaderCompPrize edit = db.LeaderCompPrize.FirstOrDefault(m => m.StatMonth == MonthFlag && m.UserId == ParentUserId);
  192. if(edit == null)
  193. {
  194. edit = db.LeaderCompPrize.Add(new LeaderCompPrize()
  195. {
  196. CreateDate = DateTime.Now,
  197. StatMonth = MonthFlag,
  198. UserId = ParentUserId,
  199. }).Entity;
  200. db.SaveChanges();
  201. }
  202. edit.DirectCount = Count; //达标市场
  203. DataTable dtsub = CustomerSqlConn.dtable("select SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(ParentNav, '" + ParentNav + "', ''),',',2),',',-1),count(1) from LeaderCompTmp where ParentUserId!=" + ParentUserId + " and ParentNav like '%," + ParentUserId + ",%' and SUBSTR(EveryMonthData,2,1)='2' group by SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(ParentNav, '" + ParentNav + "', ''),',',2),',',-1)", MysqlConn.ReadSqlConnStr);
  204. foreach(DataRow drsub in dtsub.Rows)
  205. {
  206. int SubCount = int.Parse(function.CheckInt(drsub[1].ToString()));
  207. edit.NotDirectCount += SubCount; //深度达标总数
  208. if(SubCount > Count)
  209. {
  210. SubCount = Count;
  211. }
  212. edit.SecDirectCount += SubCount; //深度达标计奖数
  213. }
  214. edit.CompPrize = edit.SecDirectCount * 500;
  215. db.SaveChanges();
  216. }
  217. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  218. db.Dispose();
  219. }
  220. private decimal GetPrize(int count, int direct)
  221. {
  222. decimal prize = 0;
  223. // if (count + direct >= 30)
  224. // {
  225. // prize = 3000 * (count + direct * 2);
  226. // }
  227. // else if (count + direct >= 25)
  228. // {
  229. // prize = 2500 * (count + direct * 2);
  230. // }
  231. // else if (count + direct >= 20)
  232. // {
  233. // prize = 2000 * (count + direct * 2);
  234. // }
  235. // else if (count + direct >= 15)
  236. // {
  237. // prize = 1500 * (count + direct * 2);
  238. // }
  239. // else if (count + direct >= 10)
  240. // {
  241. // prize = 1000 * (count + direct * 2);
  242. // }
  243. // else if (count + direct >= 5)
  244. // {
  245. // prize = 500 * (count + direct * 2);
  246. // }
  247. prize = 500 * count;
  248. return prize;
  249. }
  250. }
  251. }