using Edu.Model.Entity.Course;
using Edu.Model.ViewModel.Course;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Edu.Repository.Course
{
    /// <summary>
    /// 活动周期明细仓储层
    /// </summary>
    public class RB_Activity_PeriodsDetailRepository : BaseRepository<RB_Activity_PeriodsDetail>
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Activity_PeriodsDetail_ViewModel> GetList(RB_Activity_PeriodsDetail_ViewModel demodel)
        {
            string where = $@" 1=1";
            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.PeriodId > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.PeriodId)} ={demodel.PeriodId}";
            }
            if (demodel.UserId > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.UserId)} ={demodel.UserId}";
            }
            if (!string.IsNullOrEmpty(demodel.UserIds))
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.UserId)} in({demodel.UserIds})";
            }
            if (demodel.ActivityId > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.ActivityId)} ={demodel.ActivityId}";
            }
            if (demodel.OrderId > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.OrderId)} ={demodel.OrderId}";
            }
            if (!string.IsNullOrEmpty(demodel.OrderIds))
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.OrderId)} in({demodel.OrderIds})";
            }
            if (!string.IsNullOrEmpty(demodel.ActivityName))
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.ActivityName)} like '%{demodel.ActivityName}%'";
            }
            string sql = $@" select r.* from RB_Activity_PeriodsDetail r where {where} order by r.Id asc";
            return Get<RB_Activity_PeriodsDetail_ViewModel>(sql).ToList();
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Activity_PeriodsDetail_ViewModel> GetPageList(int pageIndex,int pageSize,out long count,RB_Activity_PeriodsDetail_ViewModel demodel)
        {
            string where = $@" 1=1";
            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.PeriodId > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.PeriodId)} ={demodel.PeriodId}";
            }
            if (demodel.UserId > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.UserId)} ={demodel.UserId}";
            }
            if (!string.IsNullOrEmpty(demodel.UserIds))
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.UserId)} in({demodel.UserIds})";
            }
            if (demodel.ActivityId > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.ActivityId)} ={demodel.ActivityId}";
            }
            if (demodel.OrderId > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.OrderId)} ={demodel.OrderId}";
            }
            if (!string.IsNullOrEmpty(demodel.OrderIds))
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.OrderId)} in({demodel.OrderIds})";
            }
            if (demodel.DSUserId > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.DSUserId)} ={demodel.DSUserId}";
            }
            if (!string.IsNullOrEmpty(demodel.ActivityName))
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.ActivityName)} like '%{demodel.ActivityName}%'";
            }
            if (!string.IsNullOrEmpty(demodel.DSUserName))
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.DSUserName)} like '%{demodel.DSUserName}%'";
            }
            string sql = $@" select r.* from RB_Activity_PeriodsDetail r where {where} order by r.Id asc";
            return GetPage<RB_Activity_PeriodsDetail_ViewModel>(pageIndex, pageSize, out count, sql).ToList();
        }

        /// <summary>
        /// 获取用户提成列表
        /// </summary>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_Activity_PeriodsDetail_ViewModel> GetActivityCommissionUserList(RB_Activity_PeriodsDetail_ViewModel demodel)
        {
            string where = $@" 1=1";
            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id >= 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.School_Id)} ={demodel.School_Id}";
            }
            if (demodel.PeriodId > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.PeriodId)} ={demodel.PeriodId}";
            }
            if (demodel.UserId > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.UserId)} ={demodel.UserId}";
            }
            if (demodel.ActivityId > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.ActivityId)} ={demodel.ActivityId}";
            }
            if (!string.IsNullOrEmpty(demodel.ActivityName))
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.ActivityName)} like '%{demodel.ActivityName}%'";
            }
            if (demodel.OrderId > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.OrderId)} ={demodel.OrderId}";
            }
            string sql = $@" select r.UserId,r.Depart_Id,r.School_Id,sum(r.CommissionMoney) as CommissionMoney 
from RB_Activity_PeriodsDetail r where {where} group by r.UserId,r.Depart_Id,r.School_Id";
            return Get<RB_Activity_PeriodsDetail_ViewModel>(sql).ToList();
        }

        /// <summary>
        /// 累计提成金额
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public decimal GetSellCommissionStatistics(RB_Activity_PeriodsDetail_ViewModel demodel)
        {
            string where = $@" 1=1";
            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.PeriodId > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.PeriodId)} ={demodel.PeriodId}";
            }
            if (demodel.UserId > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.UserId)} ={demodel.UserId}";
            }
            if (!string.IsNullOrEmpty(demodel.UserIds))
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.UserId)} in({demodel.UserIds})";
            }
            if (demodel.ActivityId > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.ActivityId)} ={demodel.ActivityId}";
            }
            if (demodel.OrderId > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.OrderId)} ={demodel.OrderId}";
            }
            if (!string.IsNullOrEmpty(demodel.OrderIds))
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.OrderId)} in({demodel.OrderIds})";
            }
            if (demodel.DSUserId > 0)
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.DSUserId)} ={demodel.DSUserId}";
            }
            if (!string.IsNullOrEmpty(demodel.ActivityName))
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.ActivityName)} like '%{demodel.ActivityName}%'";
            }
            if (!string.IsNullOrEmpty(demodel.DSUserName))
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.DSUserName)} like '%{demodel.DSUserName}%'";
            }
            string sql = $@" select sum(r.CommissionMoney) from RB_Activity_PeriodsDetail r where {where}";
            var obj = ExecuteScalar(sql);
            return obj == null ? 0 : Convert.ToDecimal(obj);
        }
    }
}