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

namespace Edu.Repository.Customer
{
    /// <summary>
    /// 客户任务完成仓储层
    /// </summary>
    public class RB_Customer_FinishtaskRepository : BaseRepository<RB_Customer_Finishtask>
    {
        /// <summary>
        /// 获取客户完成任务分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Customer_Finishtask_Extend> GetCustomerFinishtaskPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Customer_Finishtask_Extend query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" 
SELECT A.*,B.TaskKudo,B.TaskKudoImg,B.KudoDescription,C.CustomerName,D.TaskName
FROM RB_Customer_Finishtask AS A INNER JOIN rb_taskdetails AS B  ON A.TaskId=B.TaskId AND A.DetailsId=B.DetailId
     INNER JOIN rb_customer AS C ON A.CustomerId=C.CustomerId
     INNER JOIN rb_task AS D ON A.TaskId=D.Id
WHERE 1=1
");
            builder.Append(" AND A.ExchangeStatus>0 ");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_Finishtask_Extend.Group_Id), query.Group_Id);
                }
                if (query.CustomerId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_Finishtask_Extend.CustomerId), query.CustomerId);
                }
                if (query.TaskId > 0)
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Customer_Finishtask_Extend.TaskId), query.TaskId);
                }
                if (query.DetailsId > 0)
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Customer_Finishtask_Extend.DetailsId), query.DetailsId);
                }
                if (!string.IsNullOrEmpty(query.QTaskIds))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Customer_Finishtask_Extend.TaskId), query.QTaskIds);
                }
                if (!string.IsNullOrEmpty(query.CustomerName))
                {
                    builder.AppendFormat(" AND C.{0} LIKE @CustomerName ", nameof(RB_Customer_Finishtask_Extend.CustomerName));
                    parameters.Add("CustomerName", "%" + query.CustomerName.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.ExchangeCode))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @ExchangeCode ", nameof(RB_Customer_Finishtask_Extend.ExchangeCode));
                    parameters.Add("ExchangeCode", "%" + query.ExchangeCode.Trim() + "%");
                }
                if (query.ExchangeStatus > 0)
                {
                    if (query.ExchangeStatus <= 2)
                    {
                        builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_Finishtask_Extend.ExchangeStatus), query.ExchangeStatus);
                    }
                    else
                    {
                        builder.AppendFormat("  AND D.ExchangeEndTime>'{0}' ", Common.ConvertHelper.FormatDate(DateTime.Now));
                    }
                }
                if (!string.IsNullOrEmpty(query.TaskName))
                {
                    builder.AppendFormat(" AND D .{0} LIKE @TaskName ", nameof(RB_Customer_Finishtask_Extend.TaskName));
                    parameters.Add("TaskName", "%" + query.TaskName.Trim() + "%");
                }
            }
            return GetPage<RB_Customer_Finishtask_Extend>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }

    }
}
