StoreApplyHelper.cs 13 KB

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