338 lines
13 KiB
C#
338 lines
13 KiB
C#
using System;
|
||
using Model;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
|
||
namespace BLL
|
||
{
|
||
/// <summary>
|
||
/// 企业级检查明细服务
|
||
/// </summary>
|
||
public static class UnitHazardRegisterItemService
|
||
{
|
||
#region Methods
|
||
|
||
/// <summary>
|
||
/// 添加明细记录
|
||
/// </summary>
|
||
/// <param name="item">明细记录</param>
|
||
public static void AddUnitHazardRegisterItem(Model.Supervise_UnitHazardRegisterItem item)
|
||
{
|
||
Funs.DB.Supervise_UnitHazardRegisterItem.InsertOnSubmit(item);
|
||
Funs.DB.SubmitChanges();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据主表ID删除所有明细
|
||
/// </summary>
|
||
/// <param name="registerId">主表ID</param>
|
||
public static void DeleteItemsByRegisterId(string registerId)
|
||
{
|
||
var items = (from x in Funs.DB.Supervise_UnitHazardRegisterItem
|
||
where x.UnitHazardRegisterId == registerId
|
||
select x).ToList();
|
||
if (items.Count > 0)
|
||
{
|
||
Funs.DB.Supervise_UnitHazardRegisterItem.DeleteAllOnSubmit(items);
|
||
Funs.DB.SubmitChanges();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除明细记录
|
||
/// </summary>
|
||
/// <param name="itemId">明细ID</param>
|
||
public static void DeleteUnitHazardRegisterItemById(string itemId)
|
||
{
|
||
Model.Supervise_UnitHazardRegisterItem item =
|
||
Funs.DB.Supervise_UnitHazardRegisterItem.FirstOrDefault(e => e.UnitHazardRegisterItemId == itemId);
|
||
if (item != null)
|
||
{
|
||
Funs.DB.Supervise_UnitHazardRegisterItem.DeleteOnSubmit(item);
|
||
Funs.DB.SubmitChanges();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据风险级别获取明细数量
|
||
/// </summary>
|
||
/// <param name="registerId">主表ID</param>
|
||
/// <param name="riskLevel">风险级别</param>
|
||
/// <returns>明细数量</returns>
|
||
public static int GetCountByRiskLevel(string registerId, string riskLevel)
|
||
{
|
||
return (from x in Funs.DB.Supervise_UnitHazardRegisterItem
|
||
where x.UnitHazardRegisterId == registerId && x.RiskLevel == riskLevel
|
||
select x).Count();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取已完成的明细数量
|
||
/// </summary>
|
||
/// <param name="registerId">主表ID</param>
|
||
/// <returns>已完成数量</returns>
|
||
public static int GetFinishedCount(string registerId)
|
||
{
|
||
return (from x in Funs.DB.Supervise_UnitHazardRegisterItem
|
||
where x.UnitHazardRegisterId == registerId && x.CompleteStatus == 1
|
||
select x).Count();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据主表ID获取明细列表
|
||
/// </summary>
|
||
/// <param name="registerId">主表ID</param>
|
||
/// <returns>明细列表</returns>
|
||
public static List<Model.Supervise_UnitHazardRegisterItem> GetItemsByRegisterId(string registerId)
|
||
{
|
||
return (from x in Funs.DB.Supervise_UnitHazardRegisterItem
|
||
where x.UnitHazardRegisterId == registerId
|
||
orderby x.SortIndex
|
||
select x).ToList();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取主单的整改状态
|
||
/// </summary>
|
||
/// <param name="registerId">主单ID</param>
|
||
/// <returns>整改状态:0=全部未整改,1=部分整改,2=全部整改</returns>
|
||
public static int GetRectifyStatus(string registerId)
|
||
{
|
||
var items = GetItemsByRegisterId(registerId);
|
||
|
||
if (items.Count == 0)
|
||
{
|
||
return 0; // 无明细,视为全部未整改
|
||
}
|
||
|
||
int finishedCount = items.Count(x => x.CompleteStatus == 1);
|
||
int totalCount = items.Count;
|
||
|
||
if (finishedCount == 0)
|
||
{
|
||
return 0; // 全部未整改
|
||
}
|
||
else if (finishedCount < totalCount)
|
||
{
|
||
return 1; // 部分整改
|
||
}
|
||
else
|
||
{
|
||
return 2; // 全部整改
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取整改状态文本
|
||
/// </summary>
|
||
/// <param name="registerId">主单ID</param>
|
||
/// <returns>状态文本</returns>
|
||
public static string GetRectifyStatusText(string registerId)
|
||
{
|
||
int status = GetRectifyStatus(registerId);
|
||
switch (status)
|
||
{
|
||
case 0:
|
||
return "全部未整改";
|
||
|
||
case 1:
|
||
return "部分整改";
|
||
|
||
case 2:
|
||
return "全部整改";
|
||
|
||
default:
|
||
return "未知";
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取未完成的明细数量
|
||
/// </summary>
|
||
/// <param name="registerId">主表ID</param>
|
||
/// <returns>未完成数量</returns>
|
||
public static int GetUnfinishedCount(string registerId)
|
||
{
|
||
return (from x in Funs.DB.Supervise_UnitHazardRegisterItem
|
||
where x.UnitHazardRegisterId == registerId && (x.CompleteStatus == null || x.CompleteStatus == 0)
|
||
select x).Count();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据明细ID获取明细记录
|
||
/// </summary>
|
||
/// <param name="itemId">明细ID</param>
|
||
/// <returns>明细记录</returns>
|
||
public static Model.Supervise_UnitHazardRegisterItem GetUnitHazardRegisterItemById(string itemId)
|
||
{
|
||
return Funs.DB.Supervise_UnitHazardRegisterItem.FirstOrDefault(e => e.UnitHazardRegisterItemId == itemId);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取企业级检查明细列表(使用input对象,返回总数)
|
||
/// </summary>
|
||
/// <param name="input">查询条件</param>
|
||
/// <param name="pageIndex">页码(从0开始)</param>
|
||
/// <param name="pageSize">每页大小</param>
|
||
/// <param name="returnAll">是否返回所有数据(不分页)</param>
|
||
/// <param name="totalCount">总记录数</param>
|
||
/// <returns>检查明细列表</returns>
|
||
public static List<UnitHazardRegisterItemOutput> GetUnitHazardRegisterItems(
|
||
UnitHazardRegisterItemInput input,
|
||
int pageIndex,
|
||
int pageSize,
|
||
bool returnAll,
|
||
out int totalCount)
|
||
{
|
||
var db = Funs.DB;
|
||
var query = from i in db.Supervise_UnitHazardRegisterItem
|
||
join r in db.Supervise_UnitHazardRegister
|
||
on i.UnitHazardRegisterId equals r.UnitHazardRegisterId
|
||
join ct in db.Base_SuperviseCheckType
|
||
on new { MainType = r.CheckMainType, TypeCode = r.CheckType }
|
||
equals new { MainType = ct.CheckMainType, TypeCode = ct.CheckTypeCode } into ctGroup
|
||
from ct in ctGroup.DefaultIfEmpty()
|
||
join u in db.Base_Unit
|
||
on r.CheckUnitId equals u.UnitId into uGroup
|
||
from u in uGroup.DefaultIfEmpty()
|
||
join user in db.Sys_User on r.InsResponsibleUserId equals user.UserId into userGroup
|
||
from user in userGroup.DefaultIfEmpty()
|
||
select new UnitHazardRegisterItemOutput
|
||
{
|
||
UnitHazardRegisterItemId = i.UnitHazardRegisterItemId,
|
||
UnitHazardRegisterCode = r.UnitHazardRegisterCode,
|
||
CheckDate = r.CheckDate,
|
||
CheckMainTypeName = r.CheckMainType == "0" ? "安全" : "质量",
|
||
CheckTypeName = ct.CheckTypeName,
|
||
ProblemDescription = i.ProblemDescription,
|
||
ProblemType = i.ProblemType,
|
||
RiskLevel = i.RiskLevel,
|
||
RectifyRequirement = i.RectifyRequirement,
|
||
StatesName = UnitHazardRegisterService.ConvertState(r.States.Value),
|
||
CheckTeam = r.CheckTeam,
|
||
ProjectId = r.ProjectId,
|
||
CheckUnitName = u.UnitName,
|
||
States = r.States,
|
||
ProblemStatesName = i.CompleteStatus==1 ? "已整改":"未整改",
|
||
ProblemStates = i.CompleteStatus,
|
||
InsResponsibleUserId = r.InsResponsibleUserId,
|
||
InsResponsibleUserName= user.UserName
|
||
};
|
||
|
||
// 应用检查大类过滤
|
||
if (input != null && !string.IsNullOrEmpty(input.CheckMainType))
|
||
{
|
||
query = query.Where(x => x.CheckMainTypeName.Contains(input.CheckMainType == "0" ? "安全" : "质量"));
|
||
}
|
||
// 项目id过滤
|
||
if (input != null && !string.IsNullOrEmpty(input.ProjectId))
|
||
{
|
||
query = query.Where(x => x.ProjectId.Contains(input.ProjectId));
|
||
}
|
||
// 检查组/人搜索
|
||
if (input != null && !string.IsNullOrEmpty(input.CheckTeam))
|
||
{
|
||
query = query.Where(x => x.CheckTeam.Contains(input.CheckTeam));
|
||
}
|
||
|
||
// 检查类别搜索
|
||
if (input != null && !string.IsNullOrEmpty(input.CheckType))
|
||
{
|
||
query = query.Where(x => x.CheckTypeName != null && x.CheckTypeName.Contains(input.CheckType));
|
||
}
|
||
|
||
// 问题描述搜索
|
||
if (input != null && !string.IsNullOrEmpty(input.Problem))
|
||
{
|
||
query = query.Where(x => x.ProblemDescription.Contains(input.Problem));
|
||
}
|
||
|
||
// 检查时间范围
|
||
if (input != null && input.StartTime.HasValue)
|
||
{
|
||
query = query.Where(x => x.CheckDate >= input.StartTime.Value);
|
||
}
|
||
|
||
if (input != null && input.EndTime.HasValue)
|
||
{
|
||
query = query.Where(x => x.CheckDate <= input.EndTime.Value);
|
||
}
|
||
|
||
// 风险级别过滤
|
||
if (input != null && !string.IsNullOrEmpty(input.RiskLevel) && input.RiskLevel != "null")
|
||
{
|
||
query = query.Where(x => x.RiskLevel == input.RiskLevel);
|
||
}
|
||
|
||
// 状态过滤
|
||
if (input != null && input.States != null)
|
||
{
|
||
query = query.Where(x => x.States == input.States);
|
||
}
|
||
// 问题整改状态过滤
|
||
if (input != null && input.ProblemStates != null)
|
||
{
|
||
query = query.Where(x => x.ProblemStates == input.ProblemStates);
|
||
}
|
||
// 单位权限过滤
|
||
if (input != null && !string.IsNullOrEmpty(input.UnitId))
|
||
{
|
||
var thisUnit = BLL.CommonService.GetIsThisUnit();
|
||
if (thisUnit.UnitId != input.UnitId)
|
||
{
|
||
// 先获取用户有权限的项目ID列表
|
||
var projectIds = (from p in Funs.DB.Base_Project
|
||
where p.UnitId == input.UnitId
|
||
select p.ProjectId).ToList();
|
||
|
||
// 过滤查询结果
|
||
query = query.Where(x => Funs.DB.Supervise_UnitHazardRegister
|
||
.Any(r => Funs.DB.Supervise_UnitHazardRegisterItem
|
||
.Any(i => i.UnitHazardRegisterItemId == x.UnitHazardRegisterItemId &&
|
||
i.UnitHazardRegisterId == r.UnitHazardRegisterId &&
|
||
projectIds.Contains(r.ProjectId))));
|
||
}
|
||
}
|
||
|
||
// 获取总数
|
||
totalCount = query.Count();
|
||
|
||
// 排序和分页
|
||
var orderedQuery = query.OrderByDescending(x => x.CheckDate);
|
||
|
||
if (returnAll)
|
||
{
|
||
return orderedQuery.ToList();
|
||
}
|
||
|
||
return orderedQuery
|
||
.Skip(pageIndex * pageSize)
|
||
.Take(pageSize)
|
||
.ToList();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新明细记录
|
||
/// </summary>
|
||
/// <param name="item">明细记录</param>
|
||
public static void UpdateUnitHazardRegisterItem(Model.Supervise_UnitHazardRegisterItem item)
|
||
{
|
||
Model.Supervise_UnitHazardRegisterItem oldItem =
|
||
Funs.DB.Supervise_UnitHazardRegisterItem.FirstOrDefault(e => e.UnitHazardRegisterItemId == item.UnitHazardRegisterItemId);
|
||
if (oldItem != null)
|
||
{
|
||
oldItem.ProblemDescription = item.ProblemDescription;
|
||
oldItem.ProblemType = item.ProblemType;
|
||
oldItem.RiskLevel = item.RiskLevel;
|
||
oldItem.RectifyRequirement = item.RectifyRequirement;
|
||
oldItem.CompleteStatus = item.CompleteStatus;
|
||
oldItem.CompletedDate = item.CompletedDate;
|
||
oldItem.SortIndex = item.SortIndex;
|
||
Funs.DB.SubmitChanges();
|
||
}
|
||
}
|
||
|
||
#endregion Methods
|
||
}
|
||
} |