StatService.cs 182 KB

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