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

namespace Edu.Repository.WeChat
{
    /// <summary>
    /// 企业微信客户旅程仓储层
    /// </summary>
    [Serializable]
    [DB(ConnectionName = "DefaultConnection")]
    public class RB_WeChat_CustomerTripRepository : BaseRepository<RB_WeChat_CustomerTrip>
    {
        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_WeChat_CustomerTrip_ViewModel> GetPageList(int pageIndex,int pageSize,out long count, RB_WeChat_CustomerTrip_ViewModel demodel)
        {
            DynamicParameters parameters = new DynamicParameters();
            string where = $@" 1=1 and r.{nameof(RB_WeChat_CustomerTrip_ViewModel.Status)} =0";
            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_CustomerTrip_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.Type > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_CustomerTrip_ViewModel.Type)} ={(int)demodel.Type}";
            }
            if (demodel.CustomerId > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_CustomerTrip_ViewModel.CustomerId)} ={demodel.CustomerId}";
            }

            string sql = $@" 
SELECT * From RB_WeChat_CustomerTrip r
WHERE {where} 
ORDER BY r.Id DESC ";
            return GetPage<RB_WeChat_CustomerTrip_ViewModel>(pageIndex, pageSize, out count, sql, parameters).ToList();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_WeChat_CustomerTrip_ViewModel> GetList(RB_WeChat_CustomerTrip_ViewModel demodel)
        {
            DynamicParameters parameters = new DynamicParameters();
            string where = $@" 1=1 and r.{nameof(RB_WeChat_CustomerTrip_ViewModel.Status)} =0";
            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_CustomerTrip_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.Type > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_CustomerTrip_ViewModel.Type)} ={(int)demodel.Type}";
            }
            if (demodel.CustomerId > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_CustomerTrip_ViewModel.CustomerId)} ={demodel.CustomerId}";
            }

            string sql = $@" 
SELECT * From RB_WeChat_CustomerTrip r
WHERE {where} 
ORDER BY r.Id DESC ";
            return Get<RB_WeChat_CustomerTrip_ViewModel>(sql, parameters).ToList();
        }

        /// <summary>
        /// 获取最后一次跟进时间
        /// </summary>
        /// <param name="group_Id"></param>
        /// <param name="customerIds"></param>
        /// <returns></returns>
        public List<RB_WeChat_CustomerTrip_ViewModel> GetLastFollowUpTimeList(int group_Id, string customerIds)
        {
            string sql = $@"SELECT CustomerId , MAX(CreateTime) AS CreateTime FROM rb_wechat_customertrip 
WHERE `Status` =0 and Group_Id ={group_Id} and Type =3 and CustomerId in ({customerIds}) GROUP BY CustomerId";
            return Get<RB_WeChat_CustomerTrip_ViewModel>(sql).ToList();
        }
    }
}
