StoreApplyHelper.cs 20 KB

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