LeaderCompPrize2Helper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 LeaderCompPrize2Helper
  12. {
  13. public readonly static LeaderCompPrize2Helper Instance = new LeaderCompPrize2Helper();
  14. private LeaderCompPrize2Helper()
  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 > 3 && 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("yyyyMM"));
  36. }
  37. Ready(DateTime.Now.ToString("yyyyMM"));
  38. }
  39. }
  40. Thread.Sleep(60000);
  41. }
  42. }
  43. public void StartTest()//启动
  44. {
  45. Thread thread = new Thread(ListenTest);
  46. thread.IsBackground = true;
  47. thread.Start();
  48. }
  49. public void ListenTest()//启动
  50. {
  51. string check = function.ReadInstance("/LeaderComp/Repeat/202502.txt");
  52. if (string.IsNullOrEmpty(check))
  53. {
  54. function.WritePage("/LeaderComp/Repeat/", "202502.txt", DateTime.Now.ToString());
  55. Ready("202502");
  56. }
  57. check = function.ReadInstance("/LeaderComp/Repeat/202503.txt");
  58. if (string.IsNullOrEmpty(check))
  59. {
  60. function.WritePage("/LeaderComp/Repeat/", "202503.txt", DateTime.Now.ToString());
  61. Ready("202503");
  62. }
  63. }
  64. public void Ready(string curMonth)
  65. {
  66. CustomerSqlConn.op("delete from LeaderCompTradeStat;delete from LeaderCompTmp;delete from LeaderCompPrize;delete from LeaderCompAddTrade;", MysqlConn.SqlConnStr);
  67. CustomerSqlConn.op("delete from kxs_shd_leader where trade_month='" + curMonth + "'", MysqlConn.JavaUserSqlConnStr);
  68. doSomething(curMonth);
  69. SendPrize(curMonth);
  70. }
  71. public void doSomething(string month)
  72. {
  73. try
  74. {
  75. WebCMSEntities db = new WebCMSEntities();
  76. string MonthString = month;
  77. string Now = DateTime.Parse(month.Substring(0, 4) + "-" + month.Substring(4, 2) + "-01 00:00:00").AddMonths(1).ToString("yyyy-MM-dd HH:mm:ss");
  78. string PreMonthString = DateTime.Parse(month.Substring(0, 4) + "-" + month.Substring(4, 2) + "-01 00:00:00").AddMonths(-1).ToString("yyyyMM");
  79. //统计交易增量
  80. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  81. //创客团队交易额
  82. string sql = "";
  83. int rownum = 0;
  84. int num = 0;
  85. List<LeaderCompUser> UserData = new List<LeaderCompUser>();
  86. string rootsql = "select tb.user_id,pid,pid_path,leader_type,trade_amt from (select user_id,trade_amt from kxs_user_trade_info where trade_month='" + month + "' and trade_amt>0) tb LEFT JOIN kxs_user u on tb.user_id=u.id LEFT JOIN kxs_leader l on tb.user_id=l.id and l.expired_date>='" + Now + "'";
  87. function.WriteLog(rootsql, "领导人达标奖励日志");
  88. DataTable dt = CustomerSqlConn.dtable(rootsql, MysqlConn.JavaUserSqlConnStr);
  89. foreach(DataRow dr in dt.Rows)
  90. {
  91. int UserId = int.Parse(dr["user_id"].ToString());
  92. int ParentUserId = int.Parse(function.CheckInt(dr["pid"].ToString()));
  93. string ParentNav = dr["pid_path"].ToString();
  94. int LeaderType = int.Parse(function.CheckInt(dr["leader_type"].ToString()));
  95. decimal Amt = decimal.Parse(function.CheckNum(dr["trade_amt"].ToString()));
  96. LeaderCompUser item = UserData.FirstOrDefault(m => m.UserId == UserId);
  97. if(item == null)
  98. {
  99. UserData.Add(new LeaderCompUser()
  100. {
  101. UserId = UserId,
  102. ParentUserId = ParentUserId,
  103. ParentNav = ParentNav,
  104. LeaderType = LeaderType,
  105. Amt = Amt,
  106. });
  107. }
  108. else
  109. {
  110. item.Amt += Amt;
  111. }
  112. }
  113. function.WriteLog("交易数:" + dt.Rows.Count.ToString(), "领导人达标奖励日志");
  114. foreach(LeaderCompUser SubUser in UserData)
  115. {
  116. rownum += 1;
  117. function.WriteLog("rownum:" + rownum.ToString(), "领导人达标奖励日志");
  118. int UserId = SubUser.UserId;
  119. function.WriteLog("UserId:" + UserId.ToString(), "领导人达标奖励日志");
  120. decimal TradeAmount = SubUser.Amt;
  121. int ParentUserId = SubUser.ParentUserId;
  122. string ParentNav = SubUser.ParentNav;
  123. int LeaderType = SubUser.LeaderType;
  124. sql += "insert into LeaderCompTradeStat (LeaderType,ParentNav,ParentUserId,TradeAmount,StatMonth,UserId,CreateDate) values (" + LeaderType + ", '" + ParentNav + "', " + ParentUserId + ", " + TradeAmount + ", '" + month + "', " + UserId + ",now());\n";
  125. num += 1;
  126. if(num >= 200)
  127. {
  128. CustomerSqlConn.op(sql, MysqlConn.SqlConnStr);
  129. sql = "";
  130. num = 0;
  131. }
  132. }
  133. if(!string.IsNullOrEmpty(sql))
  134. {
  135. CustomerSqlConn.op(sql, MysqlConn.SqlConnStr);
  136. }
  137. //计算创客名下的达标人数,直推和间接
  138. function.WriteLog("MonthString:" + MonthString, "领导人达标奖励日志");
  139. //计算达标市场
  140. DataTable tmps = CustomerSqlConn.dtable("select id,pid,pid_path from kxs_user where id in ((select user_id from kxs_user_trade_info where trade_month='" + month + "' and is_vip=1))", MysqlConn.JavaUserSqlConnStr);
  141. function.WriteLog(tmps.Rows.Count.ToString(), "领导人达标奖励日志");
  142. foreach(DataRow stat in tmps.Rows)
  143. {
  144. int UserId = int.Parse(function.CheckInt(stat["id"].ToString()));
  145. int ParentUserId = int.Parse(function.CheckInt(stat["pid"].ToString()));
  146. string ParentNav = stat["pid_path"].ToString();
  147. LeaderCompTmp tmp = db.LeaderCompTmp.FirstOrDefault(m => m.Id == UserId);
  148. if(tmp == null)
  149. {
  150. tmp = db.LeaderCompTmp.Add(new LeaderCompTmp()
  151. {
  152. Id = UserId,
  153. ParentUserId = ParentUserId,
  154. ParentNav = ParentNav
  155. }).Entity;
  156. db.SaveChanges();
  157. }
  158. tmp.EveryMonthData = "1";
  159. db.SaveChanges();
  160. }
  161. Thread.Sleep(60000);
  162. // List<LeaderCompTradeStat> stats = db.LeaderCompTradeStat.Where(m => m.StatMonth == MonthString).ToList();
  163. 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 + "'", MysqlConn.ReadSqlConnStr);
  164. function.WriteLog(stats.Rows.Count.ToString(), "领导人达标奖励日志");
  165. string sqlString = "";
  166. foreach(DataRow stat in stats.Rows)
  167. {
  168. int UserId = int.Parse(function.CheckInt(stat["UserId"].ToString()));
  169. int ParentUserId = int.Parse(function.CheckInt(stat["ParentUserId"].ToString()));
  170. int LeaderType = int.Parse(function.CheckInt(stat["LeaderType"].ToString()));
  171. decimal totalAmount = decimal.Parse(function.CheckNum(stat["TradeAmount"].ToString()));
  172. decimal bigTradeAmount = decimal.Parse(function.CheckNum(stat["BigTradeAmount"].ToString()));
  173. decimal smallTradeAmount = totalAmount - bigTradeAmount;
  174. int BigCount = int.Parse(function.CheckInt(stat["BigCount"].ToString()));
  175. int SmallCount = int.Parse(function.CheckInt(stat["SmallCount"].ToString()));
  176. int LeaderCount = int.Parse(function.CheckInt(stat["LeaderCount"].ToString()));
  177. int OperaterCount = int.Parse(function.CheckInt(stat["OperaterCount"].ToString()));
  178. string ParentNav = stat["ParentNav"].ToString();
  179. //计算达标市场
  180. // if((BigCount == 1 && smallTradeAmount >= 12000000) || BigCount >= 2 || smallTradeAmount >= 30000000)
  181. // {
  182. // LeaderCompTmp tmp = db.LeaderCompTmp.FirstOrDefault(m => m.Id == UserId);
  183. // if(tmp == null)
  184. // {
  185. // tmp = db.LeaderCompTmp.Add(new LeaderCompTmp()
  186. // {
  187. // Id = UserId,
  188. // ParentUserId = ParentUserId,
  189. // ParentNav = ParentNav
  190. // }).Entity;
  191. // db.SaveChanges();
  192. // }
  193. // tmp.EveryMonthData = "1";
  194. // db.SaveChanges();
  195. // }
  196. //培养奖
  197. if(smallTradeAmount < 30000000) BigCount -= 1;
  198. if(BigCount > 0)
  199. {
  200. // decimal leaderOnePrize = 2500;
  201. // decimal operaterOnePrize = 3000;
  202. decimal onePrize = 0;
  203. if(BigCount == 1) onePrize = 2000;
  204. if(BigCount == 2) onePrize = 2500;
  205. if(LeaderType == 2) onePrize = 2500;
  206. if(BigCount == 3)
  207. {
  208. onePrize = 3000;
  209. // leaderOnePrize = 3000;
  210. }
  211. if(LeaderType > 2) onePrize = 3000;
  212. if(BigCount == 4)
  213. {
  214. onePrize = 4000;
  215. // leaderOnePrize = 4000;
  216. // operaterOnePrize = 4000;
  217. }
  218. if(BigCount == 5)
  219. {
  220. onePrize = 5000;
  221. // leaderOnePrize = 5000;
  222. // operaterOnePrize = 5000;
  223. }
  224. if(BigCount >= 6)
  225. {
  226. onePrize = 6000;
  227. // leaderOnePrize = 6000;
  228. // operaterOnePrize = 6000;
  229. }
  230. // decimal tradePrize = onePrize * (BigCount - LeaderCount - OperaterCount) + LeaderCount * leaderOnePrize + OperaterCount * operaterOnePrize; //培养奖金额
  231. decimal tradePrize = onePrize * BigCount; //培养奖金额
  232. 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 + "'," + UserId + ");";
  233. }
  234. }
  235. db.SaveChanges();
  236. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  237. CustomerSqlConn.op(sqlString, MysqlConn.JavaUserSqlConnStr);
  238. }
  239. catch(Exception ex)
  240. {
  241. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "领导达标人奖励异常");
  242. }
  243. }
  244. private void SendPrize(string month)
  245. {
  246. string sql = "";
  247. string MonthFlag = month;
  248. WebCMSEntities db = new WebCMSEntities();
  249. // DataTable dt = CustomerSqlConn.dtable("select * from (select UserId,ParentUserId,ParentNav,(select count(1) from LeaderCompTradeStat where ParentUserId=p.UserId and TradeAmount>=30000000) BigCount from LeaderCompTradeStat p where StatMonth='" + month + "' and LeaderType>=2) tb where BigCount>0", MysqlConn.SqlConnStr);
  250. DataTable dt = CustomerSqlConn.dtable("select * from (select UserId,ParentUserId,ParentNav,(select count(1) from LeaderCompTradeStat where ParentUserId=p.UserId and TradeAmount>=30000000) BigCount from LeaderCompTradeStat p where StatMonth='" + month + "') tb where BigCount>0", MysqlConn.SqlConnStr);
  251. foreach(DataRow dr in dt.Rows)
  252. {
  253. int UserId = int.Parse(function.CheckInt(dr["UserId"].ToString()));
  254. function.WriteLog(UserId.ToString(), "核对");
  255. string ParentNav = dr["ParentNav"].ToString();
  256. int Count = int.Parse(function.CheckInt(dr["BigCount"].ToString()));
  257. function.WriteLog(Count.ToString(), "核对");
  258. LeaderCompPrize edit = db.LeaderCompPrize.FirstOrDefault(m => m.StatMonth == MonthFlag && m.UserId == UserId);
  259. if(edit == null)
  260. {
  261. edit = db.LeaderCompPrize.Add(new LeaderCompPrize()
  262. {
  263. CreateDate = DateTime.Now,
  264. StatMonth = MonthFlag,
  265. UserId = UserId,
  266. }).Entity;
  267. db.SaveChanges();
  268. }
  269. edit.DirectCount = Count; //达标市场
  270. DataTable dtsub = CustomerSqlConn.dtable("select UserId from LeaderCompTradeStat where ParentUserId=" + UserId, MysqlConn.SqlConnStr);
  271. foreach(DataRow drsub in dtsub.Rows)
  272. {
  273. int SubCount = 0;
  274. DataTable subuser = CustomerSqlConn.dtable("select count(1) from LeaderCompTmp where Id=" + drsub["UserId"].ToString() + " or ParentNav like '%," + drsub["UserId"].ToString() + ",%'", MysqlConn.SqlConnStr);
  275. if(subuser.Rows.Count > 0)
  276. {
  277. SubCount = int.Parse(function.CheckInt(subuser.Rows[0][0].ToString()));
  278. }
  279. function.WriteLog("SubCount:" + SubCount, "核对");
  280. edit.NotDirectCount += SubCount; //深度达标总数
  281. if(SubCount > Count)
  282. {
  283. SubCount = Count;
  284. }
  285. edit.SecDirectCount += SubCount; //深度达标计奖数
  286. }
  287. function.WriteLog(edit.SecDirectCount.ToString(), "核对");
  288. decimal CompPrize = edit.SecDirectCount * 1000;
  289. edit.CompPrize = CompPrize;
  290. db.SaveChanges();
  291. LeaderCompTradeStat stat = db.LeaderCompTradeStat.FirstOrDefault(m => m.UserId == UserId) ?? new LeaderCompTradeStat();
  292. // if(stat.LeaderType > 1 && CompPrize > 0)
  293. if(CompPrize > 0)
  294. {
  295. sql += "insert into kxs_shd_leader (shd_type,trade_amt,source_bonus_amt,create_time,trade_month,user_id) values (1," + stat.TradeAmount + "," + CompPrize + ",now(),'" + MonthFlag + "'," + UserId + ");";
  296. }
  297. }
  298. CustomerSqlConn.op(sql, MysqlConn.JavaUserSqlConnStr);
  299. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  300. db.Dispose();
  301. }
  302. }
  303. }