StoreApplyHelper.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. dt = OtherMySqlConn.dtable("select UseAmount from StoreMachineApply where UserId=" + UserId + " and Status=0");
  138. foreach(DataRow dr in dt.Rows)
  139. {
  140. AmountMore += decimal.Parse(function.CheckNum(dr["UseAmount"].ToString()));
  141. }
  142. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  143. if (account == null)
  144. {
  145. account = db.UserAccount.Add(new UserAccount()
  146. {
  147. Id = UserId,
  148. UserId = UserId,
  149. }).Entity;
  150. db.SaveChanges();
  151. }
  152. account.FixedAmount = Amount;
  153. account.ValidAmount = Amount + PromissAmount + account.TempAmount - AmountMore;
  154. function.WriteLog("UserId:" + UserId + ";FixedAmount:" + account.FixedAmount + ";AmountMore:" + AmountMore + ";ValidAmount:" + account.ValidAmount, "计算分仓申请机具额度日志");
  155. }
  156. db.SaveChanges();
  157. }
  158. }
  159. }
  160. catch (Exception ex)
  161. {
  162. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "计算分仓申请机具额度异常");
  163. }
  164. db.Dispose();
  165. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "计算分仓申请机具额度日志");
  166. Thread.Sleep(60000);
  167. }
  168. }
  169. public void StartEverTime()
  170. {
  171. Thread th = new Thread(StartEverTimeDo);
  172. th.IsBackground = true;
  173. th.Start();
  174. }
  175. private void StartEverTimeDo()
  176. {
  177. while (true)
  178. {
  179. WebCMSEntities db = new WebCMSEntities();
  180. try
  181. {
  182. string data = RedisDbconn.Instance.RPop<string>("StoreApplyQueue");
  183. if(!string.IsNullOrEmpty(data))
  184. {
  185. function.WriteLog("data:" + data, "分仓向总仓申请机具日志");
  186. JsonData jsonObj = JsonMapper.ToObject(data);
  187. if(jsonObj["Kind"].ToString() == "1") // 购买临时额度
  188. {
  189. int OrderId = int.Parse(jsonObj["Data"]["OrderId"].ToString());
  190. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId);
  191. if(order != null)
  192. {
  193. decimal TotalPrice = order.TotalPrice * 2;
  194. AddAmount2(db, 1, order.UserId, TotalPrice, order.PayMode, 1, order.Id);
  195. }
  196. }
  197. else if(jsonObj["Kind"].ToString() == "2") // 增减分仓临时额度
  198. {
  199. int UserId = int.Parse(jsonObj["Data"]["UserId"].ToString());
  200. decimal Amount = decimal.Parse(jsonObj["Data"]["Amount"].ToString());
  201. int OperateType = int.Parse(jsonObj["Data"]["OperateType"].ToString());
  202. AddAmount(db, 2, UserId, Amount, OperateType);
  203. }
  204. else if(jsonObj["Kind"].ToString() == "3") // 调低额度返回余额
  205. {
  206. int UserId = int.Parse(jsonObj["Data"]["UserId"].ToString());
  207. decimal Amount = decimal.Parse(jsonObj["Data"]["Amount"].ToString());
  208. int PayMode = int.Parse(jsonObj["Data"]["PayMode"].ToString());
  209. AddAmount2(db, 3, UserId, Amount, PayMode, 0);
  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. else if(jsonObj["Kind"].ToString() == "4") // 仓库发货,预发机申请
  244. {
  245. int StoreId = int.Parse(jsonObj["Data"]["StoreId"].ToString());
  246. string SnIds = jsonObj["Data"]["SnIds"].ToString();
  247. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId);
  248. if(store != null)
  249. {
  250. decimal Amount = 0;
  251. string[] SnIdList = SnIds.Split(',');
  252. foreach(string SnIdString in SnIdList)
  253. {
  254. int SnId = int.Parse(SnIdString);
  255. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == SnId) ?? new PosMachinesTwo();
  256. if(pos.BrandId == 1 || pos.BrandId == 2 || pos.BrandId == 4 || pos.BrandId == 6 || pos.BrandId == 7 || pos.BrandId == 8)
  257. {
  258. Amount += 200;
  259. }
  260. else if(pos.BrandId == 3 || pos.BrandId == 5 || pos.BrandId == 9)
  261. {
  262. Amount += 300;
  263. }
  264. }
  265. if(Amount > 0)
  266. {
  267. AddAmount(db, 4, store.UserId, Amount, 1);
  268. }
  269. }
  270. }
  271. else if(jsonObj["Kind"].ToString() == "5") // 后台仓库调拨
  272. {
  273. int StoreId = int.Parse(jsonObj["Data"]["StoreId"].ToString());
  274. int BrandId = int.Parse(jsonObj["Data"]["BrandId"].ToString());
  275. string OpStorrString = jsonObj["Data"]["OpStoreNum"].ToString();
  276. int OpType = OpStorrString.StartsWith("-") ? 0 : 1;
  277. int OpStoreNum = int.Parse(OpStorrString.Replace("-", ""));
  278. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId);
  279. if(store != null)
  280. {
  281. decimal Amount = 0;
  282. if(BrandId == 1 || BrandId == 2 || BrandId == 4 || BrandId == 6 || BrandId == 7 || BrandId == 8)
  283. {
  284. Amount += 200 * OpStoreNum;
  285. }
  286. else if(BrandId == 3 || BrandId == 5 || BrandId == 9)
  287. {
  288. Amount += 300 * OpStoreNum;
  289. }
  290. if(Amount > 0)
  291. {
  292. AddAmount(db, 5, store.UserId, Amount, OpType);
  293. }
  294. }
  295. }
  296. db.SaveChanges();
  297. }
  298. else
  299. {
  300. Thread.Sleep(5000);
  301. }
  302. }
  303. catch (Exception ex)
  304. {
  305. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "分仓向总仓申请机具线程异常");
  306. }
  307. db.Dispose();
  308. }
  309. }
  310. public void AddAmount(WebCMSEntities db, int Kind, int UserId, decimal Amount, int OperateType = 1, int OrderId = 0)
  311. {
  312. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  313. if (account == null)
  314. {
  315. account = db.UserAccount.Add(new UserAccount()
  316. {
  317. Id = UserId,
  318. UserId = UserId,
  319. }).Entity;
  320. db.SaveChanges();
  321. }
  322. decimal BeforeTotalAmount = account.ValidAmount; //变更前总金额
  323. if(OperateType == 1)
  324. {
  325. account.ValidAmount += Amount;
  326. }
  327. else
  328. {
  329. account.ValidAmount -= Amount;
  330. }
  331. decimal AfterTotalAmount = account.ValidAmount; //变更后总金额
  332. StoreHouseAmountRecord record = db.StoreHouseAmountRecord.Add(new StoreHouseAmountRecord()
  333. {
  334. CreateDate = DateTime.Now,
  335. UpdateDate = DateTime.Now,
  336. OperateType = OperateType,
  337. AmountType = 1,
  338. AfterAmount = AfterTotalAmount,
  339. BeforeAmount = BeforeTotalAmount,
  340. UseAmount = Amount,
  341. UserId = UserId,
  342. QueryCount = OrderId,
  343. Sort = Kind,
  344. }).Entity;
  345. db.SaveChanges();
  346. }
  347. public void AddAmount2(WebCMSEntities db, int Kind, int UserId, decimal Amount, int PayMode, int OperateType = 1, int OrderId = 0)
  348. {
  349. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  350. if (account == null)
  351. {
  352. account = db.UserAccount.Add(new UserAccount()
  353. {
  354. Id = UserId,
  355. UserId = UserId,
  356. }).Entity;
  357. db.SaveChanges();
  358. }
  359. decimal BeforeTotalAmount = account.ValidAmount; //变更前总金额
  360. if(OperateType == 1)
  361. {
  362. account.TempAmount += Amount;
  363. account.ValidAmount += Amount;
  364. }
  365. else
  366. {
  367. account.TempAmount -= Amount;
  368. account.ValidAmount -= Amount;
  369. }
  370. decimal AfterTotalAmount = account.ValidAmount; //变更后总金额
  371. StoreHouseAmountRecord record = db.StoreHouseAmountRecord.Add(new StoreHouseAmountRecord()
  372. {
  373. CreateDate = DateTime.Now,
  374. UpdateDate = DateTime.Now,
  375. OperateType = OperateType,
  376. AmountType = 1,
  377. AfterAmount = AfterTotalAmount,
  378. BeforeAmount = BeforeTotalAmount,
  379. UseAmount = Amount,
  380. UserId = UserId,
  381. QueryCount = OrderId,
  382. Sort = Kind,
  383. PayMode = PayMode,
  384. }).Entity;
  385. db.SaveChanges();
  386. }
  387. private List<int> SpecialUsers10000()
  388. {
  389. List<int> ids = new List<int>();
  390. ids.Add(13185);
  391. ids.Add(514);
  392. ids.Add(24302);
  393. ids.Add(548);
  394. ids.Add(37887);
  395. ids.Add(33002);
  396. ids.Add(730);
  397. ids.Add(40950);
  398. ids.Add(72099);
  399. ids.Add(6898);
  400. ids.Add(46284);
  401. ids.Add(127884);
  402. ids.Add(3596);
  403. ids.Add(32630);
  404. ids.Add(11211);
  405. return ids;
  406. }
  407. private List<int> SpecialUsers0()
  408. {
  409. List<int> ids = new List<int>();
  410. ids.Add(21135);
  411. ids.Add(598);
  412. ids.Add(109913);
  413. ids.Add(609);
  414. ids.Add(588);
  415. ids.Add(12107);
  416. ids.Add(7641);
  417. ids.Add(4317);
  418. ids.Add(560);
  419. ids.Add(120998);
  420. ids.Add(3905);
  421. ids.Add(959);
  422. ids.Add(2502);
  423. ids.Add(1001);
  424. ids.Add(68868);
  425. ids.Add(11718);
  426. ids.Add(15493);
  427. ids.Add(459);
  428. ids.Add(97952);
  429. ids.Add(10719);
  430. ids.Add(16453);
  431. ids.Add(1337);
  432. ids.Add(110198);
  433. ids.Add(582);
  434. ids.Add(89);
  435. ids.Add(9319);
  436. ids.Add(128525);
  437. ids.Add(1109);
  438. ids.Add(28538);
  439. ids.Add(2927);
  440. ids.Add(584);
  441. return ids;
  442. }
  443. }