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_ChannelWeekRepository : BaseRepository<RB_WeChat_ChannelWeek>
    {
        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_WeChat_ChannelWeek_ViewModel> GetPageList(int pageIndex,int pageSize,out long count, RB_WeChat_ChannelWeek_ViewModel demodel)
        {
            DynamicParameters parameters = new DynamicParameters();
            string where = $@" 1=1 and r.{nameof(RB_WeChat_ChannelWeek_ViewModel.Status)} =0";
            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_ChannelWeek_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.ChannelId > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_ChannelWeek_ViewModel.ChannelId)} ={demodel.ChannelId}";
            }
            if (demodel.Type > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_ChannelWeek_ViewModel.Type)} ={demodel.Type}";
            }

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

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_WeChat_ChannelWeek_ViewModel> GetList(RB_WeChat_ChannelWeek_ViewModel demodel)
        {
            DynamicParameters parameters = new DynamicParameters();
            string where = $@" 1=1 and r.{nameof(RB_WeChat_ChannelWeek_ViewModel.Status)} =0";
            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_ChannelWeek_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.ChannelId > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_ChannelWeek_ViewModel.ChannelId)} ={demodel.ChannelId}";
            }
            if (!string.IsNullOrEmpty(demodel.ChannelIds))
            {
                where += $@" and r.{nameof(RB_WeChat_ChannelWeek_ViewModel.ChannelId)} in({demodel.ChannelIds})";
            }
            if (demodel.Type > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_ChannelWeek_ViewModel.Type)} ={demodel.Type}";
            }

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

        /// <summary>
        /// 获取周期配置用户列表
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="transferUserId"></param>
        /// <returns></returns>
        public List<RB_WeChat_ChannelWeek_ViewModel> GetChannelWeekForUser(int groupId, int transferUserId)
        {
            string sql = $@" select * from RB_WeChat_ChannelWeek where `Status` =0 and Group_Id ={groupId} and FIND_IN_SET({transferUserId},EmpIds) ";
            return Get<RB_WeChat_ChannelWeek_ViewModel>(sql).ToList();
        }
    }
}
