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

namespace Edu.Repository.Contract
{
    /// <summary>
    /// 退课协议仓储层对象
    /// </summary>
    public class RB_BackClass_ProtocolRepository : BaseRepository<RB_BackClass_Protocol>
    {
        /// <summary>
        /// 获取退课协议列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_BackClass_Protocol_ViewModel> GetBackClassProtocolListRepository(RB_BackClass_Protocol_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_BackClass_Protocol AS A
WHERE 1=1
");
            if (query != null)
            {
                if (query.BackId > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_BackClass_Protocol_ViewModel.BackId), query.BackId);
                }
                if (!string.IsNullOrEmpty(query.QBackIds))
                {
                    builder.AppendFormat(@" AND A.{0} IN({1}) ", nameof(RB_BackClass_Protocol_ViewModel.BackId), query.QBackIds);
                }
                if (query.QCreateBy > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_BackClass_Protocol_ViewModel.CreateBy), query.QCreateBy);
                }
            }
            return Get<RB_BackClass_Protocol_ViewModel>(builder.ToString()).ToList();
        }

        /// <summary>
        /// 获取退课协议分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="query"></param>
        /// <param name="rowsCount"></param>
        /// <returns></returns>
        public List<RB_BackClass_Protocol_ViewModel> GetBackClassProtocolPageRepository(int pageIndex, int pageSize, RB_BackClass_Protocol_ViewModel query,out long rowsCount)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,IFNULL(C.Id,0) AS ReceiptId,B.BackAccount,B.BackAccountName,B.GuestId
FROM RB_BackClass_Protocol AS A LEFT JOIN rb_student_backclass AS B ON A.BackId=B.BackId
     LEFT JOIN rb_education_receipt AS C ON B.BackId=C.RelationId AND C.ReceiptType=2
WHERE 1=1
");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_BackClass_Protocol_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.CreateBy > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_BackClass_Protocol_ViewModel.CreateBy), query.CreateBy);
                }
                if (query.Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_BackClass_Protocol_ViewModel.Id), query.Id);
                }
                if (query.QCreateBy > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_BackClass_Protocol_ViewModel.CreateBy), query.QCreateBy);
                }
                if (!string.IsNullOrEmpty(query.PartyAName))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @PartyAName ", nameof(RB_BackClass_Protocol_ViewModel.PartyAName));
                    parameters.Add("PartyAName", "%" + query.PartyAName.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.ProtocolNum))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @ProtocolNum ", nameof(RB_BackClass_Protocol_ViewModel.ProtocolNum));
                    parameters.Add("ProtocolNum", "%" + query.ProtocolNum.Trim() + "%");
                }
                if (query.QOrderId > 0)
                {
                    builder.AppendFormat(@" AND B.{0} IN({1}) ", nameof(RB_Student_BackClass_ViewModel.OrderId), query.QOrderId);
                }
                if (query.AuditStatus > -1 && query.AuditStatus<=3)
                {
                    if (query.AuditStatus == 1)
                    {
                        builder.AppendFormat(@" AND A.{0} IN(0,1) ", nameof(RB_BackClass_Protocol_ViewModel.AuditStatus));
                    }
                    else
                    {
                        builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_BackClass_Protocol_ViewModel.AuditStatus), query.AuditStatus);
                    }
                }
                //待签字
                if (query.AuditStatus == 4)
                {
                    builder.AppendFormat(@" AND A.AuditStatus=2 AND IFNULL(A.PartyASign,'')='' ");
                }
                //待确认
                if (query.AuditStatus == 5)
                {
                    builder.AppendFormat(@" AND A.AuditStatus=2 AND IFNULL(A.PartyASign,'')<>'' AND A.IsSure=0 ");
                }
                //生效
                if (query.AuditStatus == 6)
                {
                    builder.AppendFormat(@" AND A.AuditStatus=2 AND IFNULL(A.PartyASign,'')<>'' AND A.IsSure=1 ");
                }
            }
            return GetPage<RB_BackClass_Protocol_ViewModel>(pageIndex,pageSize,out rowsCount,builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取总退课协议条数
        /// </summary>
        /// <returns></returns>
        public int GetBackClassProtocolNumRepository()
        {
            int result = 0;
            string sql = "SELECT COUNT(1) FROM RB_BackClass_Protocol ";
            var obj = base.ExecuteScalar(sql);
            if (obj != null)
            {
                Int32.TryParse(obj.ToString(), out result);
            }
            return result + 1;
        }
    }
}