StoreApplyHelper.cs 14 KB

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