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.Dapper;

namespace Edu.Repository.WeChat
{
    /// <summary>
    /// 企业微信客户阶段流程仓储层
    /// </summary>
    public class RB_WeChat_CustomerStageFlowRepository : BaseRepository<RB_WeChat_CustomerStageFlow>
    {

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_WeChat_CustomerStageFlow_ViewModel> GetList(RB_WeChat_CustomerStageFlow_ViewModel demodel)
        {
            DynamicParameters parameters = new DynamicParameters();
            string where = $@" 1=1 and r.{nameof(RB_WeChat_CustomerStageFlow_ViewModel.Status)} =0";
            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_CustomerStageFlow_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.Enable > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_CustomerStageFlow_ViewModel.Enable)} ={demodel.Enable}";
            }
            if (demodel.IsDefault > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_CustomerStageFlow_ViewModel.IsDefault)} ={demodel.IsDefault}";
            }
            if (demodel.StageId > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_CustomerStageFlow_ViewModel.IsDefault)} <>1";
                where += $@" and FIND_IN_SET('{demodel.StageId}',r.{nameof(RB_WeChat_CustomerStageFlow_ViewModel.UseStage)})";
            }
            if (demodel.LoseId > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_CustomerStageFlow_ViewModel.IsDefault)} <>1";
                where += $@" and FIND_IN_SET('{demodel.LoseId}',r.{nameof(RB_WeChat_CustomerStageFlow_ViewModel.LoseCause)})";
            }
            if (demodel.InvalidId > 0)
            {
                where += $@" and r.{nameof(RB_WeChat_CustomerStageFlow_ViewModel.IsDefault)} <>1";
                where += $@" and FIND_IN_SET('{demodel.InvalidId}',r.{nameof(RB_WeChat_CustomerStageFlow_ViewModel.InvalidCause)})";
            }

            if (!string.IsNullOrEmpty(demodel.FlowName))
            {
                where += $@" and r.{nameof(RB_WeChat_CustomerStageFlow_ViewModel.FlowName)} like @FlowName";
                parameters.Add("FlowName", "%" + demodel.FlowName + "%");
            }

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