using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BLL
{
    /// 
    /// 考勤
    /// 
    public static class InOutService
    {
        public static Model.SGGLDB db = Funs.DB;
        /// 
        /// 记录数
        /// 
        public static int count
        {
            get;
            set;
        }
        /// 
        /// 定义变量
        /// 
        private static IQueryable qq = from x in db.T_d_facerecord
                                                             orderby x.DateTimeRecord descending
                                                             select x;
        private static IQueryable qq1 = from x in db.T_d_validcardevent
                                                                  orderby x.RecordDateTime descending
                                                                  select x;
        /// 
        /// 获取分页列表
        /// 
        /// 页码
        /// 每页数量
        /// 
        public static IEnumerable getListData(string projectId, string type, string unitId, string name, string startDate, string endDate, int PageIndex, int PageSize)
        {
            if (type == "1")
            {
                IQueryable q1 = qq1.Where(x => x.ProjectId == projectId);
                if (!string.IsNullOrEmpty(startDate))
                {
                    q1 = q1.Where(e => e.RecordDateTime >= Funs.GetNewDateTime(startDate));
                }
                if (!string.IsNullOrEmpty(endDate))
                {
                    q1 = q1.Where(e => e.RecordDateTime <= Funs.GetNewDateTime(endDate));
                }
                if (!string.IsNullOrEmpty(unitId))
                {
                    q1 = from x in q1
                         join y in db.SitePerson_Person on x.IDCardNo equals y.IdentityCard
                         where y.UnitId == unitId
                         select x;
                }
                if (!string.IsNullOrEmpty(name))
                {
                    q1 = q1.Where(e => e.EmployName.Contains(name));
                }
                count = q1.Count();
                if (count == 0)
                {
                    return null;
                }
                return from x in q1.Skip(PageSize * PageIndex).Take(PageSize)
                       select new
                       {
                           x.NewID,
                           x.ProjectId,
                           x.EmployName,
                           x.DepartmentID,
                           DepartName = x.DepartmentName,
                           x.AreaID,
                           x.AreaName,
                           x.EmployNO,
                           CardID = x.CardNO,
                           DateTimeRecord = x.RecordDateTime,
                           RecordDes = x.RecordAll,
                           InOrOut = (x.InOrOut == 2 ? "出门" : "进门"),
                       };
            }
            else
            {
                IQueryable q = qq.Where(x => x.ProjectId == projectId);
                if (type == "2")
                {
                    q = q.Where(e => e.RoleID == "微信端");
                }
                else if (type == "3")
                {
                    q = q.Where(e => e.RoleID == "手动");
                }
                else
                {
                    q = q.Where(e => e.RoleID == "白名单");
                }
                if (!string.IsNullOrEmpty(startDate))
                {
                    q = q.Where(e => e.DateTimeRecord >= Funs.GetNewDateTime(startDate));
                }
                if (!string.IsNullOrEmpty(endDate))
                {
                    q = q.Where(e => e.DateTimeRecord <= Funs.GetNewDateTime(endDate));
                }
                if (!string.IsNullOrEmpty(unitId))
                {
                    q = from x in q
                        join y in db.SitePerson_Person on x.EmployNO equals y.IdentityCard
                        where y.UnitId == unitId
                        select x;
                }
                if (!string.IsNullOrEmpty(name))
                {
                    q = q.Where(e => e.EmployName.Contains(name));
                }
                count = q.Count();
                if (count == 0)
                {
                    return null;
                }
                return from x in q.Skip(PageSize * PageIndex).Take(PageSize)
                       select new
                       {
                           x.NewID,
                           x.ProjectId,
                           x.EmployName,
                           x.DepartmentID,
                           x.DepartName,
                           x.AreaID,
                           x.AreaName,
                           x.EmployNO,
                           x.CardID,
                           x.DateTimeRecord,
                           x.RecordDes,
                           x.InOrOut,
                       };
            }
        }
    }
}