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

namespace Edu.Repository.Course
{
    /// <summary>
    /// 教育合同仓储层
    /// </summary>
    public class RB_Education_ContractRepository : BaseRepository<RB_Education_Contract>
    {
        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="demodel"></param>
        /// <param name="orderIds"></param>
        /// <returns></returns>
        public List<RB_Education_Contract_ViewModel> GetEducationContractPageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_Education_Contract_ViewModel demodel)
        {
            DynamicParameters parameters = new DynamicParameters();
            string where = $@" 1=1";
            if (demodel.Group_Id > 0)
            {
                where += $@" AND {nameof(RB_Education_Contract_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id > 0)
            {
                where += $@" AND {nameof(RB_Education_Contract_ViewModel.School_Id)} ={demodel.School_Id}";
            }
            if (demodel.OrderId > 0)
            {
                where += $@" AND {nameof(RB_Education_Contract_ViewModel.OrderId)} ={demodel.OrderId}";
            }
            if (demodel.CType > 0)
            {
                where += $@" AND {nameof(RB_Education_Contract_ViewModel.CType)} ={(int)demodel.CType}";
            }
            
            if (!string.IsNullOrEmpty(demodel.ContractNo))
            {
                where += $@" AND {nameof(RB_Education_Contract_ViewModel.ContractNo)} LIKE @ContractNo ";
                parameters.Add("ContractNo", "%" + demodel.ContractNo.Trim() + "%");
            }
            if (!string.IsNullOrEmpty(demodel.StudentName))
            {
                where += $@" AND {nameof(RB_Education_Contract_ViewModel.StudentName)} LIKE @StudentName ";
                parameters.Add("StudentName", "%" + demodel.StudentName.Trim() + "%");
            }
            if (demodel.IsAduit == 1)
            {
                if (demodel.Status >= 0)
                {
                    where += $@" AND {nameof(RB_Education_Contract_ViewModel.Status)} ={demodel.Status}";
                }
                else
                {
                    where += $@" AND {nameof(RB_Education_Contract_ViewModel.Status)} NOT IN(0,4) ";
                }
            }
            else
            {
                if (demodel.CreateBy > 0)
                {
                    where += $@" AND {nameof(RB_Education_Contract_ViewModel.CreateBy)} ={demodel.CreateBy}";
                }
                if (demodel.Status >= 0)
                {
                    where += $@" AND {nameof(RB_Education_Contract_ViewModel.Status)} ={demodel.Status}";
                }
            }
            string sql = $@" SELECT * FROM RB_Education_Contract WHERE {where} ORDER BY Id DESC ";
            return GetPage<RB_Education_Contract_ViewModel>(pageIndex, pageSize, out rowsCount, sql, parameters).ToList();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <param name="orderIds"></param>
        /// <returns></returns>
        public List<RB_Education_Contract_ViewModel> GetList(RB_Education_Contract_ViewModel demodel)
        {
            DynamicParameters parameters = new DynamicParameters();
            string where = $@" 1=1";
            if (demodel.Group_Id > 0)
            {
                where += $@" AND {nameof(RB_Education_Contract_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id > 0)
            {
                where += $@" AND {nameof(RB_Education_Contract_ViewModel.School_Id)} ={demodel.School_Id}";
            }
            if (demodel.OrderId > 0)
            {
                where += $@" AND {nameof(RB_Education_Contract_ViewModel.OrderId)} ={demodel.OrderId}";
            }
            if (!string.IsNullOrEmpty(demodel.OrderIds))
            {
                where += $@" AND {nameof(RB_Education_Contract_ViewModel.OrderId)} IN({demodel.OrderIds})";
            }
            if (!string.IsNullOrEmpty(demodel.GuestIds))
            {
                where += $@" AND {nameof(RB_Education_Contract_ViewModel.GuestId)} IN({demodel.GuestIds})";
            }
            if (demodel.CType > 0)
            {
                where += $@" AND {nameof(RB_Education_Contract_ViewModel.CType)} ={(int)demodel.CType}";
            }
            if (demodel.Status >= 0)
            {
                where += $@" AND {nameof(RB_Education_Contract_ViewModel.Status)} ={demodel.Status}";
            }
            if (!string.IsNullOrEmpty(demodel.ContractNo))
            {
                where += $@" AND {nameof(RB_Education_Contract_ViewModel.ContractNo)} LIKE @ContractNo ";
                parameters.Add("ContractNo", "%" + demodel.ContractNo.Trim() + "%");
            }
            if (!string.IsNullOrEmpty(demodel.StudentName))
            {
                where += $@" AND {nameof(RB_Education_Contract_ViewModel.StudentName)} LIKE @StudentName ";
                parameters.Add("StudentName", "%" + demodel.StudentName.Trim() + "%");
            }
            string sql = $@" SELECT * FROM RB_Education_Contract WHERE {where} ORDER BY Id DESC";
            return Get<RB_Education_Contract_ViewModel>(sql, parameters).ToList();
        }

        /// <summary>
        /// 根据查询条件获取合同的总条数
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public int GetContractCount(RB_Education_Contract_ViewModel where)
        {
            DynamicParameters parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"SELECT COUNT(1) FROM RB_Education_Contract WHERE 1=1 ");
            if (where != null)
            {
                if (where.Group_Id > 0)
                {
                    builder.AppendFormat($" AND {nameof(RB_Education_Contract_ViewModel.Group_Id)} ={where.Group_Id}");
                }
                if (where.ContractNo != null && !string.IsNullOrEmpty(where.ContractNo.Trim()))
                {
                    builder.AppendFormat($@" AND {nameof(RB_Education_Contract_ViewModel.ContractNo)} LIKE @ContractNo ");
                    parameters.Add("ContractNo", "%" + where.ContractNo.Trim() + "%");
                }
            }
            var obj = base.ExecuteScalar(builder.ToString(), parameters);
            return obj == null ? 0 : Convert.ToInt32(obj);
        }

        /// <summary>
        /// 获取订单完成情况一览   分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="demodel"></param>
        /// <param name="datebaseStr"></param>
        /// <returns></returns>
        public List<RB_Education_Contract_ViewModel> GetContractPageListForFinance(int pageIndex, int pageSize, out long rowsCount, RB_Education_Contract_ViewModel demodel, string datebaseStr)
        {
            DynamicParameters parameters = new DynamicParameters();
            string where = $@" 1=1 and o.OrderState <> 3";//排除取消订单
            where += $@" AND c.{nameof(RB_Education_Contract_ViewModel.Status)} <>4 ";//只查询已审核通过的
            if (demodel.Group_Id > 0)
            {
                where += $@" AND c.{nameof(RB_Education_Contract_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id > 0)
            {
                where += $@" AND c.{nameof(RB_Education_Contract_ViewModel.School_Id)} ={demodel.School_Id}";
            }
            if (demodel.OrderId > 0)
            {
                where += $@" AND c.{nameof(RB_Education_Contract_ViewModel.OrderId)} ={demodel.OrderId}";
            }
            if (demodel.CType > 0)
            {
                where += $@" AND c.{nameof(RB_Education_Contract_ViewModel.CType)} ={(int)demodel.CType}";
            }
            if (!string.IsNullOrEmpty(demodel.ContractNo))
            {
                where += $@" AND c.{nameof(RB_Education_Contract_ViewModel.ContractNo)} LIKE @ContractNo ";
                parameters.Add("ContractNo", "%" + demodel.ContractNo.Trim() + "%");
            }
            if (!string.IsNullOrEmpty(demodel.StudentName))
            {
                where += $@" AND c.{nameof(RB_Education_Contract_ViewModel.StudentName)} LIKE @StudentName ";
                parameters.Add("StudentName", "%" + demodel.StudentName.Trim() + "%");
            }
            if (!string.IsNullOrEmpty(demodel.CourseName))
            {
                where += $@" AND c.{nameof(RB_Education_Contract_ViewModel.CourseName)} LIKE @CourseName ";
                parameters.Add("CourseName", "%" + demodel.CourseName.Trim() + "%");
            }
            if (!string.IsNullOrEmpty(demodel.ClassName)) 
            {
                where += $@" AND c1.{nameof(RB_Education_Contract_ViewModel.ClassName)} LIKE @ClassName ";
                parameters.Add("ClassName", "%" + demodel.ClassName.Trim() + "%");
            }
            if (demodel.CreateBy > 0)
            {
                where += $@" AND c.{nameof(RB_Education_Contract_ViewModel.CreateBy)} ={demodel.CreateBy}";
            }
            if (demodel.FinanceId > 0)
            {
                where += $@" AND f.FrID ={demodel.FinanceId}";
            }
            if (demodel.TradeWay > 0)
            {
                where += $@" AND t.Type ={demodel.TradeWay}";
            }
            if (demodel.AccountId > 0)
            {
                where += $@" AND t.AccountId ={demodel.AccountId}";
            }
            if (demodel.SaleId > 0)
            {
                where += $@" AND o.EnterID ={demodel.SaleId}";
            }
            if (!string.IsNullOrEmpty(demodel.StartTime))
            {
                where += $@" AND o.CreateTime >='{demodel.StartTime}'";
            }
            if (!string.IsNullOrEmpty(demodel.EndTime))
            {
                where += $@" AND o.CreateTime <='{demodel.EndTime} 23:59:59'";
            }

            string sql = $@" SELECT c.*,c1.ClassName,c1.ClassNo,ca.AdjustPrice FROM RB_Education_Contract c
left join RB_Education_ContractAdjust ca on c.Id = ca.ContractId
left join rb_order o on c.OrderId = o.OrderId
left join rb_class c1 on o.ClassId = c1.ClassId
left join {datebaseStr}.rb_finance f on c.GuestId = f.GuestId and c.OrderId = f.OrderID and f.`Status` !=4
left join {datebaseStr}.rb_tradeway t on f.FrID = t.FinanceId
 WHERE {where} GROUP BY c.Id ORDER BY c.Id DESC   ";
            return GetPage<RB_Education_Contract_ViewModel>(pageIndex, pageSize, out rowsCount, sql, parameters).ToList();
        }
    }
}
