using System.Collections.Generic;
using System.Linq;
using Edu.Model.Entity.Finance;
using Edu.Model.ViewModel.Finance;

namespace Edu.Repository.Finance
{
    /// <summary>
    /// 财务审批流程模版仓储层
    /// </summary>
    public class Rb_Workflow_TemplateRepository : BaseRepository<Rb_Workflow_Template>
    {
        /// <summary>
        /// 获取我审批的，待我审批的。
        /// </summary>
        /// <param name="model"></param>
        /// <param name="FrIdStr"></param>
        /// <returns></returns>
        public List<Rb_Workflow_Auditrecord> GetAgreeOrWaitList(Rb_Workflow_Auditrecord model, string FrIdStr = "")
        {
            string where = " WHERE 1=1 ";
            //审核人ID
            if (model.AuditEmId > 0)
            {
                where += string.Format(" AND " + nameof(Rb_Workflow_Auditrecord.AuditEmId) + "={0}", model.AuditEmId);
            }
            if (model.WorkFlowId > 0)
            {
                where += string.Format(" AND " + nameof(Rb_Workflow_Auditrecord.WorkFlowId) + "={0}", model.WorkFlowId);
            }
            if (!string.IsNullOrWhiteSpace(FrIdStr))
            {
                where += string.Format(" AND " + nameof(Rb_Workflow_Auditrecord.WorkFlowId) + " in({0})", FrIdStr);
            }
            if (model.AuditStatus > 0)
            {
                where += string.Format(" AND " + nameof(Rb_Workflow_Auditrecord.AuditStatus) + "={0}", (int)model.AuditStatus);
            }
            if (model.TemplateType > 0)
            {
                where += string.Format(" AND " + nameof(Rb_Workflow_Auditrecord.TemplateType) + "={0}", (int)model.TemplateType);
            }
            string sql = string.Format(" SELECT * FROM {0} {1}", nameof(Rb_Workflow_Auditrecord), where);
            return Get<Rb_Workflow_Auditrecord>(sql).ToList();
        }

        /// <summary>
        /// 获取模板列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<Rb_Workflow_Template_Finance_Extend> GetList(Rb_Workflow_Template_Finance_Extend model)
        {
            string where = " WHERE 1=1 ";
            if (model.Status > 0) //状态==0的时候查询停用和正常的
            {
                where += string.Format(" AND " + nameof(Rb_Workflow_Template.Status) + "={0}", (int)model.Status);
            }
            else
            {
                where += string.Format(" AND " + nameof(Rb_Workflow_Template.Status) + "!={0}", 3);
            }
            if (model.Id > 0)
            {
                where += string.Format(" AND " + nameof(Rb_Workflow_Template.Id) + "={0}", model.Id);
            }
            if (model.Type > 0)
            {
                where += string.Format(" AND " + nameof(Rb_Workflow_Template.Type) + "={0}", (int)model.Type);
            }
            if (model.RB_GroupId > 0)
            {
                where += string.Format(" AND " + nameof(Rb_Workflow_Template.RB_GroupId) + "={0}", model.RB_GroupId);
            }
            if (model.RB_BranchId >= 0)
            {
                where += string.Format(" AND " + nameof(Rb_Workflow_Template.RB_BranchId) + "={0}", model.RB_BranchId);
            }
            if (model.TemplateType.HasValue && model.TemplateType.Value > 0)
            {
                where += string.Format(" AND " + nameof(Rb_Workflow_Template.TemplateType) + "={0}", (int)model.TemplateType);
            }
            if (!string.IsNullOrEmpty(model.TemplateIds))
            {
                where += string.Format(" AND " + nameof(Rb_Workflow_Template.Id) + " in ({0})", model.TemplateIds);
            }
            if (model.TemplateSorce > 0)
            {
                where += string.Format(" AND " + nameof(Rb_Workflow_Template.TemplateSorce) + "={0}", model.TemplateSorce);
            }
            string sql = string.Format(" SELECT * FROM Rb_Workflow_Template {0}", where);
            return Get<Rb_Workflow_Template_Finance_Extend>(sql).ToList();
        }
    }
}
