StatService.cs 189 KB

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