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_RemitRepository : BaseRepository<RB_Customer_Remit>
    {
        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Customer_Remit_Extend> GetRemitPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Customer_Remit_Extend query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" 
SELECT A.*,c.CustomerName,c.ContactNumber,c.Image as CustomerImage
FROM  RB_Customer_Remit AS A
inner join rb_customer c on A.CustomerId = c.CustomerId
WHERE 1=1
");
            if (query != null)
            {
                if (query.GroupId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_Remit_Extend.GroupId), query.GroupId);
                }
                if (query.CustomerId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_Remit_Extend.CustomerId), query.CustomerId);
                }
                if (query.Type > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_Remit_Extend.Type), query.Type);
                }
                if (query.AuditStatus > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_Remit_Extend.AuditStatus), query.AuditStatus);
                }
                if (query.CategoryId > 0)
                {
                    builder.AppendFormat(" AND C.{0}={1} ", nameof(RB_Customer_Remit_Extend.CategoryId), query.CategoryId);
                }
            }
            builder.AppendFormat(" order by A.Id desc");
            return GetPage<RB_Customer_Remit_Extend>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Customer_Remit_Extend> GetRemitListRepository(RB_Customer_Remit_Extend query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" 
SELECT A.*
FROM  RB_Customer_Remit AS A
WHERE 1=1
");
            if (query != null)
            {
                if (query.GroupId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_Remit_Extend.GroupId), query.GroupId);
                }
                if (query.CustomerId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_Remit_Extend.CustomerId), query.CustomerId);
                }
                if (query.Type > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_Remit_Extend.Type), query.Type);
                }
                if (query.AuditStatus > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_Remit_Extend.AuditStatus), query.AuditStatus);
                }
                if (query.Q_SelectNormal == 1)
                {
                    builder.AppendFormat(" AND A.{0} <>4 ", nameof(RB_Customer_Remit_Extend.AuditStatus));
                }
                if (query.Year > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_Remit_Extend.Year), query.Year);
                }
            }
            builder.AppendFormat(" order by A.Id desc");
            return Get<RB_Customer_Remit_Extend>(builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取我的已提现佣金
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public decimal GetMyWithdrawCommission(int customerId, int type, int groupId)
        {
            string sql = $@"SELECT SUM(AppliedMoney) as money FROM rb_customer_remit WHERE GroupId ={groupId} and Type ={type} and CustomerId ={customerId} and AuditStatus <>4";
            var Robj = ExecuteScalar(sql);
            return Robj == null ? 0 : Convert.ToDecimal(Robj);
        }
    }
}
