SettleAmountCheckHelper.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using MySystem.Models.Main;
  6. using Library;
  7. using System.Threading;
  8. using Microsoft.Extensions.Hosting;
  9. using System.Threading.Tasks;
  10. using LitJson;
  11. namespace MySystem
  12. {
  13. public class SettleAmountCheckHelper
  14. {
  15. public readonly static SettleAmountCheckHelper Instance = new SettleAmountCheckHelper();
  16. private SettleAmountCheckHelper()
  17. {
  18. }
  19. public void Start()
  20. {
  21. Thread th = new Thread(StartDo);
  22. th.IsBackground = true;
  23. th.Start();
  24. }
  25. public void StartDo()
  26. {
  27. while (true)
  28. {
  29. DoSomeThing();
  30. Thread.Sleep(2000);
  31. }
  32. }
  33. public void DoSomeThing()
  34. {
  35. try
  36. {
  37. WebCMSEntities db = new WebCMSEntities();
  38. DateTime checkDate = DateTime.Now.AddHours(-2);
  39. var orders = db.ConsumerOrders.Select(m => new { m.Id, m.Status, m.IsAct, m.UpdateDate, m.SettleAmount }).Where(m => m.Status > 0 && m.IsAct == 1 && m.UpdateDate < checkDate && m.SettleAmount == 0).ToList();
  40. foreach(var sub in orders)
  41. {
  42. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == sub.Id);
  43. if (order != null)
  44. {
  45. string text = function.GetWebRequest(Library.ConfigurationManager.AppSettings["SpHost"].ToString() + "/Api/PublicMethod/GetSettleOrderAmount?orderNo=" + order.SeoTitle + "&date=" + order.CreateDate.Value.ToString("yyyy-M-d"));
  46. order.SettleAmount = int.Parse(function.CheckInt(text));
  47. db.SaveChanges();
  48. }
  49. }
  50. db.Dispose();
  51. }
  52. catch(Exception ex)
  53. {
  54. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "检查未结算分账订单异常");
  55. }
  56. }
  57. }
  58. }