InstallmentDeductionService.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. /// 分期扣款(每月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 <= 10 && DateTime.Now.Hour > 2 && DateTime.Now.Hour < 19)
  29. {
  30. try
  31. {
  32. string check = function.ReadInstance("/InstallmentDeduction/check" + DateTime.Now.ToString("yyyy-MM-20") + ".txt");
  33. if (string.IsNullOrEmpty(check))
  34. {
  35. function.WritePage("/InstallmentDeduction/", "check" + DateTime.Now.ToString("yyyy-MM-20") + ".txt", DateTime.Now.ToString("HH:mm:ss"));
  36. WebCMSEntities db = new WebCMSEntities();
  37. var startdate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-20 00:00:00"));
  38. var enddate = startdate.AddDays(1);
  39. // var info = db.ToChargeBackRecordSub.Where(m => m.Status == 0 && m.StartDate >= startdate && m.StartDate < enddate).ToList();//分期扣款记录明细
  40. var info = db.ToChargeByStage.Where(m => m.Status == 0).ToList();//分期扣款记录
  41. foreach (var item in info)
  42. {
  43. var toChargeBackRecordSub = db.ToChargeBackRecordSub.FirstOrDefault(m => m.Status == 0 && m.ParentId == item.Id && m.StartDate >= startdate && m.StartDate < enddate) ?? new ToChargeBackRecordSub();
  44. if (toChargeBackRecordSub.Id > 0)
  45. {
  46. var userAccount = db.UserAccount.FirstOrDefault(m => m.Id == item.UserId);
  47. if (userAccount == null)
  48. {
  49. userAccount = db.UserAccount.Add(new UserAccount()
  50. {
  51. Id = item.UserId,
  52. UserId = item.UserId,
  53. }).Entity;
  54. db.SaveChanges();
  55. }
  56. toChargeBackRecordSub.Status = 2;
  57. if (toChargeBackRecordSub.Kind == 0) userAccount.ToChargeAmount += toChargeBackRecordSub.ChargeAmount;//增加创客预扣款
  58. if (toChargeBackRecordSub.Kind == 1) userAccount.LeaderToChargeAmount += toChargeBackRecordSub.ChargeAmount;//增加盟主预扣款
  59. if (toChargeBackRecordSub.Kind == 2) userAccount.OperateToChargeAmount += toChargeBackRecordSub.ChargeAmount;//增加运营中心预扣款
  60. var toChargeBackRecord = db.ToChargeBackRecord.Add(new ToChargeBackRecord
  61. {
  62. CreateDate = DateTime.Now,
  63. Sort = toChargeBackRecordSub.Id,
  64. UserId = item.UserId,
  65. ChargeAmount = toChargeBackRecordSub.ChargeAmount,
  66. ChargeType = 2,//分期预扣款
  67. Remark = toChargeBackRecordSub.Remark,
  68. Kind = toChargeBackRecordSub.Kind,
  69. }).Entity;
  70. }
  71. // //只能存在一笔分期扣款记录(先前有的但是余额不够未扣除的则不添加新的)
  72. // var checks = db.ToChargeBackRecord.Any(m => m.Sort > 0 && (m.Status == 0 || m.Status == 3) && m.ChargeType == 2 && m.UserId == item.UserId);
  73. // if (!checks)
  74. // {
  75. // string checkAdd = RedisDbconn.Instance.Get<string>("InstallmentDeductionAddRecord:" + item.UserId);
  76. // if (string.IsNullOrEmpty(check))
  77. // {
  78. // var toChargeBackRecordSub = db.ToChargeBackRecordSub.FirstOrDefault(m => m.Status == 0 && m.ParentId == item.Id && m.StartDate >= startdate && m.StartDate < enddate) ?? new ToChargeBackRecordSub();
  79. // if (toChargeBackRecordSub.Id > 0)
  80. // {
  81. // var userAccount = db.UserAccount.FirstOrDefault(m => m.Id == item.UserId);
  82. // if (userAccount == null)
  83. // {
  84. // userAccount = db.UserAccount.Add(new UserAccount()
  85. // {
  86. // Id = item.UserId,
  87. // UserId = item.UserId,
  88. // }).Entity;
  89. // db.SaveChanges();
  90. // }
  91. // userAccount.ToChargeAmount += toChargeBackRecordSub.ChargeAmount;//增加预扣款
  92. // var toChargeBackRecord = db.ToChargeBackRecord.Add(new ToChargeBackRecord
  93. // {
  94. // CreateDate = DateTime.Now,
  95. // Sort = toChargeBackRecordSub.Id,
  96. // UserId = item.UserId,
  97. // ChargeAmount = toChargeBackRecordSub.ChargeAmount,
  98. // ChargeType = 2,//分期预扣款
  99. // Remark = "分期预扣款",
  100. // }).Entity;
  101. // }
  102. // }
  103. // RedisDbconn.Instance.Set("InstallmentDeductionAddRecord:" + item.UserId, "wait");
  104. // RedisDbconn.Instance.SetExpire("InstallmentDeductionAddRecord:" + item.UserId, 300);
  105. // }
  106. }
  107. db.SaveChanges();
  108. }
  109. }
  110. catch (Exception ex)
  111. {
  112. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "执行分期扣费异常");
  113. }
  114. }
  115. Thread.Sleep(10000);
  116. }
  117. }
  118. }
  119. }