using Edu.Common.Enum;
using Edu.Model.Entity.WeChat;
using Edu.Model.ViewModel.WeChat;
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_LableRepository : BaseRepository<RB_WeChat_Lable>
    {
        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_WeChat_Lable_ViewModel> GetPageList(int pageIndex, int pageSize, out long count, RB_WeChat_Lable_ViewModel demodel)
        {
            string where = $@" 1=1 and r.{nameof(RB_WeChat_Lable_ViewModel.Status)} =0";
            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_Lable_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.SelectType > 0)
            {
                if (demodel.SelectType == 1)
                {
                    where += $@" and r.{nameof(RB_WeChat_Lable_ViewModel.ParentId)} =0";
                }
                else if (demodel.SelectType == 2) {
                    where += $@" and r.{nameof(RB_WeChat_Lable_ViewModel.ParentId)} >0";
                }
            }
            if (!string.IsNullOrEmpty(demodel.ParentIds))
            {
                where += $@" and r.{nameof(RB_WeChat_Lable_ViewModel.ParentId)} in({demodel.ParentIds})";
            }

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

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_WeChat_Lable_ViewModel> GetList(RB_WeChat_Lable_ViewModel demodel)
        {
            string where = $@" 1=1 and r.{nameof(RB_WeChat_Lable_ViewModel.Status)} =0";
            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_Lable_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.SelectType > 0)
            {
                if (demodel.SelectType == 1)
                {
                    where += $@" and r.{nameof(RB_WeChat_Lable_ViewModel.ParentId)} =0";
                }
                else if (demodel.SelectType == 2)
                {
                    where += $@" and r.{nameof(RB_WeChat_Lable_ViewModel.ParentId)} >0";
                }
            }
            if (!string.IsNullOrEmpty(demodel.ParentIds))
            {
                where += $@" and r.{nameof(RB_WeChat_Lable_ViewModel.ParentId)} in({demodel.ParentIds})";
            }
            if (!string.IsNullOrEmpty(demodel.LableIds))
            {
                where += $@" and r.{nameof(RB_WeChat_Lable_ViewModel.Id)} in({demodel.LableIds})";
            }

            string sql = $@" 
SELECT * From RB_WeChat_Lable r
WHERE {where} 
ORDER BY r.Id asc ";
            return Get<RB_WeChat_Lable_ViewModel>(sql).ToList();
        }

        /// <summary>
        /// 获取指定ID的标签集合
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public List<RB_WeChat_Lable_ViewModel> GetLabelListByIds(string ids)
        {
            string sql = $"select * from RB_WeChat_Lable where Id in({ids})";

            return Get<RB_WeChat_Lable_ViewModel>(sql).ToList();
        }

    }
}