StoreApplyHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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
  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. }
  187. }
  188. }
  189. db.SaveChanges();
  190. }
  191. }
  192. catch (Exception ex)
  193. {
  194. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "分仓向总仓申请机具线程异常");
  195. }
  196. db.Dispose();
  197. }
  198. }
  199. public void AddAmount(WebCMSEntities db, int UserId, decimal Amount, int OperateType = 1, int OrderId = 0)
  200. {
  201. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  202. if (account == null)
  203. {
  204. account = db.UserAccount.Add(new UserAccount()
  205. {
  206. Id = UserId,
  207. UserId = UserId,
  208. }).Entity;
  209. db.SaveChanges();
  210. }
  211. decimal BeforeTotalAmount = account.TempAmount; //变更前总金额
  212. if(OperateType == 1)
  213. {
  214. account.TempAmount += Amount;
  215. account.ValidAmount += Amount;
  216. }
  217. else
  218. {
  219. account.TempAmount -= Amount;
  220. account.ValidAmount -= Amount;
  221. }
  222. decimal AfterTotalAmount = account.TempAmount; //变更后总金额
  223. StoreHouseAmountRecord record = db.StoreHouseAmountRecord.Add(new StoreHouseAmountRecord()
  224. {
  225. CreateDate = DateTime.Now,
  226. UpdateDate = DateTime.Now,
  227. OperateType = OperateType,
  228. AmountType = 1,
  229. AfterAmount = AfterTotalAmount,
  230. BeforeAmount = BeforeTotalAmount,
  231. UseAmount = Amount,
  232. UserId = UserId,
  233. QueryCount = OrderId,
  234. }).Entity;
  235. db.SaveChanges();
  236. }
  237. }