StoreApplyHelper.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. private void DoWorks()
  24. {
  25. while (true)
  26. {
  27. WebCMSEntities db = new WebCMSEntities();
  28. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
  29. try
  30. {
  31. if(DateTime.Now.Day == 1 && DateTime.Now.Hour > 0 && DateTime.Now.Hour < 3)
  32. {
  33. string check = function.ReadInstance("/StoreApply/" + DateTime.Now.ToString("yyyyMM") + ".txt");
  34. if(string.IsNullOrEmpty(check))
  35. {
  36. function.WritePage("/StoreApply/", DateTime.Now.ToString("yyyyMM") + ".txt", DateTime.Now.ToString());
  37. Dictionary<int, decimal> dataDic = new Dictionary<int, decimal>();
  38. Dictionary<int, decimal> dataDic1 = new Dictionary<int, decimal>();
  39. Dictionary<int, decimal> dataDic2 = new Dictionary<int, decimal>();
  40. string pre = DateTime.Now.AddMonths(-2).ToString("yyyy-MM") + "-01 00:00:00";
  41. string start = DateTime.Now.AddMonths(-1).ToString("yyyy-MM") + "-01 00:00:00";
  42. string end = DateTime.Parse(start).AddMonths(1).ToString("yyyy-MM-dd HH:mm:ss");
  43. 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)");
  44. foreach(DataRow dr in dts.Rows)
  45. {
  46. int UserId = int.Parse(function.CheckInt(dr["UserId"].ToString()));
  47. if(!dataDic.ContainsKey(UserId))
  48. {
  49. dataDic.Add(UserId, 20000);
  50. }
  51. else
  52. {
  53. dataDic[UserId] += 20000;
  54. }
  55. }
  56. 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");
  57. foreach(DataRow dr in dt.Rows)
  58. {
  59. int StoreId = int.Parse(function.CheckInt(dr["StoreId"].ToString()));
  60. int Count = int.Parse(function.CheckInt(dr[1].ToString()));
  61. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
  62. if(!dataDic1.ContainsKey(store.UserId))
  63. {
  64. dataDic1.Add(store.UserId, Count * 200);
  65. }
  66. else
  67. {
  68. dataDic1[store.UserId] += Count * 200;
  69. }
  70. }
  71. 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");
  72. foreach(DataRow dr in dt.Rows)
  73. {
  74. int StoreId = int.Parse(function.CheckInt(dr["StoreId"].ToString()));
  75. int Count = int.Parse(function.CheckInt(dr[1].ToString()));
  76. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
  77. if(!dataDic2.ContainsKey(store.UserId))
  78. {
  79. dataDic2.Add(store.UserId, Count * 300);
  80. }
  81. else
  82. {
  83. dataDic2[store.UserId] += Count * 300;
  84. }
  85. }
  86. foreach(int UserId in dataDic.Keys)
  87. {
  88. decimal Amount = dataDic[UserId];
  89. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  90. if (account == null)
  91. {
  92. account = db.UserAccount.Add(new UserAccount()
  93. {
  94. Id = UserId,
  95. UserId = UserId,
  96. }).Entity;
  97. db.SaveChanges();
  98. }
  99. account.FixedAmount = Amount;
  100. account.ValidAmount = Amount + account.TempAmount;
  101. function.WriteLog("UserId:" + UserId + ";Amount:" + Amount, "计算分仓申请机具额度日志");
  102. }
  103. foreach(int UserId in dataDic1.Keys)
  104. {
  105. decimal Amount = dataDic1[UserId];
  106. decimal AmountMore = 0;
  107. // 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");
  108. // if(dtmore.Rows.Count > 0)
  109. // {
  110. // AmountMore = decimal.Parse(function.CheckNum(dtmore.Rows[0][0].ToString()));
  111. // }
  112. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  113. if (account == null)
  114. {
  115. account = db.UserAccount.Add(new UserAccount()
  116. {
  117. Id = UserId,
  118. UserId = UserId,
  119. }).Entity;
  120. db.SaveChanges();
  121. }
  122. account.FixedAmount = Amount;
  123. account.ValidAmount = Amount + account.TempAmount - AmountMore;
  124. function.WriteLog("UserId:" + UserId + ";Amount:" + Amount, "计算分仓申请机具额度日志");
  125. }
  126. foreach(int UserId in dataDic2.Keys)
  127. {
  128. decimal Amount = dataDic2[UserId];
  129. decimal AmountMore = 0;
  130. // DataTable 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");
  131. // if(dtmore.Rows.Count > 0)
  132. // {
  133. // AmountMore = decimal.Parse(function.CheckNum(dtmore.Rows[0][0].ToString()));
  134. // }
  135. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  136. if (account == null)
  137. {
  138. account = db.UserAccount.Add(new UserAccount()
  139. {
  140. Id = UserId,
  141. UserId = UserId,
  142. }).Entity;
  143. db.SaveChanges();
  144. }
  145. account.FixedAmount = Amount;
  146. account.ValidAmount = Amount + account.TempAmount - AmountMore;
  147. function.WriteLog("UserId:" + UserId + ";Amount:" + Amount, "计算分仓申请机具额度日志");
  148. }
  149. db.SaveChanges();
  150. }
  151. }
  152. }
  153. catch (Exception ex)
  154. {
  155. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "计算分仓申请机具额度异常");
  156. }
  157. db.Dispose();
  158. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "计算分仓申请机具额度日志");
  159. Thread.Sleep(60000);
  160. }
  161. }
  162. public void StartEverTime()
  163. {
  164. Thread th = new Thread(StartEverTimeDo);
  165. th.IsBackground = true;
  166. th.Start();
  167. }
  168. private void StartEverTimeDo()
  169. {
  170. while (true)
  171. {
  172. WebCMSEntities db = new WebCMSEntities();
  173. try
  174. {
  175. string data = RedisDbconn.Instance.RPop<string>("StoreApplyQueue");
  176. if(!string.IsNullOrEmpty(data))
  177. {
  178. function.WriteLog("data:" + data, "分仓向总仓申请机具日志");
  179. JsonData jsonObj = JsonMapper.ToObject(data);
  180. if(jsonObj["Kind"].ToString() == "1") // 购买临时额度
  181. {
  182. int OrderId = int.Parse(jsonObj["Data"]["OrderId"].ToString());
  183. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId);
  184. if(order != null)
  185. {
  186. decimal TotalPrice = order.TotalPrice * 2;
  187. AddAmount2(db, order.UserId, TotalPrice, 1, order.Id);
  188. }
  189. }
  190. else if(jsonObj["Kind"].ToString() == "2") // 增减分仓临时额度
  191. {
  192. int UserId = int.Parse(jsonObj["Data"]["UserId"].ToString());
  193. decimal Amount = decimal.Parse(jsonObj["Data"]["Amount"].ToString());
  194. int OperateType = int.Parse(jsonObj["Data"]["OperateType"].ToString());
  195. AddAmount(db, UserId, Amount, OperateType);
  196. }
  197. else if(jsonObj["Kind"].ToString() == "3") // 调低额度返回余额
  198. {
  199. int UserId = int.Parse(jsonObj["Data"]["UserId"].ToString());
  200. decimal Amount = decimal.Parse(jsonObj["Data"]["Amount"].ToString());
  201. AddAmount2(db, UserId, Amount, 0);
  202. decimal BalanceAmount = Amount / 2;
  203. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  204. if (account == null)
  205. {
  206. account = db.UserAccount.Add(new UserAccount()
  207. {
  208. Id = UserId,
  209. UserId = UserId,
  210. }).Entity;
  211. db.SaveChanges();
  212. }
  213. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  214. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  215. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  216. account.BalanceAmount += BalanceAmount;
  217. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  218. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  219. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  220. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  221. {
  222. CreateDate = DateTime.Now,
  223. UpdateDate = DateTime.Now,
  224. UserId = UserId, //创客
  225. ChangeType = 119, //变动类型
  226. ChangeAmount = BalanceAmount, //变更金额
  227. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  228. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  229. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  230. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  231. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  232. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  233. }).Entity;
  234. }
  235. else if(jsonObj["Kind"].ToString() == "4") // 仓库发货,预发机申请
  236. {
  237. int StoreId = int.Parse(jsonObj["Data"]["StoreId"].ToString());
  238. string SnIds = jsonObj["Data"]["SnIds"].ToString();
  239. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId);
  240. if(store != null)
  241. {
  242. decimal Amount = 0;
  243. string[] SnIdList = SnIds.Split(',');
  244. foreach(string SnIdString in SnIdList)
  245. {
  246. int SnId = int.Parse(SnIdString);
  247. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == SnId) ?? new PosMachinesTwo();
  248. if(pos.BrandId == 1 || pos.BrandId == 2 || pos.BrandId == 4 || pos.BrandId == 6 || pos.BrandId == 7 || pos.BrandId == 8)
  249. {
  250. Amount += 200;
  251. }
  252. else if(pos.BrandId == 3 || pos.BrandId == 5 || pos.BrandId == 9)
  253. {
  254. Amount += 300;
  255. }
  256. }
  257. if(Amount > 0)
  258. {
  259. AddAmount(db, store.UserId, Amount, 1);
  260. }
  261. }
  262. }
  263. else if(jsonObj["Kind"].ToString() == "5") // 后台仓库调拨
  264. {
  265. int StoreId = int.Parse(jsonObj["Data"]["StoreId"].ToString());
  266. int BrandId = int.Parse(jsonObj["Data"]["BrandId"].ToString());
  267. int OpStoreNum = int.Parse(jsonObj["Data"]["OpStoreNum"].ToString());
  268. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId);
  269. if(store != null)
  270. {
  271. decimal Amount = 0;
  272. if(BrandId == 1 || BrandId == 2 || BrandId == 4 || BrandId == 6 || BrandId == 7 || BrandId == 8)
  273. {
  274. Amount += 200;
  275. }
  276. else if(BrandId == 3 || BrandId == 5 || BrandId == 9)
  277. {
  278. Amount += 300;
  279. }
  280. if(Amount > 0)
  281. {
  282. AddAmount(db, store.UserId, Amount, 1);
  283. }
  284. }
  285. }
  286. db.SaveChanges();
  287. }
  288. else
  289. {
  290. Thread.Sleep(5000);
  291. }
  292. }
  293. catch (Exception ex)
  294. {
  295. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "分仓向总仓申请机具线程异常");
  296. }
  297. db.Dispose();
  298. }
  299. }
  300. public void AddAmount(WebCMSEntities db, int UserId, decimal Amount, int OperateType = 1, int OrderId = 0)
  301. {
  302. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  303. if (account == null)
  304. {
  305. account = db.UserAccount.Add(new UserAccount()
  306. {
  307. Id = UserId,
  308. UserId = UserId,
  309. }).Entity;
  310. db.SaveChanges();
  311. }
  312. decimal BeforeTotalAmount = account.ValidAmount; //变更前总金额
  313. if(OperateType == 1)
  314. {
  315. account.ValidAmount += Amount;
  316. }
  317. else
  318. {
  319. account.ValidAmount -= Amount;
  320. }
  321. decimal AfterTotalAmount = account.ValidAmount; //变更后总金额
  322. StoreHouseAmountRecord record = db.StoreHouseAmountRecord.Add(new StoreHouseAmountRecord()
  323. {
  324. CreateDate = DateTime.Now,
  325. UpdateDate = DateTime.Now,
  326. OperateType = OperateType,
  327. AmountType = 1,
  328. AfterAmount = AfterTotalAmount,
  329. BeforeAmount = BeforeTotalAmount,
  330. UseAmount = Amount,
  331. UserId = UserId,
  332. QueryCount = OrderId,
  333. }).Entity;
  334. db.SaveChanges();
  335. }
  336. public void AddAmount2(WebCMSEntities db, int UserId, decimal Amount, int OperateType = 1, int OrderId = 0)
  337. {
  338. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  339. if (account == null)
  340. {
  341. account = db.UserAccount.Add(new UserAccount()
  342. {
  343. Id = UserId,
  344. UserId = UserId,
  345. }).Entity;
  346. db.SaveChanges();
  347. }
  348. decimal BeforeTotalAmount = account.ValidAmount; //变更前总金额
  349. if(OperateType == 1)
  350. {
  351. account.TempAmount += Amount;
  352. account.ValidAmount += Amount;
  353. }
  354. else
  355. {
  356. account.TempAmount -= Amount;
  357. account.ValidAmount -= Amount;
  358. }
  359. decimal AfterTotalAmount = account.ValidAmount; //变更后总金额
  360. StoreHouseAmountRecord record = db.StoreHouseAmountRecord.Add(new StoreHouseAmountRecord()
  361. {
  362. CreateDate = DateTime.Now,
  363. UpdateDate = DateTime.Now,
  364. OperateType = OperateType,
  365. AmountType = 1,
  366. AfterAmount = AfterTotalAmount,
  367. BeforeAmount = BeforeTotalAmount,
  368. UseAmount = Amount,
  369. UserId = UserId,
  370. QueryCount = OrderId,
  371. }).Entity;
  372. db.SaveChanges();
  373. }
  374. }