StatService.cs 155 KB

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