using Edu.Common.Enum.User;
using Edu.Model.Entity.User;
using Edu.Model.ViewModel.User;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Edu.Repository.User
{
    /// <summary>
    /// 审批条件扩展
    /// </summary>
    public partial class Rb_Workflow_ConditionRepository : BaseRepository<Rb_Workflow_Condition>
    {
        /// <summary>
        /// 根据模板id获取信息
        /// </summary>
        /// <param name="templateId"></param>
        /// <returns></returns>
        public List<Rb_Workflow_Condition_Extend> GetConditionByTemplateId(int templateId)
        {
            return Get<Rb_Workflow_Condition_Extend>($"SELECT * from rb_workflow_condition c where c.TemplateId = {templateId}").ToList();
        }

        /// <summary>
        /// 获取模版下默认审核条件
        /// </summary>
        /// <param name="TemplateId"></param>
        /// <returns></returns>
        public List<Rb_Workflow_Condition_Extend> GetDefaultCondition(int TemplateId)
        {
            return Get<Rb_Workflow_Condition_Extend>($@"select c.*,t.TemplateType from Rb_Workflow_Condition c left join rb_workflow_template t on c.TemplateId=t.Id where c.Sort=0 and c.TemplateId={TemplateId}").ToList();
        }

        /// <summary>
        /// 获取模版下分条件审核条件
        /// </summary>
        /// <param name="TemplateId"></param>
        /// <returns></returns>
        public List<Rb_Workflow_Condition_Extend> GetOtherConditionList(int TemplateId)
        {
            return Get<Rb_Workflow_Condition_Extend>($@"select * from Rb_Workflow_Condition where Sort!=0 and TemplateId={TemplateId} order by Sort asc").ToList();
        }

        /// <summary>
        /// 删除条件表下所有的审核人 抄送人
        /// </summary>
        /// <param name="ConditionId"></param>
        /// <param name="trans">事务对象</param>
        /// <returns></returns>
        public int DelConditionAuditAndUser(int ConditionId,IDbTransaction trans) {
            string str = $@"DELETE rb_workflow_audituser from rb_workflow_audituser,rb_workflow_audit where rb_workflow_audituser.AuditId=rb_workflow_audit.Id and rb_workflow_audit.ConditionId={ConditionId} and rb_workflow_audituser.EmType=1;";
            str += $@"DELETE from rb_workflow_audit where rb_workflow_audit.ConditionId={ConditionId};";
            str += $@"DELETE from rb_workflow_AuditUser where rb_workflow_AuditUser.AuditId={ConditionId} and rb_workflow_audituser.EmType=2;";
            return Execute(str, trans);
        }

        /// <summary>
        /// 删除分条件
        /// </summary>
        /// <param name="ConditionId"></param>
        /// <returns></returns>
        public int DelOtherCondition(int ConditionId) {
            string str = $@"DELETE rb_workflow_audituser from rb_workflow_audituser,rb_workflow_audit where rb_workflow_audituser.AuditId=rb_workflow_audit.Id and rb_workflow_audit.ConditionId={ConditionId} and rb_workflow_audituser.EmType=1;";
            str += $@"DELETE from rb_workflow_audit where rb_workflow_audit.ConditionId={ConditionId};";
            str += $@"DELETE from rb_workflow_AuditUser where rb_workflow_AuditUser.AuditId={ConditionId} and rb_workflow_audituser.EmType=2;";
            str += $@"DELETE from rb_workflow_condition where rb_workflow_condition.Id={ConditionId};";
            return Execute(str);
        }

        /// <summary>
        /// 获取模版下最大的优先级
        /// </summary>
        /// <param name="TemplateId"></param>
        /// <returns></returns>
        public int GetConditionSortMax(int TemplateId) {
            object val = ExecuteScalar($@" select Max(Sort) from rb_workflow_condition where TemplateId={TemplateId}");
            if (val != null && val.ToString() != "")
            {
                return Convert.ToInt32(val);
            }
            else {
                return 0;
            }
            
        }

        /// <summary>
        /// 获取模版下最大的优先级
        /// </summary>
        /// <param name="TemplateId"></param>
        /// <param name="ConditionId"></param>
        /// <returns></returns>
        public int GetDefaultConditionDistinct(int TemplateId,int ConditionId)
        {
            string sql = $@" select count(*) from rb_workflow_condition where Sort=0 and TemplateId={TemplateId}";
            if (ConditionId > 0)
            {
                sql= $@" select count(*) from rb_workflow_condition where Sort=0 and TemplateId={TemplateId} and Id!={ConditionId}";
            }
            object val = ExecuteScalar(sql);
            if (val != null && val.ToString() != "")
            {
                return Convert.ToInt32(val);
            }
            else
            {
                return 0;
            }
        }
    }
}
