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

        /// <summary>
        /// 根据id 获取数据详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Rb_Workflow_GoOut_Extend GetModel(int Id)
        {
            string where = $@" where 1=1 and ask.Id ={Id}";
            return Get<Rb_Workflow_GoOut_Extend>($@"select ask.*,e.EmName,e.EmAccount,e.EmLoginMobile,e.EmPhoto from Rb_Workflow_GoOut 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_GoOut_Extend GetEntityDetial(int id)
        {
            return Get<Rb_Workflow_GoOut_Extend>($@"SELECT g.*,e.EmName,e.EmPhoto,t.TemplateType from rb_workflow_goout g 
                    LEFT JOIN rb_employee e on g.CreateBy = e.EmployeeId
                    LEFT JOIN rb_workflow_template t on t.Id = g.TemplateId
                    where g.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_GoOut_Extend>($"SELECT Id from rb_workflow_goout where CreateBy = {empId} and (('{startTime}'>=StartTime and '{startTime}'<=EndTime) or ('{endTime}'>=StartTime and '{endTime}'<=EndTime)) and (((`Status` = 1 or `Status` = 5) AND IsCancel = 1) or ((`Status` = 1 or `Status` = 4 ) AND IsCancel = 2))").Count() > 0 ? true : false;
        }
        /// <summary>
        /// 根据用户id获取当月所有的外出数据
        /// </summary>
        /// <param name="uid">用户编号</param>
        /// <param name="month">月份</param>
        /// <returns></returns>
        public List<Rb_Workflow_GoOut_Extend> GetSignInInfoForGoout(int uid,string month) {
            string Month = Convert.ToDateTime(month).ToString("yyyy-MM");
            return Get<Rb_Workflow_GoOut_Extend>($@"select * from rb_workflow_goout where Status in (1,5) and IsCancel=1 and CreateBy={uid} and (DATE_FORMAT(StartTime,'%Y-%m')='{Month}' or DATE_FORMAT(EndTime,'%Y-%m')='{Month}')").ToList();
        }
    }
}
