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

namespace Edu.Repository.User
{
    public class RB_User_ReturnComissionRepository : BaseRepository<RB_User_ReturnCommission>
    {

        /// <summary>
        /// 根据用户类型和编号查询用户返佣金额
        /// </summary>
        /// <param name="type"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public RB_User_ReturnComission_ViewModel GetModelBySourceId(int type,int id, int ctype)
        {
            var sql = "select * from RB_User_ReturnCommission where UserType=@ut and UserId=@uid and CommissionType=@ctype";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("ut", type);
            parameters.Add("uid", id);
            parameters.Add("ctype", ctype);

            return Get<RB_User_ReturnComission_ViewModel>(sql, parameters).ToList().FirstOrDefault();
        }

        /// <summary>
        /// 获取个人返佣情况
        /// </summary>
        /// <param name="group_Id"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public RB_User_ReturnComission_ViewModel GetCenterCommissionStatistics(int group_Id, int id)
        {
            string sql = $@"select sum(UnSettlementMoney)as UnSettlementMoney,
sum(SttlementMoney)as SttlementMoney,
sum(CashOutMoney)as CashOutMoney,
sum(CanCashOutMoney)as CanCashOutMoney 
from RB_User_ReturnCommission where GroupId ={group_Id} and UserId={id} and UserType in(1,3)";
            return Get<RB_User_ReturnComission_ViewModel>(sql).FirstOrDefault();
        }

        /// <summary>
        /// 获取同行返佣/存折统计
        /// </summary>
        /// <param name="group_Id"></param>
        /// <param name="orderSourceType"></param>
        /// <param name="orderSourceId"></param>
        /// <param name="commissionType"></param>
        /// <returns></returns>
        public RB_User_ReturnComission_ViewModel GetCustomerCommissionStat(int group_Id, int orderSourceType, int orderSourceId,int CategoryId, int commissionType)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT sum(A.UnSettlementMoney)as UnSettlementMoney,sum(A.SttlementMoney) as SttlementMoney
      ,sum(A.CashOutMoney)as CashOutMoney,sum(A.CanCashOutMoney)as CanCashOutMoney 
FROM RB_User_ReturnCommission AS A INNER JOIN rb_customer AS B ON (A.UserType=2 AND A.UserId=B.CustomerId)
WHERE 1=1
");
            if (group_Id > 0)
            {
                builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_User_ReturnComission_ViewModel.GroupId), group_Id);
            }
            if (orderSourceType > 0)
            {
                builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_User_ReturnComission_ViewModel.UserType), orderSourceType);
            }
            if (commissionType > 0)
            {
                builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_User_ReturnComission_ViewModel.CommissionType), commissionType);
            }
            if (orderSourceId > 0)
            {
                builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_User_ReturnComission_ViewModel.UserId), orderSourceId);
            }
            if (CategoryId > 0)
            {
                builder.AppendFormat(" AND B.CategoryId={0} ", CategoryId);
            }
            return Get<RB_User_ReturnComission_ViewModel>(builder.ToString()).FirstOrDefault();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_User_ReturnComission_ViewModel> GetList(RB_User_ReturnComission_ViewModel dmodel)
        {
            string where = $@" 1=1";
            if (dmodel.GroupId > 0)
            {
                where += $@" {nameof(RB_User_ReturnComission_ViewModel.GroupId)} ={dmodel.GroupId}";
            }
            if (dmodel.UserId > 0)
            {
                where += $@" {nameof(RB_User_ReturnComission_ViewModel.UserId)} ={dmodel.UserId}";
            }
            if (dmodel.UserType > 0)
            {
                where += $@" {nameof(RB_User_ReturnComission_ViewModel.UserType)} ={(int)dmodel.UserType}";
            }
            if (dmodel.CommissionType > 0)
            {
                where += $@" {nameof(RB_User_ReturnComission_ViewModel.CommissionType)} ={dmodel.CommissionType}";
            }

            string sql = $@"select * from RB_User_ReturnCommission where {where}";
            return Get<RB_User_ReturnComission_ViewModel>(sql).ToList();
        }
    }
}
