LeaderCompPrize2Helper.cs 17 KB

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