Files
SGGL_JT/SUBQHSE/BLL/DataShare/HSSE/APIHazardRegisterSyncService.cs
T

468 lines
24 KiB
C#
Raw Normal View History

2026-03-06 14:24:20 +08:00
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Model;
using Newtonsoft.Json;
using RestSharp;
using System.Net;
2026-03-23 15:22:29 +08:00
namespace BLL
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
public class APIHazardRegisterSyncService
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
#region
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
public static List<Model.HazardRegisterSyncItem> GetHazardRegisterLitsByprojectIdUnitIdPage(string projectId,
string unitId, string dataId = "")
{
using (Model.SUBQHSEDB db = new Model.SUBQHSEDB(Funs.ConnString))
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
var list = from x in db.HSSE_Hazard_HazardRegister where x.ProjectId == projectId select x;
if (!string.IsNullOrEmpty(unitId))
{
list = list.Where(x => x.ResponsibleUnit == unitId);
}
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
if (!string.IsNullOrEmpty(dataId))
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
list = list.Where(x => x.HazardRegisterId == dataId);
}
var dataList = (from x in list
join ht in db.HSSE_Hazard_HazardRegisterTypes on x.RegisterTypesId equals ht.RegisterTypesId into htTemp
from ht in htTemp.DefaultIfEmpty()
join uw in db.WBS_UnitWork on x.Place equals uw.UnitWorkId into uwTemp
from uw in uwTemp.DefaultIfEmpty()
join u1 in db.Sys_User on x.ResponsibleMan equals u1.UserId into u1Temp
from u1 in u1Temp.DefaultIfEmpty()
join u2 in db.Sys_User on x.CheckManId equals u2.UserId into u2Temp
from u2 in u2Temp.DefaultIfEmpty()
join u3 in db.Sys_User on x.ConfirmMan equals u3.UserId into u3Temp
from u3 in u3Temp.DefaultIfEmpty()
join u4 in db.Sys_User on x.ResponsibleMan2 equals u4.UserId into u4Temp
from u4 in u4Temp.DefaultIfEmpty()
join att1 in db.AttachFile on x.HazardRegisterId equals att1.ToKeyId into att1Temp
from att1 in att1Temp.DefaultIfEmpty()
join att2 in db.AttachFile on (x.HazardRegisterId + "-R") equals att2.ToKeyId into att2Temp
from att2 in att2Temp.DefaultIfEmpty()
select new HazardRegisterSyncItem
{
HazardRegisterId = x.HazardRegisterId,
HazardCode = x.HazardCode,
RegisterDate = x.RegisterDate,
RegisterDef = x.RegisterDef,
Rectification = x.Rectification,
Place = x.Place,
PlaceName = uw.UnitWorkName,
ResponsibleUnit = x.ResponsibleUnit,
Observer = x.Observer,
RectifiedDate = x.RectifiedDate,
AttachUrl = x.AttachUrl,
ProjectId = x.ProjectId,
States = x.States,
IsEffective = x.IsEffective,
ResponsibleMan = x.ResponsibleMan,
ResponsibleManName = u1.UserName,
CheckManId = x.CheckManId,
CheckManName = u2.UserName,
CheckTime = x.CheckTime,
RectificationPeriod = x.RectificationPeriod,
ImageUrl = x.ImageUrl,
RectificationImageUrl = x.RectificationImageUrl,
RectificationTime = x.RectificationTime,
ConfirmMan = x.ConfirmMan,
ConfirmManName = u3.UserName,
ConfirmDate = x.ConfirmDate,
HandleIdea = x.HandleIdea,
CutPayment = x.CutPayment,
ProblemTypes = x.ProblemTypes,
RegisterTypesId = x.RegisterTypesId,
RegisterTypesName = ht.RegisterTypesName,
CheckCycle = x.CheckCycle,
CheckItemDetailId = x.CheckItemDetailId,
SupCheckItemSetId = x.SupCheckItemSetId,
CheckItemSetId = x.CheckItemSetId,
CheckSpecialId = x.CheckSpecialId,
InstallationId = x.InstallationId,
SafeSupervisionId = x.SafeSupervisionId,
ResponsibleMan2 = x.ResponsibleMan2,
ResponsibleMan2Name = u4.UserName,
SafeSupervisionIsOK = x.SafeSupervisionIsOK,
GpsLocation = x.GpsLocation,
IsWx = x.IsWx,
DIC_ID = x.DIC_ID,
CCManIds = x.CCManIds,
ResultType = x.ResultType,
ResultId = x.ResultId,
Requirements = x.Requirements,
Risk_Level = x.Risk_Level,
ControlId = x.ControlId,
AttachFileId1 = att1.AttachFileId,
ToKeyId1 = att1.ToKeyId,
AttachSource1 = att1.AttachSource,
AttachUrl1 = att1.AttachUrl,
AttachFileId2 = att2.AttachFileId,
ToKeyId2 = att2.ToKeyId,
AttachSource2 = att2.AttachSource,
AttachUrl2 = att2.AttachUrl,
DataSource = "1",
}).ToList();
return dataList;
}
2026-03-06 14:24:20 +08:00
}
2026-03-23 15:22:29 +08:00
#endregion
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
#region
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
public static string getHazardRegisterLists(string projectId = "")
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
int code = 0;
string message = "";
try
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
string CollCropCode = string.Empty;
string unitId = string.Empty;
var thisUnit = CommonService.GetIsThisUnit(); //当前单位
if (thisUnit != null)
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
CollCropCode = thisUnit.CollCropCode; //社会统一信用代码
unitId = thisUnit.UnitId;
}
var ProjectList = (from x in Funs.DB.Base_Project
where (x.IsDelete == null || x.IsDelete == false) && x.SubjectUnit != null &&
x.SubjectProject != null
select x).ToList();
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
if (!string.IsNullOrEmpty(projectId))
{
ProjectList = ProjectList.Where(x => x.ProjectId == projectId).ToList();
}
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
if (ProjectList.Count > 0)
{
foreach (var project in ProjectList)
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
string SubjectUnitId = project.SubjectUnit; //集团的单位id
string SubjectProjectId = project.SubjectProject; //集团的项目id
string ApiUrl = project.SubjectUnitApiUrl;
string WebUrl = project.SubjectUnitWebUrl;
// CollCropCode = "91420000177570439L"; //三化建 测试使用
// SubjectProjectId = "c7ade79e-7646-4c59-a8fd-020a7e3138c6";//测试使用
string url =
"/api/HazardRegisterSync/getHazardRegisterListByProjectIdAndCollCropCode?projectId=" +
SubjectProjectId + "&collCropCode=" + CollCropCode;
string baseurl = ApiUrl + url;
string contenttype = "application/json;charset=unicode";
var strJosn = APIGetHttpService.Http(baseurl, "GET", contenttype, null, null);
if (!string.IsNullOrEmpty(strJosn))
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
JObject obj = JObject.Parse(strJosn);
code = Funs.GetNewIntOrZero(obj["code"].ToString());
message = obj["message"].ToString();
if (code == 1)
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
var getData =
JsonConvert.DeserializeObject<List<HazardRegisterSyncItem>>(obj["data"].ToString());
if (getData.Count() > 0)
{
ProcessHazardRegisterData(getData, project.ProjectId, unitId, WebUrl, "pull");
}
message = "获取成功:同步安全检查数" + getData.Count().ToString() + "条";
2026-03-06 14:24:20 +08:00
}
}
}
}
}
2026-03-23 15:22:29 +08:00
catch (Exception ex)
{
message = "获取失败:" + ex.Message;
ErrLogInfo.WriteLog("安全检查获取!", ex);
}
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
return message;
}
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
#endregion
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
#region
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
public static ReturnData pushHazardRegisterLists(string projectId, string dataId = "")
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
Model.ReturnData responeData = new Model.ReturnData();
responeData.code = 0;
responeData.message = string.Empty;
var project = Funs.DB.Base_Project.FirstOrDefault(x => x.ProjectId == projectId);
try
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
if (project != null)
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
//获取安全检查数据
var items = GetHazardRegisterLitsByprojectIdUnitIdPage(projectId, "", dataId);
//总包地址推送
if (items.Count() > 0)
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
var thisUnit = CommonService.GetIsThisUnit(); //当前单位
var apiurl = "/api/HazardRegisterSync/SaveHazardRegisterSyncData";
string ApiUrl = project.SubjectUnitApiUrl;
string WebUrl = project.SubjectUnitWebUrl;
// thisUnit.CollCropCode = "913404001520228377Y";//测试使用
// project.SubjectProject = "B409A8D7-48C7-486E-84C7-E3E7B2C0E5B7";//测试使用
var pushData = new HazardRegisterSyncData
{
CollCropCode = thisUnit.CollCropCode, //分包单位社会统一信用码
// CollCropCode = "91420000177570439L", //分包单位社会统一信用码
// ProjectId = "c7ade79e-7646-4c59-a8fd-020a7e3138c6", //主包项目Id
ProjectId = project.SubjectProject, //主包项目Id
UnitDomain = Funs.SGGLUrl, //分包单位域名地址【文件存储地址】
Items = items //安全检查数据
};
var pushContent = JsonConvert.SerializeObject(pushData);
string baseurl = ApiUrl + apiurl;
string contenttype = "application/json;charset=unicode";
var returndata = APIGetHttpService.Http(baseurl, "Post", contenttype, null, pushContent);
if (!string.IsNullOrEmpty(returndata))
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
JObject obj = JObject.Parse(returndata);
string code = obj["code"].ToString();
string message = obj["message"].ToString();
responeData.code = int.Parse(code);
responeData.message = message;
2026-03-06 14:24:20 +08:00
}
}
2026-03-23 15:22:29 +08:00
else
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
responeData.code = 0;
responeData.message = "当前没有项目安全检查数据";
2026-03-06 14:24:20 +08:00
}
}
}
2026-03-23 15:22:29 +08:00
catch (Exception ex)
{
responeData.message = "同步到总包单位失败!";
ErrLogInfo.WriteLog("【安全检查】同步到总包单位失败!", ex);
}
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
return responeData;
}
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
#endregion
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
#region
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
public static string SaveHazardRegisterSyncData(Model.HazardRegisterSyncData items)
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
int code = 0;
string message = "";
try
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
if (items.Items.Count > 0 || items.Items.Count > 0)
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
var CollCropCode = items.CollCropCode; //分包单位社会统一信用码
var ProjectId = items.ProjectId; //总包项目Id
var UnitDomain = items.UnitDomain; //分包单位域名地址【文件存储地址】
var unit = Funs.DB.Base_Unit.FirstOrDefault(x => x.CollCropCode == CollCropCode); //根据CollCropCode获取单位id
if (unit == null)
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
message = "总包单位不存在本单位,请登录总包系统检查维护本单位信息!";
2026-03-06 14:24:20 +08:00
}
else
{
2026-03-23 15:22:29 +08:00
//2、判断主包项目是否存在
var porject = BLL.ProjectService.GetProjectByProjectId(ProjectId);
if (porject == null)
{
message = "总包单位不存在本项目,请检查总包项目关联是否正确!";
}
else
{
ProcessHazardRegisterData(items.Items, ProjectId, unit.UnitId, UnitDomain, "push");
message = "数据推送成功!";
}
2026-03-06 14:24:20 +08:00
}
}
2026-03-23 15:22:29 +08:00
else
{
message = "暂无项目安全检查数据!";
}
2026-03-06 14:24:20 +08:00
}
2026-03-23 15:22:29 +08:00
catch (Exception ex)
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
throw ex;
2026-03-06 14:24:20 +08:00
}
2026-03-23 15:22:29 +08:00
return message;
}
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
#endregion
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
#region
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
/// <summary>
/// 处理单个安全检查数据的新增或更新逻辑
/// </summary>
/// <param name="item">安全检查数据项</param>
/// <param name="projectId">项目id</param>
/// <param name="unitId">单位ID</param>
/// <param name="WebUrl">Web地址</param>
private static void ProcessHazardRegisterData(List<Model.HazardRegisterSyncItem> getData, string projectId,
string unitId, string WebUrl, string type)
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
using (Model.SUBQHSEDB db = new Model.SUBQHSEDB(Funs.ConnString))
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
foreach (var item in getData)
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
Model.HSSE_Hazard_HazardRegister model =
db.HSSE_Hazard_HazardRegister.FirstOrDefault(x => x.HazardRegisterId == item.HazardRegisterId);
if (model == null)
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
Model.HSSE_Hazard_HazardRegister newModel = new Model.HSSE_Hazard_HazardRegister
{
HazardRegisterId = item.HazardRegisterId,
HazardCode = item.HazardCode,
RegisterDate = item.RegisterDate,
RegisterDef = item.RegisterDef,
Rectification = item.Rectification,
Place = APIDataShareSyncService.getUnitWorkId(item.PlaceName, projectId),
ResponsibleUnit = unitId,
Observer = item.Observer,
AttachUrl = item.AttachUrl,
ProjectId = projectId,
States = item.States,
IsEffective = item.IsEffective,
ResponsibleMan = APIDataShareSyncService.getUserId(item.ResponsibleManName),
CheckManId = APIDataShareSyncService.getUserId(item.CheckManName),
CheckTime = item.CheckTime,
RectificationPeriod = item.RectificationPeriod,
ImageUrl = item.ImageUrl,
RectificationImageUrl = item.RectificationImageUrl,
RectificationTime = item.RectificationTime,
ConfirmMan = APIDataShareSyncService.getUserId(item.ConfirmManName),
ConfirmDate = item.ConfirmDate,
HandleIdea = item.HandleIdea,
CutPayment = item.CutPayment,
ProblemTypes = item.ProblemTypes,
RegisterTypesId = APIDataShareSyncService.getRegisterTypesId(item.RegisterTypesName),
CheckCycle = item.CheckCycle,
CheckItemDetailId = item.CheckItemDetailId,
SupCheckItemSetId = item.SupCheckItemSetId,
CheckItemSetId = item.CheckItemSetId,
CheckSpecialId = item.CheckSpecialId,
InstallationId = item.InstallationId,
SafeSupervisionId = item.SafeSupervisionId,
ResponsibleMan2 = APIDataShareSyncService.getUserId(item.ResponsibleMan2Name),
SafeSupervisionIsOK = item.SafeSupervisionIsOK,
GpsLocation = item.GpsLocation,
IsWx = item.IsWx,
DIC_ID = item.DIC_ID,
CCManIds = item.CCManIds,
ResultType = item.ResultType,
ResultId = item.ResultId,
Requirements = item.Requirements,
Risk_Level = item.Risk_Level,
ControlId = item.ControlId,
};
if (type == "pull")
{
newModel.DataSource = item.DataSource;
}
db.HSSE_Hazard_HazardRegister.InsertOnSubmit(newModel);
db.SubmitChanges();
2026-03-06 14:24:20 +08:00
}
2026-03-23 15:22:29 +08:00
else
2026-03-06 14:24:20 +08:00
{
2026-03-23 15:22:29 +08:00
// model.HazardRegisterId = item.HazardRegisterId;
model.HazardCode = item.HazardCode;
model.RegisterDate = item.RegisterDate;
model.RegisterDef = item.RegisterDef;
model.Rectification = item.Rectification;
model.Place = APIDataShareSyncService.getUnitWorkId(item.PlaceName, projectId);
model.ResponsibleUnit = unitId;
model.Observer = item.Observer;
model.AttachUrl = item.AttachUrl;
model.ProjectId = projectId;
model.States = item.States;
model.IsEffective = item.IsEffective;
model.ResponsibleMan = APIDataShareSyncService.getUserId(item.ResponsibleManName);
model.CheckManId = APIDataShareSyncService.getUserId(item.CheckManName);
model.CheckTime = item.CheckTime;
model.RectificationPeriod = item.RectificationPeriod;
model.ImageUrl = item.ImageUrl;
model.RectificationImageUrl = item.RectificationImageUrl;
model.RectificationTime = item.RectificationTime;
model.ConfirmMan = APIDataShareSyncService.getUserId(item.ConfirmManName);
model.ConfirmDate = item.ConfirmDate;
model.HandleIdea = item.HandleIdea;
model.CutPayment = item.CutPayment;
model.ProblemTypes = item.ProblemTypes;
model.RegisterTypesId = APIDataShareSyncService.getRegisterTypesId(item.RegisterTypesName);
model.CheckCycle = item.CheckCycle;
model.CheckItemDetailId = item.CheckItemDetailId;
model.SupCheckItemSetId = item.SupCheckItemSetId;
model.CheckItemSetId = item.CheckItemSetId;
model.CheckSpecialId = item.CheckSpecialId;
model.InstallationId = item.InstallationId;
model.SafeSupervisionId = item.SafeSupervisionId;
model.ResponsibleMan2 = APIDataShareSyncService.getUserId(item.ResponsibleMan2Name);
model.SafeSupervisionIsOK = item.SafeSupervisionIsOK;
model.GpsLocation = item.GpsLocation;
model.IsWx = item.IsWx;
model.DIC_ID = item.DIC_ID;
model.CCManIds = item.CCManIds;
model.ResultType = item.ResultType;
model.ResultId = item.ResultId;
model.Requirements = item.Requirements;
model.Risk_Level = item.Risk_Level;
model.ControlId = item.ControlId;
if (type == "pull")
{
model.DataSource = item.DataSource;
}
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
db.SubmitChanges();
}
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
//附件处理
if (!string.IsNullOrEmpty(item.AttachUrl1))
{
BLL.FileInsertService.SaveAttachFileRecords(WebUrl, item.AttachFileId1, item.ToKeyId1,
item.AttachSource1, item.AttachUrl1);
}
else if (!string.IsNullOrEmpty(item.ImageUrl))
{
APIDataShareSyncService.SafeDownloadHeadImageAsync(WebUrl, item.ImageUrl);
}
2026-03-06 14:24:20 +08:00
2026-03-23 15:22:29 +08:00
if (!string.IsNullOrEmpty(item.AttachUrl2))
{
BLL.FileInsertService.SaveAttachFileRecords(WebUrl, item.AttachFileId2, item.ToKeyId2,
item.AttachSource2, item.AttachUrl2);
}
else if (!string.IsNullOrEmpty(item.RectificationImageUrl))
{
APIDataShareSyncService.SafeDownloadHeadImageAsync(WebUrl, item.RectificationImageUrl);
}
2026-03-06 14:24:20 +08:00
}
}
}
2026-03-23 15:22:29 +08:00
#endregion
}
2026-03-06 14:24:20 +08:00
}