StatService.cs 156 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using LitJson;
  5. using System.Linq;
  6. using System.Data;
  7. using System.Threading;
  8. using MySystem.PxcModels;
  9. namespace MySystem
  10. {
  11. public class StatService
  12. {
  13. public readonly static StatService Instance = new StatService();
  14. private StatService()
  15. { }
  16. public void Start(JobMqMsg jobInfo)
  17. {
  18. string content = "";
  19. try
  20. {
  21. if(DateTime.Now.Hour > 0)
  22. {
  23. string doDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
  24. string flag = function.ReadInstance("/Stat/" + doDate + ".txt");
  25. if (string.IsNullOrEmpty(flag))
  26. {
  27. function.WritePage("/Stat/", doDate + ".txt", DateTime.Now.ToString("HH:mm:ss"));
  28. // dosomething1(doDate);
  29. // dosomething11(doDate);
  30. // dosomething2(doDate);
  31. StartEverDay(doDate);
  32. // StartEverDayV2(doDate);
  33. StatMerchantTrade(doDate);
  34. // dosomething4(doDate);
  35. // ListenFluxRecord(doDate);
  36. }
  37. // string Msg = "success";
  38. // jobInfo.Status = Msg == "success" ? 1 : 0;
  39. // jobInfo.Msg = Msg == "success" ? "执行完成" : Msg;
  40. // RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(jobInfo), "PublicBack");
  41. }
  42. }
  43. catch (Exception ex)
  44. {
  45. if (!string.IsNullOrEmpty(content))
  46. {
  47. Dictionary<string, string> data = new Dictionary<string, string>();
  48. data.Add("ErrTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  49. data.Add("ErrMsg", ex.ToString());
  50. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(data), "public_err");
  51. }
  52. else
  53. {
  54. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public_service");
  55. }
  56. }
  57. }
  58. public void ActiveReward(JobMqMsg jobInfo)
  59. {
  60. string content = "";
  61. try
  62. {
  63. dosomething3();
  64. activePrizeWithoutDeposit();
  65. activePrizeWithoutDeposit12();
  66. }
  67. catch (Exception ex)
  68. {
  69. if (!string.IsNullOrEmpty(content))
  70. {
  71. Dictionary<string, string> data = new Dictionary<string, string>();
  72. data.Add("ErrTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  73. data.Add("ErrMsg", ex.ToString());
  74. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(data), "public_err");
  75. }
  76. else
  77. {
  78. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public_service");
  79. }
  80. }
  81. }
  82. public void clear()
  83. {
  84. RedisDbconn.Instance.Clear("TotalAmount*");
  85. RedisDbconn.Instance.Clear("TotalCloudPayAmount*");
  86. RedisDbconn.Instance.Clear("TotalPosAmount*");
  87. RedisDbconn.Instance.Clear("TeamTotalAmount*");
  88. RedisDbconn.Instance.Clear("TeamTotalCloudPayAmount*");
  89. RedisDbconn.Instance.Clear("TeamTotalPosAmount*");
  90. // RedisDbconn.Instance.Clear("TotalUser*");
  91. // RedisDbconn.Instance.Clear("TeamTotalUser*");
  92. // RedisDbconn.Instance.Clear("AddUser*");
  93. // RedisDbconn.Instance.Clear("TeamAddUser*");
  94. // RedisDbconn.Instance.Clear("TotalPosMerchant*");
  95. // RedisDbconn.Instance.Clear("TeamTotalPosMerchant*");
  96. RedisDbconn.Instance.Clear("MerTotalAmount*");
  97. }
  98. // 1. 前一天的绑定记录和创客信息匹配,设置商户型创客
  99. public void dosomething1(string date)
  100. {
  101. // TODO: 每天扫描非商户型创客的持有机具数量
  102. SpModels.WebCMSEntities spdb = new SpModels.WebCMSEntities();
  103. WebCMSEntities db = new WebCMSEntities();
  104. DateTime yesterday = DateTime.Parse(date + " 00:00:00");
  105. DateTime today = DateTime.Parse(DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00");
  106. List<SpModels.BindRecord> binds = spdb.BindRecord.Where(m => m.CreateTime >= yesterday && m.CreateTime < today).ToList();
  107. foreach (SpModels.BindRecord bind in binds)
  108. {
  109. string Mobile = bind.MerNewSnNo;
  110. string MerNo = bind.MerNo;
  111. string BeforeNum = "";
  112. string AfterNum = "";
  113. if (Mobile.Contains("****") && Mobile.Length == 11)
  114. {
  115. BeforeNum = Mobile.Substring(0, 3);
  116. AfterNum = Mobile.Substring(7);
  117. }
  118. string Name = bind.MerName;
  119. if (bind.ProductType == "2")
  120. {
  121. if (Name.Contains("-"))
  122. {
  123. Name = Name.Split('-')[1];
  124. }
  125. else if (Name.Contains("_"))
  126. {
  127. Name = Name.Split('_')[1];
  128. }
  129. }
  130. else if (bind.ProductType == "4")
  131. {
  132. Name = bind.SeoTitle;
  133. }
  134. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Mobile.StartsWith(BeforeNum) && m.Mobile.EndsWith(AfterNum) && m.RealName == Name && m.AuthFlag == 1 && m.MerchantType == 0);
  135. if (user != null)
  136. {
  137. bool checkPos = db.PosMachinesTwo.Any(m => m.UserId == user.Id);
  138. if (!checkPos)
  139. {
  140. PxcModels.MachineForSnNo forSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == bind.MerSnNo) ?? new PxcModels.MachineForSnNo();
  141. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forSnNo.SnId && m.UserId != user.Id);
  142. if (pos != null)
  143. {
  144. pos.UserId = user.Id;
  145. user.MerchantType = 1;
  146. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == MerNo) ?? new PxcModels.PosMerchantInfo();
  147. merchant.UserId = user.Id;
  148. merchant.MerUserType = 1;
  149. pos.SeoTitle = user.Id.ToString(); // 记录商户型创客的Id
  150. db.SaveChanges();
  151. }
  152. }
  153. }
  154. }
  155. spdb.Dispose();
  156. db.Dispose();
  157. }
  158. // 1. 前一天的商户型创客,判断名下是否3台激活机器,是则变为非商户型创客
  159. public void dosomething11(string date)
  160. {
  161. WebCMSEntities db = new WebCMSEntities();
  162. DateTime yesterday = DateTime.Parse(date + " 00:00:00");
  163. DateTime today = DateTime.Parse(DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00");
  164. var users = db.Users.Select(m => new { m.Id, m.MerchantType }).Where(m => m.MerchantType == 1).ToList();
  165. foreach (var user in users)
  166. {
  167. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == user.Id && m.PosSnType == 0);
  168. int couponCount = db.PosCoupons.Count(m => m.UserId == user.Id && m.IsUse == 0); //判断是否拥有3张券
  169. if (machineCount + couponCount >= 3)
  170. {
  171. Users edit = db.Users.FirstOrDefault(m => m.Id == user.Id);
  172. if (edit != null)
  173. {
  174. edit.MerchantType = 0;
  175. db.SaveChanges();
  176. }
  177. }
  178. }
  179. db.Dispose();
  180. }
  181. public void testStatTrade(string date)
  182. {
  183. string yesterday = date + " 00:00:00";
  184. string today = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
  185. string TradeMonth = DateTime.Parse(date).ToString("yyyyMM");
  186. string TradeDate = DateTime.Parse(date).ToString("yyyyMMdd");
  187. DataTable dt = dbconn.dtable("select UserId,BrandId,MerchantId,BankCardType,QrPayFlag,sum(TradeAmount) from TradeRecord where CreateDate>='" + yesterday + "' and CreateDate<'" + today + "' group by UserId,BrandId,MerchantId,BankCardType,QrPayFlag");
  188. foreach (DataRow dr in dt.Rows)
  189. {
  190. int UserId = int.Parse(dr["UserId"].ToString()); //创客ID
  191. int BrandId = int.Parse(dr["BrandId"].ToString()); //品牌
  192. int MerchantId = int.Parse(dr["MerchantId"].ToString()); //商户Id
  193. int BankCardType = int.Parse(dr["BankCardType"].ToString()); //卡类型
  194. int QrPayFlag = int.Parse(dr["QrPayFlag"].ToString()); //云闪付
  195. decimal TradeAmount = decimal.Parse(dr[5].ToString()); //当日交易额
  196. string content = "{";
  197. content += "\"DateString\":\"" + date + "\",";
  198. content += "\"UserId\":\"" + UserId + "\",";
  199. content += "\"BrandId\":\"" + BrandId + "\",";
  200. content += "\"MerchantId\":\"" + MerchantId + "\",";
  201. content += "\"BankCardType\":\"" + BankCardType + "\",";
  202. content += "\"QrPayFlag\":\"" + QrPayFlag + "\",";
  203. content += "\"TradeAmount\":\"" + TradeAmount + "\"";
  204. content += "}";
  205. RedisDbconn.Instance.AddList("TradeStatQueueTest", content);
  206. }
  207. }
  208. // 3. 前一天的激活记录,根据创客类型,如果是商户型创客,则激活奖励的起始人从此人上级计算。如果不是,则从此人开始计算
  209. public void dosomething3(int posid = 0)
  210. {
  211. WebCMSEntities db = new WebCMSEntities();
  212. DateTime yesterday = DateTime.Now.AddDays(-1);
  213. DateTime today = DateTime.Now;
  214. IQueryable<PosMachinesTwo> posList = db.PosMachinesTwo.Where(m => m.ActivationState == 1 && !string.IsNullOrEmpty(m.SeoKeyword));
  215. if (posid > 0)
  216. {
  217. posList = posList.Where(m => m.Id == posid);
  218. }
  219. else
  220. {
  221. posList = posList.Where(m => m.ActivationTime >= yesterday && m.ActivationTime < today);
  222. }
  223. foreach (PosMachinesTwo pos in posList.ToList())
  224. {
  225. // PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId && !string.IsNullOrEmpty(m.MerIdcardNo));
  226. // PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId && m.Sort == 1);
  227. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  228. if (merchant != null && pos.ActivationTime.Value <= pos.BindingTime.Value.AddDays(30))
  229. {
  230. // PosMerchantOtherInfo otherInfo = db.PosMerchantOtherInfo.FirstOrDefault(m => m.CertId == merchant.MerIdcardNo && m.PrizeFlag1 == 0);
  231. // if (otherInfo != null)
  232. // {
  233. // otherInfo.PrizeFlag1 = 1;
  234. // db.SaveChanges();
  235. string ParentNav = "";
  236. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId);
  237. if (user != null)
  238. {
  239. int GetUserId = user.Id;
  240. ParentNav = user.ParentNav;
  241. int TopUserId = 0;
  242. if (!string.IsNullOrEmpty(ParentNav))
  243. {
  244. TopUserId = int.Parse(ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  245. }
  246. decimal ActPrize = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  247. if (pos.BrandId == 1 || pos.BrandId == 3 || pos.BrandId == 4 || pos.BrandId == 5 || pos.BrandId == 6 || pos.BrandId == 8 || pos.BrandId == 9)
  248. {
  249. ActPrize = ActPrize / 100;
  250. }
  251. if (ActPrize > 0)
  252. {
  253. if (ActPrize == 99)
  254. {
  255. ActPrize = 100;
  256. }
  257. else if (ActPrize == 199)
  258. {
  259. ActPrize = 200;
  260. }
  261. else if (ActPrize == 299)
  262. {
  263. ActPrize = 300;
  264. }
  265. else if (ActPrize == 249)
  266. {
  267. ActPrize = 260;
  268. }
  269. if (GetUserId > 0)
  270. {
  271. doActiveReward(db, merchant, pos, GetUserId, ParentNav, TopUserId, ActPrize);
  272. doJk30(db, pos, merchant, GetUserId, ParentNav, TopUserId);
  273. }
  274. }
  275. else if(ActPrize == 0)
  276. {
  277. if(pos.BrandId == 6)
  278. {
  279. doActiveReward(db, merchant, pos, GetUserId, ParentNav, TopUserId, 50);
  280. }
  281. }
  282. }
  283. // }
  284. }
  285. }
  286. db.Dispose();
  287. }
  288. //金控活动机返奖励
  289. private void doJk30(WebCMSEntities db, PosMachinesTwo pos, PosMerchantInfo merchant, int GetUserId, string ParentNav, int TopUserId)
  290. {
  291. // DateTime start = DateTime.Parse("2022-04-06 00:00:00");
  292. // DateTime end = DateTime.Parse("2022-07-05 00:00:00");
  293. // if((pos.BrandId == 1 || pos.BrandId == 3) && pos.CreateDate >= start && pos.CreateDate < end)
  294. // {
  295. // decimal amt = 0;
  296. // if(DateTime.Now < DateTime.Parse("2023-05-01 00:00:00"))
  297. // {
  298. // amt = 10;
  299. // }
  300. // if(amt > 0)
  301. // {
  302. // doActiveReward(db, merchant, pos, GetUserId, ParentNav, TopUserId, amt, 0, 1);
  303. // }
  304. // }
  305. }
  306. //无押金激活奖励
  307. public void activePrizeWithoutDeposit(int posid = 0)
  308. {
  309. WebCMSEntities db = new WebCMSEntities();
  310. DateTime yesterday = DateTime.Now.AddDays(-1);
  311. DateTime today = DateTime.Now.AddMinutes(-5);
  312. IQueryable<PosMachinesTwo> posList = db.PosMachinesTwo.Where(m => m.ActivationState == 1 && string.IsNullOrEmpty(m.SeoKeyword) && m.BrandId != 12);
  313. if (posid > 0)
  314. {
  315. posList = posList.Where(m => m.Id == posid);
  316. }
  317. else
  318. {
  319. posList = posList.Where(m => m.ActivationTime >= yesterday && m.ActivationTime < today);
  320. }
  321. foreach (PosMachinesTwo pos in posList.ToList())
  322. {
  323. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  324. if (merchant != null)
  325. {
  326. string ParentNav = "";
  327. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId);
  328. if (user != null)
  329. {
  330. int GetUserId = user.Id;
  331. ParentNav = user.ParentNav;
  332. int TopUserId = 0;
  333. if (!string.IsNullOrEmpty(ParentNav))
  334. {
  335. TopUserId = int.Parse(ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  336. }
  337. if(pos.BrandId == 6) //首台无押金机返50
  338. {
  339. doActiveReward(db, merchant, pos, GetUserId, ParentNav, TopUserId, 50);
  340. }
  341. else if((pos.BrandId == 10 || pos.BrandId == 11) && DateTime.Now < DateTime.Parse("2023-05-01 00:00:00") && pos.IsFirst == 1) //首台无押金机返50
  342. {
  343. doActiveReward(db, merchant, pos, GetUserId, ParentNav, TopUserId, 50);
  344. }
  345. else
  346. {
  347. //金控活动机返奖励
  348. doJk30(db, pos, merchant, GetUserId, ParentNav, TopUserId);
  349. }
  350. }
  351. }
  352. }
  353. db.Dispose();
  354. }
  355. public void activePrizeWithoutDeposit12(int posid = 0)
  356. {
  357. WebCMSEntities db = new WebCMSEntities();
  358. DateTime yesterday = DateTime.Now.AddDays(-30);
  359. DateTime today = DateTime.Now.AddMinutes(-5);
  360. IQueryable<PosMachinesTwo> posList = db.PosMachinesTwo.Where(m => m.ActivationState == 1 && string.IsNullOrEmpty(m.SeoKeyword) && m.BrandId == 12);
  361. if (posid > 0)
  362. {
  363. posList = posList.Where(m => m.Id == posid);
  364. }
  365. else
  366. {
  367. posList = posList.Where(m => m.ActivationTime >= yesterday && m.ActivationTime < today);
  368. }
  369. foreach (PosMachinesTwo pos in posList.ToList())
  370. {
  371. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  372. if (merchant != null)
  373. {
  374. string ParentNav = "";
  375. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId);
  376. if (user != null)
  377. {
  378. int GetUserId = user.Id;
  379. ParentNav = user.ParentNav;
  380. int TopUserId = 0;
  381. if (!string.IsNullOrEmpty(ParentNav))
  382. {
  383. TopUserId = int.Parse(ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  384. }
  385. if(pos.IsFirst == 1 && pos.CreditTrade >= 5000) //首台无押金机返50
  386. {
  387. doActiveReward(db, merchant, pos, GetUserId, ParentNav, TopUserId, 50);
  388. }
  389. }
  390. }
  391. }
  392. db.Dispose();
  393. }
  394. public void doActiveReward(WebCMSEntities db, PosMerchantInfo merchant, PosMachinesTwo pos, int GetUserId, string ParentNav, int TopUserId, decimal ActPrize, int ChangeType = 0, int ActType = 0)
  395. {
  396. bool check = db.ActiveReward.Any(m => m.KqMerNo == merchant.KqMerNo && m.RewardAmount == ActPrize);
  397. bool check1 = db.ActiveReward.Any(m => m.KqSnNo == pos.PosSn && m.RewardAmount == ActPrize);
  398. if (!check && !check1)
  399. {
  400. Users machineUser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  401. db.ActiveReward.Add(new ActiveReward()
  402. {
  403. CreateDate = DateTime.Now,
  404. UpdateDate = DateTime.Now,
  405. UserId = GetUserId, //创客
  406. MerchantId = pos.BindMerchantId, //商户
  407. StandardDate = pos.ActivationTime, //达标日期
  408. RewardAmount = ActPrize, //奖励金额
  409. BrandId = pos.BrandId, //品牌
  410. UserNav = ParentNav, //创客父级
  411. DirectBuddyNo = merchant.UserId, //商户直属创客
  412. KqMerNo = merchant.KqMerNo, //渠道商户编号
  413. KqSnNo = pos.PosSn, //渠道SN号
  414. SnType = pos.PosSnType, //机具类型
  415. SnApplyUserId = pos.BuyUserId, //机具申请创客
  416. ActType = ActType, //激活类型
  417. SnStoreId = pos.StoreId, //SN仓库
  418. RewardTips = "激活奖励", //奖励描述
  419. Remark = "激活奖励", //备注
  420. ActDate = pos.ActivationTime, //激活时间
  421. TopUserId = TopUserId, //顶级创客
  422. SeoTitle = machineUser.RealName,
  423. });
  424. db.SaveChanges();
  425. string IdBrand = GetUserId + "_" + pos.BrandId;
  426. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  427. if (userData == null)
  428. {
  429. userData = db.UserMachineData.Add(new UserMachineData()
  430. {
  431. IdBrand = IdBrand,
  432. }).Entity;
  433. db.SaveChanges();
  434. }
  435. userData.ActProfit += ActPrize;
  436. db.SaveChanges();
  437. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == GetUserId);
  438. if (account == null)
  439. {
  440. account = db.UserAccount.Add(new UserAccount()
  441. {
  442. Id = GetUserId,
  443. UserId = GetUserId,
  444. }).Entity;
  445. db.SaveChanges();
  446. }
  447. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  448. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  449. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  450. account.BalanceAmount += ActPrize;
  451. account.TotalAmount += ActPrize;
  452. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  453. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  454. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  455. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  456. {
  457. CreateDate = DateTime.Now,
  458. UpdateDate = DateTime.Now,
  459. UserId = GetUserId, //创客
  460. ChangeType = ChangeType, //变动类型
  461. ProductType = pos.BrandId, //产品类型
  462. ChangeAmount = ActPrize, //变更金额
  463. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  464. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  465. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  466. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  467. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  468. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  469. }).Entity;
  470. db.SaveChanges();
  471. string dateString = pos.ActivationTime.Value.ToString("yyyyMMdd");
  472. string monthString = pos.ActivationTime.Value.ToString("yyyyMM");
  473. // 激活奖励列表
  474. List<string> dates = RedisDbconn.Instance.GetList<string>("ActiveRewardDay:" + GetUserId + ":" + pos.BrandId);
  475. if (!dates.Contains(dateString))
  476. {
  477. RedisDbconn.Instance.AddList("ActiveRewardDay:" + GetUserId + ":" + pos.BrandId, dateString);
  478. }
  479. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:" + GetUserId + ":" + pos.BrandId + ":" + dateString, ActPrize);
  480. List<string> months = RedisDbconn.Instance.GetList<string>("ActiveRewardMonth:" + GetUserId + ":" + pos.BrandId);
  481. if (!months.Contains(monthString))
  482. {
  483. RedisDbconn.Instance.AddList("ActiveRewardMonth:" + GetUserId + ":" + pos.BrandId, monthString);
  484. }
  485. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:" + GetUserId + ":" + pos.BrandId + ":" + monthString, ActPrize);
  486. // 激活奖励详情
  487. List<int> actPrizeList = RedisDbconn.Instance.GetList<int>("ActiveRewardDetail:" + GetUserId + ":" + pos.BrandId + ":" + dateString);
  488. if (!actPrizeList.Contains(pos.BindMerchantId))
  489. {
  490. RedisDbconn.Instance.AddList("ActiveRewardDetail:" + GetUserId + ":" + pos.BrandId + ":" + dateString, pos.BindMerchantId);
  491. }
  492. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:mer:" + pos.BindMerchantId + ":" + pos.BrandId + ":" + dateString, ActPrize);
  493. //收支明细
  494. RedisDbconn.Instance.Clear("UserAccount:Income:" + GetUserId + ":" + monthString);
  495. }
  496. }
  497. //根据商户信息发放激活奖励
  498. public void dosomething32(int merid = 0)
  499. {
  500. WebCMSEntities db = new WebCMSEntities();
  501. DateTime yesterday = DateTime.Now.AddHours(-3);
  502. DateTime today = DateTime.Now;
  503. IQueryable<PosMerchantInfo> merList = db.PosMerchantInfo.Where(m => m.ActiveStatus == 1 && !string.IsNullOrEmpty(m.SeoKeyword));
  504. if (merid > 0)
  505. {
  506. merList = merList.Where(m => m.Id == merid);
  507. }
  508. else
  509. {
  510. merList = merList.Where(m => m.MerStandardDate >= yesterday && m.MerStandardDate < today);
  511. }
  512. foreach (PosMerchantInfo merchant in merList.ToList())
  513. {
  514. // PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId && !string.IsNullOrEmpty(m.MerIdcardNo));
  515. // PosMerchantOtherInfo otherInfo = db.PosMerchantOtherInfo.FirstOrDefault(m => m.CertId == merchant.MerIdcardNo && m.PrizeFlag1 == 0);
  516. // if (otherInfo != null)
  517. // {
  518. // otherInfo.PrizeFlag1 = 1;
  519. // db.SaveChanges();
  520. MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == merchant.MerchantNo) ?? new MachineForMerNo();
  521. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forMerNo.SnId) ?? new PosMachinesTwo();
  522. string ParentNav = "";
  523. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId);
  524. if (user != null)
  525. {
  526. int GetUserId = user.Id;
  527. ParentNav = user.ParentNav;
  528. int TopUserId = 0;
  529. if (!string.IsNullOrEmpty(ParentNav))
  530. {
  531. TopUserId = int.Parse(ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  532. }
  533. decimal ActPrize = decimal.Parse(function.CheckNum(merchant.SeoKeyword));
  534. if (pos.BrandId == 1 || pos.BrandId == 3 || pos.BrandId == 4 || pos.BrandId == 5 || pos.BrandId == 6 || pos.BrandId == 8 || pos.BrandId == 9)
  535. {
  536. ActPrize = ActPrize / 100;
  537. }
  538. if (ActPrize > 0)
  539. {
  540. bool check = db.ActiveReward.Any(m => m.KqSnNo == pos.PosSn);
  541. if (!check)
  542. {
  543. if (ActPrize == 99)
  544. {
  545. ActPrize = 100;
  546. }
  547. else if (ActPrize == 199)
  548. {
  549. ActPrize = 200;
  550. }
  551. else if (ActPrize == 299)
  552. {
  553. ActPrize = 300;
  554. }
  555. Users machineUser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  556. db.ActiveReward.Add(new ActiveReward()
  557. {
  558. CreateDate = DateTime.Now,
  559. UpdateDate = DateTime.Now,
  560. UserId = GetUserId, //创客
  561. MerchantId = pos.BindMerchantId, //商户
  562. StandardDate = pos.ActivationTime, //达标日期
  563. RewardAmount = ActPrize, //奖励金额
  564. BrandId = pos.BrandId, //品牌
  565. UserNav = ParentNav, //创客父级
  566. DirectBuddyNo = merchant.UserId, //商户直属创客
  567. KqMerNo = merchant.KqMerNo, //渠道商户编号
  568. KqSnNo = pos.PosSn, //渠道SN号
  569. SnType = pos.PosSnType, //机具类型
  570. SnApplyUserId = pos.BuyUserId, //机具申请创客
  571. ActType = 0, //激活类型
  572. SnStoreId = pos.StoreId, //SN仓库
  573. RewardTips = "激活奖励", //奖励描述
  574. Remark = "激活奖励", //备注
  575. ActDate = pos.ActivationTime, //激活时间
  576. TopUserId = TopUserId, //顶级创客
  577. SeoTitle = machineUser.RealName,
  578. });
  579. db.SaveChanges();
  580. string IdBrand = GetUserId + "_" + pos.BrandId;
  581. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  582. if (userData == null)
  583. {
  584. userData = db.UserMachineData.Add(new UserMachineData()
  585. {
  586. IdBrand = IdBrand,
  587. }).Entity;
  588. db.SaveChanges();
  589. }
  590. userData.ActProfit += ActPrize;
  591. db.SaveChanges();
  592. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == GetUserId);
  593. if (account == null)
  594. {
  595. account = db.UserAccount.Add(new UserAccount()
  596. {
  597. Id = GetUserId,
  598. UserId = GetUserId,
  599. }).Entity;
  600. db.SaveChanges();
  601. }
  602. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  603. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  604. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  605. account.BalanceAmount += ActPrize;
  606. account.TotalAmount += ActPrize;
  607. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  608. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  609. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  610. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  611. {
  612. CreateDate = DateTime.Now,
  613. UpdateDate = DateTime.Now,
  614. UserId = GetUserId, //创客
  615. ChangeType = 0, //变动类型
  616. ProductType = pos.BrandId, //产品类型
  617. ChangeAmount = ActPrize, //变更金额
  618. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  619. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  620. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  621. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  622. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  623. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  624. }).Entity;
  625. db.SaveChanges();
  626. string dateString = pos.ActivationTime.Value.ToString("yyyyMMdd");
  627. string monthString = pos.ActivationTime.Value.ToString("yyyyMM");
  628. // 激活奖励列表
  629. List<string> dates = RedisDbconn.Instance.GetList<string>("ActiveRewardDay:" + GetUserId + ":" + pos.BrandId);
  630. if (!dates.Contains(dateString))
  631. {
  632. RedisDbconn.Instance.AddList("ActiveRewardDay:" + GetUserId + ":" + pos.BrandId, dateString);
  633. }
  634. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:" + GetUserId + ":" + pos.BrandId + ":" + dateString, ActPrize);
  635. List<string> months = RedisDbconn.Instance.GetList<string>("ActiveRewardMonth:" + GetUserId + ":" + pos.BrandId);
  636. if (!months.Contains(monthString))
  637. {
  638. RedisDbconn.Instance.AddList("ActiveRewardMonth:" + GetUserId + ":" + pos.BrandId, monthString);
  639. }
  640. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:" + GetUserId + ":" + pos.BrandId + ":" + monthString, ActPrize);
  641. // 激活奖励详情
  642. List<int> actPrizeList = RedisDbconn.Instance.GetList<int>("ActiveRewardDetail:" + GetUserId + ":" + pos.BrandId + ":" + dateString);
  643. if (!actPrizeList.Contains(pos.BindMerchantId))
  644. {
  645. RedisDbconn.Instance.AddList("ActiveRewardDetail:" + GetUserId + ":" + pos.BrandId + ":" + dateString, pos.BindMerchantId);
  646. }
  647. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:mer:" + pos.BindMerchantId + ":" + pos.BrandId + ":" + dateString, ActPrize);
  648. }
  649. }
  650. }
  651. // }
  652. }
  653. db.Dispose();
  654. }
  655. //3. 前一天的激活记录,根据创客类型,如果是商户型创客,则开机奖励的起始人从此人上级计算。如果不是,则从此人开始计算
  656. public void dosomething4(string date)
  657. {
  658. WebCMSEntities db = new WebCMSEntities();
  659. DateTime yesterday = DateTime.Parse(date + " 00:00:00");
  660. DateTime today = DateTime.Parse(DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00");
  661. List<PosMachinesTwo> posList = db.PosMachinesTwo.Where(m => m.ActivationState == 1 && m.ActivationTime >= yesterday && m.ActivationTime < today && !string.IsNullOrEmpty(m.SeoKeyword) && m.SeoKeyword != "0" && m.CreditTrade >= 1000).ToList();
  662. foreach (PosMachinesTwo pos in posList)
  663. {
  664. if (pos.BindingTime > DateTime.Now.AddDays(-30))
  665. {
  666. decimal ActPrize = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  667. if (ActPrize > 0)
  668. {
  669. // PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId && !string.IsNullOrEmpty(m.MerIdcardNo));
  670. // PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId && m.Sort == 1);
  671. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  672. if (merchant != null)
  673. {
  674. // PosMerchantOtherInfo otherInfo = db.PosMerchantOtherInfo.FirstOrDefault(m => m.CertId == merchant.MerIdcardNo && m.PrizeFlag2 == 0);
  675. // if (otherInfo != null)
  676. // {
  677. // otherInfo.PrizeFlag2 = 1;
  678. // db.SaveChanges();
  679. // 从机具所属人上级开始分开机奖励
  680. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId);
  681. if (user != null)
  682. {
  683. if (!string.IsNullOrEmpty(user.ParentNav))
  684. {
  685. decimal Prize = 20;
  686. string[] ParentNavs = user.ParentNav.Trim(',').Replace(",,", ",").Split(',');
  687. for (int i = ParentNavs.Length - 1; i >= 0; i--)
  688. {
  689. int UserId = int.Parse(ParentNavs[i]);
  690. Users puser = db.Users.FirstOrDefault(m => m.Id == UserId && m.AuthFlag == 1);
  691. if (puser != null && Prize > 0)
  692. {
  693. int machineCount = db.PosMachinesTwo.Count(m => m.BuyUserId == puser.Id && m.PosSnType == 0 && m.ActivationState == 0); //判断是否拥有3台兑换机
  694. int ActiveCount = db.PosMachinesTwo.Count(m => m.BuyUserId == puser.Id && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  695. int couponCount = db.PosCoupons.Count(m => m.UserId == puser.Id && m.IsUse == 0); //判断是否拥有3张券
  696. // int BigCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.BrandId == 3); //判断是否拥有2台大机
  697. // int BigActiveCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.ActivationState == 1 && m.BrandId == 3); //判断是否拥有2台激活大机(不限购买或赠送)
  698. if (machineCount + ActiveCount + couponCount >= 3)
  699. {
  700. int pTopUserId = 0;
  701. if (!string.IsNullOrEmpty(puser.ParentNav))
  702. {
  703. pTopUserId = int.Parse(puser.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  704. }
  705. Users machineUser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  706. OpenRewardDetail detail = db.OpenRewardDetail.Add(new OpenRewardDetail()
  707. {
  708. CreateDate = DateTime.Now,
  709. UpdateDate = DateTime.Now,
  710. TradeMonth = yesterday.ToString("yyyyMM"), //交易月
  711. TradeDate = yesterday, //达标日期
  712. UserId = puser.Id, //创客
  713. BrandId = pos.BrandId, //品牌
  714. ProductName = RelationClass.GetKqProductsInfo(pos.BrandId), //产品名称
  715. MerchantId = pos.BindMerchantId, //商户
  716. DirectUserId = merchant.UserId, //商户直属人
  717. SnNo = pos.PosSn, //SN号
  718. MerNo = merchant.KqMerNo, //渠道商户号
  719. SnType = pos.PosSnType, //机具类型
  720. StandardDate = pos.ActivationTime, //商户的激活日期
  721. SnStoreId = pos.StoreId, //SN仓库
  722. MerBuddyType = puser.MerchantType, //商户创客类型
  723. RewardType = 1, //奖励类型 1-开机直接奖励,2-开机间接奖励
  724. RewardTips = "开机奖励", //奖励描述
  725. CreditTradeAmt = pos.CreditTrade, //贷记卡交易总金额
  726. DebitTradeAmt = pos.DebitCardTrade, //借记卡交易总金额
  727. CreditRewardAmount = Prize, //贷记卡交易奖励金额
  728. RewardDesc = "开机奖励", //奖励描述
  729. TopUserId = pTopUserId, //顶级创客
  730. SeoTitle = machineUser.RealName,
  731. }).Entity;
  732. db.OpenReward.Add(new OpenReward()
  733. {
  734. CreateDate = DateTime.Now,
  735. UpdateDate = DateTime.Now,
  736. TradeMonth = yesterday.ToString("yyyyMM"), //交易月
  737. TradeDate = DateTime.Now, //达标日期
  738. UserId = puser.Id, //创客
  739. BrandId = pos.BrandId, //品牌
  740. RewardType = 2, //奖励类型
  741. CreditTradeAmt = pos.CreditTrade, //贷记卡交易总金额
  742. DebitTradeAmt = pos.DebitCardTrade, //借记卡交易总金额
  743. CreditRewardAmount = Prize, //贷记卡交易奖励金额
  744. RewardDesc = "开机奖励", //奖励描述
  745. TopUserId = pTopUserId, //顶级创客
  746. });
  747. string IdBrand = puser.Id + "_" + pos.BrandId;
  748. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  749. if (userData == null)
  750. {
  751. userData = db.UserMachineData.Add(new UserMachineData()
  752. {
  753. IdBrand = IdBrand,
  754. }).Entity;
  755. db.SaveChanges();
  756. }
  757. userData.OpenProfit += Prize;
  758. db.SaveChanges();
  759. //账户入库
  760. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id);
  761. if (account == null)
  762. {
  763. account = db.UserAccount.Add(new UserAccount()
  764. {
  765. Id = puser.Id,
  766. UserId = puser.Id,
  767. }).Entity;
  768. db.SaveChanges();
  769. }
  770. //收支明细入库
  771. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  772. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  773. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  774. account.BalanceAmount += Prize;
  775. account.TotalAmount += Prize;
  776. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  777. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  778. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  779. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  780. {
  781. CreateDate = DateTime.Now,
  782. UpdateDate = DateTime.Now,
  783. UserId = puser.Id, //创客
  784. ChangeType = 50, //变动类型
  785. ProductType = pos.BrandId, //产品类型
  786. ChangeAmount = Prize, //变更金额
  787. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  788. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  789. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  790. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  791. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  792. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  793. }).Entity;
  794. db.SaveChanges();
  795. string dateString = yesterday.ToString("yyyyMMdd");
  796. string monthString = yesterday.ToString("yyyyMM");
  797. // 开机奖励列表
  798. List<string> dates = RedisDbconn.Instance.GetList<string>("OpenRewardDay:" + puser.Id + ":" + pos.BrandId);
  799. if (!dates.Contains(dateString))
  800. {
  801. RedisDbconn.Instance.AddList("OpenRewardDay:" + puser.Id + ":" + pos.BrandId, dateString);
  802. }
  803. RedisDbconn.Instance.AddNumber("OpenRewardAmt:" + puser.Id + ":" + pos.BrandId + ":" + dateString, Prize);
  804. List<string> months = RedisDbconn.Instance.GetList<string>("OpenRewardMonth:" + puser.Id + ":" + pos.BrandId);
  805. if (!months.Contains(monthString))
  806. {
  807. RedisDbconn.Instance.AddList("OpenRewardMonth:" + puser.Id + ":" + pos.BrandId, monthString);
  808. }
  809. RedisDbconn.Instance.AddNumber("OpenRewardAmt:" + puser.Id + ":" + pos.BrandId + ":" + monthString, Prize);
  810. // 开机奖励详情
  811. RedisDbconn.Instance.AddList("OpenRewardDetail:" + puser.Id + ":" + pos.BrandId + ":" + dateString, detail);
  812. Prize = 0;
  813. }
  814. }
  815. }
  816. }
  817. }
  818. // }
  819. }
  820. }
  821. }
  822. }
  823. db.Dispose();
  824. }
  825. //实时获取开机奖励
  826. public void StartOpenReward()
  827. {
  828. Thread th = new Thread(StartOpenRewardDo);
  829. th.IsBackground = true;
  830. th.Start();
  831. }
  832. public void StartOpenRewardDo()
  833. {
  834. while(true)
  835. {
  836. string content = RedisDbconn.Instance.RPop<string>("OpenRewardQueue");
  837. if(!string.IsNullOrEmpty(content))
  838. {
  839. try
  840. {
  841. int PosId = int.Parse(content);
  842. WebCMSEntities db = new WebCMSEntities();
  843. List<PosMachinesTwo> posList = db.PosMachinesTwo.Where(m => m.Id == PosId && m.ActivationState == 1 && !string.IsNullOrEmpty(m.SeoKeyword) && m.SeoKeyword != "0" && m.CreditTrade >= 1000).ToList();
  844. foreach (PosMachinesTwo pos in posList)
  845. {
  846. function.WriteLog("\n\n" + DateTime.Now.ToString() + "\nsn:" + pos.PosSn, "开机奖励发放日志");
  847. if (pos.BindingTime > DateTime.Now.AddDays(-30))
  848. {
  849. decimal ActPrize = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  850. function.WriteLog("押金:" + pos.SeoKeyword, "开机奖励发放日志");
  851. if (ActPrize > 0)
  852. {
  853. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  854. if (merchant != null)
  855. {
  856. function.WriteLog("商户:" + merchant.KqMerNo, "开机奖励发放日志");
  857. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId);
  858. if (user != null)
  859. {
  860. function.WriteLog("所属人:" + user.MakerCode, "开机奖励发放日志");
  861. if (!string.IsNullOrEmpty(user.ParentNav))
  862. {
  863. decimal Prize = 20;
  864. string[] ParentNavs = user.ParentNav.Trim(',').Replace(",,", ",").Split(',');
  865. for (int i = ParentNavs.Length - 1; i >= 0; i--)
  866. {
  867. int UserId = int.Parse(ParentNavs[i]);
  868. Users puser = db.Users.FirstOrDefault(m => m.Id == UserId && m.AuthFlag == 1);
  869. if (puser != null && Prize > 0)
  870. {
  871. function.WriteLog("上级:" + puser.MakerCode, "开机奖励发放日志");
  872. int machineCount = db.PosMachinesTwo.Count(m => m.BuyUserId == puser.Id && m.PosSnType == 0 && m.ActivationState == 0); //判断是否拥有3台兑换机
  873. int ActiveCount = db.PosMachinesTwo.Count(m => m.BuyUserId == puser.Id && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  874. int couponCount = db.PosCoupons.Count(m => m.UserId == puser.Id && m.IsUse == 0); //判断是否拥有3张券
  875. // int BigCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.BrandId == 3); //判断是否拥有2台大机
  876. // int BigActiveCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.ActivationState == 1 && m.BrandId == 3); //判断是否拥有2台激活大机(不限购买或赠送)
  877. function.WriteLog("条件:machineCount:" + machineCount + ",ActiveCount:" + ActiveCount + ",couponCount:" + couponCount + "", "开机奖励发放日志");
  878. if (machineCount + ActiveCount + couponCount >= 3)
  879. {
  880. function.WriteLog("满足条件", "开机奖励发放日志");
  881. int pTopUserId = 0;
  882. if (!string.IsNullOrEmpty(puser.ParentNav))
  883. {
  884. pTopUserId = int.Parse(puser.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  885. }
  886. Users machineUser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  887. OpenRewardDetail detail = db.OpenRewardDetail.Add(new OpenRewardDetail()
  888. {
  889. CreateDate = DateTime.Now,
  890. UpdateDate = DateTime.Now,
  891. TradeMonth = DateTime.Now.ToString("yyyyMM"), //交易月
  892. TradeDate = DateTime.Now, //达标日期
  893. UserId = puser.Id, //创客
  894. BrandId = pos.BrandId, //品牌
  895. ProductName = RelationClass.GetKqProductsInfo(pos.BrandId), //产品名称
  896. MerchantId = pos.BindMerchantId, //商户
  897. DirectUserId = merchant.UserId, //商户直属人
  898. SnNo = pos.PosSn, //SN号
  899. MerNo = merchant.KqMerNo, //渠道商户号
  900. SnType = pos.PosSnType, //机具类型
  901. StandardDate = pos.ActivationTime, //商户的激活日期
  902. SnStoreId = pos.StoreId, //SN仓库
  903. MerBuddyType = puser.MerchantType, //商户创客类型
  904. RewardType = 1, //奖励类型 1-开机直接奖励,2-开机间接奖励
  905. RewardTips = "开机奖励", //奖励描述
  906. CreditTradeAmt = pos.CreditTrade, //贷记卡交易总金额
  907. DebitTradeAmt = pos.DebitCardTrade, //借记卡交易总金额
  908. CreditRewardAmount = Prize, //贷记卡交易奖励金额
  909. RewardDesc = "开机奖励", //奖励描述
  910. TopUserId = pTopUserId, //顶级创客
  911. SeoTitle = machineUser.RealName,
  912. }).Entity;
  913. db.OpenReward.Add(new OpenReward()
  914. {
  915. CreateDate = DateTime.Now,
  916. UpdateDate = DateTime.Now,
  917. TradeMonth = DateTime.Now.ToString("yyyyMM"), //交易月
  918. TradeDate = DateTime.Now, //达标日期
  919. UserId = puser.Id, //创客
  920. BrandId = pos.BrandId, //品牌
  921. RewardType = 2, //奖励类型
  922. CreditTradeAmt = pos.CreditTrade, //贷记卡交易总金额
  923. DebitTradeAmt = pos.DebitCardTrade, //借记卡交易总金额
  924. CreditRewardAmount = Prize, //贷记卡交易奖励金额
  925. RewardDesc = "开机奖励", //奖励描述
  926. TopUserId = pTopUserId, //顶级创客
  927. });
  928. string IdBrand = puser.Id + "_" + pos.BrandId;
  929. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  930. if (userData == null)
  931. {
  932. userData = db.UserMachineData.Add(new UserMachineData()
  933. {
  934. IdBrand = IdBrand,
  935. }).Entity;
  936. db.SaveChanges();
  937. }
  938. userData.OpenProfit += Prize;
  939. db.SaveChanges();
  940. //账户入库
  941. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id);
  942. if (account == null)
  943. {
  944. account = db.UserAccount.Add(new UserAccount()
  945. {
  946. Id = puser.Id,
  947. UserId = puser.Id,
  948. }).Entity;
  949. db.SaveChanges();
  950. }
  951. //收支明细入库
  952. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  953. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  954. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  955. account.BalanceAmount += Prize;
  956. account.TotalAmount += Prize;
  957. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  958. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  959. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  960. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  961. {
  962. CreateDate = DateTime.Now,
  963. UpdateDate = DateTime.Now,
  964. UserId = puser.Id, //创客
  965. ChangeType = 50, //变动类型
  966. ProductType = pos.BrandId, //产品类型
  967. ChangeAmount = Prize, //变更金额
  968. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  969. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  970. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  971. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  972. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  973. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  974. }).Entity;
  975. db.SaveChanges();
  976. string dateString = DateTime.Now.ToString("yyyyMMdd");
  977. string monthString = DateTime.Now.ToString("yyyyMM");
  978. // 开机奖励列表
  979. List<string> dates = RedisDbconn.Instance.GetList<string>("OpenRewardDay:" + puser.Id + ":" + pos.BrandId);
  980. if (!dates.Contains(dateString))
  981. {
  982. RedisDbconn.Instance.AddList("OpenRewardDay:" + puser.Id + ":" + pos.BrandId, dateString);
  983. }
  984. RedisDbconn.Instance.AddNumber("OpenRewardAmt:" + puser.Id + ":" + pos.BrandId + ":" + dateString, Prize);
  985. List<string> months = RedisDbconn.Instance.GetList<string>("OpenRewardMonth:" + puser.Id + ":" + pos.BrandId);
  986. if (!months.Contains(monthString))
  987. {
  988. RedisDbconn.Instance.AddList("OpenRewardMonth:" + puser.Id + ":" + pos.BrandId, monthString);
  989. }
  990. RedisDbconn.Instance.AddNumber("OpenRewardAmt:" + puser.Id + ":" + pos.BrandId + ":" + monthString, Prize);
  991. // 开机奖励详情
  992. RedisDbconn.Instance.AddList("OpenRewardDetail:" + puser.Id + ":" + pos.BrandId + ":" + dateString, detail);
  993. Prize = 0;
  994. }
  995. }
  996. }
  997. }
  998. }
  999. // }
  1000. }
  1001. }
  1002. }
  1003. }
  1004. db.Dispose();
  1005. }
  1006. catch(Exception ex)
  1007. {
  1008. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "实时获取开机奖励异常");
  1009. }
  1010. }
  1011. else
  1012. {
  1013. Thread.Sleep(5000);
  1014. }
  1015. }
  1016. }
  1017. public void dosomething42(string date)
  1018. {
  1019. WebCMSEntities db = new WebCMSEntities();
  1020. DateTime yesterday = DateTime.Parse(date + " 00:00:00");
  1021. DateTime today = DateTime.Parse(DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00");
  1022. List<PosMerchantInfo> merList = db.PosMerchantInfo.Where(m => m.ActiveStatus == 1 && m.MerStandardDate >= yesterday && m.MerStandardDate < today && !string.IsNullOrEmpty(m.SeoKeyword) && m.SeoKeyword != "0").ToList();
  1023. foreach (PosMerchantInfo merchant in merList)
  1024. {
  1025. MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == merchant.MerchantNo) ?? new MachineForMerNo();
  1026. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forMerNo.SnId) ?? new PosMachinesTwo();
  1027. if (pos.ActivationTime > DateTime.Now.AddDays(-20) && pos.CreditTrade >= 1000)
  1028. {
  1029. decimal ActPrize = decimal.Parse(function.CheckNum(merchant.SeoKeyword));
  1030. if (ActPrize > 0)
  1031. {
  1032. // PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId && !string.IsNullOrEmpty(m.MerIdcardNo));
  1033. // PosMerchantOtherInfo otherInfo = db.PosMerchantOtherInfo.FirstOrDefault(m => m.CertId == merchant.MerIdcardNo && m.PrizeFlag2 == 0);
  1034. // if (otherInfo != null)
  1035. // {
  1036. // otherInfo.PrizeFlag2 = 1;
  1037. // db.SaveChanges();
  1038. // 从机具所属人上级开始分开机奖励
  1039. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId);
  1040. if (user != null)
  1041. {
  1042. if (!string.IsNullOrEmpty(user.ParentNav))
  1043. {
  1044. decimal Prize = 20;
  1045. string[] ParentNavs = user.ParentNav.Trim(',').Replace(",,", ",").Split(',');
  1046. for (int i = ParentNavs.Length - 1; i >= 0; i--)
  1047. {
  1048. int UserId = int.Parse(ParentNavs[i]);
  1049. Users puser = db.Users.FirstOrDefault(m => m.Id == UserId && m.AuthFlag == 1);
  1050. if (puser != null && Prize > 0)
  1051. {
  1052. int machineCount = db.PosMachinesTwo.Count(m => m.BuyUserId == puser.Id && m.PosSnType == 0 && m.ActivationState == 0); //判断是否拥有3台兑换机
  1053. int ActiveCount = db.PosMachinesTwo.Count(m => m.BuyUserId == puser.Id && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  1054. int couponCount = db.PosCoupons.Count(m => m.UserId == puser.Id && m.IsUse == 0); //判断是否拥有3张券
  1055. // int BigCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.BrandId == 3); //判断是否拥有2台大机
  1056. // int BigActiveCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.ActivationState == 1 && m.BrandId == 3); //判断是否拥有2台激活大机(不限购买或赠送)
  1057. if (machineCount + ActiveCount + couponCount >= 3)
  1058. {
  1059. int pTopUserId = 0;
  1060. if (!string.IsNullOrEmpty(puser.ParentNav))
  1061. {
  1062. pTopUserId = int.Parse(puser.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  1063. }
  1064. Users machineUser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  1065. OpenRewardDetail detail = db.OpenRewardDetail.Add(new OpenRewardDetail()
  1066. {
  1067. CreateDate = DateTime.Now,
  1068. UpdateDate = DateTime.Now,
  1069. TradeMonth = yesterday.ToString("yyyyMM"), //交易月
  1070. TradeDate = yesterday, //达标日期
  1071. UserId = puser.Id, //创客
  1072. BrandId = pos.BrandId, //品牌
  1073. ProductName = RelationClass.GetKqProductsInfo(pos.BrandId), //产品名称
  1074. MerchantId = pos.BindMerchantId, //商户
  1075. DirectUserId = merchant.UserId, //商户直属人
  1076. SnNo = pos.PosSn, //SN号
  1077. MerNo = merchant.KqMerNo, //渠道商户号
  1078. SnType = pos.PosSnType, //机具类型
  1079. StandardDate = pos.ActivationTime, //商户的激活日期
  1080. SnStoreId = pos.StoreId, //SN仓库
  1081. MerBuddyType = puser.MerchantType, //商户创客类型
  1082. RewardType = 1, //奖励类型 1-开机直接奖励,2-开机间接奖励
  1083. RewardTips = "开机奖励", //奖励描述
  1084. CreditTradeAmt = pos.CreditTrade, //贷记卡交易总金额
  1085. DebitTradeAmt = pos.DebitCardTrade, //借记卡交易总金额
  1086. CreditRewardAmount = Prize, //贷记卡交易奖励金额
  1087. RewardDesc = "开机奖励", //奖励描述
  1088. TopUserId = pTopUserId, //顶级创客
  1089. SeoTitle = machineUser.RealName,
  1090. }).Entity;
  1091. db.OpenReward.Add(new OpenReward()
  1092. {
  1093. CreateDate = DateTime.Now,
  1094. UpdateDate = DateTime.Now,
  1095. TradeMonth = yesterday.ToString("yyyyMM"), //交易月
  1096. TradeDate = DateTime.Now, //达标日期
  1097. UserId = puser.Id, //创客
  1098. BrandId = pos.BrandId, //品牌
  1099. RewardType = 2, //奖励类型
  1100. CreditTradeAmt = pos.CreditTrade, //贷记卡交易总金额
  1101. DebitTradeAmt = pos.DebitCardTrade, //借记卡交易总金额
  1102. CreditRewardAmount = Prize, //贷记卡交易奖励金额
  1103. RewardDesc = "开机奖励", //奖励描述
  1104. TopUserId = pTopUserId, //顶级创客
  1105. });
  1106. string IdBrand = puser.Id + "_" + pos.BrandId;
  1107. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  1108. if (userData == null)
  1109. {
  1110. userData = db.UserMachineData.Add(new UserMachineData()
  1111. {
  1112. IdBrand = IdBrand,
  1113. }).Entity;
  1114. db.SaveChanges();
  1115. }
  1116. userData.OpenProfit += Prize;
  1117. db.SaveChanges();
  1118. //账户入库
  1119. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id);
  1120. if (account == null)
  1121. {
  1122. account = db.UserAccount.Add(new UserAccount()
  1123. {
  1124. Id = puser.Id,
  1125. UserId = puser.Id,
  1126. }).Entity;
  1127. db.SaveChanges();
  1128. }
  1129. //收支明细入库
  1130. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  1131. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  1132. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  1133. account.BalanceAmount += Prize;
  1134. account.TotalAmount += Prize;
  1135. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  1136. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  1137. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  1138. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  1139. {
  1140. CreateDate = DateTime.Now,
  1141. UpdateDate = DateTime.Now,
  1142. UserId = puser.Id, //创客
  1143. ChangeType = 50, //变动类型
  1144. ProductType = pos.BrandId, //产品类型
  1145. ChangeAmount = Prize, //变更金额
  1146. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  1147. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  1148. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  1149. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  1150. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  1151. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  1152. }).Entity;
  1153. db.SaveChanges();
  1154. string dateString = yesterday.ToString("yyyyMMdd");
  1155. string monthString = yesterday.ToString("yyyyMM");
  1156. // 开机奖励列表
  1157. List<string> dates = RedisDbconn.Instance.GetList<string>("OpenRewardDay:" + puser.Id + ":" + pos.BrandId);
  1158. if (!dates.Contains(dateString))
  1159. {
  1160. RedisDbconn.Instance.AddList("OpenRewardDay:" + puser.Id + ":" + pos.BrandId, dateString);
  1161. }
  1162. RedisDbconn.Instance.AddNumber("OpenRewardAmt:" + puser.Id + ":" + pos.BrandId + ":" + dateString, Prize);
  1163. List<string> months = RedisDbconn.Instance.GetList<string>("OpenRewardMonth:" + puser.Id + ":" + pos.BrandId);
  1164. if (!months.Contains(monthString))
  1165. {
  1166. RedisDbconn.Instance.AddList("OpenRewardMonth:" + puser.Id + ":" + pos.BrandId, monthString);
  1167. }
  1168. RedisDbconn.Instance.AddNumber("OpenRewardAmt:" + puser.Id + ":" + pos.BrandId + ":" + monthString, Prize);
  1169. // 开机奖励详情
  1170. RedisDbconn.Instance.AddList("OpenRewardDetail:" + puser.Id + ":" + pos.BrandId + ":" + dateString, detail);
  1171. Prize -= 10;
  1172. }
  1173. }
  1174. }
  1175. }
  1176. }
  1177. // }
  1178. }
  1179. }
  1180. }
  1181. db.Dispose();
  1182. }
  1183. // 4. 前一天的流量卡记录和匹配
  1184. public void ListenFluxRecord()
  1185. {
  1186. Thread th = new Thread(ListenFluxRecordD0);
  1187. th.IsBackground = true;
  1188. th.Start();
  1189. }
  1190. public void ListenFluxRecordD0()
  1191. {
  1192. while(true)
  1193. {
  1194. if(DateTime.Now.Hour > 0)
  1195. {
  1196. CheckFluxForTrade();
  1197. }
  1198. Thread.Sleep(600000);
  1199. }
  1200. }
  1201. public void doFluxRecord()
  1202. {
  1203. function.WriteLog(DateTime.Now.ToString(), "执行流量费返佣");
  1204. SpModels.WebCMSEntities spdb = new SpModels.WebCMSEntities();
  1205. WebCMSEntities db = new WebCMSEntities();
  1206. DateTime start = DateTime.Now.AddDays(-2);
  1207. List<SpModels.TradeFluxRecord> trades = spdb.TradeFluxRecord.Where(m => m.CreateDate >= start && m.Status == 1).ToList();
  1208. foreach (SpModels.TradeFluxRecord trade in trades)
  1209. {
  1210. try
  1211. {
  1212. string OrderNo = trade.TradeSerialNo; //单号
  1213. DateTime TradeDate = trade.CreateDate.Value;
  1214. string TradeMonth = TradeDate.ToString("yyyyMM");
  1215. decimal FeeAmount = trade.FeeAmount; //流量费
  1216. if (trade.ProductType == "1" || trade.ProductType == "4" || trade.ProductType == "6" || trade.ProductType == "8" || trade.ProductType == "9")
  1217. {
  1218. FeeAmount = FeeAmount / 100;
  1219. }
  1220. string TradeSnNo = trade.TradeSnNo; //机具SN
  1221. decimal FluxProfit = 0;
  1222. if (trade.ProductType == "1" && FeeAmount == 60)
  1223. {
  1224. FluxProfit = 24;
  1225. }
  1226. else if (trade.ProductType == "7" && FeeAmount == 46)
  1227. {
  1228. FluxProfit = 10;
  1229. }
  1230. else if (trade.ProductType != "1" && FeeAmount == 48)
  1231. {
  1232. FluxProfit = 12;
  1233. }
  1234. if (FluxProfit > 0)
  1235. {
  1236. MachineForSnNo forSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == TradeSnNo) ?? new MachineForSnNo();
  1237. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forSnNo.SnId) ?? new PosMachinesTwo();
  1238. // PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId && m.Sort == 1);
  1239. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  1240. if (merchant != null && pos.BrandId != 6)
  1241. {
  1242. bool checkExist = db.FluxProfitDetail.Any(m => m.MerNo == merchant.KqMerNo);
  1243. if (!checkExist && decimal.Parse(function.CheckNum(pos.SeoKeyword)) > 0)
  1244. {
  1245. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  1246. int GetUserId = user.Id;
  1247. int TopUserId = 0;
  1248. string ParentNav = user.ParentNav;
  1249. if (!string.IsNullOrEmpty(ParentNav))
  1250. {
  1251. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  1252. if (ParentNavList.Length > 1)
  1253. {
  1254. TopUserId = int.Parse(ParentNavList[1]);
  1255. }
  1256. else if (ParentNavList.Length == 1)
  1257. {
  1258. TopUserId = int.Parse(ParentNavList[0]);
  1259. }
  1260. }
  1261. FluxProfitSummary fluxProfit = db.FluxProfitSummary.FirstOrDefault(m => m.UserId == GetUserId && m.BrandId == pos.BrandId && m.TradeMonth == TradeMonth);
  1262. if (fluxProfit == null)
  1263. {
  1264. fluxProfit = db.FluxProfitSummary.Add(new FluxProfitSummary()
  1265. {
  1266. CreateDate = DateTime.Now,
  1267. UpdateDate = DateTime.Now,
  1268. UserId = GetUserId, //创客
  1269. BrandId = pos.BrandId,
  1270. TopUserId = TopUserId, //顶级创客
  1271. TradeMonth = TradeMonth, //交易月
  1272. MerUserType = user.MerchantType, //商户创客类型
  1273. Remark = "流量卡分佣", //备注
  1274. }).Entity;
  1275. db.SaveChanges();
  1276. }
  1277. fluxProfit.FluxProfitAmt += FluxProfit; //流量分润总金额
  1278. db.FluxProfitDetail.Add(new FluxProfitDetail()
  1279. {
  1280. CreateDate = DateTime.Now,
  1281. UpdateDate = DateTime.Now,
  1282. RecordNo = OrderNo, //单号
  1283. TradeDate = TradeDate.ToString("yyyyMMdd"), //交易日期
  1284. TradeTime = TradeDate.ToString("HHmmss"), //交易时间
  1285. TradeMonth = TradeMonth, //交易月
  1286. UserId = GetUserId, //创客
  1287. MerchantId = pos.BindMerchantId, //商户
  1288. MerchantUserId = pos.UserId, //商户直属人
  1289. MerNo = merchant.KqMerNo, //渠道商户编号
  1290. SnNo = pos.PosSn, //渠道SN号
  1291. FluxOrderNo = OrderNo, //流量扣费单号
  1292. TradeOrderNo = OrderNo, //交易流水号
  1293. TradeAmt = trade.TradeAmount, //商户交易额
  1294. FluxFeeAmt = FeeAmount, //流量费
  1295. FluxProfitAmt = FluxProfit, //流量分润总金额
  1296. PosType = pos.PosSnType.ToString(), //POS类型
  1297. Remark = "流量卡分佣", //备注
  1298. BrandId = pos.BrandId, //品牌
  1299. TopUserId = TopUserId, //顶级创客
  1300. MerUserType = user.MerchantType, //商户创客类型
  1301. });
  1302. string IdBrand = user.Id + "_" + pos.BrandId;
  1303. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  1304. if (userData == null)
  1305. {
  1306. userData = db.UserMachineData.Add(new UserMachineData()
  1307. {
  1308. IdBrand = IdBrand,
  1309. }).Entity;
  1310. db.SaveChanges();
  1311. }
  1312. userData.FluxProfit += FluxProfit;
  1313. db.SaveChanges();
  1314. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == GetUserId);
  1315. if (account == null)
  1316. {
  1317. account = db.UserAccount.Add(new UserAccount()
  1318. {
  1319. Id = GetUserId,
  1320. UserId = GetUserId,
  1321. }).Entity;
  1322. db.SaveChanges();
  1323. }
  1324. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  1325. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  1326. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  1327. account.BalanceAmount += FluxProfit;
  1328. account.TotalAmount += FluxProfit;
  1329. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  1330. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  1331. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  1332. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  1333. {
  1334. CreateDate = DateTime.Now,
  1335. UpdateDate = DateTime.Now,
  1336. UserId = GetUserId, //创客
  1337. ChangeType = 60, //变动类型
  1338. ProductType = pos.BrandId, //产品类型
  1339. ChangeAmount = FluxProfit, //变更金额
  1340. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  1341. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  1342. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  1343. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  1344. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  1345. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  1346. }).Entity;
  1347. db.SaveChanges();
  1348. }
  1349. SpModels.TradeFluxRecord edit = spdb.TradeFluxRecord.FirstOrDefault(m => m.Id == trade.Id);
  1350. if (edit != null)
  1351. {
  1352. edit.Status = 2;
  1353. spdb.SaveChanges();
  1354. }
  1355. }
  1356. }
  1357. }
  1358. catch (Exception ex)
  1359. {
  1360. function.WriteLog(DateTime.Now.ToString() + "\n$" + trade.Id + "$\n" + ex.ToString(), "流量卡分佣异常");
  1361. }
  1362. }
  1363. spdb.Dispose();
  1364. db.Dispose();
  1365. function.WriteLog(DateTime.Now.ToString() + "\n", "执行流量费返佣");
  1366. }
  1367. public void CheckFluxForTrade()
  1368. {
  1369. function.WriteLog(DateTime.Now.ToString(), "扫描交易记录中的流量费");
  1370. SpModels.WebCMSEntities spdb = new SpModels.WebCMSEntities();
  1371. DateTime start = DateTime.Now.AddDays(-1);
  1372. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/TradeRecordId.txt")));
  1373. var list = spdb.TradeRecord.Select(m => new { m.Id, m.CreateDate, m.ProductType, m.Status }).Where(m => m.Id >= StartId && m.Status == 2 && m.CreateDate > start && (m.ProductType == "1" || m.ProductType == "10")).OrderBy(m => m.CreateDate);
  1374. foreach (var sub in list.ToList())
  1375. {
  1376. try
  1377. {
  1378. SpModels.TradeRecord trade = spdb.TradeRecord.FirstOrDefault(m => m.Id == sub.Id);
  1379. if (trade != null)
  1380. {
  1381. if(trade.ProductType == "1") //金控
  1382. {
  1383. string content = trade.SeoDescription;
  1384. if (!string.IsNullOrEmpty(content))
  1385. {
  1386. content = System.Web.HttpUtility.UrlDecode(content);
  1387. JsonData jsonObj = JsonMapper.ToObject(content);
  1388. decimal terminalDataFlowFee = decimal.Parse(function.CheckNum(jsonObj["terminalDataFlowFee"].ToString()));
  1389. if (terminalDataFlowFee > 0)
  1390. {
  1391. spdb.TradeFluxRecord.Add(new SpModels.TradeFluxRecord()
  1392. {
  1393. SeoDescription = trade.TradeSerialNo,
  1394. FeeAmount = terminalDataFlowFee,
  1395. TradeSerialNo = function.MD5_16(trade.TradeSerialNo),
  1396. CreateDate = trade.CreateDate,
  1397. TradeSnNo = trade.TradeSnNo,
  1398. ProductType = trade.ProductType,
  1399. Status = 1,
  1400. });
  1401. spdb.SaveChanges();
  1402. }
  1403. }
  1404. }
  1405. else if(trade.ProductType == "10" || trade.ProductType == "12") //10联动、12盒易付
  1406. {
  1407. if (trade.DigAmt > 0)
  1408. {
  1409. spdb.TradeFluxRecord.Add(new SpModels.TradeFluxRecord()
  1410. {
  1411. SeoDescription = trade.TradeSerialNo,
  1412. FeeAmount = trade.DigAmt,
  1413. TradeSerialNo = function.MD5_16(trade.TradeSerialNo),
  1414. CreateDate = trade.CreateDate,
  1415. TradeSnNo = trade.TradeSnNo,
  1416. ProductType = trade.ProductType,
  1417. Status = 1,
  1418. });
  1419. spdb.SaveChanges();
  1420. }
  1421. }
  1422. trade.Status = 3;
  1423. spdb.SaveChanges();
  1424. }
  1425. }
  1426. catch (Exception ex)
  1427. {
  1428. function.WriteLog(DateTime.Now.ToString() + "\n$" + sub.Id + "$\n" + ex.ToString(), "扫描交易记录中的流量费异常");
  1429. }
  1430. }
  1431. spdb.Dispose();
  1432. function.WriteLog(DateTime.Now.ToString() + "\n", "扫描交易记录中的流量费");
  1433. doFluxRecord();
  1434. }
  1435. // 每天统计头一天的交易额
  1436. public void StartEverDay(string date)
  1437. {
  1438. string TradeMonth = DateTime.Now.ToString("yyyyMM");
  1439. if(DateTime.Now.Day == 1)
  1440. {
  1441. TradeMonth = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
  1442. }
  1443. else if(DateTime.Now.Day == 2)
  1444. {
  1445. CustomerSqlConn.op("update Users set ThisMonthTrade=0 where ThisMonthTrade>0", MysqlConn.SqlConnStr);
  1446. }
  1447. DataTable list = CustomerSqlConn.dtable("select UserId,sum(HelpNonDirectTradeAmt+HelpNonDirectDebitTradeAmt+NotHelpNonDirectTradeAmt+NotHelpNonDirectDebitTradeAmt+ProfitNonDirectTradeAmt+ProfitNonDirectDebitTradeAmt) from (select UserId,HelpNonDirectTradeAmt,HelpNonDirectDebitTradeAmt,NotHelpNonDirectTradeAmt,NotHelpNonDirectDebitTradeAmt,ProfitNonDirectTradeAmt,ProfitNonDirectDebitTradeAmt from TradeDaySummary where Id>=3348887 and TradeMonth='" + TradeMonth + "' and SeoTitle='team' and UserId>0) tb group by UserId", MysqlConn.SqlConnStr);
  1448. foreach (DataRow dr in list.Rows)
  1449. {
  1450. string UserId = dr["UserId"].ToString();
  1451. string ThisMonthTrade = dr[1].ToString();
  1452. CustomerSqlConn.op("update Users set ThisMonthTrade=" + ThisMonthTrade + " where Id=" + UserId, MysqlConn.SqlConnStr);
  1453. }
  1454. // Thread th = new Thread(StatTradeAmountEverDay);
  1455. // th.IsBackground = true;
  1456. // th.Start(date);
  1457. }
  1458. public void StatTradeAmountEverDay(object sender)
  1459. {
  1460. string date = sender.ToString();
  1461. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "执行昨天交易额日志");
  1462. WebCMSEntities db = new WebCMSEntities();
  1463. try
  1464. {
  1465. string TradeDate = date.Replace("-", "");
  1466. string TradeMonth = TradeDate.Substring(0, 6);
  1467. string start = date + " 00:00:00";
  1468. string end = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
  1469. string startId = "0", endId = "9999999999";
  1470. List<string> uids = new List<string>();
  1471. DataTable startDt = OtherMySqlConn.dtable("select min(Id) from TradeRecord where CreateDate>='" + start + "'");
  1472. if (startDt.Rows.Count > 0)
  1473. {
  1474. startId = startDt.Rows[0][0].ToString();
  1475. }
  1476. function.WriteLog(startId + "\n\n", "执行昨天交易额日志");
  1477. // DataTable endDt = OtherMySqlConn.dtable("select max(Id) from TradeRecord where CreateDate<'" + end + "'");
  1478. // if (endDt.Rows.Count > 0)
  1479. // {
  1480. // endId = endDt.Rows[0][0].ToString();
  1481. // }
  1482. DataTable userDt = OtherMySqlConn.dtable("select Id,ParentNav from Users where Id in (select DISTINCT UserId from TradeRecord where Id>=" + startId + " and Id<=" + endId + " and CreateDate>='" + start + "' and CreateDate<'" + end + "')");
  1483. function.WriteLog("交易人数:" + userDt.Rows.Count + "\n\n", "执行昨天交易额日志");
  1484. foreach (DataRow userDr in userDt.Rows)
  1485. {
  1486. int UserId = int.Parse(userDr["Id"].ToString());
  1487. string ParentNav = userDr["ParentNav"].ToString();
  1488. function.WriteLog(UserId + ":" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行昨天交易额日志");
  1489. DataTable selfdt = OtherMySqlConn.dtable("select BrandId,BankCardType,QrPayFlag,sum(TradeAmount) from TradeRecord where Id>=" + startId + " and Id<=" + endId + " and CreateDate>='" + start + "' and CreateDate<'" + end + "' and UserId=" + UserId + " group by BrandId,BankCardType,QrPayFlag");
  1490. function.WriteLog(UserId + ":" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行昨天交易额日志");
  1491. foreach (DataRow selfDr in selfdt.Rows)
  1492. {
  1493. int BrandId = int.Parse(selfDr["BrandId"].ToString());
  1494. int BankCardType = int.Parse(selfDr["BankCardType"].ToString());
  1495. int QrPayFlag = int.Parse(selfDr["QrPayFlag"].ToString());
  1496. decimal TradeAmount = decimal.Parse(selfDr[3].ToString());
  1497. UserTradeDaySummary selfStat = db.UserTradeDaySummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.QueryCount == QrPayFlag && m.SeoTitle == "self");
  1498. if (selfStat == null)
  1499. {
  1500. selfStat = db.UserTradeDaySummary.Add(new UserTradeDaySummary()
  1501. {
  1502. UserId = UserId,
  1503. TradeMonth = TradeMonth,
  1504. TradeDate = TradeDate,
  1505. BrandId = BrandId,
  1506. QueryCount = QrPayFlag,
  1507. SeoTitle = "self",
  1508. }).Entity;
  1509. db.SaveChanges();
  1510. }
  1511. if (BankCardType == 0)
  1512. {
  1513. selfStat.DirectDebitTradeAmt += TradeAmount;
  1514. }
  1515. else if (BankCardType != 0)
  1516. {
  1517. selfStat.DirectTradeAmt += TradeAmount;
  1518. }
  1519. db.SaveChanges();
  1520. }
  1521. if (!string.IsNullOrEmpty(ParentNav))
  1522. {
  1523. ParentNav += "," + UserId + ",";
  1524. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  1525. foreach (string NavUserIdString in ParentNavList)
  1526. {
  1527. if (!uids.Contains(NavUserIdString + start))
  1528. {
  1529. uids.Add(NavUserIdString + start);
  1530. int NavUserId = int.Parse(NavUserIdString);
  1531. function.WriteLog(NavUserId + ":team:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行昨天交易额日志");
  1532. DataTable teamDt = OtherMySqlConn.dtable("select BrandId,BankCardType,QrPayFlag,sum(TradeAmount) from TradeRecord where Id>=" + startId + " and Id<=" + endId + " and CreateDate>='" + start + "' and CreateDate<'" + end + "' and UserId in (select Id from Users where ParentNav like '%," + NavUserId + ",%' or Id=" + NavUserId + ") group by BrandId,BankCardType,QrPayFlag");
  1533. function.WriteLog(NavUserId + ":team:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行昨天交易额日志");
  1534. foreach (DataRow teamDr in teamDt.Rows)
  1535. {
  1536. int BrandId = int.Parse(teamDr["BrandId"].ToString());
  1537. int BankCardType = int.Parse(teamDr["BankCardType"].ToString());
  1538. int QrPayFlag = int.Parse(teamDr["QrPayFlag"].ToString());
  1539. decimal TradeAmount = decimal.Parse(teamDr[3].ToString());
  1540. UserTradeDaySummary teamStat = db.UserTradeDaySummary.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.QueryCount == QrPayFlag && m.SeoTitle == "team");
  1541. if (teamStat == null)
  1542. {
  1543. teamStat = db.UserTradeDaySummary.Add(new UserTradeDaySummary()
  1544. {
  1545. UserId = NavUserId,
  1546. TradeMonth = TradeMonth,
  1547. TradeDate = TradeDate,
  1548. BrandId = BrandId,
  1549. QueryCount = QrPayFlag,
  1550. SeoTitle = "team",
  1551. }).Entity;
  1552. db.SaveChanges();
  1553. }
  1554. if (BankCardType == 0)
  1555. {
  1556. teamStat.NonDirectDebitTradeAmt += TradeAmount;
  1557. }
  1558. else if (BankCardType != 0)
  1559. {
  1560. teamStat.NonDirectTradeAmt += TradeAmount;
  1561. }
  1562. db.SaveChanges();
  1563. }
  1564. }
  1565. }
  1566. }
  1567. }
  1568. if (DateTime.Now.Day >= 2)
  1569. {
  1570. Thread.Sleep(5000);
  1571. DataTable list = OtherMySqlConn.dtable("select UserId,sum(NonDirectTradeAmt) from UserTradeDaySummary where TradeDate='" + TradeDate + "' GROUP BY UserId");
  1572. foreach (DataRow dr in list.Rows)
  1573. {
  1574. string UserId = dr["UserId"].ToString();
  1575. string ThisMonthTrade = dr[1].ToString();
  1576. OtherMySqlConn.op("update Users set ThisMonthTrade=ThisMonthTrade+" + ThisMonthTrade + " where Id=" + UserId);
  1577. }
  1578. }
  1579. }
  1580. catch (Exception ex)
  1581. {
  1582. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计昨天的交易额异常");
  1583. }
  1584. db.Dispose();
  1585. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行昨天交易额日志");
  1586. StatMerchantTrade(date);
  1587. }
  1588. //统计商户交易额
  1589. private void StatMerchantTrade(string date)
  1590. {
  1591. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "执行商户交易额日志");
  1592. // WebCMSEntities db = new WebCMSEntities();
  1593. try
  1594. {
  1595. string start = date + " 00:00:00";
  1596. string end = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
  1597. CustomerSqlConn.op("insert into PosMerchantTradeSummay (MerchantId,BrandId,TradeDate,TradeMonth,TradeAmount,CreateDate) select *,now() from (select MerchantId,BrandId,DATE_FORMAT(CreateDate,'%Y%m%d') as TradeDate,DATE_FORMAT(CreateDate,'%Y%m') as TradeMonth,sum(TradeAmount) as TradeAmount from TradeRecord where CreateDate>='" + start + "' and CreateDate<'" + end + "' and ActStatus=1 group by MerchantId,BrandId,DATE_FORMAT(CreateDate,'%Y%m%d'),TradeDate,DATE_FORMAT(CreateDate,'%Y%m') order by MerchantId,BrandId,DATE_FORMAT(CreateDate,'%Y%m%d')) tb", MysqlConn.SqlConnStr);
  1598. RedisDbconn.Instance.Clear("PosMerchantAmount:*");
  1599. RedisDbconn.Instance.Clear("PosMerchantAmount:*:" + date.Replace("-", "").Substring(0, 6)); //商户当月交易
  1600. RedisDbconn.Instance.Clear("PosMerchantAmount:*:" + date.Replace("-", "")); //商户当日交易
  1601. // foreach (DataRow selfDr in selfdt.Rows)
  1602. // {
  1603. // int BrandId = int.Parse(selfDr["BrandId"].ToString());
  1604. // int MerchantId = int.Parse(selfDr["MerchantId"].ToString());
  1605. // string TradeDate = selfDr["TradeDate"].ToString();
  1606. // TradeDate = TradeDate.Replace("-", "");
  1607. // string TradeMonth = TradeDate.Substring(0, 6);
  1608. // decimal TradeAmount = decimal.Parse(selfDr["TradeAmount"].ToString());
  1609. // PosMerchantTradeSummay merStat = db.PosMerchantTradeSummay.FirstOrDefault(m => m.MerchantId == MerchantId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId);
  1610. // if (merStat == null)
  1611. // {
  1612. // merStat = db.PosMerchantTradeSummay.Add(new PosMerchantTradeSummay()
  1613. // {
  1614. // MerchantId = MerchantId,
  1615. // TradeMonth = TradeMonth,
  1616. // TradeDate = TradeDate,
  1617. // BrandId = BrandId,
  1618. // }).Entity;
  1619. // db.SaveChanges();
  1620. // }
  1621. // merStat.TradeAmount += TradeAmount;
  1622. // db.SaveChanges();
  1623. // }
  1624. }
  1625. catch (Exception ex)
  1626. {
  1627. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计商户的交易额");
  1628. }
  1629. // db.Dispose();
  1630. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行商户交易额日志");
  1631. }
  1632. // 5. 创客升级
  1633. public void StatUserLevel()
  1634. {
  1635. Thread th = new Thread(StatUserLevelDo);
  1636. th.IsBackground = true;
  1637. th.Start();
  1638. }
  1639. public void StatUserLevelDo()
  1640. {
  1641. while (true)
  1642. {
  1643. if(DateTime.Now.Day == 1 && DateTime.Now.Hour > 1)
  1644. {
  1645. string Month = DateTime.Now.AddDays(-1).ToString("yyyy-MM");
  1646. string flag = function.ReadInstance("/ProfitFlag/" + Month + ".txt");
  1647. if (string.IsNullOrEmpty(flag))
  1648. {
  1649. function.WritePage("/ProfitFlag/", Month + ".txt", DateTime.Now.ToString("HH:mm:ss"));
  1650. LogoutUserHelper.Instance.DoWorks(); //每月一号归档注销创客,把伞下创客归到注销创客的上级
  1651. StatUserLevelStart();
  1652. }
  1653. }
  1654. Thread.Sleep(600000);
  1655. }
  1656. }
  1657. public void StatUserLevelStart()
  1658. {
  1659. WebCMSEntities db = new WebCMSEntities();
  1660. try
  1661. {
  1662. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
  1663. function.WriteLog(DateTime.Now.ToString(), "创客升级日志");
  1664. DateTime start = DateTime.Parse(DateTime.Now.AddMonths(-1).ToString("yyyy-MM") + "-01 00:00:00");
  1665. DateTime end = start.AddDays(1);
  1666. string TradeMonth = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
  1667. List<UserLevelSet> sets = db.UserLevelSet.ToList();
  1668. bool op = true;
  1669. int startid = 0;
  1670. while(op)
  1671. {
  1672. var users = db.Users.Select(m => new { m.Id, m.AuthFlag }).Where(m => m.Id > startid && m.AuthFlag == 1).OrderBy(m => m.Id).Take(500).ToList();
  1673. function.WriteLog(users.Count.ToString(), "创客升级日志");
  1674. if(users.Count > 0)
  1675. {
  1676. foreach (var subuser in users)
  1677. {
  1678. Users user = db.Users.FirstOrDefault(m => m.Id == subuser.Id);
  1679. if (user != null)
  1680. {
  1681. int BeforeLevel = user.UserLevel;
  1682. int AfterLevel = 0;
  1683. decimal TradeAmount = 0;
  1684. //创客团队交易额
  1685. bool check = db.TradeDaySummary.Any(m => m.UserId == user.Id && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  1686. if (check)
  1687. {
  1688. TradeAmount += db.TradeDaySummary.Where(m => m.UserId == user.Id && m.TradeMonth == TradeMonth && m.SeoTitle == "team").Sum(m => m.HelpNonDirectTradeAmt + m.HelpNonDirectDebitTradeAmt + m.NotHelpNonDirectTradeAmt + m.NotHelpNonDirectDebitTradeAmt + m.ProfitNonDirectTradeAmt + m.ProfitNonDirectDebitTradeAmt);
  1689. }
  1690. //助利宝团队交易额
  1691. DataTable checkdt = OtherMySqlConn.dtable("SELECT SUM(TradeAmount) TradeAmount FROM HelpProfitMerTradeSummay WHERE (MerchantId IN(SELECT MerchantId FROM HelpProfitMerIds WHERE UserId IN(SELECT Id from Users WHERE ParentNav like '%," + user.Id + ",%' OR Id=" + user.Id + "))) AND TradeMonth=" + TradeMonth + "");
  1692. if (checkdt.Rows.Count > 0)
  1693. {
  1694. TradeAmount += decimal.Parse(function.CheckNum(checkdt.Rows[0]["TradeAmount"].ToString()));
  1695. }
  1696. UserLevelSet set = sets.FirstOrDefault(m => m.UpLevelGrowth <= TradeAmount && m.UpLevelIntegral > TradeAmount);
  1697. if (set != null)
  1698. {
  1699. AfterLevel = set.Id;
  1700. }
  1701. // K6以上计算小市场是否达标
  1702. // 升级逻辑
  1703. bool upFlag = true;
  1704. if(AfterLevel > 5)
  1705. {
  1706. DataTable dt = OtherMySqlConn.dtable("select UserId,sum(HelpNonDirectTradeAmt+NotHelpNonDirectTradeAmt+ProfitNonDirectTradeAmt+HelpNonDirectDebitTradeAmt+NotHelpNonDirectDebitTradeAmt+ProfitNonDirectDebitTradeAmt) from TradeDaySummary where UserId in (select Id from Users where ParentUserId=" + user.Id + ") and TradeMonth='" + TradeMonth + "' and SeoTitle='team' group by UserId order by sum(HelpNonDirectTradeAmt+NotHelpNonDirectTradeAmt+ProfitNonDirectTradeAmt+HelpNonDirectDebitTradeAmt+ProfitNonDirectDebitTradeAmt) desc");
  1707. int index = 0;
  1708. decimal TradeAmt = 0;
  1709. decimal BigTradeAmt = 0;
  1710. foreach(DataRow dr in dt.Rows)
  1711. {
  1712. index += 1;
  1713. if(index == 1)
  1714. {
  1715. BigTradeAmt = decimal.Parse(dr[1].ToString());
  1716. }
  1717. else if(index > 1)
  1718. {
  1719. TradeAmt += decimal.Parse(dr[1].ToString());
  1720. }
  1721. }
  1722. if(TradeAmt < 12000000 || BigTradeAmt < 30000000)
  1723. {
  1724. if(AfterLevel > BeforeLevel && BeforeLevel >= 5) //保级,只升不降
  1725. {
  1726. AfterLevel = BeforeLevel;
  1727. }
  1728. else if(AfterLevel > BeforeLevel && BeforeLevel < 5)
  1729. {
  1730. AfterLevel = 5;
  1731. }
  1732. else if(BeforeLevel > 5)
  1733. {
  1734. AfterLevel = 5;
  1735. }
  1736. else
  1737. {
  1738. upFlag = false;
  1739. }
  1740. }
  1741. }
  1742. if (AfterLevel > BeforeLevel && AfterLevel > 1 && upFlag)
  1743. {
  1744. function.WriteLog("升级:" + user.Id.ToString() + BeforeLevel + "->" + AfterLevel, "创客升级日志");
  1745. user.UserLevel = AfterLevel;
  1746. DateTime checkTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00");
  1747. UserRank rank = db.UserRank.FirstOrDefault(m => m.CreateDate >= checkTime && m.UserId == user.Id);
  1748. if(rank == null)
  1749. {
  1750. db.UserRank.Add(new UserRank()
  1751. {
  1752. CreateDate = DateTime.Now,
  1753. UpdateDate = DateTime.Now,
  1754. UserId = user.Id, //创客
  1755. WhiteRank = BeforeLevel,
  1756. Rank = AfterLevel, //当前等级
  1757. StatYear = DateTime.Now.Year, //统计年份
  1758. StatMonth = DateTime.Now.Month, //统计月份
  1759. TradeAmt = TradeAmount, //团队交易总额
  1760. UpgradeFlag = 1, //升级标识
  1761. OperateDate = DateTime.Now, //操作时间
  1762. Status = 1,
  1763. });
  1764. db.SaveChanges();
  1765. }
  1766. else
  1767. {
  1768. rank.TradeAmt = TradeAmount;
  1769. rank.Rank = AfterLevel;
  1770. db.SaveChanges();
  1771. }
  1772. }
  1773. // 降级逻辑
  1774. if(!upFlag && AfterLevel > 5)
  1775. {
  1776. AfterLevel = 5;
  1777. }
  1778. if (AfterLevel < BeforeLevel && BeforeLevel > 1)
  1779. {
  1780. function.WriteLog(user.MakerCode + "-" + user.RealName + ":K" + BeforeLevel + "->K" + AfterLevel, "创客降级日志");
  1781. user.UserLevel = AfterLevel;
  1782. DateTime checkTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00");
  1783. UserRank rank = db.UserRank.FirstOrDefault(m => m.CreateDate >= checkTime && m.UserId == user.Id);
  1784. if(rank == null)
  1785. {
  1786. db.UserRank.Add(new UserRank()
  1787. {
  1788. CreateDate = DateTime.Now,
  1789. UpdateDate = DateTime.Now,
  1790. UserId = user.Id, //创客
  1791. WhiteRank = BeforeLevel,
  1792. Rank = AfterLevel, //当前等级
  1793. StatYear = DateTime.Now.Year, //统计年份
  1794. StatMonth = DateTime.Now.Month, //统计月份
  1795. TradeAmt = TradeAmount, //团队交易总额
  1796. UpgradeFlag = 0, //升级标识
  1797. OperateDate = DateTime.Now, //操作时间
  1798. Status = 1,
  1799. });
  1800. db.SaveChanges();
  1801. }
  1802. else
  1803. {
  1804. rank.TradeAmt = TradeAmount;
  1805. rank.Rank = AfterLevel;
  1806. rank.Status = 1;
  1807. db.SaveChanges();
  1808. }
  1809. }
  1810. }
  1811. startid = subuser.Id;
  1812. }
  1813. function.WriteLog(startid.ToString(), "创客升级日志");
  1814. }
  1815. else
  1816. {
  1817. op = false;
  1818. }
  1819. }
  1820. function.WriteLog(DateTime.Now.ToString(), "创客升级日志");
  1821. }
  1822. catch (Exception ex)
  1823. {
  1824. function.WriteLog(ex.ToString(), "创客升级异常");
  1825. }
  1826. db.Dispose();
  1827. RedisDbconn.Instance.AddList("DoTableEveryMonthQueue", "1");
  1828. }
  1829. // 统计创客激活数
  1830. public void StartPosActNum()
  1831. {
  1832. Thread th = new Thread(StartPosActNumFor);
  1833. th.IsBackground = true;
  1834. th.Start();
  1835. }
  1836. public void StartPosActNumFor()
  1837. {
  1838. // DateTime end = DateTime.Parse("2022-06-15 00:00:00");
  1839. // DateTime check = DateTime.Parse("2022-06-14");
  1840. // while (check <= end)
  1841. // {
  1842. // StartPosActNumDo(check.ToString("yyyy-MM-dd"));
  1843. // check = check.AddDays(1);
  1844. // }
  1845. while (true)
  1846. {
  1847. if(DateTime.Now.Hour >= 2)
  1848. {
  1849. StartPosActNumEverTime();
  1850. }
  1851. Thread.Sleep(100);
  1852. }
  1853. }
  1854. public void StartPosActNumDo(object sender)
  1855. {
  1856. string date = sender.ToString();
  1857. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "执行创客激活数日志");
  1858. WebCMSEntities db = new WebCMSEntities();
  1859. try
  1860. {
  1861. string TradeDate = date.Replace("-", "");
  1862. string TradeMonth = TradeDate.Substring(0, 6);
  1863. string start = date + " 00:00:00";
  1864. string end = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
  1865. List<string> uids = new List<string>();
  1866. DataTable userDt = OtherMySqlConn.dtable("select Id,ParentNav from Users where Id in (select DISTINCT BuyUserId from PosMachinesTwo where QueryCount=0 and ActivationState=1 and ActivationTime>='" + start + "' and ActivationTime<'" + end + "')");
  1867. function.WriteLog("人数:" + userDt.Rows.Count + "\n\n", "执行创客激活数日志");
  1868. foreach (DataRow userDr in userDt.Rows)
  1869. {
  1870. int UserId = int.Parse(userDr["Id"].ToString());
  1871. string ParentNav = userDr["ParentNav"].ToString();
  1872. function.WriteLog(UserId + ":" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行创客激活数日志");
  1873. DataTable selfdt = OtherMySqlConn.dtable("select BrandId,count(Id) from PosMachinesTwo where QueryCount=0 and ActivationState=1 and ActivationTime>='" + start + "' and ActivationTime<'" + end + "' and BuyUserId=" + UserId + " group by BrandId");
  1874. function.WriteLog(UserId + ":" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行创客激活数日志");
  1875. foreach (DataRow selfDr in selfdt.Rows)
  1876. {
  1877. int BrandId = int.Parse(selfDr["BrandId"].ToString());
  1878. int QrPayFlag = 0;
  1879. int ActCount = int.Parse(selfDr[1].ToString());
  1880. UserTradeDaySummary selfStat = db.UserTradeDaySummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.QueryCount == QrPayFlag && m.SeoTitle == "self");
  1881. if (selfStat == null)
  1882. {
  1883. selfStat = db.UserTradeDaySummary.Add(new UserTradeDaySummary()
  1884. {
  1885. UserId = UserId,
  1886. TradeMonth = TradeMonth,
  1887. TradeDate = TradeDate,
  1888. BrandId = BrandId,
  1889. QueryCount = QrPayFlag,
  1890. SeoTitle = "self",
  1891. }).Entity;
  1892. db.SaveChanges();
  1893. }
  1894. selfStat.DirectDebitCapNum += ActCount;
  1895. db.SaveChanges();
  1896. }
  1897. if (!string.IsNullOrEmpty(ParentNav))
  1898. {
  1899. ParentNav += "," + UserId + ",";
  1900. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  1901. foreach (string NavUserIdString in ParentNavList)
  1902. {
  1903. if (!uids.Contains(NavUserIdString + start))
  1904. {
  1905. uids.Add(NavUserIdString + start);
  1906. int NavUserId = int.Parse(NavUserIdString);
  1907. function.WriteLog(NavUserId + ":team:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行创客激活数日志");
  1908. DataTable teamDt = OtherMySqlConn.dtable("select BrandId,count(Id) from PosMachinesTwo where QueryCount=0 and ActivationState=1 and ActivationTime>='" + start + "' and ActivationTime<'" + end + "' and BuyUserId in (select Id from Users where ParentNav like '%," + NavUserId + ",%' or Id=" + NavUserId + ") group by BrandId");
  1909. function.WriteLog(NavUserId + ":team:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行创客激活数日志");
  1910. foreach (DataRow teamDr in teamDt.Rows)
  1911. {
  1912. int BrandId = int.Parse(teamDr["BrandId"].ToString());
  1913. int QrPayFlag = 0;
  1914. int ActCount = int.Parse(teamDr[1].ToString());
  1915. UserTradeDaySummary teamStat = db.UserTradeDaySummary.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.QueryCount == QrPayFlag && m.SeoTitle == "team");
  1916. if (teamStat == null)
  1917. {
  1918. teamStat = db.UserTradeDaySummary.Add(new UserTradeDaySummary()
  1919. {
  1920. UserId = NavUserId,
  1921. TradeMonth = TradeMonth,
  1922. TradeDate = TradeDate,
  1923. BrandId = BrandId,
  1924. QueryCount = QrPayFlag,
  1925. SeoTitle = "team",
  1926. }).Entity;
  1927. db.SaveChanges();
  1928. }
  1929. teamStat.NonDirectDebitCapNum += ActCount;
  1930. db.SaveChanges();
  1931. }
  1932. }
  1933. }
  1934. }
  1935. }
  1936. OtherMySqlConn.op("update PosMachinesTwo set QueryCount=1 where QueryCount=0 and ActivationState=1 and ActivationTime>='" + start + "' and ActivationTime<'" + end + "'");
  1937. }
  1938. catch (Exception ex)
  1939. {
  1940. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "执行创客激活数异常");
  1941. }
  1942. db.Dispose();
  1943. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行创客激活数日志");
  1944. Thread.Sleep(60000);
  1945. }
  1946. public void StartPosActNumEverTime()
  1947. {
  1948. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
  1949. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "实时执行创客激活数日志");
  1950. WebCMSEntities db = new WebCMSEntities();
  1951. try
  1952. {
  1953. DataTable idsDt = OtherMySqlConn.dtable("select Id from PosMachinesTwo where QueryCount=0 and ActivationState=1 and ActivationTime is not null and BuyUserId>0 limit 50");
  1954. if(idsDt.Rows.Count > 0)
  1955. {
  1956. string ids = "";
  1957. foreach (DataRow idsDr in idsDt.Rows)
  1958. {
  1959. ids += idsDr["Id"].ToString() + ",";
  1960. }
  1961. DataTable userDt = OtherMySqlConn.dtable("select BuyUserId,BrandId,DATE_FORMAT(ActivationTime, '%Y%m%d'),count(Id) from PosMachinesTwo where Id in (" + ids.TrimEnd(',') + ") group by BuyUserId,BrandId,DATE_FORMAT(ActivationTime, '%Y%m%d')");
  1962. if (userDt.Rows.Count > 0)
  1963. {
  1964. function.WriteLog("人数:" + userDt.Rows.Count + "\n\n", "实时执行创客激活数日志");
  1965. foreach (DataRow userDr in userDt.Rows)
  1966. {
  1967. int UserId = int.Parse(userDr["BuyUserId"].ToString());
  1968. int BrandId = int.Parse(userDr["BrandId"].ToString());
  1969. string TradeDate = userDr[2].ToString();
  1970. int ActCount = int.Parse(function.CheckInt(userDr[3].ToString()));
  1971. int QrPayFlag = 0;
  1972. string TradeMonth = TradeDate.Substring(0, 6);
  1973. string date = TradeDate.Substring(0, 4) + "-" + TradeDate.Substring(4, 2) + "-" + TradeDate.Substring(6, 2);
  1974. string start = date + " 00:00:00";
  1975. string end = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
  1976. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  1977. string ParentNav = user.ParentNav;
  1978. UserTradeDaySummary selfStat = db.UserTradeDaySummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.QueryCount == QrPayFlag && m.SeoTitle == "self");
  1979. if (selfStat == null)
  1980. {
  1981. selfStat = db.UserTradeDaySummary.Add(new UserTradeDaySummary()
  1982. {
  1983. UserId = UserId,
  1984. TradeMonth = TradeMonth,
  1985. TradeDate = TradeDate,
  1986. BrandId = BrandId,
  1987. QueryCount = QrPayFlag,
  1988. SeoTitle = "self",
  1989. }).Entity;
  1990. db.SaveChanges();
  1991. }
  1992. selfStat.DirectDebitCapNum += ActCount;
  1993. ParentNav += "," + UserId + ",";
  1994. if (!string.IsNullOrEmpty(ParentNav))
  1995. {
  1996. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  1997. foreach (string NavUserIdString in ParentNavList)
  1998. {
  1999. int NavUserId = int.Parse(NavUserIdString);
  2000. UserTradeDaySummary teamStat = db.UserTradeDaySummary.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.QueryCount == QrPayFlag && m.SeoTitle == "team");
  2001. if (teamStat == null)
  2002. {
  2003. teamStat = db.UserTradeDaySummary.Add(new UserTradeDaySummary()
  2004. {
  2005. UserId = NavUserId,
  2006. TradeMonth = TradeMonth,
  2007. TradeDate = TradeDate,
  2008. BrandId = BrandId,
  2009. QueryCount = QrPayFlag,
  2010. SeoTitle = "team",
  2011. }).Entity;
  2012. db.SaveChanges();
  2013. }
  2014. teamStat.NonDirectDebitCapNum += ActCount;
  2015. }
  2016. }
  2017. }
  2018. db.SaveChanges();
  2019. OtherMySqlConn.op("update PosMachinesTwo set QueryCount=1 where Id in (" + ids.TrimEnd(',') + ")");
  2020. }
  2021. }
  2022. }
  2023. catch (Exception ex)
  2024. {
  2025. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "实时执行创客激活数异常");
  2026. }
  2027. db.Dispose();
  2028. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "实时执行创客激活数日志");
  2029. }
  2030. // 统计新增创客数
  2031. public void StartNewUserNum()
  2032. {
  2033. Thread th = new Thread(StartNewUserNumFor);
  2034. th.IsBackground = true;
  2035. th.Start();
  2036. }
  2037. public void StartNewUserNumFor()
  2038. {
  2039. // DateTime end = DateTime.Parse("2022-06-15 00:00:00");
  2040. // DateTime check = DateTime.Parse("2022-06-14");
  2041. // while (check <= end)
  2042. // {
  2043. // StartNewUserNumDo(check.ToString("yyyy-MM-dd"));
  2044. // check = check.AddDays(1);
  2045. // }
  2046. while (true)
  2047. {
  2048. if(DateTime.Now.Hour >= 2)
  2049. {
  2050. StartNewUserNumEverTime();
  2051. }
  2052. Thread.Sleep(120000);
  2053. }
  2054. }
  2055. public void StartNewUserNumDo(object sender)
  2056. {
  2057. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
  2058. string date = sender.ToString();
  2059. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "执行新增创客数日志");
  2060. WebCMSEntities db = new WebCMSEntities();
  2061. try
  2062. {
  2063. string StatDate = date.Replace("-", "");
  2064. string StatMonth = StatDate.Substring(0, 6);
  2065. string start = date + " 00:00:00";
  2066. string end = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
  2067. List<string> uids = new List<string>();
  2068. DataTable userDt = OtherMySqlConn.dtable("select Id,ParentNav from Users where QueryCount=0 and AuthFlag=1 and AuthDate>='" + start + "' and AuthDate<'" + end + "'");
  2069. function.WriteLog("人数:" + userDt.Rows.Count + "\n\n", "执行新增创客数日志");
  2070. foreach (DataRow userDr in userDt.Rows)
  2071. {
  2072. int UserId = int.Parse(userDr["Id"].ToString());
  2073. string ParentNav = userDr["ParentNav"].ToString();
  2074. function.WriteLog(UserId + ":" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行新增创客数日志");
  2075. DataTable selfdt = OtherMySqlConn.dtable("select count(Id) from Users where QueryCount=0 and AuthFlag=1 and AuthDate>='" + start + "' and AuthDate<'" + end + "' and ParentUserId=" + UserId + "");
  2076. function.WriteLog(UserId + ":" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行新增创客数日志");
  2077. foreach (DataRow selfDr in selfdt.Rows)
  2078. {
  2079. int AddCount = int.Parse(selfDr[0].ToString());
  2080. PullnewSummary selfStat = db.PullnewSummary.FirstOrDefault(m => m.UserId == UserId && m.StatMonth == StatMonth && m.StatDate == StatDate && m.SeoTitle == "self");
  2081. if (selfStat == null)
  2082. {
  2083. selfStat = db.PullnewSummary.Add(new PullnewSummary()
  2084. {
  2085. UserId = UserId,
  2086. StatMonth = StatMonth,
  2087. StatDate = StatDate,
  2088. SeoTitle = "self",
  2089. }).Entity;
  2090. db.SaveChanges();
  2091. }
  2092. selfStat.RecUserAuthNum += AddCount;
  2093. db.SaveChanges();
  2094. }
  2095. if (!string.IsNullOrEmpty(ParentNav))
  2096. {
  2097. ParentNav += "," + UserId + ",";
  2098. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  2099. foreach (string NavUserIdString in ParentNavList)
  2100. {
  2101. if (!uids.Contains(NavUserIdString + start))
  2102. {
  2103. uids.Add(NavUserIdString + start);
  2104. int NavUserId = int.Parse(NavUserIdString);
  2105. function.WriteLog(NavUserId + ":team:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行新增创客数日志");
  2106. DataTable teamDt = OtherMySqlConn.dtable("select count(Id) from Users where QueryCount=0 and AuthFlag=1 and AuthDate>='" + start + "' and AuthDate<'" + end + "' and ParentNav like '%," + NavUserId + ",%'");
  2107. function.WriteLog(NavUserId + ":team:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行新增创客数日志");
  2108. foreach (DataRow teamDr in teamDt.Rows)
  2109. {
  2110. int AddCount = int.Parse(teamDr[0].ToString());
  2111. PullnewSummary teamStat = db.PullnewSummary.FirstOrDefault(m => m.UserId == NavUserId && m.StatMonth == StatMonth && m.StatDate == StatDate && m.SeoTitle == "team");
  2112. if (teamStat == null)
  2113. {
  2114. teamStat = db.PullnewSummary.Add(new PullnewSummary()
  2115. {
  2116. UserId = NavUserId,
  2117. StatMonth = StatMonth,
  2118. StatDate = StatDate,
  2119. SeoTitle = "team",
  2120. }).Entity;
  2121. db.SaveChanges();
  2122. }
  2123. teamStat.RecUserAuthNum += AddCount;
  2124. db.SaveChanges();
  2125. }
  2126. }
  2127. }
  2128. }
  2129. }
  2130. OtherMySqlConn.op("update Users set QueryCount=1 where QueryCount=0 and AuthFlag=1 and AuthDate>='" + start + "' and AuthDate<'" + end + "'");
  2131. }
  2132. catch (Exception ex)
  2133. {
  2134. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "执行新增创客数异常");
  2135. }
  2136. db.Dispose();
  2137. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行新增创客数日志");
  2138. Thread.Sleep(60000);
  2139. }
  2140. public void StartNewUserNumEverTime()
  2141. {
  2142. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
  2143. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "实时执行新增创客数日志");
  2144. WebCMSEntities db = new WebCMSEntities();
  2145. try
  2146. {
  2147. DataTable idsDt = OtherMySqlConn.dtable("select Id from Users where QueryCount=0 and AuthFlag=1 and AuthDate is not null limit 50");
  2148. if(idsDt.Rows.Count > 0)
  2149. {
  2150. string ids = "";
  2151. foreach (DataRow idsDr in idsDt.Rows)
  2152. {
  2153. ids += idsDr["Id"].ToString() + ",";
  2154. }
  2155. DataTable userDt = OtherMySqlConn.dtable("select ParentUserId,DATE_FORMAT(AuthDate, '%Y%m%d'),count(Id) from Users where Id in (" + ids.TrimEnd(',') + ") group by ParentUserId,DATE_FORMAT(AuthDate, '%Y%m%d')");
  2156. if(userDt.Rows.Count > 0)
  2157. {
  2158. function.WriteLog("人数:" + userDt.Rows.Count + "\n\n", "实时执行新增创客数日志");
  2159. foreach (DataRow userDr in userDt.Rows)
  2160. {
  2161. string StatDate = userDr[1].ToString();
  2162. int UserId = int.Parse(userDr["ParentUserId"].ToString());
  2163. string StatMonth = StatDate.Substring(0, 6);
  2164. string date = StatDate.Substring(0, 4) + "-" + StatDate.Substring(4, 2) + "-" + StatDate.Substring(6, 2);
  2165. string start = date + " 00:00:00";
  2166. string end = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
  2167. int AddCount = int.Parse(userDr[2].ToString());
  2168. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  2169. string ParentNav = user.ParentNav;
  2170. PullnewSummary selfStat = db.PullnewSummary.FirstOrDefault(m => m.UserId == UserId && m.StatMonth == StatMonth && m.StatDate == StatDate && m.SeoTitle == "self");
  2171. if (selfStat == null)
  2172. {
  2173. selfStat = db.PullnewSummary.Add(new PullnewSummary()
  2174. {
  2175. UserId = UserId,
  2176. StatMonth = StatMonth,
  2177. StatDate = StatDate,
  2178. SeoTitle = "self",
  2179. }).Entity;
  2180. db.SaveChanges();
  2181. }
  2182. selfStat.RecUserAuthNum += AddCount;
  2183. ParentNav += "," + UserId + ",";
  2184. if (!string.IsNullOrEmpty(ParentNav))
  2185. {
  2186. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  2187. foreach (string NavUserIdString in ParentNavList)
  2188. {
  2189. int NavUserId = int.Parse(NavUserIdString);
  2190. PullnewSummary teamStat = db.PullnewSummary.FirstOrDefault(m => m.UserId == NavUserId && m.StatMonth == StatMonth && m.StatDate == StatDate && m.SeoTitle == "team");
  2191. if (teamStat == null)
  2192. {
  2193. teamStat = db.PullnewSummary.Add(new PullnewSummary()
  2194. {
  2195. UserId = NavUserId,
  2196. StatMonth = StatMonth,
  2197. StatDate = StatDate,
  2198. SeoTitle = "team",
  2199. }).Entity;
  2200. db.SaveChanges();
  2201. }
  2202. teamStat.RecUserAuthNum += AddCount;
  2203. }
  2204. }
  2205. }
  2206. db.SaveChanges();
  2207. OtherMySqlConn.op("update Users set QueryCount=1 where Id in (" + ids.TrimEnd(',') + ")");
  2208. }
  2209. }
  2210. }
  2211. catch (Exception ex)
  2212. {
  2213. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "实时执行新增创客数异常");
  2214. }
  2215. db.Dispose();
  2216. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "实时执行新增创客数日志");
  2217. }
  2218. // 每天统计头一天的收益
  2219. public void StatProfit()
  2220. {
  2221. Thread th = new Thread(StatProfitDo);
  2222. th.IsBackground = true;
  2223. th.Start();
  2224. }
  2225. public void StatProfitDo()
  2226. {
  2227. // DateTime end = DateTime.Parse("2022-06-16 00:00:00");
  2228. // DateTime check = DateTime.Parse("2022-06-16");
  2229. // while (check <= end)
  2230. // {
  2231. // StatProfitEverDay(check.ToString("yyyy-MM-dd"));
  2232. // Thread.Sleep(1000);
  2233. // check = check.AddDays(1);
  2234. // }
  2235. while (true)
  2236. {
  2237. if(DateTime.Now.Hour >= 2)
  2238. {
  2239. StatProfitEverDayEverTime();
  2240. }
  2241. Thread.Sleep(120000);
  2242. }
  2243. }
  2244. public void StatProfitEverDay(object sender)
  2245. {
  2246. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
  2247. string date = sender.ToString();
  2248. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "执行昨天收益日志");
  2249. WebCMSEntities db = new WebCMSEntities();
  2250. try
  2251. {
  2252. string TradeDate = date.Replace("-", "");
  2253. string TradeMonth = TradeDate.Substring(0, 6);
  2254. string start = date + " 00:00:00";
  2255. string end = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
  2256. string startId = "0";
  2257. List<string> uids = new List<string>();
  2258. DataTable startDt = OtherMySqlConn.dtable("select min(Id) from UserAccountRecord where CreateDate>='" + start + "'");
  2259. if (startDt.Rows.Count > 0)
  2260. {
  2261. startId = startDt.Rows[0][0].ToString();
  2262. }
  2263. function.WriteLog(startId + "\n\n", "执行昨天收益日志");
  2264. DataTable userDt = OtherMySqlConn.dtable("select Id,ParentNav from Users where Id in (select DISTINCT UserId from UserAccountRecord where Id>=" + startId + " and CreateDate>='" + start + "' and CreateDate<'" + end + "')");
  2265. function.WriteLog("交易人数:" + userDt.Rows.Count + "\n\n", "执行昨天收益日志");
  2266. foreach (DataRow userDr in userDt.Rows)
  2267. {
  2268. int UserId = int.Parse(userDr["Id"].ToString());
  2269. string ParentNav = userDr["ParentNav"].ToString();
  2270. function.WriteLog(UserId + ":" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行昨天收益日志");
  2271. DataTable selfdt = OtherMySqlConn.dtable("select ChangeType,ProductType,sum(ChangeAmount) from UserAccountRecord where Id>=" + startId + " and CreateDate>='" + start + "' and CreateDate<'" + end + "' and UserId=" + UserId + " group by ChangeType,ProductType");
  2272. function.WriteLog(UserId + ":" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行昨天收益日志");
  2273. foreach (DataRow selfDr in selfdt.Rows)
  2274. {
  2275. int ChangeType = int.Parse(selfDr["ChangeType"].ToString());
  2276. int ProductType = int.Parse(selfDr["ProductType"].ToString());
  2277. decimal ProfitAmount = decimal.Parse(selfDr[2].ToString());
  2278. UserRebateDetail selfStat = db.UserRebateDetail.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.RebateType == ChangeType && m.ProductType == ProductType && m.SeoTitle == "self");
  2279. if (selfStat == null)
  2280. {
  2281. selfStat = db.UserRebateDetail.Add(new UserRebateDetail()
  2282. {
  2283. UserId = UserId,
  2284. TradeMonth = TradeMonth,
  2285. TradeDate = TradeDate,
  2286. RebateType = ChangeType,
  2287. ProductType = ProductType,
  2288. SeoTitle = "self",
  2289. }).Entity;
  2290. db.SaveChanges();
  2291. }
  2292. selfStat.CreditRewardAmount += ProfitAmount;
  2293. db.SaveChanges();
  2294. }
  2295. if (!string.IsNullOrEmpty(ParentNav))
  2296. {
  2297. ParentNav += "," + UserId + ",";
  2298. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  2299. foreach (string NavUserIdString in ParentNavList)
  2300. {
  2301. if (!uids.Contains(NavUserIdString + start))
  2302. {
  2303. uids.Add(NavUserIdString + start);
  2304. int NavUserId = int.Parse(NavUserIdString);
  2305. function.WriteLog(NavUserId + ":team:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行昨天收益日志");
  2306. DataTable teamDt = OtherMySqlConn.dtable("select ChangeType,ProductType,sum(ChangeAmount) from UserAccountRecord where Id>=" + startId + " and CreateDate>='" + start + "' and CreateDate<'" + end + "' and UserId in (select Id from Users where ParentNav like '%," + NavUserId + ",%' or Id=" + NavUserId + ") group by ChangeType,ProductType");
  2307. function.WriteLog(NavUserId + ":team:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行昨天收益日志");
  2308. foreach (DataRow teamDr in teamDt.Rows)
  2309. {
  2310. int ChangeType = int.Parse(teamDr["ChangeType"].ToString());
  2311. int ProductType = int.Parse(teamDr["ProductType"].ToString());
  2312. decimal ProfitAmount = decimal.Parse(teamDr[2].ToString());
  2313. UserRebateDetail teamStat = db.UserRebateDetail.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.RebateType == ChangeType && m.ProductType == ProductType && m.SeoTitle == "team");
  2314. if (teamStat == null)
  2315. {
  2316. teamStat = db.UserRebateDetail.Add(new UserRebateDetail()
  2317. {
  2318. UserId = NavUserId,
  2319. TradeMonth = TradeMonth,
  2320. TradeDate = TradeDate,
  2321. RebateType = ChangeType,
  2322. ProductType = ProductType,
  2323. SeoTitle = "team",
  2324. }).Entity;
  2325. db.SaveChanges();
  2326. }
  2327. teamStat.CreditRewardAmount += ProfitAmount;
  2328. db.SaveChanges();
  2329. }
  2330. }
  2331. }
  2332. }
  2333. }
  2334. }
  2335. catch (Exception ex)
  2336. {
  2337. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计昨天收益异常");
  2338. }
  2339. db.Dispose();
  2340. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行昨天收益日志");
  2341. }
  2342. public void StatProfitEverDayEverTime()
  2343. {
  2344. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
  2345. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "实时统计收益日志");
  2346. WebCMSEntities db = new WebCMSEntities();
  2347. try
  2348. {
  2349. string startId = function.ReadInstance("/UserAccountRecord/Id.txt");
  2350. if(string.IsNullOrEmpty(startId))
  2351. {
  2352. startId = "160653";
  2353. }
  2354. DataTable idsDt = OtherMySqlConn.dtable("select Id from UserAccountRecord where Id>=" + startId + " and QueryCount=0 order by Id limit 50");
  2355. if(idsDt.Rows.Count > 0)
  2356. {
  2357. string ids = "";
  2358. foreach (DataRow idsDr in idsDt.Rows)
  2359. {
  2360. ids += idsDr["Id"].ToString() + ",";
  2361. startId = idsDr["Id"].ToString();
  2362. }
  2363. DataTable userDt = OtherMySqlConn.dtable("select UserId,ChangeType,ProductType,DATE_FORMAT(CreateDate, '%Y%m%d'),sum(ChangeAmount) from UserAccountRecord where Id in (" + ids.TrimEnd(',') + ") group by UserId,ChangeType,ProductType,DATE_FORMAT(CreateDate, '%Y%m%d')");
  2364. if (userDt.Rows.Count > 0)
  2365. {
  2366. function.WriteLog("收益人数:" + userDt.Rows.Count + "\n\n", "实时统计收益日志");
  2367. foreach (DataRow userDr in userDt.Rows)
  2368. {
  2369. int UserId = int.Parse(userDr["UserId"].ToString());
  2370. int ChangeType = int.Parse(userDr["ChangeType"].ToString());
  2371. int ProductType = int.Parse(userDr["ProductType"].ToString());
  2372. string TradeDate = userDr[3].ToString();
  2373. decimal ProfitAmount = decimal.Parse(userDr[4].ToString());
  2374. string TradeMonth = TradeDate.Substring(0, 6);
  2375. string date = TradeDate.Substring(0, 4) + "-" + TradeDate.Substring(4, 2) + "-" + TradeDate.Substring(6, 2);
  2376. string start = date + " 00:00:00";
  2377. string end = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
  2378. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  2379. string ParentNav = user.ParentNav;
  2380. UserRebateDetail selfStat = db.UserRebateDetail.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.RebateType == ChangeType && m.ProductType == ProductType && m.SeoTitle == "self");
  2381. if (selfStat == null)
  2382. {
  2383. selfStat = db.UserRebateDetail.Add(new UserRebateDetail()
  2384. {
  2385. UserId = UserId,
  2386. TradeMonth = TradeMonth,
  2387. TradeDate = TradeDate,
  2388. RebateType = ChangeType,
  2389. ProductType = ProductType,
  2390. SeoTitle = "self",
  2391. }).Entity;
  2392. db.SaveChanges();
  2393. }
  2394. selfStat.CreditRewardAmount += ProfitAmount;
  2395. ParentNav += "," + UserId + ",";
  2396. if (!string.IsNullOrEmpty(ParentNav))
  2397. {
  2398. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  2399. foreach (string NavUserIdString in ParentNavList)
  2400. {
  2401. int NavUserId = int.Parse(NavUserIdString);
  2402. UserRebateDetail teamStat = db.UserRebateDetail.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.RebateType == ChangeType && m.ProductType == ProductType && m.SeoTitle == "team");
  2403. if (teamStat == null)
  2404. {
  2405. teamStat = db.UserRebateDetail.Add(new UserRebateDetail()
  2406. {
  2407. UserId = NavUserId,
  2408. TradeMonth = TradeMonth,
  2409. TradeDate = TradeDate,
  2410. RebateType = ChangeType,
  2411. ProductType = ProductType,
  2412. SeoTitle = "team",
  2413. }).Entity;
  2414. db.SaveChanges();
  2415. }
  2416. teamStat.CreditRewardAmount += ProfitAmount;
  2417. }
  2418. }
  2419. }
  2420. db.SaveChanges();
  2421. OtherMySqlConn.op("update UserAccountRecord set QueryCount=1 where Id in (" + ids.TrimEnd(',') + ")");
  2422. function.WritePage("/UserAccountRecord/", "Id.txt", startId);
  2423. }
  2424. }
  2425. }
  2426. catch (Exception ex)
  2427. {
  2428. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "实时统计收益异常");
  2429. }
  2430. db.Dispose();
  2431. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "实时统计收益日志");
  2432. }
  2433. #region 判断品牌互斥条件,根据身份证号
  2434. public bool CheckRepeatByBrand(WebCMSEntities db, int MerchantId)
  2435. {
  2436. int check = db.PosMachinesTwo.Count(m => m.Status > -1 && m.BindMerchantId == MerchantId);
  2437. return false;
  2438. }
  2439. #endregion
  2440. }
  2441. }