StatService.cs 159 KB

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