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

namespace Edu.Repository.User
{
    /// <summary>
    /// 补卡审批模行程板扩展信息
    /// </summary>
    public partial class Rb_Workflow_ReissuecardRepository : BaseRepository<Rb_Workflow_Reissuecard>
    {
        /// <summary>
        /// 获取补卡分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="dmodel"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<Rb_Workflow_Reissuecard_Extend> GetReissuecardPageList(int pageIndex, int pageSize, Rb_Workflow_Reissuecard_Extend dmodel, out long count)
        {
            string where = $@" where 1=1 and t.{nameof(Rb_Workflow_Reissuecard_Extend.RB_GroupId)}={dmodel.RB_GroupId}";
            if (dmodel.RB_BranchId.HasValue && dmodel.RB_BranchId > -1)
            {
                where += $@" and t.{nameof(Rb_Workflow_Reissuecard_Extend.RB_BranchId) }={dmodel.RB_BranchId}";
            }
            if (dmodel.CreateBy.HasValue && dmodel.CreateBy > 0)
            {
                where += $@" and t.{nameof(Rb_Workflow_Reissuecard_Extend.CreateBy)}={dmodel.CreateBy}";
            }
            if (dmodel.Status.HasValue && (int)dmodel.Status > 0)
            {
                where += $@" and t.{nameof(Rb_Workflow_Reissuecard_Extend.Status)}={(int)dmodel.Status}";
            }
            if (dmodel.ToAuditId.HasValue && dmodel.ToAuditId > 0)
            {
                where += $@" and CONCAT(',',t.ToAuditIdStr,',') like '%,{dmodel.ToAuditId},%'";
            }
            if (dmodel.AuditedId.HasValue && dmodel.AuditedId > 0)
            {
                where += $@" and CONCAT(',',t.AuditedIdStr,',') like '%,{dmodel.AuditedId},%'";
            }
            string sql = $@"SELECT * FROM(select e.EmName,
case when ask.`Status`=1 then
(select group_concat(EmName) from rb_employee where instr(concat(',',(select case when ToAuditId='' then '0' else ToAuditId END from rb_workflow_auditrelevance audr 
where audr.WorkFlowId=ask.Id and audr.TemplateType={(int)WFTTemplateTypeEnum.ReissueCard} and audr.Stauts=1 ORDER BY audr.Sort ASC limit 0,1),','),concat(',',EmployeeId,',')))
ELSE '' END as ToAuditName,
case when ask.`Status`=1 then
(select ToAuditId from rb_workflow_auditrelevance audr where audr.WorkFlowId=ask.Id and audr.TemplateType={(int)WFTTemplateTypeEnum.ReissueCard} and audr.Stauts=1 ORDER BY audr.Sort ASC limit 0,1)
ELSE '' END as ToAuditIdStr,
(select group_concat(AuditedId) from rb_workflow_auditrelevance audr where audr.WorkFlowId=ask.Id and audr.TemplateType={(int)WFTTemplateTypeEnum.ReissueCard} and audr.AuditedId!='' ORDER BY audr.Sort ASC) as AuditedIdStr
,ask.* from rb_workflow_reissuecard ask left JOIN rb_employee e on ask.CreateBy=e.EmployeeId
)t {where}  ORDER BY t.CreateTime DESC";
            var pageList = GetPage<Rb_Workflow_Reissuecard_Extend>(pageIndex, pageSize, out count, sql).ToList();
            return pageList;
        }

        /// <summary>
        /// 根据id 获取数据详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Rb_Workflow_Reissuecard_Extend GetModel(int Id)
        {
            string where = $@" where 1=1 and ask.Id ={Id}";
            return Get<Rb_Workflow_Reissuecard_Extend>($@"select ask.*,e.EmName,e.EmAccount,e.EmLoginMobile,e.EmPhoto from Rb_Workflow_Reissuecard ask left join rb_employee e on ask.CreateBy=e.EmployeeId {where}").FirstOrDefault();
        }

        /// <summary>
        /// 根据id 获取补卡及模板详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Rb_Workflow_Reissuecard_Extend GetEntityDetial(int id)
        {
            return Get<Rb_Workflow_Reissuecard_Extend>($@"SELECT r.*,e.EmName,e.EmPhoto,t.TemplateType from rb_workflow_reissuecard r 
                    LEFT JOIN rb_employee e on r.CreateBy = e.EmployeeId
                    LEFT JOIN rb_workflow_template t on t.Id = r.TemplateId
                    where r.Id = {id}").FirstOrDefault();
        }
    }
}
