StatService.cs 154 KB

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