StoreApplyHelper.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. DoSomething(db);
  41. }
  42. }
  43. }
  44. catch (Exception ex)
  45. {
  46. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "计算分仓申请机具额度异常");
  47. }
  48. db.Dispose();
  49. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "计算分仓申请机具额度日志");
  50. Thread.Sleep(60000);
  51. }
  52. }
  53. public void ResetStoreReserve()
  54. {
  55. Thread th = new Thread(ResetStoreReserveDo);
  56. th.IsBackground = true;
  57. th.Start();
  58. }
  59. private void ResetStoreReserveDo()
  60. {
  61. while (true)
  62. {
  63. WebCMSEntities db = new WebCMSEntities();
  64. try
  65. {
  66. string data = RedisDbconn.Instance.RPop<string>("ResetStoreReserveQueue");
  67. if(!string.IsNullOrEmpty(data))
  68. {
  69. int UserId = int.Parse(data);
  70. if(UserId > 0) DoSomething(db, UserId);
  71. }
  72. }
  73. catch(Exception ex)
  74. {
  75. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "重置分仓额度异常");
  76. }
  77. db.Dispose();
  78. }
  79. }
  80. private void DoSomething(WebCMSEntities db, int UId = 0)
  81. {
  82. Dictionary<int, decimal> dataDic = new Dictionary<int, decimal>();
  83. string pre = DateTime.Now.AddMonths(-2).ToString("yyyy-MM") + "-01 00:00:00";
  84. string start = DateTime.Now.AddMonths(-1).ToString("yyyy-MM") + "-01 00:00:00";
  85. string end = DateTime.Parse(start).AddMonths(1).ToString("yyyy-MM-dd HH:mm:ss");
  86. string condition = "";
  87. if(UId > 0)
  88. {
  89. condition = " and StoreId in (select Id from StoreHouse where UserId=" + UId + ")";
  90. }
  91. //上月出货额度
  92. DataTable dt = OtherMySqlConn.dtable("select StoreId,count(Id) from StoreStockChange where Id>=528358 and CreateDate>='" + start + "' and CreateDate<'" + end + "' and BrandId in (1,2,4,6,7,8) and TransType in (10,11,2) and StoreId>0" + condition + " group by StoreId");
  93. foreach(DataRow dr in dt.Rows)
  94. {
  95. int StoreId = int.Parse(function.CheckInt(dr["StoreId"].ToString()));
  96. int Count = int.Parse(function.CheckInt(dr[1].ToString()));
  97. decimal AmountMore = Count * 200;
  98. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
  99. if(!dataDic.ContainsKey(store.UserId))
  100. {
  101. dataDic.Add(store.UserId, AmountMore);
  102. }
  103. else
  104. {
  105. dataDic[store.UserId] += AmountMore;
  106. }
  107. }
  108. dt = OtherMySqlConn.dtable("select StoreId,count(Id) from StoreStockChange where Id>=528358 and CreateDate>='" + start + "' and CreateDate<'" + end + "' and BrandId in (3,5,9) and TransType in (10,11,2) and StoreId>0" + condition + " group by StoreId");
  109. foreach(DataRow dr in dt.Rows)
  110. {
  111. int StoreId = int.Parse(function.CheckInt(dr["StoreId"].ToString()));
  112. int Count = int.Parse(function.CheckInt(dr[1].ToString()));
  113. decimal AmountMore = Count * 300;
  114. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
  115. if(!dataDic.ContainsKey(store.UserId))
  116. {
  117. dataDic.Add(store.UserId, AmountMore);
  118. }
  119. else
  120. {
  121. dataDic[store.UserId] += AmountMore;
  122. }
  123. }
  124. //上月没出货的创库
  125. string ids = "0";
  126. foreach(int UserId in dataDic.Keys)
  127. {
  128. ids += "," + UserId;
  129. }
  130. DataTable dts = OtherMySqlConn.dtable("select distinct UserId from StoreHouse where UserId not in (" + ids + ") and Status>-1");
  131. foreach(DataRow dr in dts.Rows)
  132. {
  133. int UserId = int.Parse(function.CheckInt(dr[0].ToString()));
  134. if(!dataDic.ContainsKey(UserId))
  135. {
  136. dataDic.Add(UserId, 0);
  137. }
  138. }
  139. foreach(int UserId in dataDic.Keys)
  140. {
  141. decimal Amount = dataDic[UserId];
  142. //上月出货额度与保底额度之间取较高值,保底额度为押金的两倍
  143. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  144. if (account == null)
  145. {
  146. account = db.UserAccount.Add(new UserAccount()
  147. {
  148. Id = UserId,
  149. UserId = UserId,
  150. }).Entity;
  151. db.SaveChanges();
  152. }
  153. decimal StoreDeposit = account.StoreDeposit;
  154. if(Amount < StoreDeposit * 2)
  155. {
  156. Amount = StoreDeposit * 2;
  157. }
  158. //被担保额度
  159. decimal PromissAmount = 0;
  160. dt = OtherMySqlConn.dtable("select PromissAmount from StoreHouseAmountPromiss where ToUserId=" + UserId + " and Status=1");
  161. foreach(DataRow dr in dt.Rows)
  162. {
  163. PromissAmount += decimal.Parse(function.CheckNum(dr["PromissAmount"].ToString()));
  164. }
  165. decimal AmountMore = 0;
  166. //仓库中机具占用额度+小分仓机具占用额度
  167. 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");
  168. if(dtmore.Rows.Count > 0)
  169. {
  170. AmountMore += decimal.Parse(function.CheckNum(dtmore.Rows[0][0].ToString()));
  171. }
  172. 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");
  173. if(dtmore.Rows.Count > 0)
  174. {
  175. AmountMore += decimal.Parse(function.CheckNum(dtmore.Rows[0][0].ToString()));
  176. }
  177. //除开小分仓中带“授”标记机具占用额度
  178. dtmore = OtherMySqlConn.dtable("select count(Id)*200 from PreSendStockDetail pos where FromStoreId in (select Id from StoreHouse where UserId=" + UserId + ") and BrandId in (1,2,4,6,7,8) and AuthFlag=1 and ApplyFlag=0 and `Status`>-1 and `Status`<2");
  179. if(dtmore.Rows.Count > 0)
  180. {
  181. AmountMore -= decimal.Parse(function.CheckNum(dtmore.Rows[0][0].ToString()));
  182. }
  183. dtmore = OtherMySqlConn.dtable("select count(Id)*300 from PreSendStockDetail pos where FromStoreId in (select Id from StoreHouse where UserId=" + UserId + ") and BrandId in (3,5,9) and AuthFlag=1 and ApplyFlag=0 and `Status`>-1 and `Status`<2");
  184. if(dtmore.Rows.Count > 0)
  185. {
  186. AmountMore -= decimal.Parse(function.CheckNum(dtmore.Rows[0][0].ToString()));
  187. }
  188. //申请补货订单占用额度
  189. dt = OtherMySqlConn.dtable("select UseAmount from StoreMachineApply where UserId=" + UserId + " and Status=0");
  190. foreach(DataRow dr in dt.Rows)
  191. {
  192. AmountMore += decimal.Parse(function.CheckNum(dr["UseAmount"].ToString()));
  193. }
  194. account.FixedAmount = Amount;
  195. account.ValidAmount = Amount + PromissAmount + account.TempAmount - AmountMore;
  196. function.WriteLog("UserId:" + UserId + ";FixedAmount:" + account.FixedAmount + ";AmountMore:" + AmountMore + ";ValidAmount:" + account.ValidAmount, "计算分仓申请机具额度日志");
  197. }
  198. db.SaveChanges();
  199. }
  200. public void StartEverTime()
  201. {
  202. Thread th = new Thread(StartEverTimeDo);
  203. th.IsBackground = true;
  204. th.Start();
  205. }
  206. private void StartEverTimeDo()
  207. {
  208. while (true)
  209. {
  210. WebCMSEntities db = new WebCMSEntities();
  211. try
  212. {
  213. string data = RedisDbconn.Instance.RPop<string>("StoreApplyQueue");
  214. if(!string.IsNullOrEmpty(data))
  215. {
  216. function.WriteLog("data:" + data, "分仓向总仓申请机具日志");
  217. JsonData jsonObj = JsonMapper.ToObject(data);
  218. if(jsonObj["Kind"].ToString() == "1") // 购买临时额度
  219. {
  220. int OrderId = int.Parse(jsonObj["Data"]["OrderId"].ToString());
  221. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId);
  222. if(order != null)
  223. {
  224. decimal TotalPrice = order.TotalPrice * 2;
  225. AddAmount2(db, 1, order.UserId, TotalPrice, order.PayMode, 1, order.Id);
  226. }
  227. }
  228. else if(jsonObj["Kind"].ToString() == "2") // 增减分仓临时额度
  229. {
  230. int UserId = int.Parse(jsonObj["Data"]["UserId"].ToString());
  231. decimal Amount = decimal.Parse(jsonObj["Data"]["Amount"].ToString());
  232. int OperateType = int.Parse(jsonObj["Data"]["OperateType"].ToString());
  233. AddAmount(db, 2, UserId, Amount, OperateType);
  234. }
  235. else if(jsonObj["Kind"].ToString() == "3") // 调低额度返回余额
  236. {
  237. int UserId = int.Parse(jsonObj["Data"]["UserId"].ToString());
  238. decimal Amount = decimal.Parse(jsonObj["Data"]["Amount"].ToString());
  239. int PayMode = int.Parse(jsonObj["Data"]["PayMode"].ToString());
  240. AddAmount2(db, 3, UserId, Amount, PayMode, 0);
  241. if(PayMode != 1)
  242. {
  243. decimal BalanceAmount = Amount / 2;
  244. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  245. if (account == null)
  246. {
  247. account = db.UserAccount.Add(new UserAccount()
  248. {
  249. Id = UserId,
  250. UserId = UserId,
  251. }).Entity;
  252. db.SaveChanges();
  253. }
  254. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  255. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  256. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  257. account.BalanceAmount += BalanceAmount;
  258. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  259. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  260. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  261. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  262. {
  263. CreateDate = DateTime.Now,
  264. UpdateDate = DateTime.Now,
  265. UserId = UserId, //创客
  266. ChangeType = 119, //变动类型
  267. ChangeAmount = BalanceAmount, //变更金额
  268. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  269. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  270. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  271. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  272. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  273. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  274. }).Entity;
  275. }
  276. }
  277. else if(jsonObj["Kind"].ToString() == "4") // 仓库发货,预发机申请
  278. {
  279. int StoreId = int.Parse(jsonObj["Data"]["StoreId"].ToString());
  280. string SnIds = jsonObj["Data"]["SnIds"].ToString();
  281. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId);
  282. if(store != null)
  283. {
  284. decimal Amount = 0;
  285. string[] SnIdList = SnIds.Split(',');
  286. foreach(string SnIdString in SnIdList)
  287. {
  288. int SnId = int.Parse(SnIdString);
  289. PreSendStockDetail prepos = db.PreSendStockDetail.FirstOrDefault(m => m.SnId == SnId && m.Status == 1 && m.ApplyFlag == 1) ?? new PreSendStockDetail();
  290. if(prepos.AuthFlag == 0)
  291. {
  292. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == SnId) ?? new PosMachinesTwo();
  293. if(pos.BrandId == 1 || pos.BrandId == 2 || pos.BrandId == 4 || pos.BrandId == 6 || pos.BrandId == 7 || pos.BrandId == 8)
  294. {
  295. Amount += 200;
  296. }
  297. else if(pos.BrandId == 3 || pos.BrandId == 5 || pos.BrandId == 9)
  298. {
  299. Amount += 300;
  300. }
  301. }
  302. }
  303. if(Amount > 0)
  304. {
  305. AddAmount(db, 4, store.UserId, Amount, 1);
  306. }
  307. }
  308. }
  309. else if(jsonObj["Kind"].ToString() == "5") // 后台仓库调拨
  310. {
  311. int StoreId = int.Parse(jsonObj["Data"]["StoreId"].ToString());
  312. int BrandId = int.Parse(jsonObj["Data"]["BrandId"].ToString());
  313. string OpStorrString = jsonObj["Data"]["OpStoreNum"].ToString();
  314. int OpType = OpStorrString.StartsWith("-") ? 0 : 1;
  315. int OpStoreNum = int.Parse(OpStorrString.Replace("-", ""));
  316. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId);
  317. if(store != null)
  318. {
  319. decimal Amount = 0;
  320. if(BrandId == 1 || BrandId == 2 || BrandId == 4 || BrandId == 6 || BrandId == 7 || BrandId == 8)
  321. {
  322. Amount += 200 * OpStoreNum;
  323. }
  324. else if(BrandId == 3 || BrandId == 5 || BrandId == 9)
  325. {
  326. Amount += 300 * OpStoreNum;
  327. }
  328. if(Amount > 0)
  329. {
  330. AddAmount(db, 5, store.UserId, Amount, OpType);
  331. }
  332. }
  333. }
  334. db.SaveChanges();
  335. }
  336. else
  337. {
  338. Thread.Sleep(5000);
  339. }
  340. }
  341. catch (Exception ex)
  342. {
  343. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "分仓向总仓申请机具线程异常");
  344. }
  345. db.Dispose();
  346. }
  347. }
  348. public void AddAmount(WebCMSEntities db, int Kind, int UserId, decimal Amount, int OperateType = 1, int OrderId = 0)
  349. {
  350. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  351. if (account == null)
  352. {
  353. account = db.UserAccount.Add(new UserAccount()
  354. {
  355. Id = UserId,
  356. UserId = UserId,
  357. }).Entity;
  358. db.SaveChanges();
  359. }
  360. decimal BeforeTotalAmount = account.ValidAmount; //变更前总金额
  361. if(OperateType == 1)
  362. {
  363. account.ValidAmount += Amount;
  364. }
  365. else
  366. {
  367. account.ValidAmount -= Amount;
  368. }
  369. decimal AfterTotalAmount = account.ValidAmount; //变更后总金额
  370. StoreHouseAmountRecord record = db.StoreHouseAmountRecord.Add(new StoreHouseAmountRecord()
  371. {
  372. CreateDate = DateTime.Now,
  373. UpdateDate = DateTime.Now,
  374. OperateType = OperateType,
  375. AmountType = 1,
  376. AfterAmount = AfterTotalAmount,
  377. BeforeAmount = BeforeTotalAmount,
  378. UseAmount = Amount,
  379. UserId = UserId,
  380. QueryCount = OrderId,
  381. Sort = Kind,
  382. }).Entity;
  383. db.SaveChanges();
  384. }
  385. public void AddAmount2(WebCMSEntities db, int Kind, int UserId, decimal Amount, int PayMode, int OperateType = 1, int OrderId = 0)
  386. {
  387. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  388. if (account == null)
  389. {
  390. account = db.UserAccount.Add(new UserAccount()
  391. {
  392. Id = UserId,
  393. UserId = UserId,
  394. }).Entity;
  395. db.SaveChanges();
  396. }
  397. decimal BeforeTotalAmount = account.ValidAmount; //变更前总金额
  398. if(OperateType == 1)
  399. {
  400. if(PayMode == 3)
  401. {
  402. account.TempAmountForBalance += Amount;
  403. }
  404. else
  405. {
  406. account.TempAmount += Amount;
  407. }
  408. account.ValidAmount += Amount;
  409. }
  410. else
  411. {
  412. if(PayMode == 3)
  413. {
  414. account.TempAmountForBalance -= Amount;
  415. }
  416. else
  417. {
  418. account.TempAmount -= Amount;
  419. }
  420. account.ValidAmount -= Amount;
  421. }
  422. decimal AfterTotalAmount = account.ValidAmount; //变更后总金额
  423. StoreHouseAmountRecord record = db.StoreHouseAmountRecord.Add(new StoreHouseAmountRecord()
  424. {
  425. CreateDate = DateTime.Now,
  426. UpdateDate = DateTime.Now,
  427. OperateType = OperateType,
  428. AmountType = 1,
  429. AfterAmount = AfterTotalAmount,
  430. BeforeAmount = BeforeTotalAmount,
  431. UseAmount = Amount,
  432. UserId = UserId,
  433. QueryCount = OrderId,
  434. Sort = Kind,
  435. PayMode = PayMode,
  436. }).Entity;
  437. db.SaveChanges();
  438. }
  439. private List<int> SpecialUsers10000()
  440. {
  441. List<int> ids = new List<int>();
  442. ids.Add(13185);
  443. ids.Add(514);
  444. ids.Add(24302);
  445. ids.Add(548);
  446. ids.Add(37887);
  447. ids.Add(33002);
  448. ids.Add(730);
  449. ids.Add(40950);
  450. ids.Add(72099);
  451. ids.Add(6898);
  452. ids.Add(46284);
  453. ids.Add(127884);
  454. ids.Add(3596);
  455. ids.Add(32630);
  456. ids.Add(11211);
  457. return ids;
  458. }
  459. private List<int> SpecialUsers0()
  460. {
  461. List<int> ids = new List<int>();
  462. ids.Add(21135);
  463. ids.Add(598);
  464. ids.Add(109913);
  465. ids.Add(609);
  466. ids.Add(588);
  467. ids.Add(12107);
  468. ids.Add(7641);
  469. ids.Add(4317);
  470. ids.Add(560);
  471. ids.Add(120998);
  472. ids.Add(3905);
  473. ids.Add(959);
  474. ids.Add(2502);
  475. ids.Add(1001);
  476. ids.Add(68868);
  477. ids.Add(11718);
  478. ids.Add(15493);
  479. ids.Add(459);
  480. ids.Add(97952);
  481. ids.Add(10719);
  482. ids.Add(16453);
  483. ids.Add(1337);
  484. ids.Add(110198);
  485. ids.Add(582);
  486. ids.Add(89);
  487. ids.Add(9319);
  488. ids.Add(128525);
  489. ids.Add(1109);
  490. ids.Add(28538);
  491. ids.Add(2927);
  492. ids.Add(584);
  493. ids.Add(6659);
  494. return ids;
  495. }
  496. }