using Edu.Model.Entity.Customer;
using Edu.Model.ViewModel.Customer;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using VT.FW.DB.Dapper;

namespace Edu.Repository.Customer
{
    /// <summary>
    /// 客户幸福存折和返佣仓储层
    /// </summary>
    public class RB_Customer_BalanceDetailRepository : BaseRepository<RB_Customer_BalanceDetail>
    {
        /// <summary>
        /// 获取客户返佣或幸福存折分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Customer_BalanceDetail_ViewModel> GetCustomerBalanceDetailPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Customer_BalanceDetail_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,IFNULL(b.ClassName,'') AS ClassName,IFNULL(B.ClassNo,'') AS ClassNo,IFNULL(C.CustomerName,'') AS CustomerName
FROM  RB_Customer_BalanceDetail AS A LEFT JOIN rb_class AS B ON A.ClassId=B.ClassId
      LEFT JOIN rb_customer AS C ON A.CustomerId=C.CustomerId
WHERE 1=1
");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_BalanceDetail_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.CustomerId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_BalanceDetail_ViewModel.CustomerId), query.CustomerId);
                }
                if (query.RebateType > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_BalanceDetail_ViewModel.RebateType), query.RebateType);
                }
                if (query.BalanceType > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_BalanceDetail_ViewModel.BalanceType), query.BalanceType);
                }
                if (query.OrderId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_BalanceDetail_ViewModel.OrderId), query.OrderId);
                }
                if (!string.IsNullOrEmpty(query.Description))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @Description ", nameof(RB_Customer_BalanceDetail_ViewModel.Description));
                    parameters.Add("Description", "%" + query.Description.Trim() + "%");
                }
            }
          return GetPage<RB_Customer_BalanceDetail_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取客户返佣或幸福存折列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Customer_BalanceDetail_ViewModel> GetCustomerBalanceDetailListRepository( RB_Customer_BalanceDetail_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,IFNULL(b.ClassName,'') AS ClassName,IFNULL(B.ClassNo,'') AS ClassNo,IFNULL(C.CustomerName,'') AS CustomerName
FROM  RB_Customer_BalanceDetail AS A LEFT JOIN rb_class AS B ON A.ClassId=B.ClassId
      LEFT JOIN rb_customer AS C ON A.CustomerId=C.CustomerId
WHERE 1=1
");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_BalanceDetail_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.CustomerId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_BalanceDetail_ViewModel.CustomerId), query.CustomerId);
                }
                if (query.RebateType > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_BalanceDetail_ViewModel.RebateType), query.RebateType);
                }
                if (query.BalanceType > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_BalanceDetail_ViewModel.BalanceType), query.BalanceType);
                }
                if (query.OrderId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_BalanceDetail_ViewModel.OrderId), query.OrderId);
                }
                if (query.BalanceState > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_BalanceDetail_ViewModel.BalanceState), query.BalanceState);
                }
                if (!string.IsNullOrEmpty(query.Description))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @Description ", nameof(RB_Customer_BalanceDetail_ViewModel.Description));
                    parameters.Add("Description", "%" + query.Description.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.QOrderIds))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Customer_BalanceDetail_ViewModel.OrderId), query.QOrderIds);
                }
            }
            return Get<RB_Customer_BalanceDetail_ViewModel>(builder.ToString(),parameters).ToList();
        }


        /// <summary>
        /// 获取可发放返佣的列表
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="dateStr"></param>
        /// <returns></returns>
        public List<RB_Customer_BalanceDetail_ViewModel> GetEduB2BOrderCanSendCommissionList(int groupId, string dateStr)
        {
            string sql = $@"SELECT b.* FROM rb_order o
INNER JOIN rb_customer_balancedetail b on o.OrderId = b.OrderId
WHERE o.Group_Id ={groupId} and o.OrderState =1 and o.CustomerId >0 and o.PreferPrice = (o.Income - o.Refund + o.PlatformTax + o.DiscountMoney) and b.BalanceType =1 and b.BalanceState in(2,3) and o.CreateTime <='{dateStr} 23:59:59' ";
            return Get<RB_Customer_BalanceDetail_ViewModel>(sql).ToList();
        }

        /// <summary>
        /// 获取我的未结算佣金
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public decimal GetMyNoSettlementCommission(int customerId, int rebateType, int groupId)
        {
            string sql = $@"SELECT SUM(Money) as money FROM rb_customer_balancedetail WHERE Group_Id ={groupId} and RebateType ={rebateType} and CustomerId ={customerId} and BalanceType =1 and BalanceState =2";
            var Robj = ExecuteScalar(sql);
            return Robj == null ? 0 : Convert.ToDecimal(Robj);
        }

        /// <summary>
        /// 获取未发放完的返佣列表
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="dateStr"></param>
        /// <returns></returns>
        public List<RB_Customer_BalanceDetail_ViewModel> SetEduB2BCustomerCommissionCanSendList(int groupId, string dateStr)
        {
            string sql = $@"SELECT b.* FROM rb_customer_balancedetail b
INNER JOIN rb_order o on b.OrderId = o.OrderId
WHERE b.Group_Id ={groupId} and o.OrderState in(1,2) and b.UpdateTime <='{dateStr} 23:59:59' and b.BalanceState =1 and b.GiveOutOK <>1 ";
            return Get<RB_Customer_BalanceDetail_ViewModel>(sql).ToList();
        }
    }
}
