StatService.cs 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using LitJson;
  5. using System.Linq;
  6. using System.Data;
  7. using MySystem.PxcModels;
  8. namespace MySystem
  9. {
  10. public class StatService
  11. {
  12. public readonly static StatService Instance = new StatService();
  13. private StatService()
  14. { }
  15. public void Start(JobMqMsg jobInfo)
  16. {
  17. string content = "";
  18. try
  19. {
  20. string doDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
  21. string flag = function.ReadInstance("/Stat/" + doDate + ".txt");
  22. if (string.IsNullOrEmpty(flag))
  23. {
  24. function.WritePage("/Stat/", doDate + ".txt", DateTime.Now.ToString("HH:mm:ss"));
  25. dosomething1(doDate);
  26. dosomething11(doDate);
  27. dosomething2(doDate);
  28. dosomething4(doDate);
  29. }
  30. string Msg = "success";
  31. jobInfo.Status = Msg == "success" ? 1 : 0;
  32. jobInfo.Msg = Msg == "success" ? "执行完成" : Msg;
  33. RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(jobInfo), "PublicBack");
  34. }
  35. catch (Exception ex)
  36. {
  37. if (!string.IsNullOrEmpty(content))
  38. {
  39. Dictionary<string, string> data = new Dictionary<string, string>();
  40. data.Add("ErrTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  41. data.Add("ErrMsg", ex.ToString());
  42. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(data), "public_err");
  43. }
  44. else
  45. {
  46. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public_service");
  47. }
  48. }
  49. }
  50. public void ActiveReward(JobMqMsg jobInfo)
  51. {
  52. string content = "";
  53. try
  54. {
  55. dosomething3();
  56. string Msg = "success";
  57. jobInfo.Status = Msg == "success" ? 1 : 0;
  58. jobInfo.Msg = Msg == "success" ? "执行完成" : Msg;
  59. RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(jobInfo), "PublicBack");
  60. }
  61. catch (Exception ex)
  62. {
  63. if (!string.IsNullOrEmpty(content))
  64. {
  65. Dictionary<string, string> data = new Dictionary<string, string>();
  66. data.Add("ErrTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  67. data.Add("ErrMsg", ex.ToString());
  68. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(data), "public_err");
  69. }
  70. else
  71. {
  72. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public_service");
  73. }
  74. }
  75. }
  76. public void clear()
  77. {
  78. RedisDbconn.Instance.Clear("TotalAmount*");
  79. RedisDbconn.Instance.Clear("TotalCloudPayAmount*");
  80. RedisDbconn.Instance.Clear("TotalPosAmount*");
  81. RedisDbconn.Instance.Clear("TeamTotalAmount*");
  82. RedisDbconn.Instance.Clear("TeamTotalCloudPayAmount*");
  83. RedisDbconn.Instance.Clear("TeamTotalPosAmount*");
  84. // RedisDbconn.Instance.Clear("TotalUser*");
  85. // RedisDbconn.Instance.Clear("TeamTotalUser*");
  86. // RedisDbconn.Instance.Clear("AddUser*");
  87. // RedisDbconn.Instance.Clear("TeamAddUser*");
  88. // RedisDbconn.Instance.Clear("TotalPosMerchant*");
  89. // RedisDbconn.Instance.Clear("TeamTotalPosMerchant*");
  90. RedisDbconn.Instance.Clear("MerTotalAmount*");
  91. }
  92. // 1. 前一天的绑定记录和创客信息匹配,设置商户型创客
  93. public void dosomething1(string date)
  94. {
  95. // TODO: 每天扫描非商户型创客的持有机具数量
  96. SpModels.WebCMSEntities spdb = new SpModels.WebCMSEntities();
  97. WebCMSEntities db = new WebCMSEntities();
  98. DateTime yesterday = DateTime.Parse(date + " 00:00:00");
  99. DateTime today = DateTime.Parse(DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00");
  100. List<SpModels.BindRecord> binds = spdb.BindRecord.Where(m => m.CreateTime >= yesterday && m.CreateTime < today).ToList();
  101. foreach (SpModels.BindRecord bind in binds)
  102. {
  103. string Mobile = bind.MerNewSnNo;
  104. string MerNo = bind.MerNo;
  105. string BeforeNum = "";
  106. string AfterNum = "";
  107. if (Mobile.Contains("****") && Mobile.Length == 11)
  108. {
  109. BeforeNum = Mobile.Substring(0, 3);
  110. AfterNum = Mobile.Substring(7);
  111. }
  112. string Name = bind.MerName;
  113. if (bind.ProductType == "2")
  114. {
  115. if (Name.Contains("-"))
  116. {
  117. Name = Name.Split('-')[1];
  118. }
  119. else if (Name.Contains("_"))
  120. {
  121. Name = Name.Split('_')[1];
  122. }
  123. }
  124. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Mobile.StartsWith(BeforeNum) && m.Mobile.EndsWith(AfterNum) && m.RealName == Name && m.AuthFlag == 1 && m.MerchantType == 0);
  125. if (user != null)
  126. {
  127. user.MerchantType = 1;
  128. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == MerNo) ?? new PxcModels.PosMerchantInfo();
  129. merchant.MerUserType = 1;
  130. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.BindMerchantId == merchant.Id);
  131. if (pos != null)
  132. {
  133. pos.SeoTitle = user.Id.ToString(); // 记录商户型创客的Id
  134. }
  135. db.SaveChanges();
  136. RedisDbconn.Instance.Set("Users:" + user.Id, user);
  137. RedisDbconn.Instance.Set("PosMerchantInfo:" + merchant.Id, merchant);
  138. RedisDbconn.Instance.Set("PosMachinesTwo:" + pos.Id, pos);
  139. }
  140. }
  141. spdb.Dispose();
  142. db.Dispose();
  143. }
  144. // 1. 前一天的商户型创客,判断名下是否3台激活机器,是则变为非商户型创客
  145. public void dosomething11(string date)
  146. {
  147. WebCMSEntities db = new WebCMSEntities();
  148. DateTime yesterday = DateTime.Parse(date + " 00:00:00");
  149. DateTime today = DateTime.Parse(DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00");
  150. var users = db.Users.Select(m => new { m.Id, m.MerchantType }).Where(m => m.MerchantType == 1).ToList();
  151. foreach (var user in users)
  152. {
  153. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == user.Id && m.PosSnType == 0);
  154. if (machineCount >= 3)
  155. {
  156. Users edit = db.Users.FirstOrDefault(m => m.Id == user.Id);
  157. if (edit != null)
  158. {
  159. edit.MerchantType = 0;
  160. db.SaveChanges();
  161. RedisDbconn.Instance.Set("Users:" + user.Id, edit);
  162. }
  163. }
  164. }
  165. db.Dispose();
  166. }
  167. // 2. 每天统计前一天的交易额,每笔交易判断商户号的所属人是商户型创客,如果是,则从此人开始累计交易额,若不是,则从此人上级开始累计交易额
  168. public void dosomething2(string date)
  169. {
  170. WebCMSEntities db = new WebCMSEntities();
  171. string yesterday = date + " 00:00:00";
  172. string today = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
  173. string TradeMonth = DateTime.Parse(date).ToString("yyyyMM");
  174. string TradeDate = DateTime.Parse(date).ToString("yyyyMMdd");
  175. DataTable dt = dbconn.dtable("select UserId,BrandId,MerchantId,QrPayFlag,sum(TradeAmount) from TradeRecord where CreateDate>='" + yesterday + "' and CreateDate<'" + today + "' group by UserId,BrandId,MerchantId,QrPayFlag");
  176. foreach (DataRow dr in dt.Rows)
  177. {
  178. int UserId = int.Parse(dr["UserId"].ToString()); //创客ID
  179. int BrandId = int.Parse(dr["BrandId"].ToString()); //品牌
  180. int MerchantId = int.Parse(dr["MerchantId"].ToString()); //商户Id
  181. int QrPayFlag = int.Parse(dr["QrPayFlag"].ToString()); //云闪付
  182. decimal TradeAmount = decimal.Parse(dr[4].ToString()); //当日交易额
  183. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == MerchantId) ?? new PosMerchantInfo();
  184. MachineForMerNo machineFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == merchant.KqMerNo) ?? new MachineForMerNo();
  185. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machineFor.SnId) ?? new PosMachinesTwo();
  186. int MerchantTypeUserId = int.Parse(function.CheckInt(pos.SeoTitle));
  187. if (MerchantTypeUserId > 0)
  188. {
  189. Users merchantUser = db.Users.FirstOrDefault(m => m.Id == MerchantTypeUserId && m.MerchantType == 1);
  190. if (merchantUser != null)
  191. {
  192. UserId = MerchantTypeUserId;
  193. }
  194. }
  195. RedisDbconn.Instance.AddNumber("MerTotalAmount:" + MerchantId, TradeAmount);
  196. RedisDbconn.Instance.AddNumber("MerTotalAmount:" + MerchantId + ":" + TradeMonth, TradeAmount); //商户当月交易
  197. RedisDbconn.Instance.AddNumber("MerTotalAmount:" + MerchantId + ":" + TradeDate, TradeAmount); //商户当日交易
  198. int Level = 0;
  199. while (UserId > 0)
  200. {
  201. Level += 1;
  202. Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
  203. if (user != null)
  204. {
  205. if (user.AuthFlag == 1)
  206. {
  207. if (Level == 1)
  208. {
  209. RedisDbconn.Instance.AddNumber("TotalAmount:" + UserId, TradeAmount); //总交易
  210. RedisDbconn.Instance.AddNumber("TotalAmount:" + UserId + ":" + TradeDate, TradeAmount); //总交易
  211. RedisDbconn.Instance.AddNumber("TotalAmount:" + UserId + ":" + TradeMonth, TradeAmount); //总交易
  212. RedisDbconn.Instance.AddNumber("TotalAmount:" + UserId + ":" + BrandId + ":" + TradeDate, TradeAmount); //总交易
  213. RedisDbconn.Instance.AddNumber("TotalAmount:" + UserId + ":" + BrandId + ":" + TradeMonth, TradeAmount); //总交易
  214. if (QrPayFlag == 1)
  215. {
  216. RedisDbconn.Instance.AddNumber("TotalCloudPayAmount:" + UserId + ":" + TradeDate, TradeAmount); //云闪付小额交易额
  217. RedisDbconn.Instance.AddNumber("TotalCloudPayAmount:" + UserId + ":" + TradeMonth, TradeAmount); //云闪付小额交易额
  218. }
  219. else
  220. {
  221. RedisDbconn.Instance.AddNumber("TotalPosAmount:" + UserId + ":" + TradeDate, TradeAmount); //POS机刷卡交易额
  222. RedisDbconn.Instance.AddNumber("TotalPosAmount:" + UserId + ":" + TradeMonth, TradeAmount); //POS机刷卡交易额
  223. }
  224. }
  225. RedisDbconn.Instance.AddNumber("TeamTotalAmount:" + UserId, TradeAmount); //总交易
  226. RedisDbconn.Instance.AddNumber("TeamTotalAmount:" + UserId + ":" + TradeDate, TradeAmount); //总交易
  227. RedisDbconn.Instance.AddNumber("TeamTotalAmount:" + UserId + ":" + TradeMonth, TradeAmount); //总交易
  228. RedisDbconn.Instance.AddNumber("TeamTotalAmount:" + UserId + ":" + BrandId + ":" + TradeDate, TradeAmount); //总交易
  229. RedisDbconn.Instance.AddNumber("TeamTotalAmount:" + UserId + ":" + BrandId + ":" + TradeMonth, TradeAmount); //总交易
  230. if (QrPayFlag == 1)
  231. {
  232. RedisDbconn.Instance.AddNumber("TeamTotalCloudPayAmount:" + UserId + ":" + TradeDate, TradeAmount); //云闪付小额交易额
  233. RedisDbconn.Instance.AddNumber("TeamTotalCloudPayAmount:" + UserId + ":" + TradeMonth, TradeAmount); //云闪付小额交易额
  234. }
  235. else
  236. {
  237. RedisDbconn.Instance.AddNumber("TeamTotalPosAmount:" + UserId + ":" + TradeDate, TradeAmount); //POS机刷卡交易额
  238. RedisDbconn.Instance.AddNumber("TeamTotalPosAmount:" + UserId + ":" + TradeMonth, TradeAmount); //POS机刷卡交易额
  239. }
  240. }
  241. UserId = user.ParentUserId;
  242. }
  243. else
  244. {
  245. UserId = 0;
  246. }
  247. }
  248. }
  249. db.Dispose();
  250. }
  251. // 3. 前一天的激活记录,根据创客类型,如果是商户型创客,则激活奖励的起始人从此人上级计算。如果不是,则从此人开始计算
  252. public void dosomething3(int posid = 0)
  253. {
  254. WebCMSEntities db = new WebCMSEntities();
  255. DateTime yesterday = DateTime.Now.AddHours(-3);
  256. DateTime today = DateTime.Now;
  257. IQueryable<PosMachinesTwo> posList = db.PosMachinesTwo.Where(m => m.ActivationState == 1 && !string.IsNullOrEmpty(m.SeoKeyword));
  258. if (posid > 0)
  259. {
  260. posList = posList.Where(m => m.Id == posid);
  261. }
  262. else
  263. {
  264. posList = posList.Where(m => m.ActivationTime >= yesterday && m.ActivationTime < today);
  265. }
  266. foreach (PosMachinesTwo pos in posList.ToList())
  267. {
  268. // PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId && !string.IsNullOrEmpty(m.MerIdcardNo));
  269. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  270. if (merchant != null)
  271. {
  272. // PosMerchantOtherInfo otherInfo = db.PosMerchantOtherInfo.FirstOrDefault(m => m.CertId == merchant.MerIdcardNo && m.PrizeFlag1 == 0);
  273. // if (otherInfo != null)
  274. // {
  275. // otherInfo.PrizeFlag1 = 1;
  276. // db.SaveChanges();
  277. string ParentNav = "";
  278. Users user = db.Users.FirstOrDefault(m => m.Id == pos.UserId);
  279. if (user != null)
  280. {
  281. int GetUserId = user.Id;
  282. ParentNav = user.ParentNav;
  283. int TopUserId = 0;
  284. if (!string.IsNullOrEmpty(ParentNav))
  285. {
  286. TopUserId = int.Parse(ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  287. }
  288. decimal ActPrize = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  289. if (pos.BrandId == 1 || pos.BrandId == 3)
  290. {
  291. ActPrize = ActPrize / 100;
  292. }
  293. if (ActPrize > 0)
  294. {
  295. bool check = db.ActiveReward.Any(m => m.KqSnNo == pos.PosSn);
  296. if (!check)
  297. {
  298. if (ActPrize == 99)
  299. {
  300. ActPrize = 100;
  301. }
  302. else if (ActPrize == 199)
  303. {
  304. ActPrize = 200;
  305. }
  306. else if (ActPrize == 299)
  307. {
  308. ActPrize = 300;
  309. }
  310. Users machineUser = db.Users.FirstOrDefault(m => m.Id == pos.UserId) ?? new Users();
  311. db.ActiveReward.Add(new ActiveReward()
  312. {
  313. CreateDate = DateTime.Now,
  314. UpdateDate = DateTime.Now,
  315. UserId = GetUserId, //创客
  316. MerchantId = pos.BindMerchantId, //商户
  317. StandardDate = pos.ActivationTime, //达标日期
  318. RewardAmount = ActPrize, //奖励金额
  319. BrandId = pos.BrandId, //品牌
  320. UserNav = ParentNav, //创客父级
  321. DirectBuddyNo = merchant.UserId, //商户直属创客
  322. KqMerNo = merchant.KqMerNo, //渠道商户编号
  323. KqSnNo = pos.PosSn, //渠道SN号
  324. SnType = pos.PosSnType, //机具类型
  325. SnApplyUserId = pos.BuyUserId, //机具申请创客
  326. ActType = 0, //激活类型
  327. SnStoreId = pos.StoreId, //SN仓库
  328. RewardTips = "激活奖励", //奖励描述
  329. Remark = "激活奖励", //备注
  330. ActDate = pos.ActivationTime, //激活时间
  331. TopUserId = TopUserId, //顶级创客
  332. SeoTitle = machineUser.RealName,
  333. });
  334. db.SaveChanges();
  335. string IdBrand = GetUserId + "_" + pos.BrandId;
  336. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  337. if (userData == null)
  338. {
  339. userData = db.UserMachineData.Add(new UserMachineData()
  340. {
  341. IdBrand = IdBrand,
  342. }).Entity;
  343. db.SaveChanges();
  344. }
  345. userData.ActProfit += ActPrize;
  346. db.SaveChanges();
  347. RedisDbconn.Instance.Set("UserMachineData:" + IdBrand, userData);
  348. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == GetUserId);
  349. if (account == null)
  350. {
  351. account = db.UserAccount.Add(new UserAccount()
  352. {
  353. Id = GetUserId,
  354. UserId = GetUserId,
  355. }).Entity;
  356. db.SaveChanges();
  357. }
  358. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  359. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  360. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  361. account.BalanceAmount += ActPrize;
  362. account.TotalAmount += ActPrize;
  363. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  364. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  365. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  366. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  367. {
  368. CreateDate = DateTime.Now,
  369. UpdateDate = DateTime.Now,
  370. UserId = GetUserId, //创客
  371. ChangeType = 0, //变动类型
  372. ProductType = pos.BrandId, //产品类型
  373. ChangeAmount = ActPrize, //变更金额
  374. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  375. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  376. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  377. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  378. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  379. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  380. }).Entity;
  381. db.SaveChanges();
  382. RedisDbconn.Instance.Set("UserAccount:" + GetUserId, account);
  383. string dateString = pos.ActivationTime.Value.ToString("yyyyMMdd");
  384. string monthString = pos.ActivationTime.Value.ToString("yyyyMM");
  385. // 激活奖励列表
  386. List<string> dates = RedisDbconn.Instance.GetList<string>("ActiveRewardDay:" + GetUserId + ":" + pos.BrandId);
  387. if (!dates.Contains(dateString))
  388. {
  389. RedisDbconn.Instance.AddList("ActiveRewardDay:" + GetUserId + ":" + pos.BrandId, dateString);
  390. }
  391. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:" + GetUserId + ":" + pos.BrandId + ":" + dateString, ActPrize);
  392. List<string> months = RedisDbconn.Instance.GetList<string>("ActiveRewardMonth:" + GetUserId + ":" + pos.BrandId);
  393. if (!months.Contains(monthString))
  394. {
  395. RedisDbconn.Instance.AddList("ActiveRewardMonth:" + GetUserId + ":" + pos.BrandId, monthString);
  396. }
  397. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:" + GetUserId + ":" + pos.BrandId + ":" + monthString, ActPrize);
  398. // 激活奖励详情
  399. List<int> actPrizeList = RedisDbconn.Instance.GetList<int>("ActiveRewardDetail:" + GetUserId + ":" + pos.BrandId + ":" + dateString);
  400. if (!actPrizeList.Contains(pos.BindMerchantId))
  401. {
  402. RedisDbconn.Instance.AddList("ActiveRewardDetail:" + GetUserId + ":" + pos.BrandId + ":" + dateString, pos.BindMerchantId);
  403. }
  404. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:mer:" + pos.BindMerchantId + ":" + pos.BrandId + ":" + dateString, ActPrize);
  405. //收支明细
  406. RedisDbconn.Instance.AddList("UserAccountRecord:" + GetUserId + ":1:" + monthString, userAccountRecord);
  407. RedisDbconn.Instance.AddNumber("UserAccount:" + GetUserId + ":1:" + monthString, ActPrize);
  408. }
  409. }
  410. }
  411. // }
  412. }
  413. }
  414. db.Dispose();
  415. }
  416. //根据商户信息发放激活奖励
  417. public void dosomething32(int merid = 0)
  418. {
  419. WebCMSEntities db = new WebCMSEntities();
  420. DateTime yesterday = DateTime.Now.AddHours(-3);
  421. DateTime today = DateTime.Now;
  422. IQueryable<PosMerchantInfo> merList = db.PosMerchantInfo.Where(m => m.ActiveStatus == 1 && !string.IsNullOrEmpty(m.SeoKeyword));
  423. if (merid > 0)
  424. {
  425. merList = merList.Where(m => m.Id == merid);
  426. }
  427. else
  428. {
  429. merList = merList.Where(m => m.MerStandardDate >= yesterday && m.MerStandardDate < today);
  430. }
  431. foreach (PosMerchantInfo merchant in merList.ToList())
  432. {
  433. // PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId && !string.IsNullOrEmpty(m.MerIdcardNo));
  434. // PosMerchantOtherInfo otherInfo = db.PosMerchantOtherInfo.FirstOrDefault(m => m.CertId == merchant.MerIdcardNo && m.PrizeFlag1 == 0);
  435. // if (otherInfo != null)
  436. // {
  437. // otherInfo.PrizeFlag1 = 1;
  438. // db.SaveChanges();
  439. MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == merchant.MerchantNo) ?? new MachineForMerNo();
  440. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forMerNo.SnId) ?? new PosMachinesTwo();
  441. string ParentNav = "";
  442. Users user = db.Users.FirstOrDefault(m => m.Id == pos.UserId);
  443. if (user != null)
  444. {
  445. int GetUserId = user.Id;
  446. ParentNav = user.ParentNav;
  447. int TopUserId = 0;
  448. if (!string.IsNullOrEmpty(ParentNav))
  449. {
  450. TopUserId = int.Parse(ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  451. }
  452. decimal ActPrize = decimal.Parse(function.CheckNum(merchant.SeoKeyword));
  453. if (pos.BrandId == 1 || pos.BrandId == 3)
  454. {
  455. ActPrize = ActPrize / 100;
  456. }
  457. if (ActPrize > 0)
  458. {
  459. bool check = db.ActiveReward.Any(m => m.KqSnNo == pos.PosSn);
  460. if (!check)
  461. {
  462. if (ActPrize == 99)
  463. {
  464. ActPrize = 100;
  465. }
  466. else if (ActPrize == 199)
  467. {
  468. ActPrize = 200;
  469. }
  470. else if (ActPrize == 299)
  471. {
  472. ActPrize = 300;
  473. }
  474. Users machineUser = db.Users.FirstOrDefault(m => m.Id == pos.UserId) ?? new Users();
  475. db.ActiveReward.Add(new ActiveReward()
  476. {
  477. CreateDate = DateTime.Now,
  478. UpdateDate = DateTime.Now,
  479. UserId = GetUserId, //创客
  480. MerchantId = pos.BindMerchantId, //商户
  481. StandardDate = pos.ActivationTime, //达标日期
  482. RewardAmount = ActPrize, //奖励金额
  483. BrandId = pos.BrandId, //品牌
  484. UserNav = ParentNav, //创客父级
  485. DirectBuddyNo = merchant.UserId, //商户直属创客
  486. KqMerNo = merchant.KqMerNo, //渠道商户编号
  487. KqSnNo = pos.PosSn, //渠道SN号
  488. SnType = pos.PosSnType, //机具类型
  489. SnApplyUserId = pos.BuyUserId, //机具申请创客
  490. ActType = 0, //激活类型
  491. SnStoreId = pos.StoreId, //SN仓库
  492. RewardTips = "激活奖励", //奖励描述
  493. Remark = "激活奖励", //备注
  494. ActDate = pos.ActivationTime, //激活时间
  495. TopUserId = TopUserId, //顶级创客
  496. SeoTitle = machineUser.RealName,
  497. });
  498. db.SaveChanges();
  499. string IdBrand = GetUserId + "_" + pos.BrandId;
  500. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  501. if (userData == null)
  502. {
  503. userData = db.UserMachineData.Add(new UserMachineData()
  504. {
  505. IdBrand = IdBrand,
  506. }).Entity;
  507. db.SaveChanges();
  508. }
  509. userData.ActProfit += ActPrize;
  510. db.SaveChanges();
  511. RedisDbconn.Instance.Set("UserMachineData:" + IdBrand, userData);
  512. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == GetUserId);
  513. if (account == null)
  514. {
  515. account = db.UserAccount.Add(new UserAccount()
  516. {
  517. Id = GetUserId,
  518. UserId = GetUserId,
  519. }).Entity;
  520. db.SaveChanges();
  521. }
  522. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  523. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  524. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  525. account.BalanceAmount += ActPrize;
  526. account.TotalAmount += ActPrize;
  527. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  528. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  529. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  530. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  531. {
  532. CreateDate = DateTime.Now,
  533. UpdateDate = DateTime.Now,
  534. UserId = GetUserId, //创客
  535. ChangeType = 0, //变动类型
  536. ProductType = pos.BrandId, //产品类型
  537. ChangeAmount = ActPrize, //变更金额
  538. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  539. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  540. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  541. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  542. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  543. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  544. }).Entity;
  545. db.SaveChanges();
  546. RedisDbconn.Instance.Set("UserAccount:" + GetUserId, account);
  547. string dateString = pos.ActivationTime.Value.ToString("yyyyMMdd");
  548. string monthString = pos.ActivationTime.Value.ToString("yyyyMM");
  549. // 激活奖励列表
  550. List<string> dates = RedisDbconn.Instance.GetList<string>("ActiveRewardDay:" + GetUserId + ":" + pos.BrandId);
  551. if (!dates.Contains(dateString))
  552. {
  553. RedisDbconn.Instance.AddList("ActiveRewardDay:" + GetUserId + ":" + pos.BrandId, dateString);
  554. }
  555. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:" + GetUserId + ":" + pos.BrandId + ":" + dateString, ActPrize);
  556. List<string> months = RedisDbconn.Instance.GetList<string>("ActiveRewardMonth:" + GetUserId + ":" + pos.BrandId);
  557. if (!months.Contains(monthString))
  558. {
  559. RedisDbconn.Instance.AddList("ActiveRewardMonth:" + GetUserId + ":" + pos.BrandId, monthString);
  560. }
  561. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:" + GetUserId + ":" + pos.BrandId + ":" + monthString, ActPrize);
  562. // 激活奖励详情
  563. List<int> actPrizeList = RedisDbconn.Instance.GetList<int>("ActiveRewardDetail:" + GetUserId + ":" + pos.BrandId + ":" + dateString);
  564. if (!actPrizeList.Contains(pos.BindMerchantId))
  565. {
  566. RedisDbconn.Instance.AddList("ActiveRewardDetail:" + GetUserId + ":" + pos.BrandId + ":" + dateString, pos.BindMerchantId);
  567. }
  568. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:mer:" + pos.BindMerchantId + ":" + pos.BrandId + ":" + dateString, ActPrize);
  569. //收支明细
  570. RedisDbconn.Instance.AddList("UserAccountRecord:" + GetUserId + ":1:" + monthString, userAccountRecord);
  571. RedisDbconn.Instance.AddNumber("UserAccount:" + GetUserId + ":1:" + monthString, ActPrize);
  572. }
  573. }
  574. }
  575. // }
  576. }
  577. db.Dispose();
  578. }
  579. //3. 前一天的激活记录,根据创客类型,如果是商户型创客,则开机奖励的起始人从此人上级计算。如果不是,则从此人开始计算
  580. public void dosomething4(string date)
  581. {
  582. WebCMSEntities db = new WebCMSEntities();
  583. DateTime yesterday = DateTime.Parse(date + " 00:00:00");
  584. DateTime today = DateTime.Parse(DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00");
  585. 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();
  586. foreach (PosMachinesTwo pos in posList)
  587. {
  588. if (pos.ActivationTime > DateTime.Now.AddDays(-20))
  589. {
  590. decimal ActPrize = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  591. if (ActPrize > 0)
  592. {
  593. // PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId && !string.IsNullOrEmpty(m.MerIdcardNo));
  594. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  595. if (merchant != null)
  596. {
  597. // PosMerchantOtherInfo otherInfo = db.PosMerchantOtherInfo.FirstOrDefault(m => m.CertId == merchant.MerIdcardNo && m.PrizeFlag2 == 0);
  598. // if (otherInfo != null)
  599. // {
  600. // otherInfo.PrizeFlag2 = 1;
  601. // db.SaveChanges();
  602. // 从机具所属人上级开始分开机奖励
  603. Users user = db.Users.FirstOrDefault(m => m.Id == pos.UserId);
  604. if (user != null)
  605. {
  606. if (!string.IsNullOrEmpty(user.ParentNav))
  607. {
  608. decimal Prize = 20;
  609. string[] ParentNavs = user.ParentNav.Trim(',').Replace(",,", ",").Split(',');
  610. for (int i = ParentNavs.Length - 1; i >= 0; i--)
  611. {
  612. int UserId = int.Parse(ParentNavs[i]);
  613. Users puser = db.Users.FirstOrDefault(m => m.Id == UserId && m.AuthFlag == 1);
  614. if (puser != null && Prize > 0)
  615. {
  616. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.PosSnType == 0); //判断是否拥有3台购买机
  617. int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  618. // int BigCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.BrandId == 3); //判断是否拥有2台大机
  619. // int BigActiveCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.ActivationState == 1 && m.BrandId == 3); //判断是否拥有2台激活大机(不限购买或赠送)
  620. if (machineCount >= 3 || ActiveCount >= 3)
  621. {
  622. int pTopUserId = 0;
  623. if (!string.IsNullOrEmpty(puser.ParentNav))
  624. {
  625. pTopUserId = int.Parse(puser.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  626. }
  627. Users machineUser = db.Users.FirstOrDefault(m => m.Id == pos.UserId) ?? new Users();
  628. OpenRewardDetail detail = db.OpenRewardDetail.Add(new OpenRewardDetail()
  629. {
  630. CreateDate = DateTime.Now,
  631. UpdateDate = DateTime.Now,
  632. TradeMonth = yesterday.ToString("yyyyMM"), //交易月
  633. TradeDate = yesterday, //达标日期
  634. UserId = puser.Id, //创客
  635. BrandId = pos.BrandId, //品牌
  636. ProductName = RelationClass.GetKqProductsInfo(pos.BrandId), //产品名称
  637. MerchantId = pos.BindMerchantId, //商户
  638. DirectUserId = merchant.UserId, //商户直属人
  639. SnNo = pos.PosSn, //SN号
  640. MerNo = merchant.KqMerNo, //渠道商户号
  641. SnType = pos.PosSnType, //机具类型
  642. StandardDate = pos.ActivationTime, //商户的激活日期
  643. SnStoreId = pos.StoreId, //SN仓库
  644. MerBuddyType = puser.MerchantType, //商户创客类型
  645. RewardType = 1, //奖励类型 1-开机直接奖励,2-开机间接奖励
  646. RewardTips = "开机奖励", //奖励描述
  647. CreditTradeAmt = pos.CreditTrade, //贷记卡交易总金额
  648. DebitTradeAmt = pos.DebitCardTrade, //借记卡交易总金额
  649. CreditRewardAmount = Prize, //贷记卡交易奖励金额
  650. RewardDesc = "开机奖励", //奖励描述
  651. TopUserId = pTopUserId, //顶级创客
  652. SeoTitle = machineUser.RealName,
  653. }).Entity;
  654. db.OpenReward.Add(new OpenReward()
  655. {
  656. CreateDate = DateTime.Now,
  657. UpdateDate = DateTime.Now,
  658. TradeMonth = yesterday.ToString("yyyyMM"), //交易月
  659. TradeDate = DateTime.Now, //达标日期
  660. UserId = puser.Id, //创客
  661. BrandId = pos.BrandId, //品牌
  662. RewardType = 2, //奖励类型
  663. CreditTradeAmt = pos.CreditTrade, //贷记卡交易总金额
  664. DebitTradeAmt = pos.DebitCardTrade, //借记卡交易总金额
  665. CreditRewardAmount = Prize, //贷记卡交易奖励金额
  666. RewardDesc = "开机奖励", //奖励描述
  667. TopUserId = pTopUserId, //顶级创客
  668. });
  669. string IdBrand = puser.Id + "_" + pos.BrandId;
  670. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  671. if (userData == null)
  672. {
  673. userData = db.UserMachineData.Add(new UserMachineData()
  674. {
  675. IdBrand = IdBrand,
  676. }).Entity;
  677. db.SaveChanges();
  678. }
  679. userData.OpenProfit += Prize;
  680. db.SaveChanges();
  681. RedisDbconn.Instance.Set("UserMachineData:" + IdBrand, userData);
  682. //账户入库
  683. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id);
  684. if (account == null)
  685. {
  686. account = db.UserAccount.Add(new UserAccount()
  687. {
  688. Id = puser.Id,
  689. UserId = puser.Id,
  690. }).Entity;
  691. db.SaveChanges();
  692. }
  693. //收支明细入库
  694. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  695. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  696. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  697. account.BalanceAmount += Prize;
  698. account.TotalAmount += Prize;
  699. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  700. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  701. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  702. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  703. {
  704. CreateDate = DateTime.Now,
  705. UpdateDate = DateTime.Now,
  706. UserId = puser.Id, //创客
  707. ChangeType = 50, //变动类型
  708. ProductType = pos.BrandId, //产品类型
  709. ChangeAmount = Prize, //变更金额
  710. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  711. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  712. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  713. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  714. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  715. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  716. }).Entity;
  717. db.SaveChanges();
  718. RedisDbconn.Instance.Set("UserAccount:" + puser.Id, account);
  719. string dateString = yesterday.ToString("yyyyMMdd");
  720. string monthString = yesterday.ToString("yyyyMM");
  721. // 开机奖励列表
  722. List<string> dates = RedisDbconn.Instance.GetList<string>("OpenRewardDay:" + puser.Id + ":" + pos.BrandId);
  723. if (!dates.Contains(dateString))
  724. {
  725. RedisDbconn.Instance.AddList("OpenRewardDay:" + puser.Id + ":" + pos.BrandId, dateString);
  726. }
  727. RedisDbconn.Instance.AddNumber("OpenRewardAmt:" + puser.Id + ":" + pos.BrandId + ":" + dateString, Prize);
  728. List<string> months = RedisDbconn.Instance.GetList<string>("OpenRewardMonth:" + puser.Id + ":" + pos.BrandId);
  729. if (!months.Contains(monthString))
  730. {
  731. RedisDbconn.Instance.AddList("OpenRewardMonth:" + puser.Id + ":" + pos.BrandId, monthString);
  732. }
  733. RedisDbconn.Instance.AddNumber("OpenRewardAmt:" + puser.Id + ":" + pos.BrandId + ":" + monthString, Prize);
  734. // 开机奖励详情
  735. RedisDbconn.Instance.AddList("OpenRewardDetail:" + puser.Id + ":" + pos.BrandId + ":" + dateString, detail);
  736. //收支明细
  737. RedisDbconn.Instance.AddList("UserAccountRecord:" + puser.Id + ":1:" + monthString, userAccountRecord);
  738. RedisDbconn.Instance.AddNumber("UserAccount:" + puser.Id + ":1:" + monthString, Prize);
  739. Prize -= 10;
  740. }
  741. }
  742. }
  743. }
  744. }
  745. // }
  746. }
  747. }
  748. }
  749. }
  750. db.Dispose();
  751. }
  752. public void dosomething42(string date)
  753. {
  754. WebCMSEntities db = new WebCMSEntities();
  755. DateTime yesterday = DateTime.Parse(date + " 00:00:00");
  756. DateTime today = DateTime.Parse(DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00");
  757. List<PosMerchantInfo> merList = db.PosMerchantInfo.Where(m => m.ActiveStatus == 1 && m.MerStandardDate >= yesterday && m.MerStandardDate < today && !string.IsNullOrEmpty(m.SeoKeyword) && m.SeoKeyword != "0").ToList();
  758. foreach (PosMerchantInfo merchant in merList)
  759. {
  760. MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == merchant.MerchantNo) ?? new MachineForMerNo();
  761. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forMerNo.SnId) ?? new PosMachinesTwo();
  762. if (pos.ActivationTime > DateTime.Now.AddDays(-20) && pos.CreditTrade >= 1000)
  763. {
  764. decimal ActPrize = decimal.Parse(function.CheckNum(merchant.SeoKeyword));
  765. if (ActPrize > 0)
  766. {
  767. // PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId && !string.IsNullOrEmpty(m.MerIdcardNo));
  768. // PosMerchantOtherInfo otherInfo = db.PosMerchantOtherInfo.FirstOrDefault(m => m.CertId == merchant.MerIdcardNo && m.PrizeFlag2 == 0);
  769. // if (otherInfo != null)
  770. // {
  771. // otherInfo.PrizeFlag2 = 1;
  772. // db.SaveChanges();
  773. // 从机具所属人上级开始分开机奖励
  774. Users user = db.Users.FirstOrDefault(m => m.Id == pos.UserId);
  775. if (user != null)
  776. {
  777. if (!string.IsNullOrEmpty(user.ParentNav))
  778. {
  779. decimal Prize = 20;
  780. string[] ParentNavs = user.ParentNav.Trim(',').Replace(",,", ",").Split(',');
  781. for (int i = ParentNavs.Length - 1; i >= 0; i--)
  782. {
  783. int UserId = int.Parse(ParentNavs[i]);
  784. Users puser = db.Users.FirstOrDefault(m => m.Id == UserId && m.AuthFlag == 1);
  785. if (puser != null && Prize > 0)
  786. {
  787. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.PosSnType == 0); //判断是否拥有3台购买机
  788. int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  789. int BigCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.BrandId == 3); //判断是否拥有2台大机
  790. int BigActiveCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.ActivationState == 1 && m.BrandId == 3); //判断是否拥有2台激活大机(不限购买或赠送)
  791. if (machineCount >= 3 || ActiveCount >= 3 || BigCount >= 2 || BigActiveCount >= 2)
  792. {
  793. int pTopUserId = 0;
  794. if (!string.IsNullOrEmpty(puser.ParentNav))
  795. {
  796. pTopUserId = int.Parse(puser.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  797. }
  798. Users machineUser = db.Users.FirstOrDefault(m => m.Id == pos.UserId) ?? new Users();
  799. OpenRewardDetail detail = db.OpenRewardDetail.Add(new OpenRewardDetail()
  800. {
  801. CreateDate = DateTime.Now,
  802. UpdateDate = DateTime.Now,
  803. TradeMonth = yesterday.ToString("yyyyMM"), //交易月
  804. TradeDate = yesterday, //达标日期
  805. UserId = puser.Id, //创客
  806. BrandId = pos.BrandId, //品牌
  807. ProductName = RelationClass.GetKqProductsInfo(pos.BrandId), //产品名称
  808. MerchantId = pos.BindMerchantId, //商户
  809. DirectUserId = merchant.UserId, //商户直属人
  810. SnNo = pos.PosSn, //SN号
  811. MerNo = merchant.KqMerNo, //渠道商户号
  812. SnType = pos.PosSnType, //机具类型
  813. StandardDate = pos.ActivationTime, //商户的激活日期
  814. SnStoreId = pos.StoreId, //SN仓库
  815. MerBuddyType = puser.MerchantType, //商户创客类型
  816. RewardType = 1, //奖励类型 1-开机直接奖励,2-开机间接奖励
  817. RewardTips = "开机奖励", //奖励描述
  818. CreditTradeAmt = pos.CreditTrade, //贷记卡交易总金额
  819. DebitTradeAmt = pos.DebitCardTrade, //借记卡交易总金额
  820. CreditRewardAmount = Prize, //贷记卡交易奖励金额
  821. RewardDesc = "开机奖励", //奖励描述
  822. TopUserId = pTopUserId, //顶级创客
  823. SeoTitle = machineUser.RealName,
  824. }).Entity;
  825. db.OpenReward.Add(new OpenReward()
  826. {
  827. CreateDate = DateTime.Now,
  828. UpdateDate = DateTime.Now,
  829. TradeMonth = yesterday.ToString("yyyyMM"), //交易月
  830. TradeDate = DateTime.Now, //达标日期
  831. UserId = puser.Id, //创客
  832. BrandId = pos.BrandId, //品牌
  833. RewardType = 2, //奖励类型
  834. CreditTradeAmt = pos.CreditTrade, //贷记卡交易总金额
  835. DebitTradeAmt = pos.DebitCardTrade, //借记卡交易总金额
  836. CreditRewardAmount = Prize, //贷记卡交易奖励金额
  837. RewardDesc = "开机奖励", //奖励描述
  838. TopUserId = pTopUserId, //顶级创客
  839. });
  840. string IdBrand = puser.Id + "_" + pos.BrandId;
  841. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  842. if (userData == null)
  843. {
  844. userData = db.UserMachineData.Add(new UserMachineData()
  845. {
  846. IdBrand = IdBrand,
  847. }).Entity;
  848. db.SaveChanges();
  849. }
  850. userData.OpenProfit += Prize;
  851. db.SaveChanges();
  852. RedisDbconn.Instance.Set("UserMachineData:" + IdBrand, userData);
  853. //账户入库
  854. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id);
  855. if (account == null)
  856. {
  857. account = db.UserAccount.Add(new UserAccount()
  858. {
  859. Id = puser.Id,
  860. UserId = puser.Id,
  861. }).Entity;
  862. db.SaveChanges();
  863. }
  864. //收支明细入库
  865. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  866. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  867. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  868. account.BalanceAmount += Prize;
  869. account.TotalAmount += Prize;
  870. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  871. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  872. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  873. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  874. {
  875. CreateDate = DateTime.Now,
  876. UpdateDate = DateTime.Now,
  877. UserId = puser.Id, //创客
  878. ChangeType = 50, //变动类型
  879. ProductType = pos.BrandId, //产品类型
  880. ChangeAmount = Prize, //变更金额
  881. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  882. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  883. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  884. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  885. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  886. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  887. }).Entity;
  888. db.SaveChanges();
  889. RedisDbconn.Instance.Set("UserAccount:" + puser.Id, account);
  890. string dateString = yesterday.ToString("yyyyMMdd");
  891. string monthString = yesterday.ToString("yyyyMM");
  892. // 开机奖励列表
  893. List<string> dates = RedisDbconn.Instance.GetList<string>("OpenRewardDay:" + puser.Id + ":" + pos.BrandId);
  894. if (!dates.Contains(dateString))
  895. {
  896. RedisDbconn.Instance.AddList("OpenRewardDay:" + puser.Id + ":" + pos.BrandId, dateString);
  897. }
  898. RedisDbconn.Instance.AddNumber("OpenRewardAmt:" + puser.Id + ":" + pos.BrandId + ":" + dateString, Prize);
  899. List<string> months = RedisDbconn.Instance.GetList<string>("OpenRewardMonth:" + puser.Id + ":" + pos.BrandId);
  900. if (!months.Contains(monthString))
  901. {
  902. RedisDbconn.Instance.AddList("OpenRewardMonth:" + puser.Id + ":" + pos.BrandId, monthString);
  903. }
  904. RedisDbconn.Instance.AddNumber("OpenRewardAmt:" + puser.Id + ":" + pos.BrandId + ":" + monthString, Prize);
  905. // 开机奖励详情
  906. RedisDbconn.Instance.AddList("OpenRewardDetail:" + puser.Id + ":" + pos.BrandId + ":" + dateString, detail);
  907. //收支明细
  908. RedisDbconn.Instance.AddList("UserAccountRecord:" + puser.Id + ":1:" + monthString, userAccountRecord);
  909. RedisDbconn.Instance.AddNumber("UserAccount:" + puser.Id + ":1:" + monthString, Prize);
  910. Prize -= 10;
  911. }
  912. }
  913. }
  914. }
  915. }
  916. // }
  917. }
  918. }
  919. }
  920. db.Dispose();
  921. }
  922. }
  923. }