using System;
using Model;
using System.Collections.Generic;
using System.Linq;
namespace BLL
{
///
/// 企业级检查主表服务
///
public static class UnitHazardRegisterService
{
#region Fields
public static Dictionary StateMap = new Dictionary
{
{ "待提交" ,(int)StateInt.待提交},
{ "待整改" ,(int)StateInt.待整改},
{ "部分整改" ,(int)StateInt.部分整改},
{ "已闭环" ,(int)StateInt.已闭环},
};
#endregion Fields
#region Enums
public enum StateInt : int
{
待提交 = 0,
待整改= 1,
部分整改 = 2,
已闭环 = 3,
}
#endregion Enums
#region Methods
///
/// 添加企业级检查记录
///
/// 企业级检查记录
public static void AddUnitHazardRegister(Model.Supervise_UnitHazardRegister register)
{
using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
{
Model.Supervise_UnitHazardRegister newRegister = new Model.Supervise_UnitHazardRegister
{
UnitHazardRegisterId = register.UnitHazardRegisterId,
UnitHazardRegisterCode = register.UnitHazardRegisterCode,
CheckDate = register.CheckDate,
CheckMainType = register.CheckMainType,
CheckType = register.CheckType,
ProjectId = register.ProjectId,
CheckUnitId = register.CheckUnitId,
CheckTeam = register.CheckTeam,
CheckManIds = register.CheckManIds,
CheckManNames = register.CheckManNames,
InsResponsibleUserId = register.InsResponsibleUserId,
EvaluationResult = register.EvaluationResult,
AttachUrl = register.AttachUrl,
States = register.States??0,
CompileMan = register.CompileMan,
CreateDate = DateTime.Now
};
db.Supervise_UnitHazardRegister.InsertOnSubmit(newRegister);
db.SubmitChanges();
}
}
public static string ConvertState(int state)
{
string result = string.Empty;
result = StateMap.First(c => c.Value == state).Key;
return result;
}
///
/// 删除企业级检查记录
///
/// 检查记录ID
public static void DeleteUnitHazardRegisterById(string registerId)
{
using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
{
var register = db.Supervise_UnitHazardRegister.FirstOrDefault(e => e.UnitHazardRegisterId == registerId);
if (register != null)
{
// 删除附件
if (!string.IsNullOrEmpty(register.AttachUrl))
{
BLL.UploadFileService.DeleteFile(Funs.RootPath, register.AttachUrl);
}
// 删除编码记录
BLL.CodeRecordsService.DeleteCodeRecordsByDataId(registerId);
// 删除附件
BLL.CommonService.DeleteAttachFileById(registerId);
// 删除流程
BLL.CommonService.DeleteFlowOperateByID(registerId);
db.Supervise_UnitHazardRegister.DeleteOnSubmit(register);
db.SubmitChanges();
}
}
}
///
/// 根据项目和时间段获取检查数量
///
/// 项目ID
/// 开始时间
/// 结束时间
/// 检查数量
public static int GetCount(string projectId, DateTime startTime, DateTime endTime)
{
return (from x in Funs.DB.Supervise_UnitHazardRegister
where x.ProjectId == projectId && x.CheckDate >= startTime && x.CheckDate <= endTime
select x).Count();
}
public static int GetCount(string checkMainType)
{
return (from x in Funs.DB.Supervise_UnitHazardRegister
where x.CheckMainType ==checkMainType
select x.UnitHazardRegisterId).Count();
}
///
/// 根据ID获取企业级检查记录
///
/// 检查记录ID
/// 企业级检查记录
public static Model.Supervise_UnitHazardRegister GetUnitHazardRegisterById(string registerId)
{
return Funs.DB.Supervise_UnitHazardRegister.FirstOrDefault(e => e.UnitHazardRegisterId == registerId);
}
///
/// 判断是否可以删除检查记录
///
/// 检查记录ID
/// true=可以删除,false=不能删除
public static bool CanDeleteRegister(string registerId)
{
var register = GetUnitHazardRegisterById(registerId);
if (register == null)
{
return false;
}
// 只有"待提交"状态可以删除,其他状态不能删除
return register.States <= (int)StateInt.待提交;
}
///
/// 获取企业级检查列表(使用input对象,返回总数)
///
/// 查询条件
/// 页码(从0开始)
/// 每页大小
/// 是否返回所有数据(不分页)
/// 总记录数
/// 检查记录列表
public static List GetUnitHazardRegisters(
UnitHazardRegisterInput input,
int pageIndex,
int pageSize,
bool returnAll,
out int totalCount)
{
var db = Funs.DB;
// 使用JOIN一次性获取所有数据,解决N+1查询问题
var query = from x in db.Supervise_UnitHazardRegister
join ct in db.Base_SuperviseCheckType
on new { MainType = x.CheckMainType, TypeCode = x.CheckType }
equals new { MainType = ct.CheckMainType, TypeCode = ct.CheckTypeCode } into ctGroup
from ct in ctGroup.DefaultIfEmpty()
join u in db.Base_Unit
on x.CheckUnitId equals u.UnitId into uGroup
from u in uGroup.DefaultIfEmpty()
join p in db.Base_Project on x.ProjectId equals p.ProjectId into pGroup
from p in pGroup.DefaultIfEmpty()
select new UnitHazardRegisterOutput
{
UnitHazardRegisterId = x.UnitHazardRegisterId,
UnitHazardRegisterCode = x.UnitHazardRegisterCode,
CheckDate = x.CheckDate,
ProjectId = x.ProjectId,
CheckMainTypeName = x.CheckMainType == "0" ? "安全" : "质量",
CheckType = x.CheckType,
CheckTypeName = ct.CheckTypeName,
CheckObjectText = p.ProjectName,
CheckUnitName = u.UnitName,
CheckTeam = x.CheckTeam,
EvaluationResult = x.EvaluationResult,
StatesName = ConvertState(x.States.Value),
AttachUrl = x.AttachUrl,
};
// 应用检查大类过滤
if (input != null && !string.IsNullOrEmpty(input.CheckMainType))
{
query = query.Where(x => x.CheckMainTypeName.Contains(input.CheckMainType == "0" ? "安全" : "质量"));
}
// 应用文本搜索
if (input != null && !string.IsNullOrEmpty(input.SearchText))
{
query = query.Where(x => x.UnitHazardRegisterCode.Contains(input.SearchText) ||
x.CheckObjectText.Contains(input.SearchText) ||
x.CheckTeam.Contains(input.SearchText) ||
x.EvaluationResult.Contains(input.SearchText));
}
// 应用检查类别过滤
if (input != null && !string.IsNullOrEmpty(input.CheckType) && input.CheckType != BLL.Const._Null)
{
query = query.Where(x => x.CheckType == input.CheckType);
}
// 项目权限过滤
if (input != null && !string.IsNullOrEmpty(input.ProjectId))
{
query = query.Where(x => x.ProjectId != null && x.ProjectId == input.ProjectId);
}
// 获取总数
totalCount = query.Count();
// 排序和分页
var orderedQuery = query.OrderByDescending(x => x.CheckDate);
if (returnAll)
{
return orderedQuery.ToList();
}
return orderedQuery
.Skip(pageIndex * pageSize)
.Take(pageSize)
.ToList();
}
///
/// 更新企业级检查记录
///
/// 企业级检查记录
public static void UpdateUnitHazardRegister(Model.Supervise_UnitHazardRegister register)
{
using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
{
Model.Supervise_UnitHazardRegister newRegister =
db.Supervise_UnitHazardRegister.FirstOrDefault(e => e.UnitHazardRegisterId == register.UnitHazardRegisterId);
if (newRegister != null)
{
newRegister.UnitHazardRegisterCode = register.UnitHazardRegisterCode;
newRegister.CheckDate = register.CheckDate;
newRegister.CheckMainType = register.CheckMainType;
newRegister.CheckType = register.CheckType;
newRegister.ProjectId = register.ProjectId;
newRegister.CheckUnitId = register.CheckUnitId;
newRegister.CheckTeam = register.CheckTeam;
newRegister.CheckManIds = register.CheckManIds;
newRegister.CheckManNames = register.CheckManNames;
newRegister.EvaluationResult = register.EvaluationResult;
newRegister.AttachUrl = register.AttachUrl;
newRegister.States = register.States;
newRegister.InsResponsibleUserId = register.InsResponsibleUserId;
newRegister.UpdateDate = DateTime.Now;
db.SubmitChanges();
}
}
}
///
/// 更新主单状态(根据明细的整改情况)
///
public static void UpdateRegisterStatus(string registerId)
{
// 获取主单的所有明细的整改状态
int rectifyStatus = BLL.UnitHazardRegisterItemService.GetRectifyStatus(registerId);
var register = BLL.UnitHazardRegisterService.GetUnitHazardRegisterById(registerId);
if (register != null)
{
// 根据整改状态更新主单状态
switch (rectifyStatus)
{
case 0: // 全部未整改
register.States = (int)UnitHazardRegisterService.StateInt.待整改; // 待整改
break;
case 1: // 部分整改
register.States = (int)UnitHazardRegisterService.StateInt.部分整改; ; // 待整改
break;
case 2: // 全部整改
register.States = (int)UnitHazardRegisterService.StateInt.已闭环; ; // 已完成
break;
}
BLL.UnitHazardRegisterService.UpdateUnitHazardRegister(register);
}
}
#endregion Methods
}
}