TimeOutPosChargeService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading;
  6. using MySystem.PxcModels;
  7. using Library;
  8. namespace MySystem
  9. {
  10. /// <summary>
  11. /// 过期机具执行扣费
  12. /// </summary>
  13. public class TimeOutPosChargeService
  14. {
  15. public readonly static TimeOutPosChargeService Instance = new TimeOutPosChargeService();
  16. private TimeOutPosChargeService()
  17. { }
  18. public void Start()
  19. {
  20. Thread th = new Thread(doSomething);
  21. th.IsBackground = true;
  22. th.Start();
  23. }
  24. public void doSomething()
  25. {
  26. while (true)
  27. {
  28. if (DateTime.Now.Hour < 9)
  29. {
  30. try
  31. {
  32. string check = function.ReadInstance("/TimeOutPosCharge/check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
  33. if (string.IsNullOrEmpty(check))
  34. {
  35. function.WritePage("/TimeOutPosCharge/", "check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString("HH:mm:ss"));
  36. WebCMSEntities db = new WebCMSEntities();
  37. var date = DateTime.Now.AddDays(-60).ToString("yyyy-MM-dd 00:00:00");//过期限制时间
  38. var time = DateTime.Parse(date);//过期限制时间
  39. DataTable dt = CustomerSqlConn.dtable("SELECT BuyUserId FROM PosMachinesTwo WHERE `Status`>-1 AND BuyUserId>0 AND BindingState=0 AND ActivationState=0 AND ScanQrTrade=0 AND RecycEndDate <'" + date + "' GROUP BY BuyUserId", MysqlConn.SqlConnStr);//扣费创客
  40. var query = db.PosMachinesTwo.Where(m => m.Status > -1 && m.BuyUserId > 0 && m.BindingState == 0 && m.ActivationState == 0 && m.ScanQrTrade == 0 && m.RecycEndDate < time).ToList();//循环过期超过15天机具
  41. var brandInfo = db.KqProducts.ToList();
  42. foreach (DataRow item in dt.Rows)
  43. {
  44. int BuyUserId = int.Parse(item["BuyUserId"].ToString());
  45. var userAccount = db.UserAccount.FirstOrDefault(m => m.Id == BuyUserId);
  46. if (userAccount == null)
  47. {
  48. userAccount = db.UserAccount.Add(new UserAccount()
  49. {
  50. Id = BuyUserId,
  51. UserId = BuyUserId,
  52. }).Entity;
  53. db.SaveChanges();
  54. }
  55. var posInfo = query.Where(m => m.BuyUserId == BuyUserId);
  56. var amount = 0;
  57. foreach (var pos in posInfo)
  58. {
  59. var poss = db.PosMachinesTwo.FirstOrDefault(m => m.Id == pos.Id) ?? new PosMachinesTwo();
  60. poss.ScanQrTrade = 999;
  61. var Brand = brandInfo.FirstOrDefault(m => m.Id == pos.BrandId);
  62. if (Brand.Name.Contains("电签"))
  63. {
  64. amount = 200;
  65. }
  66. if (Brand.Name.Contains("大POS"))
  67. {
  68. amount = 300;
  69. }
  70. userAccount.ToChargeAmount += amount;//增加预扣款
  71. var toChargeBackRecord = db.ToChargeBackRecord.Add(new ToChargeBackRecord
  72. {
  73. CreateDate = DateTime.Now,
  74. UserId = BuyUserId,
  75. ChargeAmount = amount,
  76. ChargeType = 124,//过期机具货款扣费
  77. Remark = pos.PosSn,
  78. }).Entity;
  79. }
  80. }
  81. db.SaveChanges();
  82. DoChargeAmount(db);
  83. }
  84. }
  85. catch (Exception ex)
  86. {
  87. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "过期机具执行扣费异常");
  88. }
  89. }
  90. Thread.Sleep(1000);
  91. }
  92. }
  93. public void StartDoChargeAmount()
  94. {
  95. Thread th = new Thread(StartDoChargeAmountReady);
  96. th.IsBackground = true;
  97. th.Start();
  98. }
  99. public void StartDoChargeAmountReady()
  100. {
  101. while (true)
  102. {
  103. WebCMSEntities db = new WebCMSEntities();
  104. DoChargeAmount(db);
  105. db.Dispose();
  106. Thread.Sleep(60000);
  107. }
  108. }
  109. public void ListenChargeAmount()
  110. {
  111. Thread th = new Thread(ListenChargeAmountReady);
  112. th.IsBackground = true;
  113. th.Start();
  114. }
  115. public void ListenChargeAmountReady()
  116. {
  117. while (true)
  118. {
  119. string content = RedisDbconn.Instance.RPop<string>("DoChargeAmountQueue");
  120. if (!string.IsNullOrEmpty(content))
  121. {
  122. WebCMSEntities db = new WebCMSEntities();
  123. DoChargeAmount(db, int.Parse(function.CheckInt(content)));
  124. db.Dispose();
  125. }
  126. }
  127. }
  128. public void DoChargeAmount(WebCMSEntities db, int UserId = 0)
  129. {
  130. MySystem.OpModels.WebCMSEntities opdb = new MySystem.OpModels.WebCMSEntities();
  131. IQueryable<ToChargeBackRecord> list = db.ToChargeBackRecord.Where(m => m.Status == 0);//过期机具扣费记录
  132. if (UserId > 0)
  133. {
  134. list = list.Where(m => m.UserId == UserId);
  135. }
  136. var info = list.ToList();
  137. foreach (var items in info)
  138. {
  139. function.WriteLog(DateTime.Now.ToString(), "扣款监听日志");
  140. function.WriteLog(items.UserId.ToString(), "扣款监听日志");
  141. var userAccount = db.UserAccount.FirstOrDefault(m => m.Id == items.UserId) ?? new UserAccount();
  142. var record = info.FirstOrDefault(m => m.Id == items.Id);
  143. var check = false; //扣款成功标记
  144. function.WriteLog("BalanceAmount:" + userAccount.BalanceAmount + ";ChargeAmount:" + record.ChargeAmount + ";", "扣款监听日志");
  145. function.WriteLog("in", "扣款监听日志");
  146. var ChangeType = 0;
  147. var Remark = "";
  148. if (record.ChargeType == 124)//过期机具货款扣费
  149. {
  150. ChangeType = 124;
  151. Remark = "扣机具货款";
  152. }
  153. if (record.ChargeType == 1)//普通预扣款
  154. {
  155. ChangeType = 202;
  156. // Remark = "普通扣款";
  157. Remark = items.Remark;
  158. }
  159. if (record.ChargeType == 2)//分期预扣款
  160. {
  161. ChangeType = 201;
  162. // Remark = "分期扣款";
  163. Remark = items.Remark;
  164. }
  165. if (record.Kind < 2) //主库
  166. {
  167. if (record.Kind == 0) //创客预扣款
  168. {
  169. if (userAccount.BalanceAmount >= record.ChargeAmount)
  170. {
  171. var userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord
  172. {
  173. CreateDate = DateTime.Now,
  174. UserId = items.UserId,
  175. BeforeBalanceAmount = userAccount.BalanceAmount,
  176. AfterBalanceAmount = userAccount.BalanceAmount - record.ChargeAmount,
  177. ChangeAmount = record.ChargeAmount,
  178. ChangeType = ChangeType,
  179. Remark = Remark,
  180. }).Entity;
  181. function.WriteLog("ToChargeAmount:" + userAccount.ToChargeAmount + ";ChargeAmount:" + record.ChargeAmount + ";", "扣款监听日志");
  182. if (userAccount.ToChargeAmount >= record.ChargeAmount)
  183. {
  184. function.WriteLog("ok", "扣款监听日志");
  185. userAccount.ToChargeAmount -= record.ChargeAmount;//分期扣款
  186. }
  187. userAccount.BalanceAmount -= record.ChargeAmount;//扣减余额
  188. check = true;
  189. }
  190. }
  191. if (record.Kind == 1) //盟主预扣款
  192. {
  193. if (userAccount.LeaderBalanceAmount >= record.ChargeAmount)
  194. {
  195. //11:盟主储蓄金增减单(兑换机具券 机具券兑换 系统增加(盟主储蓄金) 系统扣减(盟主储蓄金) 储备金购买)
  196. // 示例:+/-金额
  197. // 盟主储蓄金:剩余盟主储蓄金
  198. // 12:可提现余额增减单(系统增加(可提现余额) 系统扣减(可提现余额))
  199. // 示例:+/-金额
  200. // 可提现余额:剩余可提现余额
  201. // 13:盟主储蓄金减少可提现余额增加(推荐大/小盟主 商城购机 购机奖励)
  202. // 示例:-金额
  203. // 盟主储蓄金:剩余盟主储蓄金
  204. // 可提现余额:+金额
  205. var leaderReserveRecord = db.LeaderReserveRecord.Add(new LeaderReserveRecord()
  206. {
  207. CreateDate = DateTime.Now, //创建时间
  208. Sort = 12,
  209. UserId = items.UserId, //创客
  210. Remark = "盟主预扣款",
  211. SeoDescription = items.SeoDescription,
  212. ChangeType = 2,//变动类型(1 增加 2 减少)
  213. BeforeAmt = userAccount.LeaderBalanceAmount,
  214. AfterAmt = userAccount.LeaderBalanceAmount - record.ChargeAmount,
  215. ChangeAmt = record.ChargeAmount,
  216. TradeDate = DateTime.Now.ToString("yyyyMMdd"),
  217. TradeMonth = DateTime.Now.ToString("yyyyMM"),
  218. }).Entity;
  219. function.WriteLog("LeaderToChargeAmount:" + userAccount.LeaderToChargeAmount + ";ChargeAmount:" + record.ChargeAmount + ";", "扣款监听日志");
  220. if (userAccount.LeaderToChargeAmount >= record.ChargeAmount)
  221. {
  222. function.WriteLog("ok", "扣款监听日志");
  223. userAccount.LeaderToChargeAmount -= record.ChargeAmount;//扣减盟主预扣款
  224. }
  225. userAccount.LeaderBalanceAmount -= record.ChargeAmount;//扣减盟主可提现余额
  226. check = true;
  227. }
  228. }
  229. }
  230. else //运营中心
  231. {
  232. var opUserAccount = opdb.UserAccount.FirstOrDefault(m => m.Id == items.UserId) ?? new MySystem.OpModels.UserAccount();
  233. if (record.Kind == 2) //运营中心预扣款
  234. {
  235. if (opUserAccount.ValidForGetAmount >= record.ChargeAmount)
  236. {
  237. var amoutRecord = opdb.AmountRecord.Add(new MySystem.OpModels.AmountRecord
  238. {
  239. CreateDate = DateTime.Now,
  240. UserId = items.UserId,
  241. SeoDescription = "运营中心预扣款",
  242. UseAmount = record.ChargeAmount,
  243. OperateType = 2,
  244. BeforeAmount = opUserAccount.ValidAmount + opUserAccount.TotalAmt + opUserAccount.ValidForGetAmount,
  245. AfterAmount = opUserAccount.ValidAmount + opUserAccount.TotalAmt + opUserAccount.ValidForGetAmount - record.ChargeAmount,
  246. }).Entity;
  247. function.WriteLog("OperateToChargeAmount:" + userAccount.OperateToChargeAmount + ";ChargeAmount:" + record.ChargeAmount + ";", "扣款监听日志");
  248. if (userAccount.OperateToChargeAmount >= record.ChargeAmount)
  249. {
  250. function.WriteLog("ok", "扣款监听日志");
  251. userAccount.OperateToChargeAmount -= record.ChargeAmount;//扣减运营中心预扣款
  252. }
  253. opUserAccount.ValidForGetAmount -= record.ChargeAmount;//扣减运营中心可提现余额
  254. amoutRecord.AfterValidForGetAmount = opUserAccount.ValidForGetAmount;
  255. amoutRecord.AfterTotalAmt = opUserAccount.TotalAmt;
  256. amoutRecord.AfterValidAmount = opUserAccount.ValidAmount;
  257. check = true;
  258. }
  259. }
  260. }
  261. if (check)
  262. {
  263. record.Status = 1;
  264. record.UpdateDate = DateTime.Now;
  265. if (record.ChargeType == 2)
  266. {
  267. var toChargeBackRecordSub = db.ToChargeBackRecordSub.FirstOrDefault(m => m.Id == record.Sort) ?? new ToChargeBackRecordSub();
  268. var toChargeByStage = db.ToChargeByStage.FirstOrDefault(m => m.Id == toChargeBackRecordSub.ParentId && m.TimeNumber > m.QueryCount) ?? new ToChargeByStage();
  269. toChargeBackRecordSub.Status = 1;
  270. toChargeByStage.QueryCount += 1;
  271. toChargeByStage.ChargeAmount += toChargeBackRecordSub.ChargeAmount;
  272. if (toChargeByStage.TimeNumber == toChargeByStage.QueryCount)
  273. {
  274. toChargeByStage.Status = 1;
  275. }
  276. }
  277. }
  278. function.WriteLog("\n\n", "扣款监听日志");
  279. }
  280. db.SaveChanges();
  281. opdb.SaveChanges();
  282. }
  283. }
  284. }