StoreApplyHelper.cs 17 KB

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