StoreApplyHelper.cs 22 KB

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