StatService.cs 156 KB

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