StoreApplyHelper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. function.WriteLog("1", "分仓向总仓申请机具日志");
  116. int OrderId = int.Parse(jsonObj["Data"]["OrderId"].ToString());
  117. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId);
  118. if(order != null)
  119. {
  120. function.WriteLog("2", "分仓向总仓申请机具日志");
  121. decimal TotalPrice = order.TotalPrice * 2;
  122. AddAmount(db, order.UserId, TotalPrice, 1, order.Id);
  123. }
  124. }
  125. else if(jsonObj["Kind"].ToString() == "2") // 增减分仓临时额度
  126. {
  127. int UserId = int.Parse(jsonObj["Data"]["UserId"].ToString());
  128. int Amount = int.Parse(jsonObj["Data"]["Amount"].ToString());
  129. int OperateType = int.Parse(jsonObj["Data"]["OperateType"].ToString());
  130. AddAmount(db, UserId, Amount, OperateType);
  131. }
  132. else if(jsonObj["Kind"].ToString() == "3") // 调低额度返回余额
  133. {
  134. int ApplyId = int.Parse(jsonObj["Data"]["ApplyId"].ToString());
  135. StoreHouseAmountRecord record = db.StoreHouseAmountRecord.FirstOrDefault(m => m.Id == ApplyId);
  136. if(record != null)
  137. {
  138. int GetUserId = record.UserId;
  139. decimal Amount = record.UseAmount / 2;
  140. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == GetUserId);
  141. if (account == null)
  142. {
  143. account = db.UserAccount.Add(new UserAccount()
  144. {
  145. Id = GetUserId,
  146. UserId = GetUserId,
  147. }).Entity;
  148. db.SaveChanges();
  149. }
  150. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  151. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  152. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  153. account.BalanceAmount += Amount;
  154. account.TotalAmount += Amount;
  155. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  156. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  157. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  158. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  159. {
  160. CreateDate = DateTime.Now,
  161. UpdateDate = DateTime.Now,
  162. UserId = GetUserId, //创客
  163. ChangeType = 119, //变动类型
  164. ChangeAmount = Amount, //变更金额
  165. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  166. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  167. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  168. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  169. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  170. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  171. }).Entity;
  172. }
  173. }
  174. else if(jsonObj["Kind"].ToString() == "4")
  175. {
  176. int StoreId = int.Parse(jsonObj["Data"]["StoreId"].ToString());
  177. string SnIds = jsonObj["Data"]["SnIds"].ToString();
  178. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId);
  179. if(store != null)
  180. {
  181. decimal Amount = 0;
  182. string[] SnIdList = SnIds.Split(',');
  183. foreach(string SnIdString in SnIdList)
  184. {
  185. int SnId = int.Parse(SnIdString);
  186. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == SnId) ?? new PosMachinesTwo();
  187. if(pos.BrandId == 1 || pos.BrandId == 2 || pos.BrandId == 4 || pos.BrandId == 6 || pos.BrandId == 7 || pos.BrandId == 8)
  188. {
  189. Amount += 200;
  190. }
  191. else if(pos.BrandId == 3 || pos.BrandId == 5 || pos.BrandId == 9)
  192. {
  193. Amount += 300;
  194. }
  195. }
  196. if(Amount > 0)
  197. {
  198. AddAmount(db, store.UserId, Amount, 1);
  199. }
  200. }
  201. }
  202. else if(jsonObj["Kind"].ToString() == "5")
  203. {
  204. int StoreId = int.Parse(jsonObj["Data"]["StoreId"].ToString());
  205. int BrandId = int.Parse(jsonObj["Data"]["BrandId"].ToString());
  206. int OpStoreNum = int.Parse(jsonObj["Data"]["OpStoreNum"].ToString());
  207. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId);
  208. if(store != null)
  209. {
  210. decimal Amount = 0;
  211. if(BrandId == 1 || BrandId == 2 || BrandId == 4 || BrandId == 6 || BrandId == 7 || BrandId == 8)
  212. {
  213. Amount += 200;
  214. }
  215. else if(BrandId == 3 || BrandId == 5 || BrandId == 9)
  216. {
  217. Amount += 300;
  218. }
  219. if(Amount > 0)
  220. {
  221. AddAmount(db, store.UserId, Amount, 1);
  222. }
  223. }
  224. }
  225. db.SaveChanges();
  226. }
  227. }
  228. catch (Exception ex)
  229. {
  230. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "分仓向总仓申请机具线程异常");
  231. }
  232. db.Dispose();
  233. }
  234. }
  235. public void AddAmount(WebCMSEntities db, int UserId, decimal Amount, int OperateType = 1, int OrderId = 0)
  236. {
  237. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  238. if (account == null)
  239. {
  240. account = db.UserAccount.Add(new UserAccount()
  241. {
  242. Id = UserId,
  243. UserId = UserId,
  244. }).Entity;
  245. db.SaveChanges();
  246. }
  247. decimal BeforeTotalAmount = account.TempAmount; //变更前总金额
  248. if(OperateType == 1)
  249. {
  250. account.TempAmount += Amount;
  251. account.ValidAmount += Amount;
  252. }
  253. else
  254. {
  255. account.TempAmount -= Amount;
  256. account.ValidAmount -= Amount;
  257. }
  258. decimal AfterTotalAmount = account.TempAmount; //变更后总金额
  259. StoreHouseAmountRecord record = db.StoreHouseAmountRecord.Add(new StoreHouseAmountRecord()
  260. {
  261. CreateDate = DateTime.Now,
  262. UpdateDate = DateTime.Now,
  263. OperateType = OperateType,
  264. AmountType = 1,
  265. AfterAmount = AfterTotalAmount,
  266. BeforeAmount = BeforeTotalAmount,
  267. UseAmount = Amount,
  268. UserId = UserId,
  269. QueryCount = OrderId,
  270. }).Entity;
  271. db.SaveChanges();
  272. }
  273. }