StatService.cs 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using LitJson;
  5. using System.Linq;
  6. using System.Data;
  7. using System.Threading;
  8. using MySystem.PxcModels;
  9. namespace MySystem
  10. {
  11. public class StatService
  12. {
  13. public readonly static StatService Instance = new StatService();
  14. private StatService()
  15. { }
  16. public void Start(JobMqMsg jobInfo)
  17. {
  18. string content = "";
  19. try
  20. {
  21. string doDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
  22. string flag = function.ReadInstance("/Stat/" + doDate + ".txt");
  23. if (string.IsNullOrEmpty(flag))
  24. {
  25. function.WritePage("/Stat/", doDate + ".txt", DateTime.Now.ToString("HH:mm:ss"));
  26. dosomething1(doDate);
  27. dosomething11(doDate);
  28. // dosomething2(doDate);
  29. StartEverDay(doDate);
  30. dosomething4(doDate);
  31. ListenFluxRecord(doDate);
  32. }
  33. string Msg = "success";
  34. jobInfo.Status = Msg == "success" ? 1 : 0;
  35. jobInfo.Msg = Msg == "success" ? "执行完成" : Msg;
  36. RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(jobInfo), "PublicBack");
  37. }
  38. catch (Exception ex)
  39. {
  40. if (!string.IsNullOrEmpty(content))
  41. {
  42. Dictionary<string, string> data = new Dictionary<string, string>();
  43. data.Add("ErrTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  44. data.Add("ErrMsg", ex.ToString());
  45. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(data), "public_err");
  46. }
  47. else
  48. {
  49. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public_service");
  50. }
  51. }
  52. }
  53. public void ActiveReward(JobMqMsg jobInfo)
  54. {
  55. string content = "";
  56. try
  57. {
  58. dosomething3();
  59. string Msg = "success";
  60. jobInfo.Status = Msg == "success" ? 1 : 0;
  61. jobInfo.Msg = Msg == "success" ? "执行完成" : Msg;
  62. RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(jobInfo), "PublicBack");
  63. }
  64. catch (Exception ex)
  65. {
  66. if (!string.IsNullOrEmpty(content))
  67. {
  68. Dictionary<string, string> data = new Dictionary<string, string>();
  69. data.Add("ErrTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  70. data.Add("ErrMsg", ex.ToString());
  71. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(data), "public_err");
  72. }
  73. else
  74. {
  75. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public_service");
  76. }
  77. }
  78. }
  79. public void clear()
  80. {
  81. RedisDbconn.Instance.Clear("TotalAmount*");
  82. RedisDbconn.Instance.Clear("TotalCloudPayAmount*");
  83. RedisDbconn.Instance.Clear("TotalPosAmount*");
  84. RedisDbconn.Instance.Clear("TeamTotalAmount*");
  85. RedisDbconn.Instance.Clear("TeamTotalCloudPayAmount*");
  86. RedisDbconn.Instance.Clear("TeamTotalPosAmount*");
  87. // RedisDbconn.Instance.Clear("TotalUser*");
  88. // RedisDbconn.Instance.Clear("TeamTotalUser*");
  89. // RedisDbconn.Instance.Clear("AddUser*");
  90. // RedisDbconn.Instance.Clear("TeamAddUser*");
  91. // RedisDbconn.Instance.Clear("TotalPosMerchant*");
  92. // RedisDbconn.Instance.Clear("TeamTotalPosMerchant*");
  93. RedisDbconn.Instance.Clear("MerTotalAmount*");
  94. }
  95. // 1. 前一天的绑定记录和创客信息匹配,设置商户型创客
  96. public void dosomething1(string date)
  97. {
  98. // TODO: 每天扫描非商户型创客的持有机具数量
  99. SpModels.WebCMSEntities spdb = new SpModels.WebCMSEntities();
  100. WebCMSEntities db = new WebCMSEntities();
  101. DateTime yesterday = DateTime.Parse(date + " 00:00:00");
  102. DateTime today = DateTime.Parse(DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00");
  103. List<SpModels.BindRecord> binds = spdb.BindRecord.Where(m => m.CreateTime >= yesterday && m.CreateTime < today).ToList();
  104. foreach (SpModels.BindRecord bind in binds)
  105. {
  106. string Mobile = bind.MerNewSnNo;
  107. string MerNo = bind.MerNo;
  108. string BeforeNum = "";
  109. string AfterNum = "";
  110. if (Mobile.Contains("****") && Mobile.Length == 11)
  111. {
  112. BeforeNum = Mobile.Substring(0, 3);
  113. AfterNum = Mobile.Substring(7);
  114. }
  115. string Name = bind.MerName;
  116. if (bind.ProductType == "2")
  117. {
  118. if (Name.Contains("-"))
  119. {
  120. Name = Name.Split('-')[1];
  121. }
  122. else if (Name.Contains("_"))
  123. {
  124. Name = Name.Split('_')[1];
  125. }
  126. }
  127. else if (bind.ProductType == "4")
  128. {
  129. Name = bind.SeoTitle;
  130. }
  131. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Mobile.StartsWith(BeforeNum) && m.Mobile.EndsWith(AfterNum) && m.RealName == Name && m.AuthFlag == 1 && m.MerchantType == 0);
  132. if (user != null)
  133. {
  134. bool checkPos = db.PosMachinesTwo.Any(m => m.UserId == user.Id);
  135. if (!checkPos)
  136. {
  137. PxcModels.MachineForSnNo forSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == bind.MerSnNo) ?? new PxcModels.MachineForSnNo();
  138. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forSnNo.SnId && m.UserId != user.Id);
  139. if (pos != null)
  140. {
  141. pos.UserId = user.Id;
  142. user.MerchantType = 1;
  143. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == MerNo) ?? new PxcModels.PosMerchantInfo();
  144. merchant.UserId = user.Id;
  145. merchant.MerUserType = 1;
  146. pos.SeoTitle = user.Id.ToString(); // 记录商户型创客的Id
  147. RedisDbconn.Instance.Set("PosMachinesTwo:" + pos.Id, pos);
  148. db.SaveChanges();
  149. RedisDbconn.Instance.Set("Users:" + user.Id, user);
  150. RedisDbconn.Instance.Set("PosMerchantInfo:" + merchant.Id, merchant);
  151. }
  152. }
  153. }
  154. }
  155. spdb.Dispose();
  156. db.Dispose();
  157. }
  158. // 1. 前一天的商户型创客,判断名下是否3台激活机器,是则变为非商户型创客
  159. public void dosomething11(string date)
  160. {
  161. WebCMSEntities db = new WebCMSEntities();
  162. DateTime yesterday = DateTime.Parse(date + " 00:00:00");
  163. DateTime today = DateTime.Parse(DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00");
  164. var users = db.Users.Select(m => new { m.Id, m.MerchantType }).Where(m => m.MerchantType == 1).ToList();
  165. foreach (var user in users)
  166. {
  167. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == user.Id && m.PosSnType == 0);
  168. int couponCount = db.PosCoupons.Count(m => m.UserId == user.Id && m.IsUse == 0); //判断是否拥有3张券
  169. if (machineCount + couponCount >= 3)
  170. {
  171. Users edit = db.Users.FirstOrDefault(m => m.Id == user.Id);
  172. if (edit != null)
  173. {
  174. edit.MerchantType = 0;
  175. db.SaveChanges();
  176. RedisDbconn.Instance.Set("Users:" + user.Id, edit);
  177. }
  178. }
  179. }
  180. db.Dispose();
  181. }
  182. // 2. 每天统计前一天的交易额,每笔交易判断商户号的所属人是商户型创客,如果是,则从此人开始累计交易额,若不是,则从此人上级开始累计交易额
  183. public void dosomething2(string date)
  184. {
  185. string yesterday = date + " 00:00:00";
  186. string today = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
  187. string TradeMonth = DateTime.Parse(date).ToString("yyyyMM");
  188. string TradeDate = DateTime.Parse(date).ToString("yyyyMMdd");
  189. 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");
  190. foreach (DataRow dr in dt.Rows)
  191. {
  192. int UserId = int.Parse(dr["UserId"].ToString()); //创客ID
  193. int BrandId = int.Parse(dr["BrandId"].ToString()); //品牌
  194. int MerchantId = int.Parse(dr["MerchantId"].ToString()); //商户Id
  195. int BankCardType = int.Parse(dr["BankCardType"].ToString()); //卡类型
  196. int QrPayFlag = int.Parse(dr["QrPayFlag"].ToString()); //云闪付
  197. decimal TradeAmount = decimal.Parse(dr[5].ToString()); //当日交易额
  198. string content = "{";
  199. content += "\"DateString\":\"" + date + "\",";
  200. content += "\"UserId\":\"" + UserId + "\",";
  201. content += "\"BrandId\":\"" + BrandId + "\",";
  202. content += "\"MerchantId\":\"" + MerchantId + "\",";
  203. content += "\"BankCardType\":\"" + BankCardType + "\",";
  204. content += "\"QrPayFlag\":\"" + QrPayFlag + "\",";
  205. content += "\"TradeAmount\":\"" + TradeAmount + "\"";
  206. content += "}";
  207. RedisDbconn.Instance.AddList("TradeStatQueue", content);
  208. // RedisDbconn.Instance.AddList("TradeStatQueue2", content);
  209. }
  210. }
  211. public void testStatTrade(string date)
  212. {
  213. string yesterday = date + " 00:00:00";
  214. string today = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
  215. string TradeMonth = DateTime.Parse(date).ToString("yyyyMM");
  216. string TradeDate = DateTime.Parse(date).ToString("yyyyMMdd");
  217. 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");
  218. foreach (DataRow dr in dt.Rows)
  219. {
  220. int UserId = int.Parse(dr["UserId"].ToString()); //创客ID
  221. int BrandId = int.Parse(dr["BrandId"].ToString()); //品牌
  222. int MerchantId = int.Parse(dr["MerchantId"].ToString()); //商户Id
  223. int BankCardType = int.Parse(dr["BankCardType"].ToString()); //卡类型
  224. int QrPayFlag = int.Parse(dr["QrPayFlag"].ToString()); //云闪付
  225. decimal TradeAmount = decimal.Parse(dr[5].ToString()); //当日交易额
  226. string content = "{";
  227. content += "\"DateString\":\"" + date + "\",";
  228. content += "\"UserId\":\"" + UserId + "\",";
  229. content += "\"BrandId\":\"" + BrandId + "\",";
  230. content += "\"MerchantId\":\"" + MerchantId + "\",";
  231. content += "\"BankCardType\":\"" + BankCardType + "\",";
  232. content += "\"QrPayFlag\":\"" + QrPayFlag + "\",";
  233. content += "\"TradeAmount\":\"" + TradeAmount + "\"";
  234. content += "}";
  235. RedisDbconn.Instance.AddList("TradeStatQueueTest", content);
  236. }
  237. }
  238. // 3. 前一天的激活记录,根据创客类型,如果是商户型创客,则激活奖励的起始人从此人上级计算。如果不是,则从此人开始计算
  239. public void dosomething3(int posid = 0)
  240. {
  241. WebCMSEntities db = new WebCMSEntities();
  242. DateTime yesterday = DateTime.Now.AddHours(-3);
  243. DateTime today = DateTime.Now;
  244. IQueryable<PosMachinesTwo> posList = db.PosMachinesTwo.Where(m => m.ActivationState == 1 && !string.IsNullOrEmpty(m.SeoKeyword));
  245. if (posid > 0)
  246. {
  247. posList = posList.Where(m => m.Id == posid);
  248. }
  249. else
  250. {
  251. posList = posList.Where(m => m.ActivationTime >= yesterday && m.ActivationTime < today);
  252. }
  253. foreach (PosMachinesTwo pos in posList.ToList())
  254. {
  255. // PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId && !string.IsNullOrEmpty(m.MerIdcardNo));
  256. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  257. if (merchant != null)
  258. {
  259. // PosMerchantOtherInfo otherInfo = db.PosMerchantOtherInfo.FirstOrDefault(m => m.CertId == merchant.MerIdcardNo && m.PrizeFlag1 == 0);
  260. // if (otherInfo != null)
  261. // {
  262. // otherInfo.PrizeFlag1 = 1;
  263. // db.SaveChanges();
  264. string ParentNav = "";
  265. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId);
  266. if (user != null)
  267. {
  268. int GetUserId = user.Id;
  269. ParentNav = user.ParentNav;
  270. int TopUserId = 0;
  271. if (!string.IsNullOrEmpty(ParentNav))
  272. {
  273. TopUserId = int.Parse(ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  274. }
  275. decimal ActPrize = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  276. if (pos.BrandId == 1 || pos.BrandId == 3 || pos.BrandId == 4 || pos.BrandId == 5)
  277. {
  278. ActPrize = ActPrize / 100;
  279. }
  280. if (ActPrize > 0)
  281. {
  282. bool check = db.ActiveReward.Any(m => m.KqMerNo == merchant.KqMerNo);
  283. if (!check)
  284. {
  285. if (ActPrize == 99)
  286. {
  287. ActPrize = 100;
  288. }
  289. else if (ActPrize == 199)
  290. {
  291. ActPrize = 200;
  292. }
  293. else if (ActPrize == 299)
  294. {
  295. ActPrize = 300;
  296. }
  297. Users machineUser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  298. db.ActiveReward.Add(new ActiveReward()
  299. {
  300. CreateDate = DateTime.Now,
  301. UpdateDate = DateTime.Now,
  302. UserId = GetUserId, //创客
  303. MerchantId = pos.BindMerchantId, //商户
  304. StandardDate = pos.ActivationTime, //达标日期
  305. RewardAmount = ActPrize, //奖励金额
  306. BrandId = pos.BrandId, //品牌
  307. UserNav = ParentNav, //创客父级
  308. DirectBuddyNo = merchant.UserId, //商户直属创客
  309. KqMerNo = merchant.KqMerNo, //渠道商户编号
  310. KqSnNo = pos.PosSn, //渠道SN号
  311. SnType = pos.PosSnType, //机具类型
  312. SnApplyUserId = pos.BuyUserId, //机具申请创客
  313. ActType = 0, //激活类型
  314. SnStoreId = pos.StoreId, //SN仓库
  315. RewardTips = "激活奖励", //奖励描述
  316. Remark = "激活奖励", //备注
  317. ActDate = pos.ActivationTime, //激活时间
  318. TopUserId = TopUserId, //顶级创客
  319. SeoTitle = machineUser.RealName,
  320. });
  321. db.SaveChanges();
  322. string IdBrand = GetUserId + "_" + pos.BrandId;
  323. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  324. if (userData == null)
  325. {
  326. userData = db.UserMachineData.Add(new UserMachineData()
  327. {
  328. IdBrand = IdBrand,
  329. }).Entity;
  330. db.SaveChanges();
  331. }
  332. userData.ActProfit += ActPrize;
  333. db.SaveChanges();
  334. RedisDbconn.Instance.Set("UserMachineData:" + IdBrand, userData);
  335. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == GetUserId);
  336. if (account == null)
  337. {
  338. account = db.UserAccount.Add(new UserAccount()
  339. {
  340. Id = GetUserId,
  341. UserId = GetUserId,
  342. }).Entity;
  343. db.SaveChanges();
  344. }
  345. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  346. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  347. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  348. account.BalanceAmount += ActPrize;
  349. account.TotalAmount += ActPrize;
  350. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  351. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  352. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  353. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  354. {
  355. CreateDate = DateTime.Now,
  356. UpdateDate = DateTime.Now,
  357. UserId = GetUserId, //创客
  358. ChangeType = 0, //变动类型
  359. ProductType = pos.BrandId, //产品类型
  360. ChangeAmount = ActPrize, //变更金额
  361. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  362. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  363. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  364. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  365. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  366. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  367. }).Entity;
  368. db.SaveChanges();
  369. RedisDbconn.Instance.Set("UserAccount:" + GetUserId, account);
  370. string dateString = pos.ActivationTime.Value.ToString("yyyyMMdd");
  371. string monthString = pos.ActivationTime.Value.ToString("yyyyMM");
  372. // 激活奖励列表
  373. List<string> dates = RedisDbconn.Instance.GetList<string>("ActiveRewardDay:" + GetUserId + ":" + pos.BrandId);
  374. if (!dates.Contains(dateString))
  375. {
  376. RedisDbconn.Instance.AddList("ActiveRewardDay:" + GetUserId + ":" + pos.BrandId, dateString);
  377. }
  378. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:" + GetUserId + ":" + pos.BrandId + ":" + dateString, ActPrize);
  379. List<string> months = RedisDbconn.Instance.GetList<string>("ActiveRewardMonth:" + GetUserId + ":" + pos.BrandId);
  380. if (!months.Contains(monthString))
  381. {
  382. RedisDbconn.Instance.AddList("ActiveRewardMonth:" + GetUserId + ":" + pos.BrandId, monthString);
  383. }
  384. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:" + GetUserId + ":" + pos.BrandId + ":" + monthString, ActPrize);
  385. // 激活奖励详情
  386. List<int> actPrizeList = RedisDbconn.Instance.GetList<int>("ActiveRewardDetail:" + GetUserId + ":" + pos.BrandId + ":" + dateString);
  387. if (!actPrizeList.Contains(pos.BindMerchantId))
  388. {
  389. RedisDbconn.Instance.AddList("ActiveRewardDetail:" + GetUserId + ":" + pos.BrandId + ":" + dateString, pos.BindMerchantId);
  390. }
  391. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:mer:" + pos.BindMerchantId + ":" + pos.BrandId + ":" + dateString, ActPrize);
  392. //收支明细
  393. RedisDbconn.Instance.AddList("UserAccountRecord:" + GetUserId + ":1:" + monthString, userAccountRecord);
  394. RedisDbconn.Instance.AddNumber("UserAccount:" + GetUserId + ":1:" + monthString, ActPrize);
  395. RedisDbconn.Instance.Clear("UserAccount:Income:" + GetUserId + ":" + monthString);
  396. }
  397. }
  398. }
  399. // }
  400. }
  401. }
  402. db.Dispose();
  403. }
  404. //根据商户信息发放激活奖励
  405. public void dosomething32(int merid = 0)
  406. {
  407. WebCMSEntities db = new WebCMSEntities();
  408. DateTime yesterday = DateTime.Now.AddHours(-3);
  409. DateTime today = DateTime.Now;
  410. IQueryable<PosMerchantInfo> merList = db.PosMerchantInfo.Where(m => m.ActiveStatus == 1 && !string.IsNullOrEmpty(m.SeoKeyword));
  411. if (merid > 0)
  412. {
  413. merList = merList.Where(m => m.Id == merid);
  414. }
  415. else
  416. {
  417. merList = merList.Where(m => m.MerStandardDate >= yesterday && m.MerStandardDate < today);
  418. }
  419. foreach (PosMerchantInfo merchant in merList.ToList())
  420. {
  421. // PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId && !string.IsNullOrEmpty(m.MerIdcardNo));
  422. // PosMerchantOtherInfo otherInfo = db.PosMerchantOtherInfo.FirstOrDefault(m => m.CertId == merchant.MerIdcardNo && m.PrizeFlag1 == 0);
  423. // if (otherInfo != null)
  424. // {
  425. // otherInfo.PrizeFlag1 = 1;
  426. // db.SaveChanges();
  427. MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == merchant.MerchantNo) ?? new MachineForMerNo();
  428. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forMerNo.SnId) ?? new PosMachinesTwo();
  429. string ParentNav = "";
  430. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId);
  431. if (user != null)
  432. {
  433. int GetUserId = user.Id;
  434. ParentNav = user.ParentNav;
  435. int TopUserId = 0;
  436. if (!string.IsNullOrEmpty(ParentNav))
  437. {
  438. TopUserId = int.Parse(ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  439. }
  440. decimal ActPrize = decimal.Parse(function.CheckNum(merchant.SeoKeyword));
  441. if (pos.BrandId == 1 || pos.BrandId == 3)
  442. {
  443. ActPrize = ActPrize / 100;
  444. }
  445. if (ActPrize > 0)
  446. {
  447. bool check = db.ActiveReward.Any(m => m.KqSnNo == pos.PosSn);
  448. if (!check)
  449. {
  450. if (ActPrize == 99)
  451. {
  452. ActPrize = 100;
  453. }
  454. else if (ActPrize == 199)
  455. {
  456. ActPrize = 200;
  457. }
  458. else if (ActPrize == 299)
  459. {
  460. ActPrize = 300;
  461. }
  462. Users machineUser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  463. db.ActiveReward.Add(new ActiveReward()
  464. {
  465. CreateDate = DateTime.Now,
  466. UpdateDate = DateTime.Now,
  467. UserId = GetUserId, //创客
  468. MerchantId = pos.BindMerchantId, //商户
  469. StandardDate = pos.ActivationTime, //达标日期
  470. RewardAmount = ActPrize, //奖励金额
  471. BrandId = pos.BrandId, //品牌
  472. UserNav = ParentNav, //创客父级
  473. DirectBuddyNo = merchant.UserId, //商户直属创客
  474. KqMerNo = merchant.KqMerNo, //渠道商户编号
  475. KqSnNo = pos.PosSn, //渠道SN号
  476. SnType = pos.PosSnType, //机具类型
  477. SnApplyUserId = pos.BuyUserId, //机具申请创客
  478. ActType = 0, //激活类型
  479. SnStoreId = pos.StoreId, //SN仓库
  480. RewardTips = "激活奖励", //奖励描述
  481. Remark = "激活奖励", //备注
  482. ActDate = pos.ActivationTime, //激活时间
  483. TopUserId = TopUserId, //顶级创客
  484. SeoTitle = machineUser.RealName,
  485. });
  486. db.SaveChanges();
  487. string IdBrand = GetUserId + "_" + pos.BrandId;
  488. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  489. if (userData == null)
  490. {
  491. userData = db.UserMachineData.Add(new UserMachineData()
  492. {
  493. IdBrand = IdBrand,
  494. }).Entity;
  495. db.SaveChanges();
  496. }
  497. userData.ActProfit += ActPrize;
  498. db.SaveChanges();
  499. RedisDbconn.Instance.Set("UserMachineData:" + IdBrand, userData);
  500. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == GetUserId);
  501. if (account == null)
  502. {
  503. account = db.UserAccount.Add(new UserAccount()
  504. {
  505. Id = GetUserId,
  506. UserId = GetUserId,
  507. }).Entity;
  508. db.SaveChanges();
  509. }
  510. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  511. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  512. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  513. account.BalanceAmount += ActPrize;
  514. account.TotalAmount += ActPrize;
  515. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  516. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  517. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  518. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  519. {
  520. CreateDate = DateTime.Now,
  521. UpdateDate = DateTime.Now,
  522. UserId = GetUserId, //创客
  523. ChangeType = 0, //变动类型
  524. ProductType = pos.BrandId, //产品类型
  525. ChangeAmount = ActPrize, //变更金额
  526. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  527. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  528. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  529. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  530. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  531. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  532. }).Entity;
  533. db.SaveChanges();
  534. RedisDbconn.Instance.Set("UserAccount:" + GetUserId, account);
  535. string dateString = pos.ActivationTime.Value.ToString("yyyyMMdd");
  536. string monthString = pos.ActivationTime.Value.ToString("yyyyMM");
  537. // 激活奖励列表
  538. List<string> dates = RedisDbconn.Instance.GetList<string>("ActiveRewardDay:" + GetUserId + ":" + pos.BrandId);
  539. if (!dates.Contains(dateString))
  540. {
  541. RedisDbconn.Instance.AddList("ActiveRewardDay:" + GetUserId + ":" + pos.BrandId, dateString);
  542. }
  543. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:" + GetUserId + ":" + pos.BrandId + ":" + dateString, ActPrize);
  544. List<string> months = RedisDbconn.Instance.GetList<string>("ActiveRewardMonth:" + GetUserId + ":" + pos.BrandId);
  545. if (!months.Contains(monthString))
  546. {
  547. RedisDbconn.Instance.AddList("ActiveRewardMonth:" + GetUserId + ":" + pos.BrandId, monthString);
  548. }
  549. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:" + GetUserId + ":" + pos.BrandId + ":" + monthString, ActPrize);
  550. // 激活奖励详情
  551. List<int> actPrizeList = RedisDbconn.Instance.GetList<int>("ActiveRewardDetail:" + GetUserId + ":" + pos.BrandId + ":" + dateString);
  552. if (!actPrizeList.Contains(pos.BindMerchantId))
  553. {
  554. RedisDbconn.Instance.AddList("ActiveRewardDetail:" + GetUserId + ":" + pos.BrandId + ":" + dateString, pos.BindMerchantId);
  555. }
  556. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:mer:" + pos.BindMerchantId + ":" + pos.BrandId + ":" + dateString, ActPrize);
  557. //收支明细
  558. RedisDbconn.Instance.AddList("UserAccountRecord:" + GetUserId + ":1:" + monthString, userAccountRecord);
  559. RedisDbconn.Instance.AddNumber("UserAccount:" + GetUserId + ":1:" + monthString, ActPrize);
  560. }
  561. }
  562. }
  563. // }
  564. }
  565. db.Dispose();
  566. }
  567. //3. 前一天的激活记录,根据创客类型,如果是商户型创客,则开机奖励的起始人从此人上级计算。如果不是,则从此人开始计算
  568. public void dosomething4(string date)
  569. {
  570. WebCMSEntities db = new WebCMSEntities();
  571. DateTime yesterday = DateTime.Parse(date + " 00:00:00");
  572. DateTime today = DateTime.Parse(DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00");
  573. 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();
  574. foreach (PosMachinesTwo pos in posList)
  575. {
  576. if (pos.ActivationTime > DateTime.Now.AddDays(-20))
  577. {
  578. decimal ActPrize = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  579. if (ActPrize > 0)
  580. {
  581. // PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId && !string.IsNullOrEmpty(m.MerIdcardNo));
  582. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  583. if (merchant != null)
  584. {
  585. // PosMerchantOtherInfo otherInfo = db.PosMerchantOtherInfo.FirstOrDefault(m => m.CertId == merchant.MerIdcardNo && m.PrizeFlag2 == 0);
  586. // if (otherInfo != null)
  587. // {
  588. // otherInfo.PrizeFlag2 = 1;
  589. // db.SaveChanges();
  590. // 从机具所属人上级开始分开机奖励
  591. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId);
  592. if (user != null)
  593. {
  594. if (!string.IsNullOrEmpty(user.ParentNav))
  595. {
  596. decimal Prize = 20;
  597. string[] ParentNavs = user.ParentNav.Trim(',').Replace(",,", ",").Split(',');
  598. for (int i = ParentNavs.Length - 1; i >= 0; i--)
  599. {
  600. int UserId = int.Parse(ParentNavs[i]);
  601. Users puser = db.Users.FirstOrDefault(m => m.Id == UserId && m.AuthFlag == 1);
  602. if (puser != null && Prize > 0)
  603. {
  604. int machineCount = db.PosMachinesTwo.Count(m => m.BuyUserId == puser.Id && m.PosSnType == 0); //判断是否拥有3台购买机
  605. int ActiveCount = db.PosMachinesTwo.Count(m => m.BuyUserId == puser.Id && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  606. int couponCount = db.PosCoupons.Count(m => m.UserId == puser.Id && m.IsUse == 0); //判断是否拥有3张券
  607. // int BigCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.BrandId == 3); //判断是否拥有2台大机
  608. // int BigActiveCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.ActivationState == 1 && m.BrandId == 3); //判断是否拥有2台激活大机(不限购买或赠送)
  609. if (machineCount + ActiveCount + couponCount >= 3)
  610. {
  611. int pTopUserId = 0;
  612. if (!string.IsNullOrEmpty(puser.ParentNav))
  613. {
  614. pTopUserId = int.Parse(puser.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  615. }
  616. Users machineUser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  617. OpenRewardDetail detail = db.OpenRewardDetail.Add(new OpenRewardDetail()
  618. {
  619. CreateDate = DateTime.Now,
  620. UpdateDate = DateTime.Now,
  621. TradeMonth = yesterday.ToString("yyyyMM"), //交易月
  622. TradeDate = yesterday, //达标日期
  623. UserId = puser.Id, //创客
  624. BrandId = pos.BrandId, //品牌
  625. ProductName = RelationClass.GetKqProductsInfo(pos.BrandId), //产品名称
  626. MerchantId = pos.BindMerchantId, //商户
  627. DirectUserId = merchant.UserId, //商户直属人
  628. SnNo = pos.PosSn, //SN号
  629. MerNo = merchant.KqMerNo, //渠道商户号
  630. SnType = pos.PosSnType, //机具类型
  631. StandardDate = pos.ActivationTime, //商户的激活日期
  632. SnStoreId = pos.StoreId, //SN仓库
  633. MerBuddyType = puser.MerchantType, //商户创客类型
  634. RewardType = 1, //奖励类型 1-开机直接奖励,2-开机间接奖励
  635. RewardTips = "开机奖励", //奖励描述
  636. CreditTradeAmt = pos.CreditTrade, //贷记卡交易总金额
  637. DebitTradeAmt = pos.DebitCardTrade, //借记卡交易总金额
  638. CreditRewardAmount = Prize, //贷记卡交易奖励金额
  639. RewardDesc = "开机奖励", //奖励描述
  640. TopUserId = pTopUserId, //顶级创客
  641. SeoTitle = machineUser.RealName,
  642. }).Entity;
  643. db.OpenReward.Add(new OpenReward()
  644. {
  645. CreateDate = DateTime.Now,
  646. UpdateDate = DateTime.Now,
  647. TradeMonth = yesterday.ToString("yyyyMM"), //交易月
  648. TradeDate = DateTime.Now, //达标日期
  649. UserId = puser.Id, //创客
  650. BrandId = pos.BrandId, //品牌
  651. RewardType = 2, //奖励类型
  652. CreditTradeAmt = pos.CreditTrade, //贷记卡交易总金额
  653. DebitTradeAmt = pos.DebitCardTrade, //借记卡交易总金额
  654. CreditRewardAmount = Prize, //贷记卡交易奖励金额
  655. RewardDesc = "开机奖励", //奖励描述
  656. TopUserId = pTopUserId, //顶级创客
  657. });
  658. string IdBrand = puser.Id + "_" + pos.BrandId;
  659. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  660. if (userData == null)
  661. {
  662. userData = db.UserMachineData.Add(new UserMachineData()
  663. {
  664. IdBrand = IdBrand,
  665. }).Entity;
  666. db.SaveChanges();
  667. }
  668. userData.OpenProfit += Prize;
  669. db.SaveChanges();
  670. RedisDbconn.Instance.Set("UserMachineData:" + IdBrand, userData);
  671. //账户入库
  672. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id);
  673. if (account == null)
  674. {
  675. account = db.UserAccount.Add(new UserAccount()
  676. {
  677. Id = puser.Id,
  678. UserId = puser.Id,
  679. }).Entity;
  680. db.SaveChanges();
  681. }
  682. //收支明细入库
  683. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  684. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  685. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  686. account.BalanceAmount += Prize;
  687. account.TotalAmount += Prize;
  688. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  689. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  690. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  691. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  692. {
  693. CreateDate = DateTime.Now,
  694. UpdateDate = DateTime.Now,
  695. UserId = puser.Id, //创客
  696. ChangeType = 50, //变动类型
  697. ProductType = pos.BrandId, //产品类型
  698. ChangeAmount = Prize, //变更金额
  699. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  700. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  701. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  702. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  703. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  704. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  705. }).Entity;
  706. db.SaveChanges();
  707. RedisDbconn.Instance.Set("UserAccount:" + puser.Id, account);
  708. string dateString = yesterday.ToString("yyyyMMdd");
  709. string monthString = yesterday.ToString("yyyyMM");
  710. // 开机奖励列表
  711. List<string> dates = RedisDbconn.Instance.GetList<string>("OpenRewardDay:" + puser.Id + ":" + pos.BrandId);
  712. if (!dates.Contains(dateString))
  713. {
  714. RedisDbconn.Instance.AddList("OpenRewardDay:" + puser.Id + ":" + pos.BrandId, dateString);
  715. }
  716. RedisDbconn.Instance.AddNumber("OpenRewardAmt:" + puser.Id + ":" + pos.BrandId + ":" + dateString, Prize);
  717. List<string> months = RedisDbconn.Instance.GetList<string>("OpenRewardMonth:" + puser.Id + ":" + pos.BrandId);
  718. if (!months.Contains(monthString))
  719. {
  720. RedisDbconn.Instance.AddList("OpenRewardMonth:" + puser.Id + ":" + pos.BrandId, monthString);
  721. }
  722. RedisDbconn.Instance.AddNumber("OpenRewardAmt:" + puser.Id + ":" + pos.BrandId + ":" + monthString, Prize);
  723. // 开机奖励详情
  724. RedisDbconn.Instance.AddList("OpenRewardDetail:" + puser.Id + ":" + pos.BrandId + ":" + dateString, detail);
  725. //收支明细
  726. RedisDbconn.Instance.AddList("UserAccountRecord:" + puser.Id + ":1:" + monthString, userAccountRecord);
  727. RedisDbconn.Instance.AddNumber("UserAccount:" + puser.Id + ":1:" + monthString, Prize);
  728. Prize -= 10;
  729. }
  730. }
  731. }
  732. }
  733. }
  734. // }
  735. }
  736. }
  737. }
  738. }
  739. db.Dispose();
  740. }
  741. public void dosomething42(string date)
  742. {
  743. WebCMSEntities db = new WebCMSEntities();
  744. DateTime yesterday = DateTime.Parse(date + " 00:00:00");
  745. DateTime today = DateTime.Parse(DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00");
  746. List<PosMerchantInfo> merList = db.PosMerchantInfo.Where(m => m.ActiveStatus == 1 && m.MerStandardDate >= yesterday && m.MerStandardDate < today && !string.IsNullOrEmpty(m.SeoKeyword) && m.SeoKeyword != "0").ToList();
  747. foreach (PosMerchantInfo merchant in merList)
  748. {
  749. MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == merchant.MerchantNo) ?? new MachineForMerNo();
  750. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forMerNo.SnId) ?? new PosMachinesTwo();
  751. if (pos.ActivationTime > DateTime.Now.AddDays(-20) && pos.CreditTrade >= 1000)
  752. {
  753. decimal ActPrize = decimal.Parse(function.CheckNum(merchant.SeoKeyword));
  754. if (ActPrize > 0)
  755. {
  756. // PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId && !string.IsNullOrEmpty(m.MerIdcardNo));
  757. // PosMerchantOtherInfo otherInfo = db.PosMerchantOtherInfo.FirstOrDefault(m => m.CertId == merchant.MerIdcardNo && m.PrizeFlag2 == 0);
  758. // if (otherInfo != null)
  759. // {
  760. // otherInfo.PrizeFlag2 = 1;
  761. // db.SaveChanges();
  762. // 从机具所属人上级开始分开机奖励
  763. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId);
  764. if (user != null)
  765. {
  766. if (!string.IsNullOrEmpty(user.ParentNav))
  767. {
  768. decimal Prize = 20;
  769. string[] ParentNavs = user.ParentNav.Trim(',').Replace(",,", ",").Split(',');
  770. for (int i = ParentNavs.Length - 1; i >= 0; i--)
  771. {
  772. int UserId = int.Parse(ParentNavs[i]);
  773. Users puser = db.Users.FirstOrDefault(m => m.Id == UserId && m.AuthFlag == 1);
  774. if (puser != null && Prize > 0)
  775. {
  776. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.PosSnType == 0); //判断是否拥有3台购买机
  777. int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  778. int couponCount = db.PosCoupons.Count(m => m.UserId == puser.Id && m.IsUse == 0); //判断是否拥有3张券
  779. // int BigCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.BrandId == 3); //判断是否拥有2台大机
  780. // int BigActiveCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.ActivationState == 1 && m.BrandId == 3); //判断是否拥有2台激活大机(不限购买或赠送)
  781. if (machineCount + ActiveCount + couponCount >= 3)
  782. {
  783. int pTopUserId = 0;
  784. if (!string.IsNullOrEmpty(puser.ParentNav))
  785. {
  786. pTopUserId = int.Parse(puser.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  787. }
  788. Users machineUser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  789. OpenRewardDetail detail = db.OpenRewardDetail.Add(new OpenRewardDetail()
  790. {
  791. CreateDate = DateTime.Now,
  792. UpdateDate = DateTime.Now,
  793. TradeMonth = yesterday.ToString("yyyyMM"), //交易月
  794. TradeDate = yesterday, //达标日期
  795. UserId = puser.Id, //创客
  796. BrandId = pos.BrandId, //品牌
  797. ProductName = RelationClass.GetKqProductsInfo(pos.BrandId), //产品名称
  798. MerchantId = pos.BindMerchantId, //商户
  799. DirectUserId = merchant.UserId, //商户直属人
  800. SnNo = pos.PosSn, //SN号
  801. MerNo = merchant.KqMerNo, //渠道商户号
  802. SnType = pos.PosSnType, //机具类型
  803. StandardDate = pos.ActivationTime, //商户的激活日期
  804. SnStoreId = pos.StoreId, //SN仓库
  805. MerBuddyType = puser.MerchantType, //商户创客类型
  806. RewardType = 1, //奖励类型 1-开机直接奖励,2-开机间接奖励
  807. RewardTips = "开机奖励", //奖励描述
  808. CreditTradeAmt = pos.CreditTrade, //贷记卡交易总金额
  809. DebitTradeAmt = pos.DebitCardTrade, //借记卡交易总金额
  810. CreditRewardAmount = Prize, //贷记卡交易奖励金额
  811. RewardDesc = "开机奖励", //奖励描述
  812. TopUserId = pTopUserId, //顶级创客
  813. SeoTitle = machineUser.RealName,
  814. }).Entity;
  815. db.OpenReward.Add(new OpenReward()
  816. {
  817. CreateDate = DateTime.Now,
  818. UpdateDate = DateTime.Now,
  819. TradeMonth = yesterday.ToString("yyyyMM"), //交易月
  820. TradeDate = DateTime.Now, //达标日期
  821. UserId = puser.Id, //创客
  822. BrandId = pos.BrandId, //品牌
  823. RewardType = 2, //奖励类型
  824. CreditTradeAmt = pos.CreditTrade, //贷记卡交易总金额
  825. DebitTradeAmt = pos.DebitCardTrade, //借记卡交易总金额
  826. CreditRewardAmount = Prize, //贷记卡交易奖励金额
  827. RewardDesc = "开机奖励", //奖励描述
  828. TopUserId = pTopUserId, //顶级创客
  829. });
  830. string IdBrand = puser.Id + "_" + pos.BrandId;
  831. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  832. if (userData == null)
  833. {
  834. userData = db.UserMachineData.Add(new UserMachineData()
  835. {
  836. IdBrand = IdBrand,
  837. }).Entity;
  838. db.SaveChanges();
  839. }
  840. userData.OpenProfit += Prize;
  841. db.SaveChanges();
  842. RedisDbconn.Instance.Set("UserMachineData:" + IdBrand, userData);
  843. //账户入库
  844. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id);
  845. if (account == null)
  846. {
  847. account = db.UserAccount.Add(new UserAccount()
  848. {
  849. Id = puser.Id,
  850. UserId = puser.Id,
  851. }).Entity;
  852. db.SaveChanges();
  853. }
  854. //收支明细入库
  855. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  856. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  857. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  858. account.BalanceAmount += Prize;
  859. account.TotalAmount += Prize;
  860. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  861. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  862. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  863. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  864. {
  865. CreateDate = DateTime.Now,
  866. UpdateDate = DateTime.Now,
  867. UserId = puser.Id, //创客
  868. ChangeType = 50, //变动类型
  869. ProductType = pos.BrandId, //产品类型
  870. ChangeAmount = Prize, //变更金额
  871. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  872. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  873. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  874. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  875. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  876. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  877. }).Entity;
  878. db.SaveChanges();
  879. RedisDbconn.Instance.Set("UserAccount:" + puser.Id, account);
  880. string dateString = yesterday.ToString("yyyyMMdd");
  881. string monthString = yesterday.ToString("yyyyMM");
  882. // 开机奖励列表
  883. List<string> dates = RedisDbconn.Instance.GetList<string>("OpenRewardDay:" + puser.Id + ":" + pos.BrandId);
  884. if (!dates.Contains(dateString))
  885. {
  886. RedisDbconn.Instance.AddList("OpenRewardDay:" + puser.Id + ":" + pos.BrandId, dateString);
  887. }
  888. RedisDbconn.Instance.AddNumber("OpenRewardAmt:" + puser.Id + ":" + pos.BrandId + ":" + dateString, Prize);
  889. List<string> months = RedisDbconn.Instance.GetList<string>("OpenRewardMonth:" + puser.Id + ":" + pos.BrandId);
  890. if (!months.Contains(monthString))
  891. {
  892. RedisDbconn.Instance.AddList("OpenRewardMonth:" + puser.Id + ":" + pos.BrandId, monthString);
  893. }
  894. RedisDbconn.Instance.AddNumber("OpenRewardAmt:" + puser.Id + ":" + pos.BrandId + ":" + monthString, Prize);
  895. // 开机奖励详情
  896. RedisDbconn.Instance.AddList("OpenRewardDetail:" + puser.Id + ":" + pos.BrandId + ":" + dateString, detail);
  897. //收支明细
  898. RedisDbconn.Instance.AddList("UserAccountRecord:" + puser.Id + ":1:" + monthString, userAccountRecord);
  899. RedisDbconn.Instance.AddNumber("UserAccount:" + puser.Id + ":1:" + monthString, Prize);
  900. Prize -= 10;
  901. }
  902. }
  903. }
  904. }
  905. }
  906. // }
  907. }
  908. }
  909. }
  910. db.Dispose();
  911. }
  912. // 4. 前一天的流量卡记录和匹配
  913. public void ListenFluxRecord(string date)
  914. {
  915. Thread th = new Thread(CheckFluxForTrade);
  916. th.IsBackground = true;
  917. th.Start(date);
  918. }
  919. public void doFluxRecord(string date)
  920. {
  921. function.WriteLog(DateTime.Now.ToString(), "执行流量费返佣");
  922. SpModels.WebCMSEntities spdb = new SpModels.WebCMSEntities();
  923. WebCMSEntities db = new WebCMSEntities();
  924. DateTime yesterday = DateTime.Parse(date + " 00:00:00");
  925. DateTime today = DateTime.Parse(DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00");
  926. List<SpModels.TradeFluxRecord> trades = spdb.TradeFluxRecord.Where(m => m.CreateDate >= yesterday && m.CreateDate < today && m.Status == 1).ToList();
  927. foreach (SpModels.TradeFluxRecord trade in trades)
  928. {
  929. try
  930. {
  931. string OrderNo = trade.TradeSerialNo; //单号
  932. DateTime TradeDate = trade.CreateDate.Value;
  933. string TradeMonth = TradeDate.ToString("yyyyMM");
  934. decimal FeeAmount = trade.FeeAmount; //流量费
  935. if (trade.ProductType == "1" || trade.ProductType == "4")
  936. {
  937. FeeAmount = FeeAmount / 100;
  938. }
  939. string TradeSnNo = trade.TradeSnNo; //机具SN
  940. decimal FluxProfit = 0;
  941. if (trade.ProductType == "1" && FeeAmount == 60)
  942. {
  943. FluxProfit = 24;
  944. }
  945. else if (trade.ProductType != "1" && FeeAmount == 48)
  946. {
  947. FluxProfit = 12;
  948. }
  949. if (FluxProfit > 0)
  950. {
  951. MachineForSnNo forSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == TradeSnNo) ?? new MachineForSnNo();
  952. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forSnNo.SnId) ?? new PosMachinesTwo();
  953. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId) ?? new PosMerchantInfo();
  954. bool checkExist = db.FluxProfitDetail.Any(m => m.MerNo == merchant.KqMerNo);
  955. if (!checkExist)
  956. {
  957. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  958. int GetUserId = user.Id;
  959. int TopUserId = 0;
  960. string ParentNav = user.ParentNav;
  961. if (!string.IsNullOrEmpty(ParentNav))
  962. {
  963. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  964. if (ParentNavList.Length > 1)
  965. {
  966. TopUserId = int.Parse(ParentNavList[1]);
  967. }
  968. else if (ParentNavList.Length == 1)
  969. {
  970. TopUserId = int.Parse(ParentNavList[0]);
  971. }
  972. }
  973. FluxProfitSummary fluxProfit = db.FluxProfitSummary.FirstOrDefault(m => m.UserId == GetUserId && m.BrandId == pos.BrandId && m.TradeMonth == TradeMonth);
  974. if (fluxProfit == null)
  975. {
  976. fluxProfit = db.FluxProfitSummary.Add(new FluxProfitSummary()
  977. {
  978. CreateDate = DateTime.Now,
  979. UpdateDate = DateTime.Now,
  980. UserId = GetUserId, //创客
  981. BrandId = pos.BrandId,
  982. TopUserId = TopUserId, //顶级创客
  983. TradeMonth = TradeMonth, //交易月
  984. MerUserType = user.MerchantType, //商户创客类型
  985. Remark = "流量卡分佣", //备注
  986. }).Entity;
  987. db.SaveChanges();
  988. }
  989. fluxProfit.FluxProfitAmt += FluxProfit; //流量分润总金额
  990. db.FluxProfitDetail.Add(new FluxProfitDetail()
  991. {
  992. CreateDate = DateTime.Now,
  993. UpdateDate = DateTime.Now,
  994. RecordNo = OrderNo, //单号
  995. TradeDate = TradeDate.ToString("yyyyMMdd"), //交易日期
  996. TradeTime = TradeDate.ToString("HHmmss"), //交易时间
  997. TradeMonth = TradeMonth, //交易月
  998. UserId = GetUserId, //创客
  999. MerchantId = pos.BindMerchantId, //商户
  1000. MerchantUserId = pos.UserId, //商户直属人
  1001. MerNo = merchant.KqMerNo, //渠道商户编号
  1002. SnNo = pos.PosSn, //渠道SN号
  1003. FluxOrderNo = OrderNo, //流量扣费单号
  1004. TradeOrderNo = OrderNo, //交易流水号
  1005. TradeAmt = trade.TradeAmount, //商户交易额
  1006. FluxFeeAmt = FeeAmount, //流量费
  1007. FluxProfitAmt = FluxProfit, //流量分润总金额
  1008. PosType = pos.PosSnType.ToString(), //POS类型
  1009. Remark = "流量卡分佣", //备注
  1010. BrandId = pos.BrandId, //品牌
  1011. TopUserId = TopUserId, //顶级创客
  1012. MerUserType = user.MerchantType, //商户创客类型
  1013. });
  1014. string IdBrand = user.Id + "_" + pos.BrandId;
  1015. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  1016. if (userData == null)
  1017. {
  1018. userData = db.UserMachineData.Add(new UserMachineData()
  1019. {
  1020. IdBrand = IdBrand,
  1021. }).Entity;
  1022. db.SaveChanges();
  1023. }
  1024. userData.FluxProfit += FluxProfit;
  1025. db.SaveChanges();
  1026. RedisDbconn.Instance.Clear("UserMachineData:" + IdBrand);
  1027. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == GetUserId);
  1028. if (account == null)
  1029. {
  1030. account = db.UserAccount.Add(new UserAccount()
  1031. {
  1032. Id = GetUserId,
  1033. UserId = GetUserId,
  1034. }).Entity;
  1035. db.SaveChanges();
  1036. }
  1037. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  1038. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  1039. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  1040. account.BalanceAmount += FluxProfit;
  1041. account.TotalAmount += FluxProfit;
  1042. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  1043. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  1044. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  1045. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  1046. {
  1047. CreateDate = DateTime.Now,
  1048. UpdateDate = DateTime.Now,
  1049. UserId = GetUserId, //创客
  1050. ChangeType = 60, //变动类型
  1051. ProductType = pos.BrandId, //产品类型
  1052. ChangeAmount = FluxProfit, //变更金额
  1053. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  1054. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  1055. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  1056. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  1057. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  1058. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  1059. }).Entity;
  1060. db.SaveChanges();
  1061. RedisDbconn.Instance.Clear("UserAccount:" + GetUserId);
  1062. }
  1063. SpModels.TradeFluxRecord edit = spdb.TradeFluxRecord.FirstOrDefault(m => m.Id == trade.Id);
  1064. if (edit != null)
  1065. {
  1066. edit.Status = 2;
  1067. spdb.SaveChanges();
  1068. }
  1069. }
  1070. }
  1071. catch (Exception ex)
  1072. {
  1073. function.WriteLog(DateTime.Now.ToString() + "\n$" + trade.Id + "$\n" + ex.ToString(), "流量卡分佣异常");
  1074. }
  1075. }
  1076. spdb.Dispose();
  1077. db.Dispose();
  1078. function.WriteLog(DateTime.Now.ToString() + "\n", "执行流量费返佣");
  1079. }
  1080. public void CheckFluxForTrade(object sender)
  1081. {
  1082. string date = sender.ToString();
  1083. function.WriteLog(DateTime.Now.ToString(), "扫描金控交易记录中的流量费");
  1084. SpModels.WebCMSEntities spdb = new SpModels.WebCMSEntities();
  1085. DateTime start = DateTime.Parse(date + " 00:00:00");
  1086. DateTime end = start.AddDays(1);
  1087. var list = spdb.TradeRecord.Select(m => new { m.Id, m.CreateDate, m.ProductType }).Where(m => m.CreateDate > start && m.CreateDate < end && m.ProductType == "1").OrderBy(m => m.CreateDate);
  1088. foreach (var sub in list.ToList())
  1089. {
  1090. try
  1091. {
  1092. SpModels.TradeRecord trade = spdb.TradeRecord.FirstOrDefault(m => m.Id == sub.Id);
  1093. if (trade != null)
  1094. {
  1095. string content = trade.SeoDescription;
  1096. if (!string.IsNullOrEmpty(content))
  1097. {
  1098. content = System.Web.HttpUtility.UrlDecode(content);
  1099. JsonData jsonObj = JsonMapper.ToObject(content);
  1100. decimal terminalDataFlowFee = decimal.Parse(function.CheckNum(jsonObj["terminalDataFlowFee"].ToString()));
  1101. if (terminalDataFlowFee > 0)
  1102. {
  1103. spdb.TradeFluxRecord.Add(new SpModels.TradeFluxRecord()
  1104. {
  1105. SeoDescription = trade.TradeSerialNo,
  1106. FeeAmount = terminalDataFlowFee,
  1107. TradeSerialNo = function.MD5_16(trade.TradeSerialNo),
  1108. CreateDate = trade.CreateDate,
  1109. TradeSnNo = trade.TradeSnNo,
  1110. ProductType = trade.ProductType,
  1111. Status = 1,
  1112. });
  1113. spdb.SaveChanges();
  1114. }
  1115. }
  1116. }
  1117. }
  1118. catch (Exception ex)
  1119. {
  1120. function.WriteLog(DateTime.Now.ToString() + "\n$" + sub.Id + "$\n" + ex.ToString(), "扫描金控交易记录中的流量费异常");
  1121. }
  1122. }
  1123. spdb.Dispose();
  1124. function.WriteLog(DateTime.Now.ToString() + "\n", "扫描金控交易记录中的流量费");
  1125. doFluxRecord(date);
  1126. }
  1127. // 每天统计头一天的交易额
  1128. public void StartEverDay(string date)
  1129. {
  1130. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
  1131. // DateTime end = DateTime.Parse("2022-04-19 00:00:00");
  1132. // DateTime check = DateTime.Parse("2022-03-11");
  1133. // while (check <= end)
  1134. // {
  1135. // Thread th = new Thread(StatTradeAmountEverDay);
  1136. // th.IsBackground = true;
  1137. // th.Start(check.ToString("yyyy-MM-dd"));
  1138. // check = check.AddDays(1);
  1139. // }
  1140. Thread th = new Thread(StatTradeAmountEverDay);
  1141. th.IsBackground = true;
  1142. th.Start(date);
  1143. }
  1144. public void StatTradeAmountEverDay(object sender)
  1145. {
  1146. string date = sender.ToString();
  1147. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "执行昨天交易额日志");
  1148. WebCMSEntities db = new WebCMSEntities();
  1149. try
  1150. {
  1151. string TradeDate = date.Replace("-", "");
  1152. string TradeMonth = TradeDate.Substring(0, 6);
  1153. string start = date + " 00:00:00";
  1154. string end = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
  1155. string startId = "0", endId = "9999999999";
  1156. List<string> uids = new List<string>();
  1157. // DataTable startDt = OtherMySqlConn.dtable("select min(Id) from TradeRecord where CreateDate>='" + start + "'");
  1158. // if (startDt.Rows.Count > 0)
  1159. // {
  1160. // startId = startDt.Rows[0][0].ToString();
  1161. // }
  1162. // DataTable endDt = OtherMySqlConn.dtable("select max(Id) from TradeRecord where CreateDate<'" + end + "'");
  1163. // if (endDt.Rows.Count > 0)
  1164. // {
  1165. // endId = endDt.Rows[0][0].ToString();
  1166. // }
  1167. 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 + "')");
  1168. foreach (DataRow userDr in userDt.Rows)
  1169. {
  1170. int UserId = int.Parse(userDr["Id"].ToString());
  1171. string ParentNav = userDr["ParentNav"].ToString();
  1172. 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");
  1173. foreach (DataRow selfDr in selfdt.Rows)
  1174. {
  1175. int BrandId = int.Parse(selfDr["BrandId"].ToString());
  1176. int BankCardType = int.Parse(selfDr["BankCardType"].ToString());
  1177. int QrPayFlag = int.Parse(selfDr["QrPayFlag"].ToString());
  1178. decimal TradeAmount = decimal.Parse(selfDr[3].ToString());
  1179. UserTradeDaySummary selfStat = db.UserTradeDaySummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.QueryCount == QrPayFlag && m.SeoTitle == "self");
  1180. if (selfStat == null)
  1181. {
  1182. selfStat = db.UserTradeDaySummary.Add(new UserTradeDaySummary()
  1183. {
  1184. UserId = UserId,
  1185. TradeMonth = TradeMonth,
  1186. TradeDate = TradeDate,
  1187. BrandId = BrandId,
  1188. QueryCount = QrPayFlag,
  1189. SeoTitle = "self",
  1190. }).Entity;
  1191. db.SaveChanges();
  1192. }
  1193. if (BankCardType == 0)
  1194. {
  1195. selfStat.DirectDebitTradeAmt += TradeAmount;
  1196. }
  1197. else if (BankCardType != 0)
  1198. {
  1199. selfStat.DirectTradeAmt += TradeAmount;
  1200. }
  1201. db.SaveChanges();
  1202. }
  1203. RedisDbconn.Instance.Clear("TotalAmount:" + UserId);
  1204. RedisDbconn.Instance.Clear("TotalAmount:" + UserId + ":" + TradeMonth);
  1205. RedisDbconn.Instance.Clear("TotalCloudPayAmount:" + UserId + ":" + TradeMonth);
  1206. RedisDbconn.Instance.Clear("TotalPosAmount:" + UserId + ":" + TradeMonth);
  1207. RedisDbconn.Instance.Clear("TotalAmount:" + UserId + ":*:" + TradeMonth);
  1208. RedisDbconn.Instance.Clear("TotalAmount:" + UserId + ":" + TradeDate);
  1209. RedisDbconn.Instance.Clear("TotalCloudPayAmount:" + UserId + ":" + TradeDate);
  1210. RedisDbconn.Instance.Clear("TotalPosAmount:" + UserId + ":" + TradeDate);
  1211. RedisDbconn.Instance.Clear("TotalAmount:" + UserId + ":*:" + TradeDate);
  1212. if (!string.IsNullOrEmpty(ParentNav))
  1213. {
  1214. ParentNav += "," + UserId + ",";
  1215. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  1216. foreach (string NavUserIdString in ParentNavList)
  1217. {
  1218. if (!uids.Contains(NavUserIdString + start))
  1219. {
  1220. uids.Add(NavUserIdString + start);
  1221. int NavUserId = int.Parse(NavUserIdString);
  1222. 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");
  1223. foreach (DataRow teamDr in teamDt.Rows)
  1224. {
  1225. int BrandId = int.Parse(teamDr["BrandId"].ToString());
  1226. int BankCardType = int.Parse(teamDr["BankCardType"].ToString());
  1227. int QrPayFlag = int.Parse(teamDr["QrPayFlag"].ToString());
  1228. decimal TradeAmount = decimal.Parse(teamDr[3].ToString());
  1229. UserTradeDaySummary teamStat = db.UserTradeDaySummary.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.QueryCount == QrPayFlag && m.SeoTitle == "team");
  1230. if (teamStat == null)
  1231. {
  1232. teamStat = db.UserTradeDaySummary.Add(new UserTradeDaySummary()
  1233. {
  1234. UserId = NavUserId,
  1235. TradeMonth = TradeMonth,
  1236. TradeDate = TradeDate,
  1237. BrandId = BrandId,
  1238. QueryCount = QrPayFlag,
  1239. SeoTitle = "team",
  1240. }).Entity;
  1241. db.SaveChanges();
  1242. }
  1243. if (BankCardType == 0)
  1244. {
  1245. teamStat.NonDirectDebitTradeAmt += TradeAmount;
  1246. }
  1247. else if (BankCardType != 0)
  1248. {
  1249. teamStat.NonDirectTradeAmt += TradeAmount;
  1250. }
  1251. db.SaveChanges();
  1252. }
  1253. RedisDbconn.Instance.Clear("TeamTotalAmount:" + NavUserId);
  1254. RedisDbconn.Instance.Clear("TeamTotalAmount:" + NavUserId + ":" + TradeMonth);
  1255. RedisDbconn.Instance.Clear("TeamTotalCloudPayAmount:" + NavUserId + ":" + TradeMonth);
  1256. RedisDbconn.Instance.Clear("TeamTotalPosAmount:" + NavUserId + ":" + TradeMonth);
  1257. RedisDbconn.Instance.Clear("TeamTotalAmount:" + NavUserId + ":*:" + TradeMonth);
  1258. RedisDbconn.Instance.Clear("TeamTotalAmount:" + NavUserId + ":" + TradeDate);
  1259. RedisDbconn.Instance.Clear("TeamTotalCloudPayAmount:" + NavUserId + ":" + TradeDate);
  1260. RedisDbconn.Instance.Clear("TeamTotalPosAmount:" + NavUserId + ":" + TradeDate);
  1261. RedisDbconn.Instance.Clear("TeamTotalAmount:" + NavUserId + ":*:" + TradeDate);
  1262. }
  1263. }
  1264. }
  1265. }
  1266. }
  1267. catch (Exception ex)
  1268. {
  1269. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计昨天的交易额");
  1270. }
  1271. db.Dispose();
  1272. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行昨天交易额日志");
  1273. StatMerchantTrade(date);
  1274. }
  1275. public void StatTradeAmountEverDaySum()
  1276. {
  1277. // WebCMSEntities db = new WebCMSEntities();
  1278. // OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
  1279. // DataTable dt = OtherMySqlConn.dtable("select UserId,BrandId,QueryCount,TradeDate,TradeMonth,sum(DirectDebitTradeAmt) as DirectDebitTradeAmt,sum(DirectTradeAmt) as DirectTradeAmt,sum(NonDirectDebitTradeAmt) as NonDirectDebitTradeAmt,sum(NonDirectTradeAmt) as NonDirectTradeAmt from UserTradeDaySummary group by UserId,BrandId,QueryCount,TradeDate,TradeMonth order by UserId,BrandId,QueryCount");
  1280. // string html = "<table>";
  1281. // foreach (DataRow dr in dt.Rows)
  1282. // {
  1283. // html += "<tr>";
  1284. // string TradeDate = dr["TradeDate"].ToString();
  1285. // string TradeMonth = dr["TradeMonth"].ToString();
  1286. // int UserId = int.Parse(dr["UserId"].ToString());
  1287. // int BrandId = int.Parse(dr["BrandId"].ToString());
  1288. // int QrPayFlag = int.Parse(dr["QueryCount"].ToString());
  1289. // decimal DirectDebitTradeAmt = decimal.Parse(dr["DirectDebitTradeAmt"].ToString());
  1290. // decimal DirectTradeAmt = decimal.Parse(dr["DirectTradeAmt"].ToString());
  1291. // decimal NonDirectDebitTradeAmt = decimal.Parse(dr["NonDirectDebitTradeAmt"].ToString());
  1292. // decimal NonDirectTradeAmt = decimal.Parse(dr["NonDirectTradeAmt"].ToString());
  1293. // decimal CurAmount = 0, CurTotalAmount = 0, CurMonthAmount = 0, CurDayAmount = 0;
  1294. // CurTotalAmount = RedisDbconn.Instance.Get<decimal>("TotalAmount:" + UserId); //总交易
  1295. // CurMonthAmount = RedisDbconn.Instance.Get<decimal>("TotalAmount:" + UserId + ":" + TradeDate); //总交易
  1296. // CurDayAmount = RedisDbconn.Instance.Get<decimal>("TotalAmount:" + UserId + ":" + TradeMonth); //总交易
  1297. // CurAmount = RedisDbconn.Instance.Get<decimal>("TotalAmount:" + UserId + ":" + BrandId + ":" + TradeDate); //总交易
  1298. // decimal TotalPosAmount = 0;
  1299. // decimal TotalCloudPayAmount = 0;
  1300. // decimal TeamTotalPosAmount = 0;
  1301. // decimal TeamTotalCloudPayAmount = 0;
  1302. // if (QrPayFlag == 1)
  1303. // {
  1304. // TotalCloudPayAmount = RedisDbconn.Instance.Get<decimal>("TotalCloudPayAmount:" + UserId + ":" + TradeDate); //云闪付小额交易额
  1305. // TeamTotalCloudPayAmount = RedisDbconn.Instance.Get<decimal>("TeamTotalCloudPayAmount:" + UserId + ":" + TradeDate); //POS机刷卡交易额
  1306. // }
  1307. // else
  1308. // {
  1309. // TotalPosAmount = RedisDbconn.Instance.Get<decimal>("TotalPosAmount:" + UserId + ":" + TradeDate); //POS机刷卡交易额
  1310. // TeamTotalPosAmount = RedisDbconn.Instance.Get<decimal>("TeamTotalPosAmount:" + UserId + ":" + TradeDate); //云闪付小额交易额
  1311. // }
  1312. // Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  1313. // html += "<td>应得</td>";
  1314. // html += "<td>" + user.MakerCode + "</td>";
  1315. // html += "<td>" + user.RealName + "</td>";
  1316. // html += "<td>" + user.Mobile + "</td>";
  1317. // html += "<td>" + BrandId + "</td>";
  1318. // html += "<td>" + QrPayFlag + "</td>";
  1319. // html += "<td>" + DirectDebitTradeAmt + "</td>";
  1320. // html += "<td>" + DirectTradeAmt + "</td>";
  1321. // html += "<td>" + NonDirectDebitTradeAmt + "</td>";
  1322. // html += "<td>" + NonDirectTradeAmt + "</td>";
  1323. // html += "</tr>";
  1324. // html += "<tr>";
  1325. // html += "<td>当前</td>";
  1326. // html += "<td></td>";
  1327. // html += "<td></td>";
  1328. // html += "<td>当天品牌:" + CurAmount + "</td>";
  1329. // html += "<td>总交易:" + CurTotalAmount + "</td>";
  1330. // html += "<td>当月:" + CurMonthAmount + "</td>";
  1331. // html += "<td>当天:" + CurDayAmount + "</td>";
  1332. // html += "<td>" + TotalPosAmount + "</td>";
  1333. // html += "<td>" + TotalCloudPayAmount + "</td>";
  1334. // html += "<td>" + TeamTotalPosAmount + "</td>";
  1335. // html += "<td>" + TeamTotalCloudPayAmount + "</td>";
  1336. // html += "</tr>";
  1337. // }
  1338. // html += "</table>";
  1339. // db.Dispose();
  1340. // OtherMySqlConn.connstr = "";
  1341. // function.WritePage("/html/", "1.html", html);
  1342. }
  1343. //统计商户交易额
  1344. private void StatMerchantTrade(string date)
  1345. {
  1346. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "执行商户交易额日志");
  1347. // WebCMSEntities db = new WebCMSEntities();
  1348. try
  1349. {
  1350. string start = date + " 00:00:00";
  1351. string end = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
  1352. 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");
  1353. RedisDbconn.Instance.Clear("PosMerchantAmount:*");
  1354. RedisDbconn.Instance.Clear("PosMerchantAmount:*:" + date.Replace("-", "").Substring(0, 6)); //商户当月交易
  1355. RedisDbconn.Instance.Clear("PosMerchantAmount:*:" + date.Replace("-", "")); //商户当日交易
  1356. // foreach (DataRow selfDr in selfdt.Rows)
  1357. // {
  1358. // int BrandId = int.Parse(selfDr["BrandId"].ToString());
  1359. // int MerchantId = int.Parse(selfDr["MerchantId"].ToString());
  1360. // string TradeDate = selfDr["TradeDate"].ToString();
  1361. // TradeDate = TradeDate.Replace("-", "");
  1362. // string TradeMonth = TradeDate.Substring(0, 6);
  1363. // decimal TradeAmount = decimal.Parse(selfDr["TradeAmount"].ToString());
  1364. // PosMerchantTradeSummay merStat = db.PosMerchantTradeSummay.FirstOrDefault(m => m.MerchantId == MerchantId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId);
  1365. // if (merStat == null)
  1366. // {
  1367. // merStat = db.PosMerchantTradeSummay.Add(new PosMerchantTradeSummay()
  1368. // {
  1369. // MerchantId = MerchantId,
  1370. // TradeMonth = TradeMonth,
  1371. // TradeDate = TradeDate,
  1372. // BrandId = BrandId,
  1373. // }).Entity;
  1374. // db.SaveChanges();
  1375. // }
  1376. // merStat.TradeAmount += TradeAmount;
  1377. // db.SaveChanges();
  1378. // }
  1379. }
  1380. catch (Exception ex)
  1381. {
  1382. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计商户的交易额");
  1383. }
  1384. // db.Dispose();
  1385. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行商户交易额日志");
  1386. }
  1387. }
  1388. }