StatService.cs 159 KB

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