| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Threading;
- using MySystem.Models;
- using Library;
- namespace MySystem
- {
- /// <summary>
- /// 定时下架商品
- /// </summary>
- 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);
- }
- }
- }
- }
|