448 lines
19 KiB
C#
448 lines
19 KiB
C#
using Newtonsoft.Json.Linq;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Configuration;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Net.Http;
|
||
using System.Threading.Tasks;
|
||
using Model;
|
||
using Newtonsoft.Json;
|
||
using RestSharp;
|
||
using System.Net;
|
||
|
||
namespace BLL
|
||
{
|
||
/// <summary>
|
||
/// 质量计量器具同步服务
|
||
/// </summary>
|
||
public class APIInspectionMachineSyncService
|
||
{
|
||
#region 根据项目、单位获取质量计量器具列表
|
||
|
||
/// <summary>
|
||
/// 根据项目、单位获取质量计量器具列表
|
||
/// </summary>
|
||
/// <param name="projectId">项目ID</param>
|
||
/// <param name="unitId">单位ID</param>
|
||
/// <param name="dataId">数据ID(可选,用于单条数据同步)</param>
|
||
/// <returns>质量计量器具数据列表</returns>
|
||
public static List<Model.InspectionMachineSyncItem> GetInspectionMachineListsByProjectIdUnitIdPage(
|
||
string projectId, string unitId, string dataId = "")
|
||
{
|
||
using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
|
||
{
|
||
var list = from x in db.Comprehensive_InspectionMachine
|
||
where x.ProjectId == projectId
|
||
select x;
|
||
|
||
if (!string.IsNullOrEmpty(unitId))
|
||
{
|
||
list = list.Where(x => x.UnitId == unitId);
|
||
}
|
||
|
||
if (!string.IsNullOrEmpty(dataId))
|
||
{
|
||
list = list.Where(x => x.InspectionMachineId == dataId);
|
||
}
|
||
|
||
var dataList = (from x in list
|
||
join unit in db.Base_Unit on x.UnitId equals unit.UnitId into unitTemp
|
||
from unit in unitTemp.DefaultIfEmpty()
|
||
join pro in db.Base_CNProfessional on x.CNProfessionalId equals pro.CNProfessionalId into proTemp
|
||
from pro in proTemp.DefaultIfEmpty()
|
||
join u1 in db.Sys_User on x.CompileMan equals u1.UserId into u1Temp
|
||
from u1 in u1Temp.DefaultIfEmpty()
|
||
select new InspectionMachineSyncItem
|
||
{
|
||
InspectionMachineId = x.InspectionMachineId,
|
||
InspectionMachineCode = x.InspectionMachineCode,
|
||
InspectionMachineName = x.InspectionMachineName,
|
||
SpecificationModel = x.SpecificationModel,
|
||
UnitsCount = x.UnitsCount,
|
||
//SType = x.SType,
|
||
InspectionType = x.InspectionType,
|
||
InspectionDate = x.InspectionDate,
|
||
NextTestDate = x.NextTestDate,
|
||
TestCycle = x.TestCycle,
|
||
IsCheckOK = x.IsCheckOK,
|
||
IsVerification = x.IsVerification,
|
||
Status = x.Status,
|
||
IsOnSite = x.IsOnSite,
|
||
LeaveDate = x.LeaveDate,
|
||
UnitId = x.UnitId,
|
||
UnitName = unit.UnitName,
|
||
CNProfessionalId = x.CNProfessionalId,
|
||
CNProfessionalName = pro.ProfessionalName,
|
||
CompileMan = x.CompileMan,
|
||
CompileManName = u1.UserName,
|
||
CompileDate = x.CompileDate,
|
||
AttachUrl = x.AttachUrl,
|
||
ProjectId = x.ProjectId,
|
||
DataSource = "1"
|
||
}).ToList();
|
||
|
||
return dataList;
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
|
||
#region 拉取质量计量器具数据
|
||
|
||
/// <summary>
|
||
/// 拉取质量计量器具数据
|
||
/// </summary>
|
||
/// <returns>处理结果消息</returns>
|
||
public static string getInspectionMachineLists()
|
||
{
|
||
int code = 0;
|
||
string message = "";
|
||
try
|
||
{
|
||
string CollCropCode = string.Empty;
|
||
string unitId = string.Empty;
|
||
var thisUnit = CommonService.GetIsThisUnit(); //当前单位
|
||
if (thisUnit != null)
|
||
{
|
||
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();
|
||
|
||
if (ProjectList.Count > 0)
|
||
{
|
||
foreach (var project in ProjectList)
|
||
{
|
||
string SubjectUnitId = project.SubjectUnit; //集团的单位id
|
||
string SubjectProjectId = project.SubjectProject; //集团的项目id
|
||
|
||
//获取对应单位的apiurl地址
|
||
var Url = BLL.UnitService.getUnitApiUrlByUnitId(SubjectUnitId);
|
||
var ApiUrl = "";
|
||
var WebUrl = "";
|
||
if (!string.IsNullOrEmpty(Url))
|
||
{
|
||
var urls = Url.Split(',');
|
||
ApiUrl = urls[0];
|
||
if (urls.Length > 1)
|
||
{
|
||
WebUrl = urls[1];
|
||
}
|
||
}
|
||
|
||
string url = "/api/InspectionMachineSync/getInspectionMachineListByProjectIdAndCollCropCode?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))
|
||
{
|
||
JObject obj = JObject.Parse(strJosn);
|
||
code = Funs.GetNewIntOrZero(obj["code"].ToString());
|
||
message = obj["message"].ToString();
|
||
|
||
if (code == 1)
|
||
{
|
||
var getData = JsonConvert.DeserializeObject<List<InspectionMachineSyncItem>>(obj["data"].ToString());
|
||
if (getData.Count() > 0)
|
||
{
|
||
ProcessInspectionMachineData(getData, project.ProjectId, unitId, WebUrl);
|
||
}
|
||
message = "获取成功:同步质量计量器具数" + getData.Count().ToString() + "条";
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
message = "获取失败:" + ex.Message;
|
||
ErrLogInfo.WriteLog("质量计量器具获取!", ex);
|
||
}
|
||
|
||
return message;
|
||
}
|
||
|
||
#endregion
|
||
|
||
|
||
#region 推送质量计量器具数据
|
||
|
||
/// <summary>
|
||
/// 推送质量计量器具数据
|
||
/// </summary>
|
||
/// <param name="projectId">项目ID</param>
|
||
/// <param name="dataId">数据ID(可选,用于单条数据推送)</param>
|
||
/// <returns>推送结果</returns>
|
||
public static ReturnData pushInspectionMachineLists(string projectId, string dataId = "")
|
||
{
|
||
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
|
||
{
|
||
if (project != null)
|
||
{
|
||
//获取质量计量器具数据
|
||
var items = GetInspectionMachineListsByProjectIdUnitIdPage(projectId, "", dataId);
|
||
|
||
if (items.Count() > 0)
|
||
{
|
||
var thisUnit = CommonService.GetIsThisUnit(); //当前单位
|
||
var apiurl = "/api/InspectionMachineSync/SaveInspectionMachineSyncData";
|
||
|
||
//总包单位接口地址
|
||
var Url = BLL.UnitService.getUnitApiUrlByUnitId(project.SubjectUnit);
|
||
var ApiUrl = "";
|
||
var WebUrl = "";
|
||
|
||
if (!string.IsNullOrEmpty(Url))
|
||
{
|
||
var urls = Url.Split(',');
|
||
ApiUrl = urls[0];
|
||
if (urls.Length > 1)
|
||
{
|
||
WebUrl = urls[1];
|
||
}
|
||
}
|
||
|
||
var pushData = new InspectionMachineSyncData
|
||
{
|
||
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))
|
||
{
|
||
JObject obj = JObject.Parse(returndata);
|
||
string code = obj["code"].ToString();
|
||
string message = obj["message"].ToString();
|
||
responeData.code = int.Parse(code);
|
||
responeData.message = message;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
responeData.code = 0;
|
||
responeData.message = "当前没有质量计量器具数据";
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
responeData.message = "同步到总包单位失败!";
|
||
ErrLogInfo.WriteLog("【质量计量器具】同步到总包单位失败!", ex);
|
||
}
|
||
|
||
return responeData;
|
||
}
|
||
|
||
#endregion
|
||
|
||
|
||
#region 接收保存质量计量器具数据
|
||
|
||
/// <summary>
|
||
/// 接收保存质量计量器具数据
|
||
/// </summary>
|
||
/// <param name="items">质量计量器具同步数据</param>
|
||
/// <returns>处理结果消息</returns>
|
||
public static string SaveInspectionMachineSyncData(Model.InspectionMachineSyncData items)
|
||
{
|
||
int code = 0;
|
||
string message = "";
|
||
try
|
||
{
|
||
if (items.Items.Count > 0 || items.Items.Count > 0)
|
||
{
|
||
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)
|
||
{
|
||
message = "总包单位不存在本单位,请登录总包系统检查维护本单位信息!";
|
||
}
|
||
else
|
||
{
|
||
//判断主包项目是否存在
|
||
var porject = BLL.ProjectService.GetProjectByProjectId(ProjectId);
|
||
if (porject == null)
|
||
{
|
||
message = "总包单位不存在本项目,请检查总包项目关联是否正确!";
|
||
}
|
||
else
|
||
{
|
||
ProcessInspectionMachineData(items.Items, ProjectId, unit.UnitId, UnitDomain);
|
||
message = "数据推送成功!";
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
message = "暂无质量计量器具数据!";
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
|
||
return message;
|
||
}
|
||
|
||
#endregion
|
||
|
||
|
||
#region 处理质量计量器具数据的新增或更新逻辑
|
||
|
||
/// <summary>
|
||
/// 处理质量计量器具数据的新增或更新逻辑
|
||
/// </summary>
|
||
/// <param name="getData">质量计量器具数据列表</param>
|
||
/// <param name="projectId">项目id</param>
|
||
/// <param name="unitId">单位ID</param>
|
||
/// <param name="WebUrl">Web地址</param>
|
||
private static void ProcessInspectionMachineData(
|
||
List<Model.InspectionMachineSyncItem> getData,
|
||
string projectId,
|
||
string unitId,
|
||
string WebUrl)
|
||
{
|
||
using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
|
||
{
|
||
foreach (var item in getData)
|
||
{
|
||
Model.Comprehensive_InspectionMachine model = db.Comprehensive_InspectionMachine.FirstOrDefault(x => x.InspectionMachineId == item.InspectionMachineId);
|
||
|
||
if (model == null)
|
||
{
|
||
//新增
|
||
Model.Comprehensive_InspectionMachine newModel = new Model.Comprehensive_InspectionMachine
|
||
{
|
||
InspectionMachineId = item.InspectionMachineId,
|
||
ProjectId = projectId,
|
||
UnitId = getUnitIdByUnitName(item.UnitName) ?? unitId,
|
||
InspectionMachineCode = item.InspectionMachineCode,
|
||
InspectionMachineName = item.InspectionMachineName,
|
||
SpecificationModel = item.SpecificationModel,
|
||
UnitsCount = item.UnitsCount,
|
||
//SType = item.SType,
|
||
InspectionType = item.InspectionType,
|
||
InspectionDate = item.InspectionDate,
|
||
NextTestDate = item.NextTestDate,
|
||
TestCycle = item.TestCycle,
|
||
IsCheckOK = item.IsCheckOK,
|
||
IsVerification = item.IsVerification,
|
||
Status = item.Status,
|
||
IsOnSite = item.IsOnSite,
|
||
LeaveDate = item.LeaveDate,
|
||
CNProfessionalId = getCNProfessionalId(item.CNProfessionalName),
|
||
CompileMan = APIDataShareSyncService.getUserId(item.CompileManName),
|
||
CompileDate = item.CompileDate,
|
||
AttachUrl = item.AttachUrl
|
||
};
|
||
|
||
db.Comprehensive_InspectionMachine.InsertOnSubmit(newModel);
|
||
db.SubmitChanges();
|
||
}
|
||
else
|
||
{
|
||
//更新
|
||
model.ProjectId = projectId;
|
||
model.UnitId = getUnitIdByUnitName(item.UnitName) ?? unitId;
|
||
model.InspectionMachineCode = item.InspectionMachineCode;
|
||
model.InspectionMachineName = item.InspectionMachineName;
|
||
model.SpecificationModel = item.SpecificationModel;
|
||
model.UnitsCount = item.UnitsCount;
|
||
//model.SType = item.SType;
|
||
model.InspectionType = item.InspectionType;
|
||
model.InspectionDate = item.InspectionDate;
|
||
model.NextTestDate = item.NextTestDate;
|
||
model.TestCycle = item.TestCycle;
|
||
model.IsCheckOK = item.IsCheckOK;
|
||
model.IsVerification = item.IsVerification;
|
||
model.Status = item.Status;
|
||
model.IsOnSite = item.IsOnSite;
|
||
model.LeaveDate = item.LeaveDate;
|
||
model.CNProfessionalId = getCNProfessionalId(item.CNProfessionalName);
|
||
model.CompileMan = APIDataShareSyncService.getUserId(item.CompileManName);
|
||
model.CompileDate = item.CompileDate;
|
||
model.AttachUrl = item.AttachUrl;
|
||
db.SubmitChanges();
|
||
}
|
||
|
||
//处理附件
|
||
if (!string.IsNullOrEmpty(item.AttachUrl))
|
||
{
|
||
APIDataShareSyncService.OperationAttachFile(WebUrl, item.InspectionMachineId,
|
||
BLL.Const.InspectionMachineMenuId, item.AttachUrl);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
|
||
#region 辅助映射方法
|
||
|
||
/// <summary>
|
||
/// 获取专业ID(根据名称)
|
||
/// </summary>
|
||
/// <param name="professionalName">专业名称</param>
|
||
/// <returns>专业ID</returns>
|
||
private static string getCNProfessionalId(string professionalName)
|
||
{
|
||
string professionalId = null;
|
||
if (!string.IsNullOrEmpty(professionalName))
|
||
{
|
||
var pro = Funs.DB.Base_CNProfessional.FirstOrDefault(x => x.ProfessionalName == professionalName);
|
||
if (pro != null)
|
||
{
|
||
professionalId = pro.CNProfessionalId;
|
||
}
|
||
}
|
||
return professionalId;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取单位ID(根据名称)
|
||
/// </summary>
|
||
/// <param name="unitName">单位名称</param>
|
||
/// <returns>单位ID</returns>
|
||
private static string getUnitIdByUnitName(string unitName)
|
||
{
|
||
string unitId = null;
|
||
if (!string.IsNullOrEmpty(unitName))
|
||
{
|
||
var unit = Funs.DB.Base_Unit.FirstOrDefault(x => x.UnitName == unitName);
|
||
if (unit != null)
|
||
{
|
||
unitId = unit.UnitId;
|
||
}
|
||
}
|
||
return unitId;
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
}
|