StatService.cs 156 KB

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