LeaderCompPrizeHelper.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. Ready(DateTime.Now.ToString("yyyyMM"));
  34. }
  35. }
  36. Thread.Sleep(60000);
  37. }
  38. }
  39. public void StartTest()//启动
  40. {
  41. Thread thread = new Thread(ListenTest);
  42. thread.IsBackground = true;
  43. thread.Start();
  44. }
  45. public void ListenTest()//启动
  46. {
  47. Ready("202410");
  48. }
  49. public void Ready(string curMonth)
  50. {
  51. CustomerSqlConn.op("delete from LeaderCompTradeStat;delete from LeaderCompTmp;delete from LeaderCompPrize;delete from LeaderCompAddTrade;", MysqlConn.SqlConnStr);
  52. CustomerSqlConn.op("delete from kxs_shd_leader where trade_month='" + curMonth + "'", MysqlConn.JavaUserSqlConnStr);
  53. doSomething(curMonth);
  54. SendPrize(curMonth, curMonth);
  55. }
  56. public void doSomething(string month)
  57. {
  58. try
  59. {
  60. WebCMSEntities db = new WebCMSEntities();
  61. string MonthString = month;
  62. string PreMonthString = DateTime.Parse(month.Substring(0, 4) + "-" + month.Substring(4, 2) + "-01 00:00:00").AddMonths(-1).ToString("yyyyMM");
  63. //统计交易增量
  64. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  65. //创客团队交易额
  66. string sql = "";
  67. int rownum = 0;
  68. int num = 0;
  69. 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_" + month + " 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_" + month + " 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<'" + month + "' 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='" + PreMonthString + "') 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);
  70. function.WriteLog("交易数:" + dt.Rows.Count.ToString(), "领导人达标奖励日志");
  71. foreach(DataRow dr in dt.Rows)
  72. {
  73. rownum += 1;
  74. function.WriteLog("rownum:" + rownum.ToString(), "领导人达标奖励日志");
  75. int UserId = int.Parse(dr["user_id"].ToString());
  76. decimal TradeAmount = decimal.Parse(dr["amt"].ToString());
  77. int ParentUserld = int.Parse(dr["pid"].ToString());
  78. string ParentNav = dr["pid_path"].ToString();
  79. int LeaderType = int.Parse(dr["leader_type"].ToString());
  80. sql += "insert into LeaderCompTradeStat (LeaderType,ParentNav,ParentUserld,TradeAmount,StatMonth,Userld,CreateDate) values (" + LeaderType + ", '" + ParentNav + "', " + ParentUserld + ", " + TradeAmount + ", '" + month + "', " + UserId + ",now());\n";
  81. num += 1;
  82. if(num >= 200)
  83. {
  84. function.WriteLog("执行一次sql:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  85. CustomerSqlConn.op(sql, MysqlConn.SqlConnStr);
  86. sql = "";
  87. num = 0;
  88. }
  89. }
  90. if(!string.IsNullOrEmpty(sql))
  91. {
  92. function.WriteLog("最后执行sql:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "领导人达标奖励日志");
  93. CustomerSqlConn.op(sql, MysqlConn.SqlConnStr);
  94. }
  95. //计算创客名下的达标人数,直推和间接
  96. function.WriteLog("MonthString:" + MonthString, "领导人达标奖励日志");
  97. Thread.Sleep(600);
  98. // List<LeaderCompTradeStat> stats = db.LeaderCompTradeStat.Where(m => m.StatMonth == MonthString).ToList();
  99. 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);
  100. function.WriteLog(stats.Rows.Count.ToString(), "领导人达标奖励日志");
  101. string sqlString = "";
  102. foreach(DataRow stat in stats.Rows)
  103. {
  104. int UserId = int.Parse(function.CheckInt(stat["UserId"].ToString()));
  105. int ParentUserId = int.Parse(function.CheckInt(stat["ParentUserId"].ToString()));
  106. int LeaderType = int.Parse(function.CheckInt(stat["LeaderType"].ToString()));
  107. decimal totalAmount = decimal.Parse(function.CheckNum(stat["TradeAmount"].ToString()));
  108. decimal bigTradeAmount = decimal.Parse(function.CheckNum(stat["BigTradeAmount"].ToString()));
  109. decimal smallTradeAmount = totalAmount - bigTradeAmount;
  110. int BigCount = int.Parse(function.CheckInt(stat["BigCount"].ToString()));
  111. int SmallCount = int.Parse(function.CheckInt(stat["SmallCount"].ToString()));
  112. int LeaderCount = int.Parse(function.CheckInt(stat["LeaderCount"].ToString()));
  113. int OperaterCount = int.Parse(function.CheckInt(stat["OperaterCount"].ToString()));
  114. string ParentNav = stat["ParentNav"].ToString();
  115. //计算达标市场
  116. if((totalAmount >= 30000000 && totalAmount - bigTradeAmount >= 12000000) || BigCount >= 2 || totalAmount - bigTradeAmount >= 30000000)
  117. {
  118. LeaderCompTmp tmp = db.LeaderCompTmp.FirstOrDefault(m => m.Id == UserId);
  119. if(tmp == null)
  120. {
  121. tmp = db.LeaderCompTmp.Add(new LeaderCompTmp()
  122. {
  123. Id = UserId,
  124. ParentUserId = ParentUserId,
  125. ParentNav = ParentNav
  126. }).Entity;
  127. db.SaveChanges();
  128. }
  129. tmp.EveryMonthData = "1";
  130. db.SaveChanges();
  131. }
  132. //培养奖
  133. if(totalAmount - bigTradeAmount < 30000000) BigCount -= 1;
  134. if(BigCount > 0 && LeaderType > 1)
  135. {
  136. decimal leaderOnePrize = 2500;
  137. decimal operaterOnePrize = 3000;
  138. decimal onePrize = 0;
  139. if(BigCount == 1) onePrize = 2000;
  140. if(BigCount == 2) onePrize = 2500;
  141. if(BigCount == 3)
  142. {
  143. onePrize = 3000;
  144. leaderOnePrize = 3000;
  145. }
  146. if(BigCount == 4)
  147. {
  148. onePrize = 4000;
  149. leaderOnePrize = 4000;
  150. operaterOnePrize = 4000;
  151. }
  152. if(BigCount == 5)
  153. {
  154. onePrize = 5000;
  155. leaderOnePrize = 5000;
  156. operaterOnePrize = 5000;
  157. }
  158. if(BigCount >= 6)
  159. {
  160. onePrize = 6000;
  161. leaderOnePrize = 6000;
  162. operaterOnePrize = 6000;
  163. }
  164. decimal tradePrize = onePrize * (BigCount - LeaderCount - OperaterCount) + LeaderCount * leaderOnePrize + OperaterCount * operaterOnePrize; //培养奖金额
  165. sqlString += "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 + ");";
  166. }
  167. }
  168. db.SaveChanges();
  169. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  170. CustomerSqlConn.op(sqlString, MysqlConn.JavaUserSqlConnStr);
  171. }
  172. catch(Exception ex)
  173. {
  174. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "领导达标人奖励异常");
  175. }
  176. }
  177. private void SendPrize(string month, string checkDate)
  178. {
  179. string sql = "";
  180. string MonthFlag = month + "-" + checkDate;
  181. WebCMSEntities db = new WebCMSEntities();
  182. DataTable dt = CustomerSqlConn.dtable("select ParentUserId,ParentNav,count(1) from LeaderCompTmp where EveryMonthData='1' GROUP BY ParentUserId,ParentNav", MysqlConn.SqlConnStr);
  183. foreach(DataRow dr in dt.Rows)
  184. {
  185. int ParentUserId = int.Parse(function.CheckInt(dr["ParentUserId"].ToString()));
  186. string ParentNav = dr["ParentNav"].ToString();
  187. int Count = int.Parse(function.CheckInt(dr[2].ToString()));
  188. LeaderCompPrize edit = db.LeaderCompPrize.FirstOrDefault(m => m.StatMonth == MonthFlag && m.UserId == ParentUserId);
  189. if(edit == null)
  190. {
  191. edit = db.LeaderCompPrize.Add(new LeaderCompPrize()
  192. {
  193. CreateDate = DateTime.Now,
  194. StatMonth = MonthFlag,
  195. UserId = ParentUserId,
  196. }).Entity;
  197. db.SaveChanges();
  198. }
  199. edit.DirectCount = Count; //达标市场
  200. 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);
  201. foreach(DataRow drsub in dtsub.Rows)
  202. {
  203. int SubCount = int.Parse(function.CheckInt(drsub[1].ToString()));
  204. edit.NotDirectCount += SubCount; //深度达标总数
  205. if(SubCount > Count)
  206. {
  207. SubCount = Count;
  208. }
  209. edit.SecDirectCount += SubCount; //深度达标计奖数
  210. }
  211. decimal CompPrize = edit.SecDirectCount * 1000;
  212. edit.CompPrize = CompPrize;
  213. db.SaveChanges();
  214. LeaderCompTradeStat stat = db.LeaderCompTradeStat.FirstOrDefault(m => m.UserId == ParentUserId) ?? new LeaderCompTradeStat();
  215. 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 + ");";
  216. }
  217. CustomerSqlConn.op(sql, MysqlConn.JavaUserSqlConnStr);
  218. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  219. db.Dispose();
  220. }
  221. }
  222. }