CNCEC_SUBQHSE_WUHUAN/SGGL/BLL/API/HSSE/APIPageDataService.cs

281 lines
13 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System;
namespace BLL
{
public static class APIPageDataService
{
#region
/// <summary>
/// 获取当前人数
/// </summary>
/// <param name="projectId">项目ID</param>
/// <returns></returns>
public static List<Model.PageDataPersonInOutItem> getPersonNum(string projectId, DateTime dateValue)
{
using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
{
var startDate = dateValue.Date;
var endDate = startDate.AddDays(1);
var query =
from x in db.SitePerson_PersonInOutNow
where x.ChangeTime >= startDate && x.ChangeTime < endDate && x.ProjectId == projectId
group x by new { x.PersonId, x.ProjectId } into g
select new
{
g.Key.PersonId,
g.Key.ProjectId,
MaxChangeTime = g.Max(x => x.ChangeTime)
};
var finalQuery =
from record in query
join detail in db.SitePerson_PersonInOutNow
on new { record.PersonId, record.ProjectId, record.MaxChangeTime }
equals new { detail.PersonId, detail.ProjectId, MaxChangeTime = detail.ChangeTime }
join y in db.SitePerson_Person on record.PersonId equals y.PersonId
join z in db.Base_WorkPost on y.WorkPostId equals z.WorkPostId
where detail.IsIn == true
select new Model.PageDataPersonInOutItem
{
PersonId = record.PersonId,
ProjectId = record.ProjectId,
ChangeTime = record.MaxChangeTime,
IsIn = true,
PostType = z.PostType
};
return finalQuery.ToList();
}
}
public static List<Model.PageDataPersonInOutItem> getPersonNum(List<string> projectIds, DateTime dateValue)
{
using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
{
var startDate = dateValue.Date;
var endDate = startDate.AddDays(1);
/* List<Model.PageDataPersonInOutItem> getSiteInOutList = new List<Model.PageDataPersonInOutItem>();
var getDayAll = from x in db.SitePerson_PersonInOutNow
join y in db.SitePerson_Person on x.PersonId equals y.PersonId
join z in db.Base_WorkPost on y.WorkPostId equals z.WorkPostId
where x.ChangeTime >= startDate &&
x.ChangeTime < endDate
select new { x.PersonId, x.ChangeTime, x.IsIn, z.PostType,x.ProjectId };
if (projectIds.Any())
{
getDayAll= getDayAll.Where(x => projectIds.Contains(x.ProjectId));
}
if (getDayAll.Any())
{
var getInMaxs = from x in getDayAll
group x by new {x.PersonId,x.ProjectId} into g
select new Model.PageDataPersonInOutItem
{
PersonId = g.First().PersonId,
ProjectId = g.First().ProjectId,
ChangeTime = g.Max(x => x.ChangeTime),
IsIn = g.First().IsIn,
PostType = g.First().PostType
};
getSiteInOutList = getInMaxs.Where(x => x.IsIn == true).ToList();
}*/
var query =
from x in db.SitePerson_PersonInOutNow
where x.ChangeTime >= startDate && x.ChangeTime < endDate
group x by new { x.PersonId, x.ProjectId } into g
select new
{
g.Key.PersonId,
g.Key.ProjectId,
MaxChangeTime = g.Max(x => x.ChangeTime)
};
if (projectIds.Any())
{
query = query.Where(x => projectIds.Contains(x.ProjectId));
}
var finalQuery =
from record in query
join detail in db.SitePerson_PersonInOutNow
on new { record.PersonId, record.ProjectId, record.MaxChangeTime }
equals new { detail.PersonId, detail.ProjectId, MaxChangeTime = detail.ChangeTime }
join y in db.SitePerson_Person on record.PersonId equals y.PersonId
join z in db.Base_WorkPost on y.WorkPostId equals z.WorkPostId
where detail.IsIn == true
select new Model.PageDataPersonInOutItem
{
PersonId= record.PersonId,
ProjectId = record.ProjectId,
ChangeTime = record.MaxChangeTime,
IsIn = true,
PostType = z.PostType
};
return finalQuery.ToList();
}
}
#endregion
#region
/// <summary>
/// 获取当前人工时
/// </summary>
/// <param name="projectId">项目ID</param>
/// <returns></returns>
public static int getSafeHours(string projectId)
{
using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
{
int safeHours = 0;
var getPersonInOutNumber = db.SitePerson_PersonInOutNumber.FirstOrDefault(x => x.ProjectId == projectId && x.InOutDate.Year == DateTime.Now.Year
&& x.InOutDate.Month == DateTime.Now.Month && x.InOutDate.Day == DateTime.Now.Day);
if (getPersonInOutNumber != null)
{ //// 获取工时
safeHours = getPersonInOutNumber.WorkHours ?? 0;
}
else
{
safeHours = getSafeWorkTime(projectId);
}
return safeHours;
}
}
#endregion
/// <summary>
/// 获取当前人工时
/// </summary>
/// <returns></returns>
public static int getSafeWorkTime(string projectId)
{
Model.SGGLDB db = Funs.DB;
int SafeHours = 0;
//// 查找当前项目 最新的人工时数量记录
int getMaxWorkHours = db.SitePerson_PersonInOutNumber.Where(x => x.ProjectId == projectId).Max(x => x.WorkHours) ?? 0;
var getAllPersonInOutList = from x in db.SitePerson_PersonInOutNow
where x.ProjectId == projectId && x.ChangeTime.Value.Year == DateTime.Now.Year && x.ChangeTime.Value.Month == DateTime.Now.Month
&& x.ChangeTime.Value.Day == DateTime.Now.Day
select x;
var getPersonOutTimes = getAllPersonInOutList.Where(x => x.IsIn == false);
var getInLists = getAllPersonInOutList.Where(x => x.IsIn == true);
if (getPersonOutTimes.Count() > 0)
{
List<string> personIdList = new List<string>();
foreach (var item in getPersonOutTimes)
{
var getMaxInTime = getInLists.Where(x => x.ChangeTime < item.ChangeTime
&& x.PersonId == item.PersonId && x.ChangeTime.Value.AddDays(1) >= item.ChangeTime).Max(x => x.ChangeTime);
if (getMaxInTime.HasValue)
{
SafeHours += Convert.ToInt32((item.ChangeTime - getMaxInTime).Value.TotalMinutes);
}
else
{
personIdList.Add(item.PersonId);
}
}
if (personIdList.Count() > 0)
{
SafeHours += (personIdList.Distinct().Count() * 8 * 60);
}
}
SafeHours = Convert.ToInt32((SafeHours) * 1.0 / 60) + getMaxWorkHours;
return SafeHours;
}
#region
/// <summary>
/// 获取当前人数
/// </summary>
/// <param name="projectId">项目ID</param>
/// <returns></returns>
public static int getPersonInNowNum(string projectId, DateTime dateValue)
{
using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
{
var getDayAll = from x in db.SitePerson_PersonInOutNow
where x.ProjectId == projectId && x.ChangeTime.Value.Year == dateValue.Year && x.ChangeTime.Value.Month == dateValue.Month
&& x.ChangeTime.Value.Day == dateValue.Day && x.IsIn == true
select x;
return getDayAll.Select(x => x.PersonId).Distinct().Count(); ;
}
}
#endregion
#region
/// <summary>
/// 获取当前现场人数
/// </summary>
/// <returns></returns>
public static List<Model.PageDataPersonInOutItem> getPersonNumByCompany(DateTime dateValue)
{
using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
{
List<Model.PageDataPersonInOutItem> getSiteInOutList = new List<Model.PageDataPersonInOutItem>();
var getDayAll = from x in db.SitePerson_PersonInOutNow
join y in db.SitePerson_Person on x.PersonId equals y.PersonId
join z in db.Base_WorkPost on y.WorkPostId equals z.WorkPostId
where x.ChangeTime.Value.Year == dateValue.Year && x.ChangeTime.Value.Month == dateValue.Month
&& x.ChangeTime.Value.Day == dateValue.Day
select new { x.PersonId, x.ChangeTime, x.IsIn, z.PostType };
if (getDayAll.Any())
{
var getInMaxs = from x in getDayAll
group x by x.PersonId into g
select new Model.PageDataPersonInOutItem
{
PersonId = g.First().PersonId,
ChangeTime = g.Max(x => x.ChangeTime),
IsIn = g.First().IsIn,
PostType = g.First().PostType
};
if (getInMaxs.Any())
{
getSiteInOutList = getInMaxs.Where(x => x.IsIn == true).ToList();
}
}
return getSiteInOutList;
}
}
public static List<Model.PageDataPersonInOutItem> getPersonNumByCompany(DateTime dateValue,string[] pids)
{
using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
{
DateTime dt1 = dateValue.Date;
DateTime dt2= dateValue.Date.AddDays(1);
List<Model.PageDataPersonInOutItem> getSiteInOutList = new List<Model.PageDataPersonInOutItem>();
var getDayAll = from x in db.SitePerson_PersonInOutNow
join y in db.SitePerson_Person on x.PersonId equals y.PersonId
join z in db.Base_WorkPost on y.WorkPostId equals z.WorkPostId
where pids.Contains(x.ProjectId) && x.ChangeTime<= dt2 && x.ChangeTime>= dt1
select new { x.PersonId, x.ChangeTime, x.IsIn, z.PostType };
if (getDayAll.Any())
{
var getInMaxs = from x in getDayAll.ToList()
group x by x.PersonId into g
select new Model.PageDataPersonInOutItem
{
PersonId = g.First().PersonId,
ChangeTime = g.Max(x => x.ChangeTime),
IsIn = g.First().IsIn,
PostType = g.First().PostType
};
if (getInMaxs.Any())
{
getSiteInOutList = getInMaxs.Where(x => x.IsIn == true).ToList();
}
}
return getSiteInOutList;
}
}
#endregion
}
}