ResetSmallStoreHelper.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 ResetSmallStoreHelper
  11. {
  12. public readonly static ResetSmallStoreHelper Instance = new ResetSmallStoreHelper();
  13. private ResetSmallStoreHelper()
  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. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "计算小分仓额度日志");
  28. WebCMSEntities db = new WebCMSEntities();
  29. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
  30. try
  31. {
  32. if(DateTime.Now.Day == 1 && DateTime.Now.Hour > 0 && DateTime.Now.Hour < 3)
  33. {
  34. string check = function.ReadInstance("/ResetSmallStore/" + DateTime.Now.ToString("yyyyMM") + ".txt");
  35. if(string.IsNullOrEmpty(check))
  36. {
  37. function.WritePage("/ResetSmallStore/", DateTime.Now.ToString("yyyyMM") + ".txt", DateTime.Now.ToString());
  38. string Month = DateTime.Now.ToString("yyyyMM");
  39. DataTable dt = OtherMySqlConn.dtable("select UserId,sum(ProfitAmount) from ProfitRecord where SeoTitle='" + Month + "' group by UserId");
  40. foreach(DataRow dr in dt.Rows)
  41. {
  42. int UserId = int.Parse(function.CheckInt(dr["UserId"].ToString()));
  43. decimal ProfitAmount = decimal.Parse(function.CheckNum(dr[1].ToString()));
  44. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  45. if (account == null)
  46. {
  47. account = db.UserAccount.Add(new UserAccount()
  48. {
  49. Id = UserId,
  50. UserId = UserId,
  51. }).Entity;
  52. db.SaveChanges();
  53. }
  54. account.ThisMonthPreAmount = ProfitAmount;
  55. account.ValidPreAmount = ProfitAmount;
  56. }
  57. db.SaveChanges();
  58. }
  59. }
  60. }
  61. catch (Exception ex)
  62. {
  63. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "计算小分仓额度异常");
  64. }
  65. db.Dispose();
  66. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "计算小分仓额度日志");
  67. Thread.Sleep(60000);
  68. }
  69. }
  70. public void StartEverTime()
  71. {
  72. Thread th = new Thread(StartEverTimeDo);
  73. th.IsBackground = true;
  74. th.Start();
  75. }
  76. private void StartEverTimeDo()
  77. {
  78. while (true)
  79. {
  80. WebCMSEntities db = new WebCMSEntities();
  81. try
  82. {
  83. string data = RedisDbconn.Instance.RPop<string>("StoreApplyQueue");
  84. if(!string.IsNullOrEmpty(data))
  85. {
  86. function.WriteLog("data:" + data, "分仓向总仓申请机具日志");
  87. JsonData jsonObj = JsonMapper.ToObject(data);
  88. if(jsonObj["Kind"].ToString() == "1") // 购买临时额度
  89. {
  90. int OrderId = int.Parse(jsonObj["Data"]["OrderId"].ToString());
  91. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId);
  92. if(order != null)
  93. {
  94. decimal TotalPrice = order.TotalPrice * 2;
  95. AddAmount2(db, 1, order.UserId, TotalPrice, 1, order.Id);
  96. }
  97. }
  98. else if(jsonObj["Kind"].ToString() == "2") // 增减分仓临时额度
  99. {
  100. int UserId = int.Parse(jsonObj["Data"]["UserId"].ToString());
  101. decimal Amount = decimal.Parse(jsonObj["Data"]["Amount"].ToString());
  102. int OperateType = int.Parse(jsonObj["Data"]["OperateType"].ToString());
  103. AddAmount(db, 2, UserId, Amount, OperateType);
  104. }
  105. else if(jsonObj["Kind"].ToString() == "3") // 调低额度返回余额
  106. {
  107. int UserId = int.Parse(jsonObj["Data"]["UserId"].ToString());
  108. decimal Amount = decimal.Parse(jsonObj["Data"]["Amount"].ToString());
  109. AddAmount2(db, 3, UserId, Amount, 0);
  110. decimal BalanceAmount = Amount / 2;
  111. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  112. if (account == null)
  113. {
  114. account = db.UserAccount.Add(new UserAccount()
  115. {
  116. Id = UserId,
  117. UserId = UserId,
  118. }).Entity;
  119. db.SaveChanges();
  120. }
  121. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  122. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  123. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  124. account.BalanceAmount += BalanceAmount;
  125. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  126. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  127. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  128. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  129. {
  130. CreateDate = DateTime.Now,
  131. UpdateDate = DateTime.Now,
  132. UserId = UserId, //创客
  133. ChangeType = 119, //变动类型
  134. ChangeAmount = BalanceAmount, //变更金额
  135. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  136. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  137. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  138. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  139. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  140. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  141. }).Entity;
  142. }
  143. else if(jsonObj["Kind"].ToString() == "4") // 仓库发货,预发机申请
  144. {
  145. int StoreId = int.Parse(jsonObj["Data"]["StoreId"].ToString());
  146. string SnIds = jsonObj["Data"]["SnIds"].ToString();
  147. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId);
  148. if(store != null)
  149. {
  150. decimal Amount = 0;
  151. string[] SnIdList = SnIds.Split(',');
  152. foreach(string SnIdString in SnIdList)
  153. {
  154. int SnId = int.Parse(SnIdString);
  155. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == SnId) ?? new PosMachinesTwo();
  156. if(pos.BrandId == 1 || pos.BrandId == 2 || pos.BrandId == 4 || pos.BrandId == 6 || pos.BrandId == 7 || pos.BrandId == 8)
  157. {
  158. Amount += 200;
  159. }
  160. else if(pos.BrandId == 3 || pos.BrandId == 5 || pos.BrandId == 9)
  161. {
  162. Amount += 300;
  163. }
  164. }
  165. if(Amount > 0)
  166. {
  167. AddAmount(db, 4, store.UserId, Amount, 1);
  168. }
  169. }
  170. }
  171. else if(jsonObj["Kind"].ToString() == "5") // 后台仓库调拨
  172. {
  173. int StoreId = int.Parse(jsonObj["Data"]["StoreId"].ToString());
  174. int BrandId = int.Parse(jsonObj["Data"]["BrandId"].ToString());
  175. string OpStorrString = jsonObj["Data"]["OpStoreNum"].ToString();
  176. int OpType = OpStorrString.StartsWith("-") ? 0 : 1;
  177. int OpStoreNum = int.Parse(OpStorrString.Replace("-", ""));
  178. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId);
  179. if(store != null)
  180. {
  181. decimal Amount = 0;
  182. if(BrandId == 1 || BrandId == 2 || BrandId == 4 || BrandId == 6 || BrandId == 7 || BrandId == 8)
  183. {
  184. Amount += 200 * OpStoreNum;
  185. }
  186. else if(BrandId == 3 || BrandId == 5 || BrandId == 9)
  187. {
  188. Amount += 300 * OpStoreNum;
  189. }
  190. if(Amount > 0)
  191. {
  192. AddAmount(db, 5, store.UserId, Amount, OpType);
  193. }
  194. }
  195. }
  196. db.SaveChanges();
  197. }
  198. else
  199. {
  200. Thread.Sleep(5000);
  201. }
  202. }
  203. catch (Exception ex)
  204. {
  205. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "分仓向总仓申请机具线程异常");
  206. }
  207. db.Dispose();
  208. }
  209. }
  210. public void AddAmount(WebCMSEntities db, int Kind, int UserId, decimal Amount, int OperateType = 1, int OrderId = 0)
  211. {
  212. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  213. if (account == null)
  214. {
  215. account = db.UserAccount.Add(new UserAccount()
  216. {
  217. Id = UserId,
  218. UserId = UserId,
  219. }).Entity;
  220. db.SaveChanges();
  221. }
  222. decimal BeforeTotalAmount = account.ValidAmount; //变更前总金额
  223. if(OperateType == 1)
  224. {
  225. account.ValidAmount += Amount;
  226. }
  227. else
  228. {
  229. account.ValidAmount -= Amount;
  230. }
  231. decimal AfterTotalAmount = account.ValidAmount; //变更后总金额
  232. StoreHouseAmountRecord record = db.StoreHouseAmountRecord.Add(new StoreHouseAmountRecord()
  233. {
  234. CreateDate = DateTime.Now,
  235. UpdateDate = DateTime.Now,
  236. OperateType = OperateType,
  237. AmountType = 1,
  238. AfterAmount = AfterTotalAmount,
  239. BeforeAmount = BeforeTotalAmount,
  240. UseAmount = Amount,
  241. UserId = UserId,
  242. QueryCount = OrderId,
  243. Sort = Kind,
  244. }).Entity;
  245. db.SaveChanges();
  246. }
  247. public void AddAmount2(WebCMSEntities db, int Kind, int UserId, decimal Amount, int OperateType = 1, int OrderId = 0)
  248. {
  249. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  250. if (account == null)
  251. {
  252. account = db.UserAccount.Add(new UserAccount()
  253. {
  254. Id = UserId,
  255. UserId = UserId,
  256. }).Entity;
  257. db.SaveChanges();
  258. }
  259. decimal BeforeTotalAmount = account.ValidAmount; //变更前总金额
  260. if(OperateType == 1)
  261. {
  262. account.TempAmount += Amount;
  263. account.ValidAmount += Amount;
  264. }
  265. else
  266. {
  267. account.TempAmount -= Amount;
  268. account.ValidAmount -= Amount;
  269. }
  270. decimal AfterTotalAmount = account.ValidAmount; //变更后总金额
  271. StoreHouseAmountRecord record = db.StoreHouseAmountRecord.Add(new StoreHouseAmountRecord()
  272. {
  273. CreateDate = DateTime.Now,
  274. UpdateDate = DateTime.Now,
  275. OperateType = OperateType,
  276. AmountType = 1,
  277. AfterAmount = AfterTotalAmount,
  278. BeforeAmount = BeforeTotalAmount,
  279. UseAmount = Amount,
  280. UserId = UserId,
  281. QueryCount = OrderId,
  282. Sort = Kind,
  283. }).Entity;
  284. db.SaveChanges();
  285. }
  286. private List<int> SpecialUsers10000()
  287. {
  288. List<int> ids = new List<int>();
  289. ids.Add(13185);
  290. ids.Add(514);
  291. ids.Add(24302);
  292. ids.Add(548);
  293. ids.Add(37887);
  294. ids.Add(33002);
  295. ids.Add(730);
  296. ids.Add(40950);
  297. ids.Add(72099);
  298. ids.Add(6898);
  299. ids.Add(46284);
  300. ids.Add(127884);
  301. ids.Add(3596);
  302. ids.Add(32630);
  303. ids.Add(11211);
  304. return ids;
  305. }
  306. private List<int> SpecialUsers0()
  307. {
  308. List<int> ids = new List<int>();
  309. ids.Add(21135);
  310. ids.Add(598);
  311. ids.Add(109913);
  312. ids.Add(609);
  313. ids.Add(588);
  314. ids.Add(12107);
  315. ids.Add(7641);
  316. ids.Add(4317);
  317. ids.Add(560);
  318. ids.Add(120998);
  319. ids.Add(3905);
  320. ids.Add(959);
  321. ids.Add(2502);
  322. ids.Add(1001);
  323. ids.Add(68868);
  324. ids.Add(11718);
  325. ids.Add(15493);
  326. ids.Add(459);
  327. ids.Add(97952);
  328. ids.Add(10719);
  329. ids.Add(16453);
  330. ids.Add(1337);
  331. ids.Add(110198);
  332. ids.Add(582);
  333. ids.Add(89);
  334. ids.Add(9319);
  335. ids.Add(128525);
  336. ids.Add(1109);
  337. ids.Add(28538);
  338. ids.Add(2927);
  339. ids.Add(584);
  340. return ids;
  341. }
  342. }