StoreApplyHelper.cs 22 KB

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