TestHelper.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Linq;
  5. using System.Data;
  6. using MySystem;
  7. using MySystem.PxcModels;
  8. using Library;
  9. public class TestHelper
  10. {
  11. public readonly static TestHelper Instance = new TestHelper();
  12. private TestHelper()
  13. {
  14. }
  15. public void Start()
  16. {
  17. Thread th = new Thread(StartEverDay);
  18. th.IsBackground = true;
  19. th.Start();
  20. }
  21. private void DoWorks()
  22. {
  23. // WebCMSEntities db = new WebCMSEntities();
  24. // bool op = true;
  25. // while (op)
  26. // {
  27. // WebCMSEntities db = new WebCMSEntities();
  28. // int StartId = RedisDbconn.Instance.Get<int>("SetCertUserId");
  29. // var list = db.Users.Select(m => new { m.Id, m.CertId }).Where(m => m.Id > StartId && string.IsNullOrEmpty(m.CertId)).OrderBy(m => m.Id).Take(20).ToList();
  30. // if (list.Count > 0)
  31. // {
  32. // foreach (var sub in list)
  33. // {
  34. // Users user = db.Users.FirstOrDefault(m => m.Id == sub.Id && string.IsNullOrEmpty(m.CertId));
  35. // if (user != null)
  36. // {
  37. // DataTable dt = dbconn.dtable("select CertId from TmpCert where MakerCode='" + user.MakerCode + "'");
  38. // if (dt.Rows.Count > 0)
  39. // {
  40. // string CertId = dt.Rows[0]["CertId"].ToString();
  41. // user.CertId = CertId;
  42. // }
  43. // RedisDbconn.Instance.Set("SetCertUserId", sub.Id);
  44. // }
  45. // }
  46. // db.SaveChanges();
  47. // db.Dispose();
  48. // }
  49. // else
  50. // {
  51. // op = false;
  52. // }
  53. // Thread.Sleep(10);
  54. // }
  55. // IQueryable<Users> users = db.Users.Where(m => m.AuthFlag == 1).OrderBy(m => m.Id);
  56. // foreach (Users user in users.ToList())
  57. // {
  58. // string path = function.CreateQRCode2(ConfigurationManager.AppSettings["SourceHost"].ToString() + "p/user-inviteregist-1?Id=" + user.Id, function.MD5_16(user.Id.ToString() + "8745"), "/bsserver_com/static/ReferenceQrCode/");
  59. // path = path.Replace("//", "/");
  60. // string resultpath = "/bsserver_com/static/ReferenceQrCode/" + function.MD5_16(user.Id.ToString() + "8745") + "Pic.png";
  61. // MakeReferenceQrCodeService.Instance.MakeQRCode(function.getPath("/static/QrCodeBg.png"), function.getPath(path), function.getPath(resultpath), user);
  62. // resultpath = resultpath.Replace("bsserver_com/", "");
  63. // user.ReferenceQrCode = resultpath;
  64. // db.SaveChanges();
  65. // RedisDbconn.Instance.Set("Users:" + user.Id, user);
  66. // function.WriteLog(user.Id.ToString(), "生成邀请二维码");
  67. // }
  68. // function.WriteLog("finish", "生成邀请二维码");
  69. // bool op = true;
  70. // while (op)
  71. // {
  72. // int startId = RedisDbconn.Instance.Get<int>("RemoveUserId");
  73. // List<int> uids = db.Users.Select(m => m.Id).Where(m => m > startId && m > 1).OrderBy(m => m).Take(100).ToList();
  74. // if (uids.Count > 0)
  75. // {
  76. // foreach (int uid in uids)
  77. // {
  78. // DoUsers(db, uid);
  79. // RedisDbconn.Instance.Set("RemoveUserId", uid);
  80. // }
  81. // Thread.Sleep(200);
  82. // }
  83. // else
  84. // {
  85. // op = false;
  86. // }
  87. // }
  88. // db.Dispose();
  89. }
  90. // public void DoUsers(WebCMSEntities db, int uid)
  91. // {
  92. // Users user = db.Users.FirstOrDefault(m => m.Id == uid);
  93. // if (user != null)
  94. // {
  95. // string Mobile = function.CheckNull(user.Mobile);
  96. // if (!Mobile.Contains("*") && Mobile.Substring(0, 1) == "1" && Mobile.Length == 11)
  97. // {
  98. // string ParentNav = user.ParentNav;
  99. // if (!string.IsNullOrEmpty(ParentNav))
  100. // {
  101. // string NewParentNav = "";
  102. // int NewParentUserId = 1;
  103. // string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  104. // Array.Reverse(ParentNavList);
  105. // int i = 0;
  106. // foreach (string ParentId in ParentNavList)
  107. // {
  108. // int puid = int.Parse(ParentId);
  109. // Users puser = db.Users.FirstOrDefault(m => m.Id == puid);
  110. // if (puser != null)
  111. // {
  112. // Mobile = function.CheckNull(puser.Mobile);
  113. // if (!Mobile.Contains("*") && Mobile.Substring(0, 1) == "1" && Mobile.Length == 11)
  114. // {
  115. // i += 1;
  116. // NewParentNav = "," + ParentId + "," + NewParentNav;
  117. // if (i == 1)
  118. // {
  119. // NewParentUserId = puid;
  120. // }
  121. // }
  122. // }
  123. // }
  124. // if (string.IsNullOrEmpty(NewParentNav))
  125. // {
  126. // NewParentNav = ",1,";
  127. // }
  128. // user.ParentNav = NewParentNav;
  129. // user.ParentUserId = NewParentUserId;
  130. // }
  131. // else
  132. // {
  133. // user.ParentNav = ",1,";
  134. // user.ParentUserId = 1;
  135. // }
  136. // db.SaveChanges();
  137. // }
  138. // }
  139. // }
  140. private void domerchantType()
  141. {
  142. // OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
  143. // OtherMySqlConn.op("update PosMerchantInfo set MerUserType=0");
  144. // OtherMySqlConn.op("update Users set MerchantType=0");
  145. // bool op = true;
  146. // while (op)
  147. // {
  148. // MySystem.SpModels.WebCMSEntities spdb = new MySystem.SpModels.WebCMSEntities();
  149. // WebCMSEntities db = new WebCMSEntities();
  150. // DateTime end = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00");
  151. // int startId = int.Parse(function.CheckInt(RedisDbconn.Instance.Get<string>("BindRecordId")));
  152. // List<MySystem.SpModels.BindRecord> binds = spdb.BindRecord.Where(m => m.Id > startId && m.CreateTime < end).OrderBy(m => m.Id).Take(200).ToList();
  153. // if (binds.Count > 0)
  154. // {
  155. // foreach (MySystem.SpModels.BindRecord bind in binds)
  156. // {
  157. // string Mobile = bind.MerNewSnNo;
  158. // string MerNo = bind.MerNo;
  159. // string BeforeNum = "";
  160. // string AfterNum = "";
  161. // if (Mobile.Contains("****") && Mobile.Length == 11)
  162. // {
  163. // BeforeNum = Mobile.Substring(0, 3);
  164. // AfterNum = Mobile.Substring(7);
  165. // }
  166. // string Name = bind.MerName;
  167. // if (bind.ProductType == "2")
  168. // {
  169. // if (Name.Contains("-"))
  170. // {
  171. // Name = Name.Split('-')[1];
  172. // }
  173. // else if (Name.Contains("_"))
  174. // {
  175. // Name = Name.Split('_')[1];
  176. // }
  177. // }
  178. // else if (bind.ProductType == "4")
  179. // {
  180. // Name = bind.SeoTitle;
  181. // }
  182. // Users user = db.Users.FirstOrDefault(m => m.Mobile.StartsWith(BeforeNum) && m.Mobile.EndsWith(AfterNum) && m.RealName == Name && m.AuthFlag == 1 && m.MerchantType == 0);
  183. // if (user != null)
  184. // {
  185. // PosMachinesTwo checkPos = db.PosMachinesTwo.FirstOrDefault(m => m.UserId == user.Id);
  186. // if (checkPos == null)
  187. // {
  188. // MachineForSnNo forSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == bind.MerSnNo) ?? new MachineForSnNo();
  189. // PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forSnNo.SnId && m.UserId != user.Id);
  190. // if (pos != null)
  191. // {
  192. // pos.UserId = user.Id;
  193. // user.MerchantType = 1;
  194. // PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId) ?? new PosMerchantInfo();
  195. // merchant.UserId = user.Id;
  196. // merchant.MerUserType = 1;
  197. // pos.SeoTitle = user.Id.ToString(); // 记录商户型创客的Id
  198. // RedisDbconn.Instance.Set("PosMachinesTwo:" + pos.Id, pos);
  199. // db.SaveChanges();
  200. // RedisDbconn.Instance.Set("Users:" + user.Id, user);
  201. // RedisDbconn.Instance.Set("PosMerchantInfo:" + merchant.Id, merchant);
  202. // }
  203. // }
  204. // else
  205. // {
  206. // int count = db.PosMachinesTwo.Count(m => m.UserId == user.Id);
  207. // if (count == 1 && checkPos.UserId != checkPos.BuyUserId)
  208. // {
  209. // user.MerchantType = 1;
  210. // PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == checkPos.BindMerchantId) ?? new PosMerchantInfo();
  211. // merchant.UserId = user.Id;
  212. // merchant.MerUserType = 1;
  213. // checkPos.SeoTitle = user.Id.ToString(); // 记录商户型创客的Id
  214. // RedisDbconn.Instance.Set("PosMachinesTwo:" + checkPos.Id, checkPos);
  215. // db.SaveChanges();
  216. // RedisDbconn.Instance.Set("Users:" + user.Id, user);
  217. // RedisDbconn.Instance.Set("PosMerchantInfo:" + merchant.Id, merchant);
  218. // }
  219. // }
  220. // }
  221. // RedisDbconn.Instance.Set("BindRecordId", bind.Id.ToString());
  222. // }
  223. // }
  224. // else
  225. // {
  226. // op = false;
  227. // }
  228. // spdb.Dispose();
  229. // db.Dispose();
  230. // }
  231. // RedisDbconn.Instance.Clear("BindRecordId");
  232. // DataTable dt = OtherMySqlConn.dtable("select PosSn,UserId from PosMachinesTwo where UserId in (select Id from Users where MerchantType=1);");
  233. // foreach (DataRow dr in dt.Rows)
  234. // {
  235. // string PosSn = dr["PosSn"].ToString();
  236. // string UserId = dr["UserId"].ToString();
  237. // OtherMySqlConn.op("update TradeRecord set UserId=" + UserId + " where SnNo='" + PosSn + "' and CreateDate>='2022-04-01 00:00:00'");
  238. // }
  239. // OtherMySqlConn.connstr = "";
  240. // StatService.Instance.StartEverDay2();
  241. }
  242. public void StartEverDay()
  243. {
  244. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
  245. DateTime end = DateTime.Parse("2022-04-19 00:00:00");
  246. DateTime check = DateTime.Parse("2022-03-11");
  247. while (check <= end)
  248. {
  249. StatMerchantTrade(check.ToString("yyyy-MM-dd"));
  250. check = check.AddDays(1);
  251. }
  252. }
  253. private void StatMerchantTrade(string date)
  254. {
  255. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "执行商户交易额日志");
  256. WebCMSEntities db = new WebCMSEntities();
  257. try
  258. {
  259. DataTable selfdt = OtherMySqlConn.dtable("select MerchantId,BrandId,DATE_FORMAT(CreateDate,'%Y-%m-%d') as TradeDate,sum(TradeAmount) as TradeAmount from TradeRecord group by MerchantId,BrandId,DATE_FORMAT(CreateDate,'%Y-%m-%d') order by MerchantId,BrandId,DATE_FORMAT(CreateDate,'%Y-%m-%d')");
  260. foreach (DataRow selfDr in selfdt.Rows)
  261. {
  262. int BrandId = int.Parse(selfDr["BrandId"].ToString());
  263. int MerchantId = int.Parse(selfDr["MerchantId"].ToString());
  264. string TradeDate = selfDr["TradeDate"].ToString();
  265. TradeDate = TradeDate.Replace("-", "");
  266. string TradeMonth = TradeDate.Substring(0, 6);
  267. decimal TradeAmount = decimal.Parse(selfDr["TradeAmount"].ToString());
  268. PosMerchantTradeSummay merStat = db.PosMerchantTradeSummay.FirstOrDefault(m => m.MerchantId == MerchantId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId);
  269. if (merStat == null)
  270. {
  271. merStat = db.PosMerchantTradeSummay.Add(new PosMerchantTradeSummay()
  272. {
  273. MerchantId = MerchantId,
  274. TradeMonth = TradeMonth,
  275. TradeDate = TradeDate,
  276. BrandId = BrandId,
  277. }).Entity;
  278. db.SaveChanges();
  279. }
  280. merStat.TradeAmount += TradeAmount;
  281. db.SaveChanges();
  282. RedisDbconn.Instance.Clear("MerTotalAmount:" + MerchantId);
  283. RedisDbconn.Instance.Clear("MerTotalAmount:" + MerchantId + ":" + TradeMonth); //商户当月交易
  284. RedisDbconn.Instance.Clear("MerTotalAmount:" + MerchantId + ":" + TradeDate); //商户当日交易
  285. }
  286. }
  287. catch (Exception ex)
  288. {
  289. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计商户的交易额");
  290. }
  291. db.Dispose();
  292. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行商户交易额日志");
  293. }
  294. }