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_EvectionRepository : BaseRepository<Rb_Workflow_Evection>
    {
        /// <summary>
        /// 获取出差分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="dmodel"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<Rb_Workflow_Evection_Extend> GetEvectionPageList(int pageIndex, int pageSize, Rb_Workflow_Evection_Extend dmodel, out long count)
        {
            string where = $@" where 1=1 and t.{nameof(Rb_Workflow_Evection_Extend.RB_GroupId)}={dmodel.RB_GroupId}";
            if (dmodel.RB_BranchId.HasValue && dmodel.RB_BranchId > -1)
            {
                where += $@" and t.{nameof(Rb_Workflow_Evection_Extend.RB_BranchId) }={dmodel.RB_BranchId}";
            }
            if (dmodel.CreateBy.HasValue && dmodel.CreateBy > 0)
            {
                where += $@" and t.{nameof(Rb_Workflow_Evection_Extend.CreateBy)}={dmodel.CreateBy}";
            }
            if (dmodel.Status.HasValue && (int)dmodel.Status > 0)
            {
                where += $@" and t.{nameof(Rb_Workflow_Evection_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.Evection} 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.Evection} 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.Evection} and audr.AuditedId!='' ORDER BY audr.Sort ASC) as AuditedIdStr
,ask.* from rb_workflow_evection ask left JOIN rb_employee e on ask.CreateBy=e.EmployeeId
)t {where}  ORDER BY t.CreateTime DESC";
            var pageList = GetPage<Rb_Workflow_Evection_Extend>(pageIndex, pageSize, out count, sql).ToList();
            return pageList;
        }


        /// <summary>
        /// 根据id 获取数据详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Rb_Workflow_Evection_Extend GetModel(int Id)
        {
            string where = $@" where 1=1 and ask.Id ={Id}";
            return Get<Rb_Workflow_Evection_Extend>($@"select ask.*,e.EmName,e.EmAccount,e.EmLoginMobile,e.EmPhoto from Rb_Workflow_Evection 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_Evection_Extend GetEntityDetial(int id)
        {
            return Get<Rb_Workflow_Evection_Extend>($@"SELECT we.*,e.EmName,e.EmPhoto,t.TemplateType from rb_workflow_evection we 
                    LEFT JOIN rb_employee e on we.CreateBy = e.EmployeeId
                    LEFT JOIN rb_workflow_template t on t.Id = we.TemplateId
                    where we.Id = {id}").FirstOrDefault();
        }

        /// <summary>
        /// 验证改时间段是否有重复数据
        /// </summary>
        /// <param name="empId">员工id</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>true 有重复数据,fasle没有重复数据</returns>
        public bool ValverifyTime(int empId, string startTime, string endTime)
        {
            return Get<Rb_Workflow_Evection_Extend>($"SELECT e.Id from rb_workflow_evection e INNER JOIN rb_workflow_travel t on e.Id = t.EvectionId  where e.CreateBy = {empId} and (('{startTime}'>=t.StartTime and '{startTime}'<=t.EndTime) or ('{endTime}'>=t.StartTime and '{endTime}'<=t.EndTime)) and (((e.`Status` = 1 or e.`Status` = 5) AND e.IsCancel = 1) or ((e.`Status` = 1 or e.`Status` = 4 ) AND e.IsCancel = 2))").Count() > 0 ? true : false;
        }
        /// <summary>
        /// 获取当月出差所有的数据
        /// </summary>
        /// <param name="uid">用户编号</param>
        /// <param name="month">月份</param>
        /// <returns></returns>
        public List<Rb_Workflow_Travel_Extend> GetSignInInfoForEvection(int uid,string month) {
            string Month = Convert.ToDateTime(month).ToString("yyyy-MM");
            return Get<Rb_Workflow_Travel_Extend>($@"select t.* from rb_workflow_evection e left join rb_workflow_travel t on e.Id=t.EvectionId where e.Status in (1,5) and e.IsCancel=1 and e.CreateBy={uid} and (DATE_FORMAT(t.StartTime,'%Y-%m')='{Month}' or DATE_FORMAT(t.EndTime,'%Y-%m')='{Month}')").ToList();
        }
    }
}