LeaderCompPrizeHelper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 < 12)
  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("yyyyMM"));
  36. Thread.Sleep(2000);
  37. CustomerSqlConn.op("insert into LeaderCompPrizeBak select * from LeaderCompPrize;insert into LeaderCompAddTradeBak select * from LeaderCompAddTrade;", MysqlConn.SqlConnStr);
  38. CustomerSqlConn.op("delete from LeaderCompTmpBak;insert into LeaderCompTmpBak (Id,EveryMonthData,ParentNav,ParentUserId) select Id,EveryMonthData,ParentNav,ParentUserId from LeaderCompTmp;", MysqlConn.SqlConnStr);
  39. CustomerSqlConn.op("insert into LeaderCompTradeStatBak (`CreateDate`, `TradeAmount`, `ParentNav`, `ParentUserId`, `UserId`, `StatMonth`) select `CreateDate`, `TradeAmount`, `ParentNav`, `ParentUserId`, `UserId`, `StatMonth` from LeaderCompTradeStat;", MysqlConn.SqlConnStr);
  40. }
  41. Ready(DateTime.Now.ToString("yyyyMM"));
  42. }
  43. }
  44. Thread.Sleep(60000);
  45. }
  46. }
  47. public void Ready(string curMonth)
  48. {
  49. CustomerSqlConn.op("delete from LeaderCompTradeStat;delete from LeaderCompTmp;delete from LeaderCompPrize;delete from LeaderCompAddTrade;", MysqlConn.SqlConnStr);
  50. doSomething(curMonth, curMonth);
  51. SendPrize(curMonth, curMonth);
  52. }
  53. public void doSomething(string month, string checkMonth)
  54. {
  55. try
  56. {
  57. WebCMSEntities db = new WebCMSEntities();
  58. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  59. MpMainModels.WebCMSEntities mpmaindb = new MpMainModels.WebCMSEntities();
  60. MpMainModels2.WebCMSEntities mpmaindb2 = new MpMainModels2.WebCMSEntities();
  61. string MonthString = month + "-" + checkMonth;
  62. //统计交易增量
  63. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  64. //创客团队交易额
  65. string sql = "";
  66. int rownum = 0;
  67. int num = 0;
  68. 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);
  69. function.WriteLog("交易数:" + dt.Rows.Count.ToString(), "领导人达标奖励日志");
  70. foreach(DataRow dr in dt.Rows)
  71. {
  72. rownum += 1;
  73. function.WriteLog("rownum:" + rownum.ToString(), "领导人达标奖励日志");
  74. int UserId = int.Parse(dr["UserId"].ToString());
  75. decimal TradeAmount = decimal.Parse(dr["Amt"].ToString());
  76. //稳定期B
  77. bool check = db.TradeDaySummary2.Any(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team");
  78. if (check)
  79. {
  80. TradeAmount += db.TradeDaySummary2.Where(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team").Sum(m => m.ProfitTradeAmt + m.ProfitDebitTradeAmt);
  81. }
  82. //码牌团队交易额(直联)
  83. check = mpmaindb.UserAmountSummary.Any(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.IsAct == 1);
  84. if (check)
  85. {
  86. TradeAmount += mpmaindb.UserAmountSummary.Where(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.IsAct == 1).Sum(m => m.TotalAmount) * 4;
  87. }
  88. check = mpmaindb.UserAmountSummary.Any(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.IsAct == 1);
  89. if (check)
  90. {
  91. TradeAmount += mpmaindb.UserAmountSummary.Where(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.IsAct == 1).Sum(m => m.TotalAmount);
  92. }
  93. //码牌团队交易额(银联)
  94. check = mpmaindb2.UserAmountSummary.Any(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.IsAct == 1);
  95. if (check)
  96. {
  97. TradeAmount += mpmaindb2.UserAmountSummary.Where(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.IsAct == 1).Sum(m => m.TotalAmount) * 4;
  98. }
  99. check = mpmaindb2.UserAmountSummary.Any(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.IsAct == 1);
  100. if (check)
  101. {
  102. TradeAmount += mpmaindb2.UserAmountSummary.Where(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.IsAct == 1).Sum(m => m.TotalAmount);
  103. }
  104. //广电卡扶持期按1万/张计入职级
  105. check = db.UserTradeMonthSummary.Any(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.BrandId == 14);
  106. if (check)
  107. {
  108. int ActCount = db.UserTradeMonthSummary.Where(m => m.UserId == UserId && m.TradeMonth == month && m.SeoTitle == "team" && m.BrandId == 14).Sum(m => m.ActiveBuddyMerStatus);
  109. ActCount += UserDataDbconn.Instance.GetSimActTradeAmt(UserId, month, "Team");
  110. if(db.UserSimActSummary.Any(m => m.UserId == UserId && m.Kind == 1))
  111. {
  112. ActCount -= db.UserSimActSummary.Where(m => m.UserId == UserId && m.Kind == 1).Sum(m => m.ActCount);
  113. }
  114. TradeAmount += ActCount * 10000;
  115. }
  116. sql += "insert into LeaderCompTradeStat (CreateDate,UserId,StatMonth,TradeAmount) values (now(), " + UserId + ", '" + month + "-" + checkMonth + "', " + TradeAmount + ");\n";
  117. num += 1;
  118. if(num >= 200)
  119. {
  120. function.WriteLog("执行一次sql:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  121. CustomerSqlConn.op(sql, MysqlConn.SqlConnStr);
  122. sql = "";
  123. num = 0;
  124. }
  125. }
  126. if(!string.IsNullOrEmpty(sql))
  127. {
  128. function.WriteLog("最后执行sql:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  129. CustomerSqlConn.op(sql, MysqlConn.SqlConnStr);
  130. }
  131. 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);
  132. function.WriteLog("start2-----" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  133. //计算创客名下的达标人数,直推和间接
  134. function.WriteLog("MonthString:" + MonthString, "领导人达标奖励日志");
  135. Thread.Sleep(600000);
  136. // List<LeaderCompTradeStat> stats = db.LeaderCompTradeStat.Where(m => m.StatMonth == MonthString).ToList();
  137. 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.SqlConnStr);
  138. function.WriteLog(stats.Rows.Count.ToString(), "领导人达标奖励日志");
  139. foreach(DataRow stat in stats.Rows)
  140. {
  141. int UserId = int.Parse(function.CheckInt(stat["UserId"].ToString()));
  142. int ParentUserId = int.Parse(function.CheckInt(stat["ParentUserId"].ToString()));
  143. decimal totalAmount = decimal.Parse(function.CheckNum(stat["TradeAmount"].ToString()));
  144. decimal smallTradeAmount = decimal.Parse(function.CheckNum(stat["SmallTradeAmount"].ToString()));
  145. string ParentNav = stat["ParentNav"].ToString();
  146. function.WriteLog("1-----" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  147. function.WriteLog("UserId:" + UserId, "领导人达标奖励日志");
  148. function.WriteLog("totalAmount:" + totalAmount, "领导人达标奖励日志");
  149. function.WriteLog("3-----" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  150. //直推创客交易额大于等于3000万即成为达标市场
  151. if(totalAmount >= 30000000 && !string.IsNullOrEmpty(ParentNav))
  152. {
  153. function.WriteLog("4-----" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  154. LeaderCompTmp tmp = db.LeaderCompTmp.FirstOrDefault(m => m.Id == UserId);
  155. if(tmp == null)
  156. {
  157. tmp = db.LeaderCompTmp.Add(new LeaderCompTmp()
  158. {
  159. Id = UserId,
  160. ParentUserId = ParentUserId,
  161. ParentNav = ParentNav
  162. }).Entity;
  163. db.SaveChanges();
  164. }
  165. tmp.EveryMonthData += "1";
  166. db.SaveChanges();
  167. function.WriteLog("5-----" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  168. }
  169. if(smallTradeAmount >= 30000000 && !string.IsNullOrEmpty(ParentNav))
  170. {
  171. function.WriteLog("4-----" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  172. LeaderCompTmp tmp = db.LeaderCompTmp.FirstOrDefault(m => m.Id == UserId);
  173. if(tmp == null)
  174. {
  175. tmp = db.LeaderCompTmp.Add(new LeaderCompTmp()
  176. {
  177. Id = UserId,
  178. ParentUserId = ParentUserId,
  179. ParentNav = ParentNav
  180. }).Entity;
  181. db.SaveChanges();
  182. }
  183. tmp.EveryMonthData += "2";
  184. db.SaveChanges();
  185. function.WriteLog("5-----" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  186. }
  187. function.WriteLog("7-----" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  188. function.WriteLog(DateTime.Now.ToString() + "------" + UserId, "领导人达标奖励日志");
  189. }
  190. db.SaveChanges();
  191. opdb.Dispose();
  192. mpmaindb.Dispose();
  193. mpmaindb2.Dispose();
  194. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  195. }
  196. catch(Exception ex)
  197. {
  198. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "领导达标人奖励异常");
  199. }
  200. }
  201. private void SendPrize(string month, string checkDate)
  202. {
  203. string MonthFlag = month + "-" + checkDate;
  204. WebCMSEntities db = new WebCMSEntities();
  205. DataTable dt = CustomerSqlConn.dtable("select ParentUserId,ParentNav,count(1) from LeaderCompTmp where SUBSTR(EveryMonthData,1,1)='1' GROUP BY ParentUserId,ParentNav", MysqlConn.SqlConnStr);
  206. foreach(DataRow dr in dt.Rows)
  207. {
  208. int ParentUserId = int.Parse(function.CheckInt(dr["ParentUserId"].ToString()));
  209. string ParentNav = dr["ParentNav"].ToString();
  210. int Count = int.Parse(function.CheckInt(dr[2].ToString()));
  211. LeaderCompPrize edit = db.LeaderCompPrize.FirstOrDefault(m => m.StatMonth == MonthFlag && m.UserId == ParentUserId);
  212. if(edit == null)
  213. {
  214. edit = db.LeaderCompPrize.Add(new LeaderCompPrize()
  215. {
  216. CreateDate = DateTime.Now,
  217. StatMonth = MonthFlag,
  218. UserId = ParentUserId,
  219. }).Entity;
  220. db.SaveChanges();
  221. }
  222. edit.DirectCount = Count; //达标市场
  223. 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.SqlConnStr);
  224. foreach(DataRow drsub in dtsub.Rows)
  225. {
  226. int SubCount = int.Parse(function.CheckInt(drsub[1].ToString()));
  227. edit.NotDirectCount += SubCount; //深度达标总数
  228. if(SubCount > Count)
  229. {
  230. SubCount = Count;
  231. }
  232. edit.SecDirectCount += SubCount; //深度达标计奖数
  233. }
  234. edit.CompPrize = edit.SecDirectCount * 500;
  235. db.SaveChanges();
  236. }
  237. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  238. db.Dispose();
  239. }
  240. private decimal GetPrize(int count, int direct)
  241. {
  242. decimal prize = 0;
  243. // if (count + direct >= 30)
  244. // {
  245. // prize = 3000 * (count + direct * 2);
  246. // }
  247. // else if (count + direct >= 25)
  248. // {
  249. // prize = 2500 * (count + direct * 2);
  250. // }
  251. // else if (count + direct >= 20)
  252. // {
  253. // prize = 2000 * (count + direct * 2);
  254. // }
  255. // else if (count + direct >= 15)
  256. // {
  257. // prize = 1500 * (count + direct * 2);
  258. // }
  259. // else if (count + direct >= 10)
  260. // {
  261. // prize = 1000 * (count + direct * 2);
  262. // }
  263. // else if (count + direct >= 5)
  264. // {
  265. // prize = 500 * (count + direct * 2);
  266. // }
  267. prize = 500 * count;
  268. return prize;
  269. }
  270. }
  271. }