StoreApplyHelper.cs 16 KB

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