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_Course_OfferRepository : BaseRepository<RB_Course_Offer>
    {
        /// <summary>
        /// 获取报价单分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Course_Offer_ViewModel> GetCourseOfferPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Course_Offer_ViewModel query)
        {
            rowsCount = 0;
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" 
SELECT A.* 
FROM RB_Course_Offer AS A
WHERE 1=1
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Offer_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query == null)
            {
                return new List<RB_Course_Offer_ViewModel>();
            }
            else
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Offer_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.School_Id > 0)
                {
                    //builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Offer_ViewModel.School_Id), query.School_Id);
                }
                if (!string.IsNullOrEmpty(query.Name))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @Name ", nameof(RB_Course_Offer_ViewModel.Name));
                    parameters.Add("Name", "%" + query.Name.Trim() + "%");
                }
                if (query.CreateBy > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Offer_ViewModel.CreateBy), query.CreateBy);
                }
                if (!string.IsNullOrEmpty(query.CustomerInfo))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @CustomerInfo ", nameof(RB_Course_Offer_ViewModel.CustomerInfo));
                    parameters.Add("CustomerInfo", "%" + query.CustomerInfo.Trim() + "%");
                }
                if (query.CustomerSource > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Offer_ViewModel.CustomerSource), (int)query.CustomerSource);
                }
                if (query.CustomerType > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Offer_ViewModel.CustomerType), (int)query.CustomerType);
                }
                if (query.CustomerStatus > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Offer_ViewModel.CustomerStatus), query.CustomerStatus);
                }
                if (query.Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Offer_ViewModel.Id), query.Id);
                }
                if (!string.IsNullOrEmpty(query.QStart))
                {
                    builder.AppendFormat(" AND A.{0}>='{1}' ", nameof(RB_Course_Offer_ViewModel.CreateTime), query.QStart);
                }
                if (!string.IsNullOrEmpty(query.QEnd))
                {
                    builder.AppendFormat(" AND A.{0}<='{1} 23:59:59' ", nameof(RB_Course_Offer_ViewModel.CreateTime), query.QEnd);
                }
                if (!string.IsNullOrEmpty(query.SerialNum))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @SerialNum ", nameof(RB_Course_Offer_ViewModel.SerialNum));
                    parameters.Add("SerialNum", "%" + query.SerialNum.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.CustomerName))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @CustomerName ", nameof(RB_Course_Offer_ViewModel.CustomerName));
                    parameters.Add("CustomerName", "%" + query.CustomerName.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.CustomerTel))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @CustomerTel ", nameof(RB_Course_Offer_ViewModel.CustomerTel));
                    parameters.Add("CustomerTel", "%" + query.CustomerTel.Trim() + "%");
                }
                if (query.OfferType > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Offer_ViewModel.OfferType), (int)query.OfferType);
                }
            }
            builder.AppendFormat(" ORDER BY A.{0} DESC ", nameof(RB_Course_Offer_ViewModel.Id));
            return GetPage<RB_Course_Offer_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取报价单列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Course_Offer_ViewModel> GetCourseOfferListRepository(RB_Course_Offer_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" 
SELECT A.* 
FROM RB_Course_Offer AS A
WHERE 1=1
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Offer_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query == null)
            {
                return new List<RB_Course_Offer_ViewModel>();
            }
            else
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Offer_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.School_Id > 0)
                {
                    //builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Offer_ViewModel.School_Id), query.School_Id);
                }
                if (!string.IsNullOrEmpty(query.Name))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @Name ", nameof(RB_Course_Offer_ViewModel.Name));
                    parameters.Add("Name", "%" + query.Name.Trim() + "%");
                }
                if (query.CreateBy > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Offer_ViewModel.CreateBy), query.CreateBy);
                }
                if (!string.IsNullOrEmpty(query.CustomerInfo))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @CustomerInfo ", nameof(RB_Course_Offer_ViewModel.CustomerInfo));
                    parameters.Add("CustomerInfo", "%" + query.CustomerInfo.Trim() + "%");
                }
                if (query.CustomerSource > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Offer_ViewModel.CustomerSource), (int)query.CustomerSource);
                }
                if (query.CustomerType > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Offer_ViewModel.CustomerType), (int)query.CustomerType);
                }
                if (query.CustomerStatus > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Offer_ViewModel.CustomerStatus), query.CustomerStatus);
                }
                if (query.OfferType > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Offer_ViewModel.OfferType), (int)query.OfferType);
                }
            }
            return Get<RB_Course_Offer_ViewModel>(builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 生成报价单流水号
        /// </summary>
        /// <returns></returns>
        public string CreateSerialNumRepository()
        {
            int count = 0;
            string sql = " SELECT COUNT(1) FROM RB_Course_Offer ";
            var obj = base.ExecuteScalar(sql);
            if (obj != null)
            {
                Int32.TryParse(obj.ToString(), out count);
            }
            string result = DateTime.Now.ToString("yyyyMMdd") + (count + 1).ToString("D4");
            return result;
        }
    }
}