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);
        }
    }
}
