StoreApplyHelper.cs 22 KB

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