using Mall.Repository.Product;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Mall.Model.Entity.Product;
using VT.FW.DB;
using Mall.Repository.Education;
using Mall.Model.Entity.Education;

namespace Mall.WindowsService.Module
{
    /// <summary>
    /// 资讯自动上架下架
    /// </summary>
    public class EducationArticleShelvesModule
    {
        /// <summary>
        /// 资讯评论信息
        /// </summary>
        private static readonly RB_Education_ArticleRepository educationArticleRepository = new RB_Education_ArticleRepository();

        /// <summary>
        /// 商品自动上架时间
        /// </summary>
        /// <returns></returns>
        public static bool ShelvesEducationArticleModule()
        {
            bool flag = false;
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT ID,ArticleStatus,ShelvesDate,DownDate 
FROM rb_education_article 
WHERE Status=0 AND '{0}'>=ShelvesDate AND '{0}'<=DownDate  AND GoodsStatus<>1  
", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            var list = educationArticleRepository.Get<RB_Education_Article>(builder.ToString()).ToList();
            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    Dictionary<string, object> fileds = new Dictionary<string, object>()
                    {
                        {nameof(RB_Education_Article.ArticleStatus),1 }
                    };
                    flag = educationArticleRepository.Update(fileds, new WhereHelper(nameof(RB_Education_Article.ID), item.ID));
                }
            }
            return flag;
        }

        /// <summary>
        /// 商品自动下架
        /// </summary>
        /// <returns></returns>
        public static bool DownEducationArticleModule()
        {
            bool flag = false;
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT ID,ArticleStatus,ShelvesDate,DownDate
FROM rb_education_article 
WHERE Status=0 AND DownDate<='{0}' AND GoodsStatus=1 ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            var list = educationArticleRepository.Get<RB_Education_Article>(builder.ToString()).ToList();
            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    Dictionary<string, object> fileds = new Dictionary<string, object>()
                    {
                        {nameof(RB_Education_Article.ArticleStatus),2 }
                    };
                    flag = educationArticleRepository.Update(fileds, new WhereHelper(nameof(RB_Education_Article.ID), item.ID));
                }
            }
            return flag;
        }
    }
}
