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_CustomerClueRuleRepository : BaseRepository<RB_WeChat_CustomerClueRule>
    {

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

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

        /// <summary>
        /// 执行排序+1
        /// </summary>
        /// <param name="group_Id"></param>
        /// <param name="id"></param>
        public void UpdateLastClueRuleSortAdd(int group_Id, int id, int minSort =0, int maxSort =0)
        {
            string sql = $@" update RB_WeChat_CustomerClueRule set Sort = Sort +1 where Status =0 and Group_Id ={group_Id} and Sort >0 and Id <>{id}";
            if (minSort > 0) {
                sql += $" and Sort >=" + minSort;
            }
            if (maxSort > 0) {
                sql += $" and Sort <=" + maxSort;
            }
            Execute(sql);
        }

        /// <summary>
        /// 执行排序-1
        /// </summary>
        /// <param name="group_Id"></param>
        /// <param name="id"></param>
        /// <param name="minSort"></param>
        /// <param name="maxSort"></param>
        public void UpdateLastClueRuleSortSubtract(int group_Id, int id, int minSort = 0, int maxSort = 0)
        {
            string sql = $@" update RB_WeChat_CustomerClueRule set Sort = Sort -1 where Status =0 and Group_Id ={group_Id} and Sort >0 and Id <>{id}";
            if (minSort > 0)
            {
                sql += $" and Sort >=" + minSort;
            }
            if (maxSort > 0)
            {
                sql += $" and Sort <=" + maxSort;
            }
            Execute(sql);
        }
    }
}
