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

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

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

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

        /// <summary>
        /// 获取渠道码客户数量
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="type"></param>
        /// <param name="channelId"></param>
        /// <returns></returns>
        public List<RB_WeChat_ChannelRecord_ViewModel> GetChanncelCustomerNum(int groupId, int type , string channelId)
        {
            string sql = $@"SELECT ChannelId , COUNT(0) AS CustomerNum FROM rb_wechat_channelrecord 
where Group_Id ={groupId} and `Status` =0 and Type ={type} and ChannelId in ({channelId}) GROUP BY ChannelId";

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

        /// <summary>
        /// 获取当天新增数量
        /// </summary>
        /// <param name="group_Id"></param>
        /// <param name="channelId"></param>
        /// <returns></returns>
        public RB_WeChat_ChannelRecord_ViewModel GetWeChatChannelStatisticsForDay(int group_Id, int channelId)
        {
            string sql = $@"SELECT 
SUM(CASE WHEN Type =1 THEN 1 else 0 END) AS AddNum,
SUM(CASE WHEN Type =2 THEN 1 else 0 END) AS BlacklistNum,
SUM(CASE WHEN Type =3 THEN 1 else 0 END) AS DelNum
 FROM rb_wechat_channelrecord WHERE `Status` =0 and Group_Id ={group_Id} and ChannelId ={channelId} and CreateTime >= '{DateTime.Now.ToString("yyyy-MM-dd")}' and CreateTime <= '{DateTime.Now.ToString("yyyy-MM-dd")} 23:59:59'";
            return Get<RB_WeChat_ChannelRecord_ViewModel>(sql).FirstOrDefault();
        }

        /// <summary>
        /// 获取时间范围的新增客户统计
        /// </summary>
        /// <param name="group_Id"></param>
        /// <param name="channelId"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<RB_WeChat_ChannelRecord_ViewModel> GetWeChatChannelStatisticsForDate(int group_Id, int channelId, string startTime, string endTime)
        {
            string sql = $@"SELECT 
DATE_FORMAT(CreateTime,'%Y-%m-%d') AS DateStr,
SUM(CASE WHEN Type =1 THEN 1 else 0 END) AS AddNum,
SUM(CASE WHEN Type =2 THEN 1 else 0 END) AS BlacklistNum,
SUM(CASE WHEN Type =3 THEN 1 else 0 END) AS DelNum
 FROM rb_wechat_channelrecord WHERE `Status` =0 and Group_Id ={group_Id} and ChannelId ={channelId} and CreateTime >='{startTime}' and CreateTime <='{endTime} 23:59:59'
GROUP BY DATE_FORMAT(CreateTime,'%Y-%m-%d')";
            return Get<RB_WeChat_ChannelRecord_ViewModel>(sql).ToList();
        }

        /// <summary>
        /// 获取时间范围的新增客户统计
        /// </summary>
        /// <param name="group_Id"></param>
        /// <param name="channelId"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<RB_WeChat_ChannelRecord_ViewModel> GetWeChatChannelStatisticsForMonth(int group_Id, int channelId, string startTime, string endTime)
        {
            string sql = $@"SELECT 
DATE_FORMAT(CreateTime,'%Y-%m') AS DateStr,
SUM(CASE WHEN Type =1 THEN 1 else 0 END) AS AddNum,
SUM(CASE WHEN Type =2 THEN 1 else 0 END) AS BlacklistNum,
SUM(CASE WHEN Type =3 THEN 1 else 0 END) AS DelNum
 FROM rb_wechat_channelrecord WHERE `Status` =0 and Group_Id ={group_Id} and ChannelId ={channelId} and CreateTime >='{startTime}' and CreateTime <='{endTime} 23:59:59'
GROUP BY DATE_FORMAT(CreateTime,'%Y-%m')";
            return Get<RB_WeChat_ChannelRecord_ViewModel>(sql).ToList();
        }


        /// <summary>
        /// 获取时间范围的新增客户统计
        /// </summary>
        /// <param name="group_Id"></param>
        /// <param name="channelId"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<RB_WeChat_ChannelRecord_ViewModel> GetWeChatChannelStatisticsForDatePageList(int pageIndex,int pageSize, out long count,int group_Id, int channelId, string startTime, string endTime)
        {
            string sql = $@"SELECT 
DATE_FORMAT(CreateTime,'%Y-%m-%d') AS DateStr,
SUM(CASE WHEN Type =1 THEN 1 else 0 END) AS AddNum,
SUM(CASE WHEN Type =2 THEN 1 else 0 END) AS BlacklistNum,
SUM(CASE WHEN Type =3 THEN 1 else 0 END) AS DelNum
 FROM rb_wechat_channelrecord WHERE `Status` =0 and Group_Id ={group_Id} and ChannelId ={channelId} and CreateTime >='{startTime}' and CreateTime <='{endTime} 23:59:59'
GROUP BY DATE_FORMAT(CreateTime,'%Y-%m-%d')
order by DATE_FORMAT(CreateTime,'%Y-%m-%d') asc
";
            return GetPage<RB_WeChat_ChannelRecord_ViewModel>(pageIndex, pageSize, out count, sql).ToList();
        }

        /// <summary>
        /// 获取时间范围的新增客户统计
        /// </summary>
        /// <param name="group_Id"></param>
        /// <param name="channelId"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<RB_WeChat_ChannelRecord_ViewModel> GetWeChatChannelStatisticsForMonthPageList(int pageIndex, int pageSize, out long count,int group_Id, int channelId, string startTime, string endTime)
        {
            string sql = $@"SELECT 
DATE_FORMAT(CreateTime,'%Y-%m') AS DateStr,
SUM(CASE WHEN Type =1 THEN 1 else 0 END) AS AddNum,
SUM(CASE WHEN Type =2 THEN 1 else 0 END) AS BlacklistNum,
SUM(CASE WHEN Type =3 THEN 1 else 0 END) AS DelNum
 FROM rb_wechat_channelrecord WHERE `Status` =0 and Group_Id ={group_Id} and ChannelId ={channelId} and CreateTime >='{startTime}' and CreateTime <='{endTime} 23:59:59'
GROUP BY DATE_FORMAT(CreateTime,'%Y-%m')
order by DATE_FORMAT(CreateTime,'%Y-%m') asc
";
            return GetPage<RB_WeChat_ChannelRecord_ViewModel>(pageIndex, pageSize, out count, sql).ToList();
        }
    }
}
