StoreApplyHelper.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. using LitJson;
  10. public class StoreApplyHelper
  11. {
  12. public readonly static StoreApplyHelper Instance = new StoreApplyHelper();
  13. private StoreApplyHelper()
  14. {
  15. }
  16. public void Start()
  17. {
  18. Thread th = new Thread(DoWorks);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. // 每月1号重置仓库额度
  23. private void DoWorks()
  24. {
  25. while (true)
  26. {
  27. WebCMSEntities db = new WebCMSEntities();
  28. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
  29. try
  30. {
  31. if(DateTime.Now.Day == 1 && DateTime.Now.Hour > 0 && DateTime.Now.Hour < 3)
  32. {
  33. string check = function.ReadInstance("/StoreApply/" + DateTime.Now.ToString("yyyyMM") + ".txt");
  34. if(string.IsNullOrEmpty(check))
  35. {
  36. function.WritePage("/StoreApply/", DateTime.Now.ToString("yyyyMM") + ".txt", DateTime.Now.ToString());
  37. Dictionary<int, decimal> dataDic = new Dictionary<int, decimal>();
  38. string pre = DateTime.Now.AddMonths(-2).ToString("yyyy-MM") + "-01 00:00:00";
  39. string start = DateTime.Now.AddMonths(-1).ToString("yyyy-MM") + "-01 00:00:00";
  40. string end = DateTime.Parse(start).AddMonths(1).ToString("yyyy-MM-dd HH:mm:ss");
  41. DataTable dts = OtherMySqlConn.dtable("select UserId from StoreHouse where CreateDate>='" + pre + "' and CreateDate<'" + end + "' and UserId in (select UserId from (select UserId,count(Id) from StoreHouse group by UserId HAVING count(Id)=1) tb)");
  42. foreach(DataRow dr in dts.Rows)
  43. {
  44. int UserId = int.Parse(function.CheckInt(dr["UserId"].ToString()));
  45. if(!dataDic.ContainsKey(UserId))
  46. {
  47. dataDic.Add(UserId, 20000);
  48. }
  49. else
  50. {
  51. dataDic[UserId] += 20000;
  52. }
  53. }
  54. DataTable dt = OtherMySqlConn.dtable("select StoreId,count(Id) from StoreStockChange where CreateDate>='" + start + "' and CreateDate<'" + end + "' and BrandId in (1,2,4,6,7,8) and TransType in (10,11,2) and StoreId>0 group by StoreId");
  55. foreach(DataRow dr in dt.Rows)
  56. {
  57. int StoreId = int.Parse(function.CheckInt(dr["StoreId"].ToString()));
  58. int Count = int.Parse(function.CheckInt(dr[1].ToString()));
  59. decimal AmountMore = Count * 200;
  60. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
  61. if(!dataDic.ContainsKey(store.UserId))
  62. {
  63. dataDic.Add(store.UserId, AmountMore);
  64. }
  65. else
  66. {
  67. dataDic[store.UserId] += AmountMore;
  68. }
  69. }
  70. dt = OtherMySqlConn.dtable("select StoreId,count(Id) from StoreStockChange where CreateDate>='" + start + "' and CreateDate<'" + end + "' and BrandId in (3,5,9) and TransType in (10,11,2) and StoreId>0 group by StoreId");
  71. foreach(DataRow dr in dt.Rows)
  72. {
  73. int StoreId = int.Parse(function.CheckInt(dr["StoreId"].ToString()));
  74. int Count = int.Parse(function.CheckInt(dr[1].ToString()));
  75. decimal AmountMore = Count * 300;
  76. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
  77. if(!dataDic.ContainsKey(store.UserId))
  78. {
  79. dataDic.Add(store.UserId, AmountMore);
  80. }
  81. else
  82. {
  83. dataDic[store.UserId] += AmountMore;
  84. }
  85. }
  86. foreach(int UserId in dataDic.Keys)
  87. {
  88. decimal Amount = dataDic[UserId];
  89. decimal AmountMore = 0;
  90. // DataTable dtmore = OtherMySqlConn.dtable("select count(Id)*200 from PosMachinesTwo pos where StoreId in (select Id from StoreHouse where UserId=" + UserId + ") and BuyUserId=0 and BrandId in (1,2,4,6,7,8) and `Status`>-1");
  91. // if(dtmore.Rows.Count > 0)
  92. // {
  93. // AmountMore += decimal.Parse(function.CheckNum(dtmore.Rows[0][0].ToString()));
  94. // }
  95. // dtmore = OtherMySqlConn.dtable("select count(Id)*300 from PosMachinesTwo pos where StoreId in (select Id from StoreHouse where UserId=" + UserId + ") and BuyUserId=0 and BrandId in (3,5,9) and `Status`>-1");
  96. // if(dtmore.Rows.Count > 0)
  97. // {
  98. // AmountMore += decimal.Parse(function.CheckNum(dtmore.Rows[0][0].ToString()));
  99. // }
  100. if(Amount < 10000 && SpecialUsers10000().Contains(UserId))
  101. {
  102. Amount = 10000;
  103. }
  104. if(Amount < 20000 && SpecialUsers20000().Contains(UserId))
  105. {
  106. Amount = 20000;
  107. }
  108. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  109. if (account == null)
  110. {
  111. account = db.UserAccount.Add(new UserAccount()
  112. {
  113. Id = UserId,
  114. UserId = UserId,
  115. }).Entity;
  116. db.SaveChanges();
  117. }
  118. account.FixedAmount = Amount;
  119. account.ValidAmount = Amount + account.TempAmount - AmountMore;
  120. function.WriteLog("UserId:" + UserId + ";FixedAmount:" + account.FixedAmount + ";AmountMore:" + AmountMore + ";ValidAmount:" + account.ValidAmount, "计算分仓申请机具额度日志");
  121. }
  122. db.SaveChanges();
  123. }
  124. }
  125. }
  126. catch (Exception ex)
  127. {
  128. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "计算分仓申请机具额度异常");
  129. }
  130. db.Dispose();
  131. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "计算分仓申请机具额度日志");
  132. Thread.Sleep(60000);
  133. }
  134. }
  135. public void StartEverTime()
  136. {
  137. Thread th = new Thread(StartEverTimeDo);
  138. th.IsBackground = true;
  139. th.Start();
  140. }
  141. private void StartEverTimeDo()
  142. {
  143. while (true)
  144. {
  145. WebCMSEntities db = new WebCMSEntities();
  146. try
  147. {
  148. string data = RedisDbconn.Instance.RPop<string>("StoreApplyQueue");
  149. if(!string.IsNullOrEmpty(data))
  150. {
  151. function.WriteLog("data:" + data, "分仓向总仓申请机具日志");
  152. JsonData jsonObj = JsonMapper.ToObject(data);
  153. if(jsonObj["Kind"].ToString() == "1") // 购买临时额度
  154. {
  155. int OrderId = int.Parse(jsonObj["Data"]["OrderId"].ToString());
  156. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId);
  157. if(order != null)
  158. {
  159. decimal TotalPrice = order.TotalPrice * 2;
  160. AddAmount2(db, order.UserId, TotalPrice, 1, order.Id);
  161. }
  162. }
  163. else if(jsonObj["Kind"].ToString() == "2") // 增减分仓临时额度
  164. {
  165. int UserId = int.Parse(jsonObj["Data"]["UserId"].ToString());
  166. decimal Amount = decimal.Parse(jsonObj["Data"]["Amount"].ToString());
  167. int OperateType = int.Parse(jsonObj["Data"]["OperateType"].ToString());
  168. AddAmount(db, UserId, Amount, OperateType);
  169. }
  170. else if(jsonObj["Kind"].ToString() == "3") // 调低额度返回余额
  171. {
  172. int UserId = int.Parse(jsonObj["Data"]["UserId"].ToString());
  173. decimal Amount = decimal.Parse(jsonObj["Data"]["Amount"].ToString());
  174. AddAmount2(db, UserId, Amount, 0);
  175. decimal BalanceAmount = Amount / 2;
  176. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  177. if (account == null)
  178. {
  179. account = db.UserAccount.Add(new UserAccount()
  180. {
  181. Id = UserId,
  182. UserId = UserId,
  183. }).Entity;
  184. db.SaveChanges();
  185. }
  186. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  187. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  188. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  189. account.BalanceAmount += BalanceAmount;
  190. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  191. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  192. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  193. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  194. {
  195. CreateDate = DateTime.Now,
  196. UpdateDate = DateTime.Now,
  197. UserId = UserId, //创客
  198. ChangeType = 119, //变动类型
  199. ChangeAmount = BalanceAmount, //变更金额
  200. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  201. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  202. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  203. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  204. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  205. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  206. }).Entity;
  207. }
  208. else if(jsonObj["Kind"].ToString() == "4") // 仓库发货,预发机申请
  209. {
  210. int StoreId = int.Parse(jsonObj["Data"]["StoreId"].ToString());
  211. string SnIds = jsonObj["Data"]["SnIds"].ToString();
  212. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId);
  213. if(store != null)
  214. {
  215. decimal Amount = 0;
  216. string[] SnIdList = SnIds.Split(',');
  217. foreach(string SnIdString in SnIdList)
  218. {
  219. int SnId = int.Parse(SnIdString);
  220. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == SnId) ?? new PosMachinesTwo();
  221. if(pos.BrandId == 1 || pos.BrandId == 2 || pos.BrandId == 4 || pos.BrandId == 6 || pos.BrandId == 7 || pos.BrandId == 8)
  222. {
  223. Amount += 200;
  224. }
  225. else if(pos.BrandId == 3 || pos.BrandId == 5 || pos.BrandId == 9)
  226. {
  227. Amount += 300;
  228. }
  229. }
  230. if(Amount > 0)
  231. {
  232. AddAmount(db, store.UserId, Amount, 1);
  233. }
  234. }
  235. }
  236. else if(jsonObj["Kind"].ToString() == "5") // 后台仓库调拨
  237. {
  238. int StoreId = int.Parse(jsonObj["Data"]["StoreId"].ToString());
  239. int BrandId = int.Parse(jsonObj["Data"]["BrandId"].ToString());
  240. string OpStorrString = jsonObj["Data"]["OpStoreNum"].ToString();
  241. int OpType = OpStorrString.StartsWith("-") ? 0 : 1;
  242. int OpStoreNum = int.Parse(OpStorrString.Replace("-", ""));
  243. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId);
  244. if(store != null)
  245. {
  246. decimal Amount = 0;
  247. if(BrandId == 1 || BrandId == 2 || BrandId == 4 || BrandId == 6 || BrandId == 7 || BrandId == 8)
  248. {
  249. Amount += 200 * OpStoreNum;
  250. }
  251. else if(BrandId == 3 || BrandId == 5 || BrandId == 9)
  252. {
  253. Amount += 300 * OpStoreNum;
  254. }
  255. if(Amount > 0)
  256. {
  257. AddAmount(db, store.UserId, Amount, OpType);
  258. }
  259. }
  260. }
  261. db.SaveChanges();
  262. }
  263. else
  264. {
  265. Thread.Sleep(5000);
  266. }
  267. }
  268. catch (Exception ex)
  269. {
  270. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "分仓向总仓申请机具线程异常");
  271. }
  272. db.Dispose();
  273. }
  274. }
  275. public void AddAmount(WebCMSEntities db, int UserId, decimal Amount, int OperateType = 1, int OrderId = 0)
  276. {
  277. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  278. if (account == null)
  279. {
  280. account = db.UserAccount.Add(new UserAccount()
  281. {
  282. Id = UserId,
  283. UserId = UserId,
  284. }).Entity;
  285. db.SaveChanges();
  286. }
  287. decimal BeforeTotalAmount = account.ValidAmount; //变更前总金额
  288. if(OperateType == 1)
  289. {
  290. account.ValidAmount += Amount;
  291. }
  292. else
  293. {
  294. account.ValidAmount -= Amount;
  295. }
  296. decimal AfterTotalAmount = account.ValidAmount; //变更后总金额
  297. StoreHouseAmountRecord record = db.StoreHouseAmountRecord.Add(new StoreHouseAmountRecord()
  298. {
  299. CreateDate = DateTime.Now,
  300. UpdateDate = DateTime.Now,
  301. OperateType = OperateType,
  302. AmountType = 1,
  303. AfterAmount = AfterTotalAmount,
  304. BeforeAmount = BeforeTotalAmount,
  305. UseAmount = Amount,
  306. UserId = UserId,
  307. QueryCount = OrderId,
  308. }).Entity;
  309. db.SaveChanges();
  310. }
  311. public void AddAmount2(WebCMSEntities db, int UserId, decimal Amount, int OperateType = 1, int OrderId = 0)
  312. {
  313. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  314. if (account == null)
  315. {
  316. account = db.UserAccount.Add(new UserAccount()
  317. {
  318. Id = UserId,
  319. UserId = UserId,
  320. }).Entity;
  321. db.SaveChanges();
  322. }
  323. decimal BeforeTotalAmount = account.ValidAmount; //变更前总金额
  324. if(OperateType == 1)
  325. {
  326. account.TempAmount += Amount;
  327. account.ValidAmount += Amount;
  328. }
  329. else
  330. {
  331. account.TempAmount -= Amount;
  332. account.ValidAmount -= Amount;
  333. }
  334. decimal AfterTotalAmount = account.ValidAmount; //变更后总金额
  335. StoreHouseAmountRecord record = db.StoreHouseAmountRecord.Add(new StoreHouseAmountRecord()
  336. {
  337. CreateDate = DateTime.Now,
  338. UpdateDate = DateTime.Now,
  339. OperateType = OperateType,
  340. AmountType = 1,
  341. AfterAmount = AfterTotalAmount,
  342. BeforeAmount = BeforeTotalAmount,
  343. UseAmount = Amount,
  344. UserId = UserId,
  345. QueryCount = OrderId,
  346. }).Entity;
  347. db.SaveChanges();
  348. }
  349. private List<int> SpecialUsers10000()
  350. {
  351. List<int> ids = new List<int>();
  352. ids.Add(514);
  353. ids.Add(548);
  354. ids.Add(730);
  355. ids.Add(3596);
  356. ids.Add(11211);
  357. ids.Add(13185);
  358. ids.Add(24302);
  359. ids.Add(32630);
  360. ids.Add(33002);
  361. ids.Add(37887);
  362. ids.Add(40950);
  363. ids.Add(46284);
  364. ids.Add(59190);
  365. ids.Add(72099);
  366. ids.Add(120875);
  367. ids.Add(127884);
  368. return ids;
  369. }
  370. private List<int> SpecialUsers20000()
  371. {
  372. List<int> ids = new List<int>();
  373. ids.Add(8);
  374. ids.Add(415);
  375. ids.Add(527);
  376. ids.Add(532);
  377. ids.Add(553);
  378. ids.Add(558);
  379. ids.Add(1193);
  380. ids.Add(1195);
  381. ids.Add(1703);
  382. ids.Add(1797);
  383. ids.Add(1893);
  384. ids.Add(2275);
  385. ids.Add(2825);
  386. ids.Add(3015);
  387. ids.Add(3211);
  388. ids.Add(3492);
  389. ids.Add(4216);
  390. ids.Add(4328);
  391. ids.Add(4507);
  392. ids.Add(5774);
  393. ids.Add(6446);
  394. ids.Add(6538);
  395. ids.Add(6619);
  396. ids.Add(6751);
  397. ids.Add(10677);
  398. ids.Add(13650);
  399. ids.Add(15506);
  400. ids.Add(15600);
  401. ids.Add(15714);
  402. ids.Add(17767);
  403. ids.Add(18833);
  404. ids.Add(20667);
  405. ids.Add(21051);
  406. ids.Add(23099);
  407. ids.Add(23564);
  408. ids.Add(23883);
  409. ids.Add(23944);
  410. ids.Add(24528);
  411. ids.Add(26577);
  412. ids.Add(26875);
  413. ids.Add(27632);
  414. ids.Add(30869);
  415. ids.Add(32427);
  416. ids.Add(32458);
  417. ids.Add(32534);
  418. ids.Add(35808);
  419. ids.Add(37242);
  420. ids.Add(37556);
  421. ids.Add(40282);
  422. ids.Add(40886);
  423. ids.Add(41420);
  424. ids.Add(43735);
  425. ids.Add(44417);
  426. ids.Add(44697);
  427. ids.Add(46548);
  428. ids.Add(49927);
  429. ids.Add(50886);
  430. ids.Add(52449);
  431. ids.Add(52814);
  432. ids.Add(54784);
  433. ids.Add(58960);
  434. ids.Add(60371);
  435. ids.Add(60846);
  436. ids.Add(63010);
  437. ids.Add(68483);
  438. ids.Add(68705);
  439. ids.Add(70879);
  440. ids.Add(71019);
  441. ids.Add(71648);
  442. ids.Add(72029);
  443. ids.Add(74591);
  444. ids.Add(77107);
  445. ids.Add(77489);
  446. ids.Add(78313);
  447. ids.Add(80516);
  448. ids.Add(84627);
  449. ids.Add(88225);
  450. ids.Add(88579);
  451. ids.Add(89269);
  452. ids.Add(93049);
  453. ids.Add(93825);
  454. ids.Add(97771);
  455. ids.Add(99622);
  456. ids.Add(105286);
  457. ids.Add(106581);
  458. ids.Add(120563);
  459. ids.Add(120814);
  460. ids.Add(120844);
  461. ids.Add(120997);
  462. ids.Add(121082);
  463. ids.Add(121218);
  464. ids.Add(121465);
  465. ids.Add(121710);
  466. ids.Add(123354);
  467. ids.Add(123362);
  468. ids.Add(124423);
  469. ids.Add(124462);
  470. ids.Add(125184);
  471. ids.Add(125377);
  472. ids.Add(125578);
  473. ids.Add(125848);
  474. ids.Add(126613);
  475. ids.Add(126620);
  476. ids.Add(127561);
  477. ids.Add(128758);
  478. ids.Add(129380);
  479. ids.Add(129796);
  480. ids.Add(131010);
  481. ids.Add(132488);
  482. ids.Add(133216);
  483. ids.Add(133590);
  484. ids.Add(135440);
  485. return ids;
  486. }
  487. }