TimeOutPosChargeReturnService.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. /// 过期兑换机具循环截止时间超过15天激活扣费退还
  12. /// </summary>
  13. public class TimeOutPosChargeReturnService
  14. {
  15. public readonly static TimeOutPosChargeReturnService Instance = new TimeOutPosChargeReturnService();
  16. private TimeOutPosChargeReturnService()
  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("/TimeOutPosChargeReturn/check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
  33. if (string.IsNullOrEmpty(check))
  34. {
  35. function.WritePage("/TimeOutPosChargeReturn/", "check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString("HH:mm:ss"));
  36. WebCMSEntities db = new WebCMSEntities();
  37. DataTable dt = CustomerSqlConn.dtable("SELECT a.Id Id FROM ToChargeBackRecord a LEFT JOIN PosMachinesTwo b ON a.Remark=b.PosSn WHERE a.`Status`=1 AND b.PosSnType=0 AND b.BindingState=1 AND b.ActivationState=1", MysqlConn.SqlConnStr);//循环过期超过15天激活机具退费创客
  38. foreach (DataRow item in dt.Rows)
  39. {
  40. int Id = int.Parse(item["Id"].ToString());
  41. var toChargeBackRecord = db.ToChargeBackRecord.FirstOrDefault(m => m.Id == Id && m.Status == 1) ?? new ToChargeBackRecord();
  42. if (toChargeBackRecord.Id > 0)
  43. {
  44. var userAccount = db.UserAccount.FirstOrDefault(m => m.Id == toChargeBackRecord.UserId) ?? new UserAccount();
  45. var userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord
  46. {
  47. CreateDate = DateTime.Now,
  48. UserId = toChargeBackRecord.UserId,
  49. BeforeBalanceAmount = userAccount.BalanceAmount,
  50. AfterBalanceAmount = userAccount.BalanceAmount + toChargeBackRecord.ChargeAmount,
  51. ChangeAmount = toChargeBackRecord.ChargeAmount,
  52. ChangeType = 126,//过期兑换机具超过15天激活扣费退还
  53. Remark = "机具货款退还",
  54. }).Entity;
  55. toChargeBackRecord.Status = 2;//过期兑换机具循环截止时间超过15天激活扣费退还标识
  56. userAccount.BalanceAmount += toChargeBackRecord.ChargeAmount;
  57. db.SaveChanges();
  58. }
  59. }
  60. }
  61. }
  62. catch (Exception ex)
  63. {
  64. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "过期兑换机具超过15天激活扣费退还");
  65. }
  66. }
  67. Thread.Sleep(1000);
  68. }
  69. }
  70. }
  71. }