StoreApplyHelper.cs 22 KB

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