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

namespace Edu.Repository.Sell
{
    public class RB_ReturnMoney_BatchRepository:BaseRepository<RB_ReturnMoney_Batch>
    {

        /// <summary>
        /// 获取提成分页数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="type"></param>
        /// <param name="month"></param>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public List<RB_ReturnMoney_Batch_ViewModel> GetPageList(int pageIndex, int pageSize, out long count, int type, string month, int groupId)
        {
            string sql = $@"select IFNULL(SUM(b.CommissionMoeny),0) as TotalEmployee,IFNULL(SUM(c.CommissionMoeny),0) as TotalCustomer,a.FinanceIds,
 IFNULL(SUM(d.CommissionMoeny),0) as TotalInternal,IFNULL(SUM(e.CommissionMoeny),0) as TotalTrans,  a.Id,a.BatchName,TotalMoney,a.CreateBy,a.CreateDate,DATE_FORMAT(a.CreateDate,'%Y-%m-%d %H:%i:%S') as CreateDateStr";
            sql += $@" from RB_ReturnMoney_Batch a  LEFT JOIN 
 (SELECT SUM(CommissionMoeny) as CommissionMoeny,BatchId FROM rb_order_returncomission  where OrderSourceType=1 and `Status` in(1,2) GROUP BY  OrderSourceType) as b
 on a.Id=b.BatchId
LEFT JOIN 
 (SELECT SUM(CommissionMoeny) as CommissionMoeny,BatchId FROM rb_order_returncomission  where OrderSourceType=2 and `Status` in(1,2) GROUP BY  OrderSourceType) as c
 on a.Id=c.BatchId
LEFT JOIN 
 (SELECT SUM(CommissionMoeny) as CommissionMoeny,BatchId FROM rb_order_returncomission  where OrderSourceType=3 and `Status` in(1,2) GROUP BY  OrderSourceType ) as d
 on a.Id=d.BatchId
LEFT JOIN 
 (SELECT SUM(CommissionMoeny) as CommissionMoeny,BatchId FROM rb_order_returncomission  where OrderSourceType=4 and `Status` in(1,2) GROUP BY  OrderSourceType ) as e
 on a.Id=e.BatchId  ";
            sql += " where a.GroupId=@gid";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("gid", groupId);
            if (!string.IsNullOrEmpty(month)) 
            {
                sql += " and DATE_FORMAT(a.CreateDate,'%Y-%m')=@month";
                parameters.Add("month", month);
            }

            if (type != 0)
            {
                sql += " and a.CommissionType=@type";
                parameters.Add("type", type);
            }

            sql += " order by a.CreateDate desc";

            return GetPage<RB_ReturnMoney_Batch_ViewModel>(pageIndex, pageSize, out count, sql, parameters).ToList();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_ReturnMoney_Batch_ViewModel> GetList(RB_ReturnMoney_Batch_ViewModel demodel)
        {
            string where = $@" 1=1";

            if (demodel.GroupId > 0)
            {
                where += $@" and r.{nameof(RB_ReturnMoney_Batch_ViewModel.GroupId)} ={demodel.GroupId}";
            }
            if (demodel.CommissionType > 0)
            {
                where += $@" and r.{nameof(RB_ReturnMoney_Batch_ViewModel.CommissionType)} ={demodel.CommissionType}";
            }

            string sql = $@" select r.* from RB_ReturnMoney_Batch r where {where} order by r.CreateDate desc";
            return Get<RB_ReturnMoney_Batch_ViewModel>(sql).ToList();
        }

        /// <summary>
        /// 查询当月份是否存在提成
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public bool ExsitModelByMonth(int groupId,string month,int type)
        {
            var sql = $@"select Count(0) from RB_ReturnMoney_Batch a where a.GroupId={groupId} and DATE_FORMAT(a.CreateDate,'%Y-%m')='{month}' and a.CommissionType={type}";
            var result = ExecuteScalar(sql);
            return Convert.ToInt32(result) > 0;
        }
    }
}
