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

namespace Edu.Repository.System
{
    /// <summary>
    /// 客户阶段仓储层
    /// </summary>
    public class RB_StageRepository:BaseRepository<RB_Stage>
    {
        /// <summary>
        /// 查找指定同业下，各阶段客户数量
        /// </summary>
        /// <param name="gid"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<RB_Stage_ViewModel> GetPeersCustomerStageCount(int gid, int id)
        {
            string sql = "select Id,StageName,`No`,(select Count(0) from rb_Student where CustomerId=@id and StuStage=a.Id) as ChildCount from rb_stage a where Group_Id=@gid order by `No`";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("gid", gid);
            parameters.Add("id", id);
            return Get<RB_Stage_ViewModel>(sql, parameters).ToList();
        }

        /// <summary>
        /// 获取客户阶段列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Stage> GetStageListRepostory(RB_Stage query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Stage AS A
WHERE 1=1
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Stage.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Stage.Group_Id), query.Group_Id);
                }
            }
            builder.AppendFormat(" ORDER BY A.{0} ASC ", nameof(RB_Stage.No));
            return Get<RB_Stage>(builder.ToString()).ToList();
        }
    }
}
