using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Threading; using MySystem.Models; using Library; namespace MySystem { /// /// 定时下架商品 /// public class BanProductByTimeService { public readonly static BanProductByTimeService Instance = new BanProductByTimeService(); private BanProductByTimeService() { } public void Start() { Thread th = new Thread(doSomething); th.IsBackground = true; th.Start(); } public void doSomething() { while (true) { try { WebCMSEntities db = new WebCMSEntities(); var check = db.Products.Any(m => m.Status == 1 && m.EndDate <= DateTime.Now); if (check) { var lists = db.Products.Where(m => m.Status == 1 && m.EndDate <= DateTime.Now).ToList(); foreach (var item in lists) { var list = lists.FirstOrDefault(m => m.Id == item.Id); list.Status = 0; list.EndDate = null; list.UpdateMan = "商品自动下架"; } db.SaveChanges(); } } catch (Exception ex) { function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "商品自动下架异常"); } Thread.Sleep(1000); } } } }