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_SignInRepository : BaseRepository<Rb_Workflow_SignIn>
    {
        /// <summary>
        /// 获取签到列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="dmodel"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<Rb_Workflow_SignIn_Extend> GetPageList(int pageIndex, int pageSize, Rb_Workflow_SignIn_Extend dmodel, out long count)
        {
            string where = $@" where 1=1 and t.{nameof(Rb_Workflow_SignIn.RB_GroupId)}={dmodel.RB_GroupId}";
            if (dmodel.RB_BranchId.HasValue && dmodel.RB_BranchId > -1)
            {
                where += $@" and t.{nameof(Rb_Workflow_SignIn.RB_BranchId) }={dmodel.RB_BranchId}";
            }
            if (dmodel.EmployeeId.HasValue && dmodel.EmployeeId > 0)
            {
                where += $@" and t.{nameof(Rb_Workflow_SignIn.EmployeeId)}={dmodel.EmployeeId}";
            }
            if (!string.IsNullOrWhiteSpace(dmodel.StartTime))
            {
                where += $@" and t.{nameof(Rb_Workflow_SignIn.SignInTime)}>='{dmodel.StartTime+" 00:00:00"}'";
            }
            if (!string.IsNullOrWhiteSpace(dmodel.EndTime))
            {
                where += $@" and t.{nameof(Rb_Workflow_SignIn.SignInTime)}<='{dmodel.EndTime + " 23:59:59"}'";
            }
            string sql = $@"SELECT t.*,e.EmName,e.BName FROM Rb_Workflow_SignIn t left join rb_employee e on t.EmployeeId=e.EmployeeId {where} ";
            if (dmodel.orderBy.HasValue && dmodel.orderBy == 1)
            {
                sql += $@" ORDER BY t.SignInTime asc";
            }
            else {
                sql += $@" ORDER BY t.SignInTime DESC";
            }
            var pageList = GetPage<Rb_Workflow_SignIn_Extend>(pageIndex, pageSize, out count, sql).ToList();
            return pageList;
        }
        /// <summary>
        /// 签到管理
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="dmodel"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<Rb_Workflow_SignIn_Extend> GetSignInManagementPageList(int pageIndex, int pageSize, Rb_Workflow_SignIn_Extend dmodel, out long count)
        {
            string where = $@" and {nameof(Rb_Workflow_SignIn.RB_GroupId)}={dmodel.RB_GroupId}";
            if (dmodel.RB_BranchId.HasValue && dmodel.RB_BranchId > -1)
            {
                where += $@" and {nameof(Rb_Workflow_SignIn.RB_BranchId) }={dmodel.RB_BranchId}";
            }
            if (dmodel.EmployeeId.HasValue && dmodel.EmployeeId > 0)
            {
                where += $@" and {nameof(Rb_Workflow_SignIn.EmployeeId)}={dmodel.EmployeeId}";
            }
            if (!string.IsNullOrWhiteSpace(dmodel.StartTime))
            {
                where += $@" and {nameof(Rb_Workflow_SignIn.SignInTime)}>='{dmodel.StartTime + " 00:00:00"}'";
            }
            if (!string.IsNullOrWhiteSpace(dmodel.EndTime))
            {
                where += $@" and {nameof(Rb_Workflow_SignIn.SignInTime)}<='{dmodel.EndTime + " 23:59:59"}'";
            }
            string sql = string.Format(@"
SELECT s.EmployeeId,s.SignTime,e.EmName,e.BName
       ,DATE_FORMAT(s_start.SignInTime,'%H:%i') AS StartTime,IFNULL(s_start.Address,'') AS StartAddress
       ,DATE_FORMAT(s_end.SignInTime,'%H:%i') AS EndTime,IFNULL(s_end.Address,'') AS EndAddress
FROM
	(
		SELECT EmployeeId,DATE_FORMAT(SignInTime,'%Y-%m-%d') AS SignTime,MIN(Id) AS MinId,MAX(Id) AS MaxId
		FROM rb_workflow_signin 
        WHERE 1=1 {0}
		GROUP BY EmployeeId,DATE_FORMAT(SignInTime,'%Y-%m-%d')
	) AS s 
  LEFT JOIN rb_employee e on s.EmployeeId=e.EmployeeId  
  LEFT JOIN rb_workflow_signin AS s_start ON s.MinId=s_start.Id
  LEFT JOIN rb_workflow_signin AS s_end ON s.MaxId=s_end.Id ",where);
            var pageList = GetPage<Rb_Workflow_SignIn_Extend>(pageIndex, pageSize, out count, sql).ToList();
            return pageList;
        }
    }
}
