InstallmentDeductionService.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading;
  6. using MySystem.Models;
  7. using Library;
  8. namespace MySystem
  9. {
  10. /// <summary>
  11. /// 分期扣款(每月20号执行)
  12. /// </summary>
  13. public class InstallmentDeductionService
  14. {
  15. public readonly static InstallmentDeductionService Instance = new InstallmentDeductionService();
  16. private InstallmentDeductionService()
  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.Day == 20 && DateTime.Now.Hour < 12)
  29. // if (DateTime.Now.Hour < 20)
  30. {
  31. try
  32. {
  33. string check = function.ReadInstance("/InstallmentDeduction/check" + DateTime.Now.ToString("yyyy-MM-20") + ".txt");
  34. if (string.IsNullOrEmpty(check))
  35. {
  36. function.WritePage("/InstallmentDeduction/", "check" + DateTime.Now.ToString("yyyy-MM-20") + ".txt", DateTime.Now.ToString("HH:mm:ss"));
  37. WebCMSEntities db = new WebCMSEntities();
  38. var info = db.ToChargeBackRecord.Where(m => m.Sort > 0 && m.Status == 0 && m.ChargeType == 2).ToList();//分期扣款记录
  39. foreach (var items in info)
  40. {
  41. var userAccount = db.UserAccount.FirstOrDefault(m => m.Id == items.UserId) ?? new UserAccount();
  42. var record = info.FirstOrDefault(m => m.Id == items.Id);
  43. ToChargeBackRecordSub toChargeBackRecordSub = new ToChargeBackRecordSub();
  44. ToChargeByStage toChargeByStage = new ToChargeByStage();
  45. if (userAccount.BalanceAmount >= record.ChargeAmount)
  46. {
  47. var userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord
  48. {
  49. CreateDate = DateTime.Now,
  50. UserId = items.UserId,
  51. BeforeBalanceAmount = userAccount.BalanceAmount,
  52. AfterBalanceAmount = userAccount.BalanceAmount - record.ChargeAmount,
  53. ChangeAmount = record.ChargeAmount,
  54. ChangeType = 201,//分期扣款(201 分期扣款 202 普通预扣款)
  55. Remark = "分期扣款",
  56. }).Entity;
  57. toChargeBackRecordSub = db.ToChargeBackRecordSub.FirstOrDefault(m => m.Id == record.Sort) ?? new ToChargeBackRecordSub();
  58. toChargeByStage = db.ToChargeByStage.FirstOrDefault(m => m.Id == toChargeBackRecordSub.ParentId && m.TimeNumber > m.QueryCount) ?? new ToChargeByStage();
  59. record.Status = 1;
  60. toChargeBackRecordSub.Status = 1;
  61. toChargeByStage.QueryCount += 1;
  62. if (toChargeByStage.TimeNumber == toChargeByStage.QueryCount)
  63. {
  64. toChargeByStage.Status = 1;
  65. }
  66. if (userAccount.ToChargeAmount >= record.ChargeAmount)
  67. {
  68. userAccount.ToChargeAmount -= record.ChargeAmount;//分期扣款
  69. }
  70. userAccount.BalanceAmount -= record.ChargeAmount;//扣减余额
  71. if (record.Status == 1)
  72. {
  73. if (toChargeByStage.Id > 0 && toChargeByStage.TimeNumber > toChargeByStage.QueryCount)
  74. {
  75. var toChargeBackRecordSubs = db.ToChargeBackRecordSub.FirstOrDefault(m => m.Status == 0 && m.ParentId == toChargeByStage.Id) ?? new ToChargeBackRecordSub();
  76. var time = DateTime.Now.ToString("yyyy-MM-20");
  77. var date = DateTime.Parse(toChargeBackRecordSubs.StartDate.ToString()).ToString("yyyy-MM-dd");
  78. if (toChargeBackRecordSubs.Id > 0 && time == date)
  79. {
  80. userAccount.ToChargeAmount += toChargeBackRecordSubs.ChargeAmount;//增加预扣款
  81. var toChargeBackRecord = db.ToChargeBackRecord.Add(new ToChargeBackRecord
  82. {
  83. CreateDate = DateTime.Now,
  84. UserId = toChargeByStage.UserId,
  85. ChargeAmount = toChargeBackRecordSubs.ChargeAmount,
  86. ChargeType = 2,//2 分期扣款 1 普通预扣
  87. Remark = "分期扣款",
  88. }).Entity;
  89. }
  90. }
  91. }
  92. }
  93. }
  94. db.SaveChanges();
  95. }
  96. }
  97. catch (Exception ex)
  98. {
  99. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "执行分期扣费异常");
  100. }
  101. }
  102. Thread.Sleep(1000);
  103. }
  104. }
  105. }
  106. }