LeaderCompPrizeHelper.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 < 19)
  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 StartTest()//启动
  48. {
  49. Thread thread = new Thread(ListenTest);
  50. thread.IsBackground = true;
  51. thread.Start();
  52. }
  53. public void ListenTest()//启动
  54. {
  55. Ready("202410");
  56. }
  57. public void Ready(string curMonth)
  58. {
  59. CustomerSqlConn.op("delete from LeaderCompTradeStat;delete from LeaderCompTmp;delete from LeaderCompPrize;delete from LeaderCompAddTrade;", MysqlConn.SqlConnStr);
  60. doSomething(curMonth);
  61. SendPrize(curMonth, curMonth);
  62. }
  63. public void doSomething(string month)
  64. {
  65. try
  66. {
  67. WebCMSEntities db = new WebCMSEntities();
  68. ReadModels.WebCMSEntities readdb = new ReadModels.WebCMSEntities();
  69. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  70. MpMainModels.WebCMSEntities mpmaindb = new MpMainModels.WebCMSEntities();
  71. MpMainModels2.WebCMSEntities mpmaindb2 = new MpMainModels2.WebCMSEntities();
  72. string MonthString = month;
  73. //统计交易增量
  74. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  75. //创客团队交易额
  76. string sql = "";
  77. int rownum = 0;
  78. int num = 0;
  79. DataTable dt = CustomerSqlConn.dtable("select user_id,pid,pid_path,leader_type,(case when amt is null then 0 else amt end) amt from (select tb1.user_id,pid,pid_path,leader_type,trade_amt+act_trade_amt+unact_trade_amt+act_num-logout_num amt from (select user_id,sum(help_direct_trade_amt+pro_direct_trade_amt+pro_a_direct_trade_amt+pro_b_direct_trade_amt) trade_amt from kxs_user_trade_#{now,yyyyMM}# where total_type=1 and brand_id not in (18,19,20,21,29,101,100) group by user_id) tb1 LEFT JOIN (select user_id,sum(help_direct_trade_amt)*4 act_trade_amt,sum(pro_direct_trade_amt) unact_trade_amt from kxs_user_trade_#{now,yyyyMM}# where total_type=1 and brand_id in (101,100,18,19,20,21,29) group by user_id) tb2 on tb1.user_id=tb2.user_id LEFT JOIN (select user_id,sum(act_num)*10000 act_num from kxs_user_act_trade where total_type=1 and brand_id in (14,23,24,25,26,32) and trade_month<'#{now,yyyyMM}#' group by user_id) tb3 on tb1.user_id=tb3.user_id LEFT JOIN (select user_id,logout_num*10000 logout_num from kxs_user_logout_trade where total_type=1 and trade_month='#{now,-1,MONTH,yyyyMM}#') tb4 on tb1.user_id=tb4.user_id LEFT JOIN kxs_user.kxs_user u on tb1.user_id=u.id LEFT JOIN kxs_user.kxs_leader l on tb1.user_id=l.id and l.expired_date>now()) tball where amt>0", MysqlConn.JavaStatSqlConnStr);
  80. function.WriteLog("交易数:" + dt.Rows.Count.ToString(), "领导人达标奖励日志");
  81. foreach(DataRow dr in dt.Rows)
  82. {
  83. rownum += 1;
  84. function.WriteLog("rownum:" + rownum.ToString(), "领导人达标奖励日志");
  85. int UserId = int.Parse(dr["user_id"].ToString());
  86. decimal TradeAmount = decimal.Parse(dr["amt"].ToString());
  87. int ParentUserld = int.Parse(dr["pid"].ToString());
  88. string ParentNav = dr["pid_path"].ToString();
  89. int LeaderType = int.Parse(dr["leader_type"].ToString());
  90. sql += "insert into LeaderCompTradeStat (LeaderType,ParentNav,ParentUserld,TradeAmount,StatMonth,Userld,CreateDate) values (" + LeaderType + ", '" + ParentNav + "', " + ParentUserld + ", " + TradeAmount + ", '" + month + "', " + UserId + ",now());\n";
  91. num += 1;
  92. if(num >= 200)
  93. {
  94. function.WriteLog("执行一次sql:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  95. CustomerSqlConn.op(sql, MysqlConn.SqlConnStr);
  96. sql = "";
  97. num = 0;
  98. }
  99. }
  100. if(!string.IsNullOrEmpty(sql))
  101. {
  102. function.WriteLog("最后执行sql:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  103. CustomerSqlConn.op(sql, MysqlConn.SqlConnStr);
  104. }
  105. //计算创客名下的达标人数,直推和间接
  106. function.WriteLog("MonthString:" + MonthString, "领导人达标奖励日志");
  107. Thread.Sleep(600);
  108. // List<LeaderCompTradeStat> stats = db.LeaderCompTradeStat.Where(m => m.StatMonth == MonthString).ToList();
  109. DataTable stats = CustomerSqlConn.dtable("select UserId,ParentUserId,ParentNav,TradeAmount,LeaderType,(select sum(TradeAmount) from LeaderCompTradeStat where ParentUserId=p.UserId and TradeAmount>=30000000) BigTradeAmount,(select count(1) from LeaderCompTradeStat where ParentUserId=p.UserId and TradeAmount>=30000000) BigCount,(select count(1) from LeaderCompTradeStat where ParentUserId=p.UserId and TradeAmount<30000000) SmallCount,(select count(1) from LeaderCompTradeStat where ParentUserId=p.UserId and LeaderType=2) LeaderCount,(select count(1) from LeaderCompTradeStat where ParentUserId=p.UserId and LeaderType>2) OperaterCount from LeaderCompTradeStat p where StatMonth='" + MonthString + "' and UserId>1", MysqlConn.SqlConnStr);
  110. function.WriteLog(stats.Rows.Count.ToString(), "领导人达标奖励日志");
  111. string sql = "";
  112. foreach(DataRow stat in stats.Rows)
  113. {
  114. int UserId = int.Parse(function.CheckInt(stat["UserId"].ToString()));
  115. int ParentUserId = int.Parse(function.CheckInt(stat["ParentUserId"].ToString()));
  116. int LeaderType = int.Parse(function.CheckInt(stat["LeaderType"].ToString()));
  117. decimal totalAmount = decimal.Parse(function.CheckNum(stat["TradeAmount"].ToString()));
  118. decimal bigTradeAmount = decimal.Parse(function.CheckNum(stat["BigTradeAmount"].ToString()));
  119. decimal smallTradeAmount = totalAmount - bigTradeAmount;
  120. int BigCount = int.Parse(function.CheckInt(stat["BigCount"].ToString()));
  121. int SmallCount = int.Parse(function.CheckInt(stat["SmallCount"].ToString()));
  122. int LeaderCount = int.Parse(function.CheckInt(stat["LeaderCount"].ToString()));
  123. int OperaterCount = int.Parse(function.CheckInt(stat["OperaterCount"].ToString()));
  124. string ParentNav = stat["ParentNav"].ToString();
  125. //计算达标市场
  126. if((totalAmount >= 30000000 && totalAmount - bigTradeAmount >= 12000000) || BigCount >= 2 || totalAmount - bigTradeAmount >= 30000000)
  127. {
  128. LeaderCompTmp tmp = db.LeaderCompTmp.FirstOrDefault(m => m.Id == UserId);
  129. if(tmp == null)
  130. {
  131. tmp = db.LeaderCompTmp.Add(new LeaderCompTmp()
  132. {
  133. Id = UserId,
  134. ParentUserId = ParentUserId,
  135. ParentNav = ParentNav
  136. }).Entity;
  137. db.SaveChanges();
  138. }
  139. tmp.EveryMonthData = "1";
  140. db.SaveChanges();
  141. }
  142. //培养奖
  143. if(totalAmount - bigTradeAmount < 30000000) BigCount -= 1;
  144. if(BigCount > 0 && LeaderType > 1)
  145. {
  146. decimal leaderOnePrize = 2500;
  147. decimal operaterOnePrize = 3000;
  148. decimal onePrize = 0;
  149. if(BigCount == 1) onePrize = 2000;
  150. if(BigCount == 2) onePrize = 2500;
  151. if(BigCount == 3)
  152. {
  153. onePrize = 3000;
  154. leaderOnePrize = 3000;
  155. }
  156. if(BigCount == 4)
  157. {
  158. onePrize = 4000;
  159. leaderOnePrize = 4000;
  160. operaterOnePrize = 4000;
  161. }
  162. if(BigCount == 5)
  163. {
  164. onePrize = 5000;
  165. leaderOnePrize = 5000;
  166. operaterOnePrize = 5000;
  167. }
  168. if(BigCount >= 6)
  169. {
  170. onePrize = 6000;
  171. leaderOnePrize = 6000;
  172. operaterOnePrize = 6000;
  173. }
  174. decimal tradePrize = onePrize * (BigCount - LeaderCount - OperaterCount) + LeaderCount * leaderOnePrize + OperaterCount * operaterOnePrize; //培养奖金额
  175. sql += "insert into kxs_shd_leader (shd_type,trade_amt,source_bonus_amt,create_time,trade_month,user_id) values (0," + totalAmount + "," + tradePrize + ",now(),'" + MonthString + "'," + ParentUserId + ");";
  176. }
  177. }
  178. db.SaveChanges();
  179. readdb.SaveChanges();
  180. opdb.Dispose();
  181. mpmaindb.Dispose();
  182. mpmaindb2.Dispose();
  183. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  184. CustomerSqlConn.op(sql, MysqlConn.JavaUserSqlConnStr);
  185. }
  186. catch(Exception ex)
  187. {
  188. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "领导达标人奖励异常");
  189. }
  190. }
  191. private void SendPrize(string month, string checkDate)
  192. {
  193. string sql = "";
  194. string MonthFlag = month + "-" + checkDate;
  195. WebCMSEntities db = new WebCMSEntities();
  196. DataTable dt = CustomerSqlConn.dtable("select ParentUserId,ParentNav,count(1) from LeaderCompTmp where EveryMonthData='1' GROUP BY ParentUserId,ParentNav", MysqlConn.SqlConnStr);
  197. foreach(DataRow dr in dt.Rows)
  198. {
  199. int ParentUserId = int.Parse(function.CheckInt(dr["ParentUserId"].ToString()));
  200. string ParentNav = dr["ParentNav"].ToString();
  201. int Count = int.Parse(function.CheckInt(dr[2].ToString()));
  202. LeaderCompPrize edit = db.LeaderCompPrize.FirstOrDefault(m => m.StatMonth == MonthFlag && m.UserId == ParentUserId);
  203. if(edit == null)
  204. {
  205. edit = db.LeaderCompPrize.Add(new LeaderCompPrize()
  206. {
  207. CreateDate = DateTime.Now,
  208. StatMonth = MonthFlag,
  209. UserId = ParentUserId,
  210. }).Entity;
  211. db.SaveChanges();
  212. }
  213. edit.DirectCount = Count; //达标市场
  214. DataTable dtsub = CustomerSqlConn.dtable("select SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(ParentNav, '" + ParentNav + "', ''),',',1),',',-1),count(1) from LeaderCompTmp where ParentUserId!=" + ParentUserId + " and ParentNav like '%," + ParentUserId + ",%' and EveryMonthData='1' group by SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(ParentNav, '" + ParentNav + "', ''),',',1),',',-1)", MysqlConn.SqlConnStr);
  215. foreach(DataRow drsub in dtsub.Rows)
  216. {
  217. int SubCount = int.Parse(function.CheckInt(drsub[1].ToString()));
  218. edit.NotDirectCount += SubCount; //深度达标总数
  219. if(SubCount > Count)
  220. {
  221. SubCount = Count;
  222. }
  223. edit.SecDirectCount += SubCount; //深度达标计奖数
  224. }
  225. decimal CompPrize = edit.SecDirectCount * 1000;
  226. edit.CompPrize = CompPrize;
  227. db.SaveChanges();
  228. LeaderCompTradeStat stat = db.LeaderCompTradeStat.FirstOrDefault(m => m.UserId == ParentUserId) ?? new LeaderCompTradeStat();
  229. sql += "insert into kxs_shd_leader (shd_type,trade_amt,source_bonus_amt,create_time,trade_month,user_id) values (1," + CompPrize + "," + stat.TradeAmount + ",now(),'" + MonthFlag + "'," + ParentUserId + ");";
  230. }
  231. CustomerSqlConn.op(sql, MysqlConn.JavaUserSqlConnStr);
  232. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  233. db.Dispose();
  234. }
  235. }
  236. }