using Edu.Common.Enum;
using Edu.Model.Entity.WeChat;
using Edu.Model.ViewModel.WeChat;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VT.FW.DB.Dapper;

namespace Edu.Repository.WeChat
{
    /// <summary>
    /// 企业素材仓储层
    /// </summary>
    public class RB_WeChat_MediumRepository : BaseRepository<RB_WeChat_Medium>
    {
        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_WeChat_Medium_ViewModel> GetPageList(int pageIndex,int pageSize,out long count,RB_WeChat_Medium_ViewModel demodel)
        {
            DynamicParameters parameters = new DynamicParameters();
            string where = $@" 1=1 and r.{nameof(RB_WeChat_Medium_ViewModel.Status)} =0";
            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_Medium_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.MediaGroupId > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_Medium_ViewModel.MediaGroupId)} ={demodel.MediaGroupId}";
            }
            if (demodel.Type > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_Medium_ViewModel.Type)} ={(int)demodel.Type}";
            }
            if (!string.IsNullOrEmpty(demodel.Content))
            {
                where += $@" and r.{nameof(RB_WeChat_Medium_ViewModel.Content)} like @Content";
                parameters.Add("Content", "%" + demodel.Content + "%");
            }

            string sql = $@" 
SELECT * From RB_WeChat_Medium r
WHERE {where} 
ORDER BY r.Id DESC ";
            return GetPage<RB_WeChat_Medium_ViewModel>(pageIndex, pageSize, out count, sql, parameters).ToList();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_WeChat_Medium_ViewModel> GetList(RB_WeChat_Medium_ViewModel demodel)
        {
            DynamicParameters parameters = new DynamicParameters();
            string where = $@" 1=1 and r.{nameof(RB_WeChat_Medium_ViewModel.Status)} =0";
            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_Medium_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.MediaGroupId > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_Medium_ViewModel.MediaGroupId)} ={demodel.MediaGroupId}";
            }
            if (!string.IsNullOrEmpty(demodel.MediumIds))
            {
                where += $@" and r.{nameof(RB_WeChat_Medium_ViewModel.Id)} in({demodel.MediumIds})";
            }
            if (!string.IsNullOrEmpty(demodel.Content))
            {
                where += $@" and r.{nameof(RB_WeChat_Medium_ViewModel.Content)} like @Content";
                parameters.Add("Content", "%" + demodel.Content + "%");
            }

            string sql = $@" 
SELECT * From RB_WeChat_Medium r
WHERE {where} 
ORDER BY r.Id DESC ";
            return Get<RB_WeChat_Medium_ViewModel>(sql, parameters).ToList();
        }

        /// <summary>
        /// 获取可上传企业微信的素材列表
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public List<RB_WeChat_Medium_ViewModel> GetAllCanUploadList(int groupId)
        {
            string sql = $@"SELECT * FROM rb_wechat_medium WHERE `Status`=0 and Group_Id ={groupId} and Type in(2,4,5,6,7)
and (Media_Id='' or DATE_ADD(LastUploadTime,INTERVAL 3 DAY) <= '{DateTime.Now.AddHours(1).ToString("yyyy-MM-dd HH:mm:ss")}')";
            return Get<RB_WeChat_Medium_ViewModel>(sql).ToList();
        }
    }
}
