StoreApplyHelper.cs 22 KB

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