LeaderCompPrize2Helper.cs 17 KB

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