using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mall.Model.Entity.Education;
using Mall.Model.Extend.Education;
using VT.FW.DB.Dapper;

namespace Mall.Repository.Education
{
    /// <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.DSUserIds))
            {
                where += $@" and r.{nameof(RB_Activity_PeriodsDetail_ViewModel.DSUserId)} in({demodel.DSUserIds})";
            }


            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="activityId"></param>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public decimal GetActivityCommission(int activityId, int groupId)
        {
            string sql = $@"SELECT SUM(CommissionMoney) AS CommissionMoney FROM rb_activity_periodsdetail WHERE ActivityId ={activityId} and Group_Id ={groupId}";

            var obj = ExecuteScalar(sql);
            return obj == null ? 0 : Convert.ToInt32(obj);
        }
    }
}
