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

namespace Edu.Repository.StudyAbroad
{
    /// <summary>
    /// 留学就业仓储层
    /// </summary>
    public class RB_StudyAbroadRepository : BaseRepository<RB_StudyAbroad>
    {
        /// <summary>
        /// 获取留学就业分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_StudyAbroad_ViewModel> GetStudyAbroadPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_StudyAbroad_ViewModel query)
        {
            rowsCount = 0;
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,IFNULL(B.`Name`,'') AS SupplierName,IFNULL(B.ContractUrl,'') AS ContractUrl,IFNULL(c.SName,'') AS SchoolName
FROM RB_StudyAbroad AS A LEFT JOIN rb_supplier AS B ON A.SupplierId=B.Id
     LEFT JOIN rb_school AS C ON A.School_Id=C.SId
WHERE 1=1
");
            builder.AppendFormat(" AND A.{0}={1}", nameof(RB_StudyAbroad_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query == null)
            {
                return new List<RB_StudyAbroad_ViewModel>();
            }
            else
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1}", nameof(RB_StudyAbroad_ViewModel.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.Name))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @Name ", nameof(RB_StudyAbroad_ViewModel.Name));
                    parameters.Add("Name", "%" + query.Name.Trim() + "%");
                }
                if (query.Type > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1}", nameof(RB_StudyAbroad_ViewModel.Type), query.Type);
                }
                if (query.SaleState > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1}", nameof(RB_StudyAbroad_ViewModel.SaleState),(int)query.SaleState);
                }
                return GetPage<RB_StudyAbroad_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
            }
        }


        /// <summary>
        /// 获取留学就业审核分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_StudyAbroad_ViewModel> GetStudyAbroadAuditPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_StudyAbroad_ViewModel query)
        {
            rowsCount = 0;
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,IFNULL(B.`Name`,'') AS SupplierName,IFNULL(B.ContractUrl,'') AS ContractUrl
FROM RB_StudyAbroad AS A LEFT JOIN rb_supplier AS B ON A.SupplierId=B.Id
WHERE 1=1
");
            builder.AppendFormat(" AND A.{0}={1}", nameof(RB_StudyAbroad_ViewModel.Status), (int)DateStateEnum.Normal);
            builder.AppendFormat(" AND A.{0}>{1}", nameof(RB_StudyAbroad_ViewModel.SaleState), (int)SaleStateEnum.NoPerfect);
            if (query == null)
            {
                return new List<RB_StudyAbroad_ViewModel>();
            }
            else
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1}", nameof(RB_StudyAbroad_ViewModel.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.Name))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @Name ", nameof(RB_StudyAbroad_ViewModel.Name));
                    parameters.Add("Name", "%" + query.Name.Trim() + "%");
                }
                if (query.Type > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1}", nameof(RB_StudyAbroad_ViewModel.Type), query.Type);
                }

                //待审核
                if (query.AuditState == 1)
                {
                    builder.AppendFormat(@" AND (
                                                     (A.SaleState=2 AND A.DirectorId>0 AND A.DirectorStatus=1 AND A.ManagerStatus=0 AND A.ManagerId={0}) 
                                                  OR (A.SaleState=2 AND A.DirectorStatus=0 AND  A.DirectorId={0})
                                                ) ", query.AuditManId);
                }
                //已审核
                else if(query.AuditState == 2)
                {
                    builder.AppendFormat(@"  AND (
                                                     (A.ManagerStatus>0 AND A.ManagerId={0}) 
                                                  OR (A.DirectorStatus>0 AND A.DirectorId={0})
                                                 )", query.AuditManId);
                }
               
                return GetPage<RB_StudyAbroad_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
            }
        }

        /// <summary>
        /// 获取留学就业列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_StudyAbroad_ViewModel> GetStudyAbroadListRepository(RB_StudyAbroad_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_StudyAbroad AS A
WHERE 1=1
");
            builder.AppendFormat(" AND A.{0}={1}", nameof(RB_StudyAbroad_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query == null)
            {
                return new List<RB_StudyAbroad_ViewModel>();
            }
            else
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1}", nameof(RB_StudyAbroad_ViewModel.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.Name))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @Name ", nameof(RB_StudyAbroad_ViewModel.Name));
                    parameters.Add("Name", "%" + query.Name.Trim() + "%");
                }
                if (query.SaleState > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1}", nameof(RB_StudyAbroad_ViewModel.SaleState), (int)query.SaleState);
                }
                if (!string.IsNullOrEmpty(query.QIds))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_StudyAbroad_ViewModel.Id), query.QIds);
                }
                if (query.Type > 0)
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_StudyAbroad_ViewModel.Type), query.Type);
                }
                return Get<RB_StudyAbroad_ViewModel>(builder.ToString(), parameters).ToList();
            }
        }

        /// <summary>
        /// 获取留学就业列表扩展
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_StudyAbroad_ViewModel> GetStudyAbroadListExtRepository(RB_StudyAbroad_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,IFNULL(B.`Name`,'') AS SupplierName,IFNULL(B.ContractUrl,'') AS ContractUrl,IFNULL(C.SName,'') AS SchoolName
FROM RB_StudyAbroad AS A LEFT JOIN rb_supplier AS B ON A.SupplierId=B.Id
LEFT JOIN rb_school AS C ON A.School_Id=C.SId
WHERE 1=1
");
            builder.AppendFormat(" AND A.{0}={1}", nameof(RB_StudyAbroad_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query == null)
            {
                return new List<RB_StudyAbroad_ViewModel>();
            }
            else
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1}", nameof(RB_StudyAbroad_ViewModel.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.Name))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @Name ", nameof(RB_StudyAbroad_ViewModel.Name));
                    parameters.Add("Name", "%" + query.Name.Trim() + "%");
                }
                if (query.Type > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1}", nameof(RB_StudyAbroad_ViewModel.Type), query.Type);
                }
                if (query.AuditType == 1)
                {
                    builder.AppendFormat(" AND A.SaleState=2 AND A.DirectorStatus=0 ");
                }
                else if (query.AuditType == 2)
                {
                    builder.AppendFormat(" AND A.SaleState=2 AND A.DirectorId>0 AND A.DirectorStatus=1 AND A.ManagerStatus=0 ");
                }
                if (query.SaleState > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1}", nameof(RB_StudyAbroad_ViewModel.SaleState), (int)query.SaleState);
                }
                if (query.Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1}", nameof(RB_StudyAbroad_ViewModel.Id), query.Id);
                }
                if (!string.IsNullOrEmpty(query.StartTime))
                {
                    builder.AppendFormat(" AND A.{0} >='{1}'", nameof(RB_StudyAbroad_ViewModel.CreateTime), query.StartTime);
                }
                if (!string.IsNullOrEmpty(query.EndTime))
                {
                    builder.AppendFormat(" AND A.{0} <='{1} 23:59:59'", nameof(RB_StudyAbroad_ViewModel.CreateTime), query.EndTime);
                }
                return Get<RB_StudyAbroad_ViewModel>(builder.ToString(), parameters).ToList();
            }
        }
    }
}
