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_CopyToPeopleRepository : BaseRepository<Rb_Workflow_CopyToPeople>
    {
        /// <summary>
        /// 抄送我的审批（通用）
        /// </summary>
        /// <param name="searchKey">搜索关键字</param>
        /// <param name="empId">员工id</param>
        /// <param name="appAuditStatus">app获取审核状态</param>
        /// <param name="readeType">抄送我的阅读状态 0全部，1未读</param>
        /// <param name="applyType">申请筛选类型</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowCount">总条数</param>
        /// <returns></returns>
        public List<Rb_Workflow_CopyToPeople_Extend> CopyToMyAudit(string searchKey, int empId, AppAuditStatusEnum appAuditStatus, int readeType, int applyType, int pageIndex, int pageSize, out long rowCount)
        {
            string sql = $@"SELECT t.WorkFlowId,t.ReadStauts,t.TemplateType from (
        (SELECT a.*,'请假' as TemplateName,e.EmName,ask.CreateTime as AuditCreateTime,ask.`Status` from rb_workflow_copytopeople a INNER  JOIN rb_workflow_askforleave ask 
        ON a.WorkFlowId =  ask.Id and a.TemplateType = 1 and a.EmployeeId = {empId}
        INNER JOIN rb_employee e ON e.EmployeeId = ask.CreateBy)UNION
        (SELECT a.*,'出差' as TemplateName,e.EmName,ask.CreateTime as AuditCreateTime,ask.`Status` from rb_workflow_copytopeople a INNER  JOIN rb_workflow_evection ask 
        ON a.WorkFlowId =  ask.Id and a.TemplateType = 2 and a.EmployeeId = {empId}
        INNER JOIN rb_employee e ON e.EmployeeId = ask.CreateBy)UNION
        (SELECT a.*,'外出' as TemplateName,e.EmName,ask.CreateTime as AuditCreateTime,ask.`Status` from rb_workflow_copytopeople a INNER  JOIN rb_workflow_goout ask 
        ON a.WorkFlowId =  ask.Id and a.TemplateType = 3 and a.EmployeeId = {empId}
        INNER JOIN rb_employee e ON e.EmployeeId = ask.CreateBy)UNION
        (SELECT a.*,'补卡' as TemplateName,e.EmName,ask.CreateTime as AuditCreateTime,ask.`Status` from rb_workflow_copytopeople a INNER  JOIN rb_workflow_goout ask 
        ON a.WorkFlowId =  ask.Id and a.TemplateType = 4 and a.EmployeeId = {empId}
        INNER JOIN rb_employee e ON e.EmployeeId = ask.CreateBy)
        ) as t";

            StringBuilder sb = new StringBuilder();
            sb.Append($"where EmployeeId = {empId} and Stauts=2 ");

            if (!string.IsNullOrWhiteSpace(searchKey))
            {
                sb.Append($" AND (TemplateName LIKE '%{searchKey}%' or EmName LIKE '%{searchKey}%')");
            }
            else
            {
                if (readeType == 1)
                {
                    sb.Append($" AND ReadStauts=1");
                }
                if (applyType != 0)
                {
                    sb.Append($" AND TemplateType={applyType}");
                }
                if (appAuditStatus == AppAuditStatusEnum.AuditComplete)
                {
                    sb.Append($" AND (`Status`={(int)WFAuditStatus.Through} Or `Status`={(int)WFAuditStatus.NotThrough})");
                }
                else if (appAuditStatus == AppAuditStatusEnum.InComplete)
                {
                    sb.Append($" AND `Status`={(int)WFAuditStatus.InReview}");
                }
                else if (appAuditStatus == AppAuditStatusEnum.ToWithdraw)
                {
                    sb.Append($" AND `Status`={(int)WFAuditStatus.ToWithdraw}");
                }
            }
            sb.Append($" ORDER BY AuditCreateTime DESC");
            return GetPage<Rb_Workflow_CopyToPeople_Extend>(pageIndex, pageSize, out rowCount, $"{sql}  {sb.ToString()}").ToList();
        }
        /// <summary>
        /// 获取申请单下所有的抄送人信息
        /// </summary>
        /// <param name="WorkFlowId"></param>
        /// <param name="TemplateType"></param>
        /// <returns></returns>
        public List<Rb_Workflow_CopyToPeople_Extend> GetCopyToPeopleList(int WorkFlowId, int TemplateType)
        {
            string where = $@" where 1=1 and c.{nameof(Rb_Workflow_CopyToPeople.WorkFlowId)}={WorkFlowId} and c.{nameof(Rb_Workflow_CopyToPeople.TemplateType)}={TemplateType}";
            return Get<Rb_Workflow_CopyToPeople_Extend>($@" select c.*,e.EmName,e.EmPhoto,e.EmAccount,e.EmLoginMobile from Rb_Workflow_CopyToPeople c left join rb_employee e on c.EmployeeId=e.EmployeeId {where}").ToList();
        }
    }
}
