CNCEC_SUBQHSE_WUHUAN/SGGL/BLL/DataShare/HSSE/APIPersonSyncService.cs

502 lines
27 KiB
C#
Raw Permalink Normal View History

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-16 20:12:55 +08:00
namespace BLL
{
2026-03-16 20:12:55 +08:00
public class APIPersonSyncService
{
2026-03-16 20:12:55 +08:00
#region
2026-03-16 20:12:55 +08:00
public static List<Model.PersonSyncItem> GetPersonLitsByProjectIdAndUnitId(string projectId, string unitId,
string dataId = "")
{
using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
{
2026-03-16 20:12:55 +08:00
var list = from x in db.SitePerson_Person where x.ProjectId == projectId select x;
if (!string.IsNullOrEmpty(unitId))
{
list = list.Where(x => x.UnitId == unitId);
}
2026-03-16 20:12:55 +08:00
if (!string.IsNullOrEmpty(dataId))
{
2026-03-16 20:12:55 +08:00
list = list.Where(x => x.PersonId == dataId);
}
2026-03-16 20:12:55 +08:00
var personList = (from x in list
join wp in db.Base_WorkPost on x.WorkPostId equals wp.WorkPostId into wpTemp
from wp in wpTemp.DefaultIfEmpty()
join tg in db.ProjectData_TeamGroup on x.TeamGroupId equals tg.TeamGroupId into tgTemp
from tg in tgTemp.DefaultIfEmpty()
join c in db.Base_Certificate on x.CertificateId equals c.CertificateId into cTemp
from c in cTemp.DefaultIfEmpty()
join att1 in db.AttachFile on (x.PersonId + "#1") equals att1.ToKeyId into att1Temp
from att1 in att1Temp.DefaultIfEmpty()
join att2 in db.AttachFile on (x.PersonId + "#2") equals att2.ToKeyId into att2Temp
from att2 in att2Temp.DefaultIfEmpty()
join att3 in db.AttachFile on (x.PersonId + "#3") equals att3.ToKeyId into att3Temp
from att3 in att3Temp.DefaultIfEmpty()
join att4 in db.AttachFile on (x.PersonId + "#4") equals att4.ToKeyId into att4Temp
from att4 in att4Temp.DefaultIfEmpty()
join att5 in db.AttachFile on (x.PersonId + "#5") equals att5.ToKeyId into att5Temp
from att5 in att5Temp.DefaultIfEmpty()
select new Model.PersonSyncItem
{
PersonId = x.PersonId,
CardNo = x.CardNo,
PersonName = x.PersonName,
Sex = x.Sex,
IdentityCard = x.IdentityCard,
Address = x.Address,
ProjectId = x.ProjectId,
UnitId = x.UnitId,
TeamGroupId = x.TeamGroupId,
WorkAreaId = x.WorkAreaId,
WorkPostId = x.WorkPostId,
InTime = x.InTime,
OutTime = x.OutTime,
OutResult = x.OutResult,
Telephone = x.Telephone,
// x.PositionId,//职务
// x.PostTitleId,//职称
PhotoUrl = x.PhotoUrl,
IsUsed = x.IsUsed,
IsCardUsed = x.IsCardUsed,
PersonIndex = x.PersonIndex,
// x.DepartId,//部门
CertificateId = x.CertificateId,
CertificateItem = APIDataShareSyncService.GetPersonQualityByPersonId(x.PersonId), //特岗证书
CertificateCode = x.CertificateCode,
CertificateName = c.CertificateName,
CertificateLimitTime = x.CertificateLimitTime,
QualificationCertificateUrl = x.QualificationCertificateUrl,
TrainingCertificateUrl = x.TrainingCertificateUrl,
QRCodeAttachUrl = x.QRCodeAttachUrl,
Password = x.Password,
FromPersonId = x.FromPersonId,
// x.AuditorId,审核人
AuditorDate = x.AuditorDate,
ExchangeTime = x.ExchangeTime,
ExchangeTime2 = x.ExchangeTime2,
IDCardUrl = x.IDCardUrl,
IsForeign = x.IsForeign,
IsOutside = x.IsOutside,
EduLevel = x.EduLevel,
MaritalStatus = x.MaritalStatus,
Isprint = x.Isprint,
MainCNProfessionalId = x.MainCNProfessionalId,
ViceCNProfessionalId = x.ViceCNProfessionalId,
Birthday = x.Birthday,
IdcardType = x.IdcardType,
IdcardStartDate = x.IdcardStartDate,
IdcardEndDate = x.IdcardEndDate,
IdcardForever = x.IdcardForever,
PoliticsStatus = x.PoliticsStatus,
IdcardAddress = x.IdcardAddress,
Nation = x.Nation,
CountryCode = x.CountryCode,
IsSafetyMonitoring = x.IsSafetyMonitoring,
ProvinceCode = x.ProvinceCode,
IsCardNoOK = x.IsCardNoOK,
AttachFileId1 = att1.AttachFileId,
ToKeyId1 = att1.ToKeyId,
AttachSource1 = att1.AttachSource,
AttachUrl1 = att1.AttachUrl,
AttachFileId2 = att2.AttachFileId,
ToKeyId2 = att2.ToKeyId,
AttachSource2 = att2.AttachSource,
AttachUrl2 = att2.AttachUrl,
AttachFileId3 = att3.AttachFileId,
ToKeyId3 = att3.ToKeyId,
AttachSource3 = att3.AttachSource,
AttachUrl3 = att3.AttachUrl,
AttachFileId4 = att4.AttachFileId,
ToKeyId4 = att4.ToKeyId,
AttachSource4 = att4.AttachSource,
AttachUrl4 = att4.AttachUrl,
AttachFileId5 = att5.AttachFileId,
ToKeyId5 = att5.ToKeyId,
AttachSource5 = att5.AttachSource,
AttachUrl5 = att5.AttachUrl,
WorkPostName = wp.WorkPostName, //岗位名称
PostType = wp.PostType, //岗位诶类型
IsHsse = wp.IsHsse, //岗位是否是安管人员
IsCQMS = wp.IsCQMS, //岗位是否是质量管理
TeamGroupName = tg.TeamGroupName,
WorkAreaName = APIDataShareSyncService.GetWorkAreaNames(x.WorkAreaId, projectId)
}).ToList();
return personList;
}
}
2026-03-16 20:12:55 +08:00
#endregion
2026-03-16 20:12:55 +08:00
#region
2026-03-16 20:12:55 +08:00
public static async Task<string> getPersonLists()
{
2026-03-16 20:12:55 +08:00
int code = 0;
string message = "";
try
{
2026-03-16 20:12:55 +08:00
string CollCropCode = string.Empty;
string unitId = string.Empty;
var thisUnit = CommonService.GetIsThisUnit(); //当前单位
if (thisUnit != null)
{
2026-03-16 20:12:55 +08:00
CollCropCode = thisUnit.CollCropCode; //社会统一信用代码
unitId = thisUnit.UnitId;
}
2026-03-16 20:12:55 +08:00
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();
if (ProjectList.Count > 0)
{
foreach (var project in ProjectList)
{
2026-03-16 20:12:55 +08:00
string SubjectUnitId = project.SubjectUnit; //集团的单位id
string SubjectProjectId = project.SubjectProject; //集团的单位id
string ApiUrl = project.SubjectUnitApiUrl;
string WebUrl = project.SubjectUnitWebUrl;
// CollCropCode = "913404001520228377Y";//测试使用
// SubjectProjectId = "B409A8D7-48C7-486E-84C7-E3E7B2C0E5B7";//测试使用
// unitId = "aaa9e72b-e3a6-441e-a7bb-afc008adebc9";//测试使用
string url = "/api/PersonSync/getPersonListByProjectIdAndCollCropCode?projectId=" +
SubjectProjectId + "&collCropCode=" + CollCropCode;
string baseurl = ApiUrl + url;
string contenttype = "application/json;charset=unicode";
// var strJosn = "";
// using (HttpClient client = new HttpClient())
// {
// client.Timeout = TimeSpan.FromMinutes(5); // 设置超时时间为5分钟
// client.DefaultRequestHeaders.Add("token",
// "AF17168B-87BD-4GLY-1111-F0A0A1158F9B"); // 前提是所有二级单位都有sysGLY账号且id是AF17168B-87BD-4GLY-1111-F0A0A1158F9B可作为token
// strJosn = await client.GetStringAsync(baseurl);
// }
var strJosn = APIGetHttpService.Http(baseurl, "GET", contenttype, null, null);
if (!string.IsNullOrEmpty(strJosn))
{
2026-03-16 20:12:55 +08:00
JObject obj = JObject.Parse(strJosn);
code = Funs.GetNewIntOrZero(obj["code"].ToString());
message = obj["message"].ToString();
if (code == 1)
{
2026-03-16 20:12:55 +08:00
var getData =
JsonConvert.DeserializeObject<List<PersonSyncItem>>(obj["data"].ToString());
if (getData.Count() > 0)
{
ProcessPersonData(getData, project.ProjectId, unitId, WebUrl);
}
message = "获取成功:同步人员数" + getData.Count().ToString() + "条";
}
}
}
}
}
2026-03-16 20:12:55 +08:00
catch (Exception ex)
{
message = "获取失败:" + ex.Message;
ErrLogInfo.WriteLog("人员获取!", ex);
}
2026-03-16 20:12:55 +08:00
return message;
}
2026-03-16 20:12:55 +08:00
#endregion
2026-03-16 20:12:55 +08:00
#region
2026-03-16 20:12:55 +08:00
public static ReturnData pushPersonLists(string projectId, string dataId)
{
2026-03-16 20:12:55 +08:00
Model.ReturnData responeData = new Model.ReturnData();
responeData.code = 0;
responeData.message = string.Empty;
try
{
2026-03-16 20:12:55 +08:00
var project = Funs.DB.Base_Project.FirstOrDefault(x => x.ProjectId == projectId);
if (project != null)
{
2026-03-16 20:12:55 +08:00
//获取人员数据
var items = GetPersonLitsByProjectIdAndUnitId(projectId, "", dataId);
//总包地址推送
if (items.Count() > 0)
{
2026-03-16 20:12:55 +08:00
var thisUnit = CommonService.GetIsThisUnit(); //当前单位
var apiurl = "/api/PersonSync/SavePersonSyncData";
//总包单位接口地址
string ApiUrl = project.SubjectUnitApiUrl;
string WebUrl = project.SubjectUnitWebUrl;
// thisUnit.CollCropCode = "913404001520228377Y"; //分包单位社会统一信用码 测试使用
// project.SubjectProject = "B409A8D7-48C7-486E-84C7-E3E7B2C0E5B7";//测试使用
var pushData = new PersonSyncData
{
CollCropCode = thisUnit.CollCropCode, //分包单位社会统一信用码
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-16 20:12:55 +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-16 20:12:55 +08:00
else
{
2026-03-16 20:12:55 +08:00
responeData.code = 0;
responeData.message = "当前没有项目人员数据";
}
}
}
2026-03-16 20:12:55 +08:00
catch (Exception ex)
{
responeData.message = "同步到总包单位失败!";
ErrLogInfo.WriteLog("【项目人员】同步到总包单位失败!", ex);
}
2026-03-16 20:12:55 +08:00
return responeData;
}
2026-03-16 20:12:55 +08:00
#endregion
2026-03-16 20:12:55 +08:00
#region
2026-03-16 20:12:55 +08:00
public static async Task<string> SavePersonSyncData(Model.PersonSyncData items)
{
2026-03-16 20:12:55 +08:00
int code = 0;
string message = "";
try
{
2026-03-16 20:12:55 +08:00
if (items.Items.Count > 0)
{
2026-03-16 20:12:55 +08:00
//获取分包单位CollCropCode
var CollCropCode = items.CollCropCode;
var ProjectId = items.ProjectId;
var UnitDomain = items.UnitDomain; //分包单位域名地址【文件存储地址】
//1、判断分包单位是否存在
//根据分包单位CollCropCode获取单位id
var unit = Funs.DB.Base_Unit.FirstOrDefault(x => x.CollCropCode == CollCropCode);
if (unit == null)
{
2026-03-16 20:12:55 +08:00
message = "总包单位不存在本单位,请登录总包系统检查维护本单位信息!";
}
else
{
2026-03-16 20:12:55 +08:00
//2、判断主包项目是否存在
var porject = BLL.ProjectService.GetProjectByProjectId(ProjectId);
if (porject == null)
{
message = "总包单位不存在本项目,请检查总包项目关联是否正确!";
}
else
{
ProcessPersonData(items.Items, ProjectId, unit.UnitId, UnitDomain);
message = "数据推送成功!";
}
}
}
2026-03-16 20:12:55 +08:00
else
{
message = "暂无项目人员数据!";
}
}
2026-03-16 20:12:55 +08:00
catch (Exception ex)
{
2026-03-16 20:12:55 +08:00
throw ex;
}
2026-03-16 20:12:55 +08:00
return message;
}
2026-03-16 20:12:55 +08:00
#endregion
2026-03-16 20:12:55 +08:00
#region
2026-03-16 20:12:55 +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 async void ProcessPersonData(List<Model.PersonSyncItem> getData, string projectId, string unitId,
string WebUrl)
{
2026-03-16 20:12:55 +08:00
using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
{
2026-03-16 20:12:55 +08:00
foreach (var item in getData)
{
2026-03-16 20:12:55 +08:00
try
{
2026-03-16 20:12:55 +08:00
string IdentityCard = item.IdentityCard;
var model = db.SitePerson_Person.FirstOrDefault(e => e.IdentityCard == IdentityCard && e.ProjectId == projectId);
if (model == null)
{
2026-03-16 20:12:55 +08:00
Model.SitePerson_Person newModel = new Model.SitePerson_Person
{
PersonId = item.PersonId,
CardNo = item.CardNo,
PersonName = item.PersonName,
Sex = item.Sex,
IdentityCard = item.IdentityCard,
Address = item.Address,
ProjectId = projectId,
UnitId = unitId,
TeamGroupId = APIDataShareSyncService.GetTeamGroupId(item.TeamGroupName, projectId, unitId),
WorkAreaId = APIDataShareSyncService.getWorkAreaId(item.WorkAreaName, projectId),
WorkPostId = APIDataShareSyncService.getWorkPostId(item.WorkPostName, item.PostType,
item.IsHsse.ToString(), item.IsCQMS.ToString()),
InTime = item.InTime,
OutTime = item.OutTime,
OutResult = item.OutResult,
Telephone = item.Telephone,
PhotoUrl = item.PhotoUrl,
HeadImage = APIDataShareSyncService.SafeDownloadHeadImageAsync(WebUrl, item.PhotoUrl),
IsUsed = item.IsUsed,
IsCardUsed = item.IsCardUsed,
PersonIndex = item.PersonIndex,
CertificateId = item.CertificateId,
CertificateCode = item.CertificateCode,
CertificateLimitTime = item.CertificateLimitTime,
QualificationCertificateUrl = item.QualificationCertificateUrl,
TrainingCertificateUrl = item.TrainingCertificateUrl,
QRCodeAttachUrl = item.QRCodeAttachUrl,
Password = item.Password,
FromPersonId = item.FromPersonId,
AuditorDate = item.AuditorDate,
ExchangeTime = item.ExchangeTime,
ExchangeTime2 = item.ExchangeTime2,
IDCardUrl = item.IDCardUrl,
IsForeign = item.IsForeign,
IsOutside = item.IsOutside,
EduLevel = item.EduLevel,
MaritalStatus = item.MaritalStatus,
Isprint = item.Isprint,
MainCNProfessionalId = item.MainCNProfessionalId,
ViceCNProfessionalId = item.ViceCNProfessionalId,
Birthday = item.Birthday,
IdcardType = item.IdcardType,
IdcardStartDate = item.IdcardStartDate,
IdcardEndDate = item.IdcardEndDate,
IdcardForever = item.IdcardForever,
PoliticsStatus = item.PoliticsStatus,
IdcardAddress = item.IdcardAddress,
Nation = item.Nation,
CountryCode = item.CountryCode,
IsSafetyMonitoring = item.IsSafetyMonitoring,
ProvinceCode = item.ProvinceCode,
IsCardNoOK = item.IsCardNoOK
};
db.SitePerson_Person.InsertOnSubmit(newModel);
db.SubmitChanges();
}
else
{
// model.PersonId = item.PersonId;
model.CardNo = item.CardNo;
model.PersonName = item.PersonName;
model.Sex = item.Sex;
model.IdentityCard = item.IdentityCard;
model.Address = item.Address;
model.ProjectId = projectId;
model.UnitId = unitId;
model.TeamGroupId = APIDataShareSyncService.GetTeamGroupId(item.TeamGroupName, projectId, unitId);
model.WorkAreaId = APIDataShareSyncService.getWorkAreaId(item.WorkAreaName, projectId);
model.WorkPostId = APIDataShareSyncService.getWorkPostId(item.WorkPostName, item.PostType, item.IsHsse.ToString(), item.IsCQMS.ToString());
model.InTime = item.InTime;
model.OutTime = item.OutTime;
model.OutResult = item.OutResult;
model.Telephone = item.Telephone;
model.PhotoUrl = item.PhotoUrl;
model.HeadImage = APIDataShareSyncService.SafeDownloadHeadImageAsync(WebUrl, item.PhotoUrl);
model.IsUsed = item.IsUsed;
model.IsCardUsed = item.IsCardUsed;
model.PersonIndex = item.PersonIndex;
model.CertificateId = item.CertificateId;
model.CertificateCode = item.CertificateCode;
model.CertificateLimitTime = item.CertificateLimitTime;
model.QualificationCertificateUrl = item.QualificationCertificateUrl;
model.TrainingCertificateUrl = item.TrainingCertificateUrl;
model.QRCodeAttachUrl = item.QRCodeAttachUrl;
model.Password = item.Password;
model.FromPersonId = item.FromPersonId;
model.AuditorDate = item.AuditorDate;
model.ExchangeTime = item.ExchangeTime;
model.ExchangeTime2 = item.ExchangeTime2;
model.IDCardUrl = item.IDCardUrl;
model.IsForeign = item.IsForeign;
model.IsOutside = item.IsOutside;
model.EduLevel = item.EduLevel;
model.MaritalStatus = item.MaritalStatus;
model.Isprint = item.Isprint;
model.MainCNProfessionalId = item.MainCNProfessionalId;
model.ViceCNProfessionalId = item.ViceCNProfessionalId;
model.Birthday = item.Birthday;
model.IdcardType = item.IdcardType;
model.IdcardStartDate = item.IdcardStartDate;
model.IdcardEndDate = item.IdcardEndDate;
model.IdcardForever = item.IdcardForever;
model.PoliticsStatus = item.PoliticsStatus;
model.IdcardAddress = item.IdcardAddress;
model.Nation = item.Nation;
model.CountryCode = item.CountryCode;
model.IsSafetyMonitoring = item.IsSafetyMonitoring;
model.ProvinceCode = item.ProvinceCode;
model.IsCardNoOK = item.IsCardNoOK;
db.SubmitChanges();
}
// var jsonData = JsonConvert.SerializeObject(item);
// ErrLogInfo.WriteLog($"【人员接收数据】{jsonData}");
//附件处理
BLL.FileInsertService.SaveAttachFileRecords(WebUrl, item.AttachFileId1, item.ToKeyId1, item.AttachSource1, item.AttachUrl1);
BLL.FileInsertService.SaveAttachFileRecords(WebUrl, item.AttachFileId2, item.ToKeyId2, item.AttachSource2, item.AttachUrl2);
BLL.FileInsertService.SaveAttachFileRecords(WebUrl, item.AttachFileId3, item.ToKeyId3, item.AttachSource3, item.AttachUrl3);
BLL.FileInsertService.SaveAttachFileRecords(WebUrl, item.AttachFileId4, item.ToKeyId4, item.AttachSource4, item.AttachUrl4);
BLL.FileInsertService.SaveAttachFileRecords(WebUrl, item.AttachFileId5, item.ToKeyId5, item.AttachSource5, item.AttachUrl5);
if (item.CertificateItem != null)
{
APIDataShareSyncService.ProcessPersonQualityData(item.CertificateItem, item.PersonId);
}
}
2026-03-16 20:12:55 +08:00
catch (Exception ex)
{
2026-03-16 20:12:55 +08:00
// 记录异常但不中断整个批处理
BLL.ErrLogInfo.WriteLog($"处理人员数据时出错 PersonId: {item?.PersonId}, 错误: {ex.Message}");
continue; // 继续处理下一个记录
}
}
}
}
2026-03-16 20:12:55 +08:00
#endregion
}
}