using Model; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections; using System.Linq; using System.Runtime.InteropServices.ComTypes; namespace BLL { /// /// 作业票数据共享互通 /// public class APILicenseSyncService { #region 分包单位推送数据到总包单位 /// /// 推送分包作业票定稿数据 /// /// 项目Id /// 作业票定稿数据Id(为空的时候推送全部) /// public static ReturnData PushLicenseManagerLists(string projectId, string dataId = "") { Model.ReturnData responeData = new Model.ReturnData(); responeData.code = 0; responeData.message = string.Empty; using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString)) { try { var list = from x in db.View_License_LicenseManager where x.ProjectId == projectId select x; if (!string.IsNullOrWhiteSpace(dataId)) { list = list.Where(x => x.LicenseManagerId == dataId); } var dataList = (from x in list join blt in db.Base_LicenseType on x.LicenseTypeId equals blt.LicenseTypeId into bltGroup from blt in bltGroup.DefaultIfEmpty() join su in db.Sys_User on x.CompileMan equals su.UserId into sUser from su in sUser.DefaultIfEmpty() //join uw in db.WBS_UnitWork on x.WorkAreaId equals uw.UnitWorkId into uwGroup //from uw in uwGroup.DefaultIfEmpty() join att in db.AttachFile on x.LicenseManagerId equals att.ToKeyId into attTemp from att in attTemp.DefaultIfEmpty() join att1 in db.AttachFile on (x.LicenseManagerId + "_GB") equals att1.ToKeyId into att1Temp from att1 in att1Temp.DefaultIfEmpty() orderby x.CompileDate descending select new LicenseManagerItem { ProjectId = x.ProjectId, UnitId = x.UnitId, LicenseManagerId = x.LicenseManagerId, LicenseManagerCode = x.LicenseManagerCode, LicenseManageName = x.LicenseManageName, LicenseTypeId = x.LicenseTypeId, LicenseTypeCode = blt.LicenseTypeCode, LicenseTypeName = blt.LicenseTypeName, LicenseManageContents = x.LicenseManageContents, CompileMan = x.CompileMan, CompileManName = su.UserName, CompileDate = x.CompileDate, States = x.States, WorkAreaId = x.WorkAreaId, //UnitWorkCode = uw.UnitWorkCode, //UnitWorkName = uw.UnitWorkName, UnitWorkName = x.WorkAreaName, StartDate = x.StartDate, EndDate = x.EndDate, ApplicantMan = x.ApplicantMan, WorkStates = x.WorkStates, IsHighRisk = x.IsHighRisk, //LicenseCodes = x.LicenseCodes, SourceDes = x.SourceDes, //AttachUrl = AttachFileService.getFileUrl(x.LicenseManagerId),//附件 //AttachUrl1 = AttachFileService.getFileUrl(x.LicenseManagerId + "_GB"),//关闭附件 AttachFileId = att.AttachFileId, ToKeyId = att.ToKeyId, AttachSource = att.AttachSource, AttachUrl = att.AttachUrl, AttachFileId1 = att1.AttachFileId, ToKeyId1 = att1.ToKeyId, AttachSource1 = att1.AttachSource, AttachUrl1 = att1.AttachUrl, }).ToList(); if (dataList.Count() > 0) { var thisUnit = CommonService.GetIsThisUnit(); var porject = BLL.ProjectService.GetProjectByProjectId(projectId); var pushData = new LicenseManagerData { CollCropCode = thisUnit.CollCropCode,//分包单位社会统一信用码 UnitId = thisUnit.UnitId,//分包单位Id UnitName = thisUnit.UnitName,//分包单位名称 ShortUnitName = thisUnit.ShortUnitName,//分包单位简称 UnitDomain = Funs.SGGLUrl,//分包单位域名地址【文件存储地址】 SubjectUnit = porject.SubjectUnit,//主包单位Id SubjectProject = porject.SubjectProject,//主包项目Id Items = dataList//数据 }; var pushContent = JsonConvert.SerializeObject(pushData); //获取总包单位接口apiurl地址 var Url = BLL.UnitService.getUnitApiUrlByUnitId(porject.SubjectUnit); var ApiUrl = string.Empty; var WebUrl = string.Empty; if (Url != null) { var urls = Url.Split(','); ApiUrl = urls[0]; if (urls.Length > 1) { WebUrl = urls[1]; } } var baseurl = $"{ApiUrl}/api/LicenseSync/ReceiveSaveProjectLicenseManagerData"; string contenttype = "application/json;charset=unicode"; //Hashtable newToken = new Hashtable //{ // { "token", ServerService.GetToken().Token } //}; var returndata = APIGetHttpService.Http(baseurl, "Post", contenttype, null, pushContent); if (!string.IsNullOrEmpty(returndata)) { JObject obj = JObject.Parse(returndata); string mess = obj["message"].ToString(); string code = obj["code"].ToString(); } } else { responeData.message = "当前项目没有作业票定稿数据"; } } catch (Exception ex) { responeData.message = "同步到总包单位失败!"; ErrLogInfo.WriteLog("【作业票定稿】同步到总包单位失败!", ex); } } return responeData; } #endregion #region 总包单位接收分包单位推送数据 /// /// 总包单位接收保存分包单位推送的作业票定稿数据 /// /// /// public static string ReceiveSaveProjectLicenseManagerData(LicenseManagerData data) { using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString)) { string result = string.Empty; if (data.Items.Count() > 0) { //var jsonData = JsonConvert.SerializeObject(data); //ErrLogInfo.WriteLog($"【作业票定稿接收数据】{jsonData}"); //1、判断分包单位是否存在 var unit = UnitService.getUnitByCollCropCodeUnitName(data.CollCropCode, data.UnitName); if (unit == null) { result = "总包单位不存在本单位,请登录总包系统检查维护本单位信息!"; } else { //2、判断主包项目是否存在 var porject = BLL.ProjectService.GetProjectByProjectId(data.SubjectProject); if (porject == null) { result = "总包单位不存在本项目,请检查总包项目关联是否正确!"; } else { int succ = 0; //3、保存数据 foreach (var item in data.Items) { try { var model= db.License_LicenseManager.FirstOrDefault(e => e.LicenseManagerId == item.LicenseManagerId); if (model != null) {//编辑 model.ProjectId = data.SubjectProject; model.UnitId = unit.UnitId; model.LicenseManagerCode = item.LicenseManagerCode; model.LicenseManageName = item.LicenseManageName; //model.LicenseTypeId = item.LicenseTypeId; model.LicenseTypeId = APIDataShareSyncService.GetLicenseTypeId(item.LicenseTypeId, item.LicenseTypeCode, item.LicenseTypeName); //model.LicenseManageContents = HttpUtility.HtmlEncode(item.LicenseManageContents); model.LicenseManageContents = item.LicenseManageContents; model.States = item.States; model.StartDate = item.StartDate; model.EndDate = item.EndDate; model.ApplicantMan = item.ApplicantMan; model.IsHighRisk = item.IsHighRisk; model.WorkStates = item.WorkStates; model.SourceDes = $"{(!string.IsNullOrWhiteSpace(data.ShortUnitName) ? data.ShortUnitName : data.UnitName)}#{item.CompileManName}#单位工程:{item.UnitWorkName}"; model.CompileDate = item.CompileDate; //model.CompileMan = item.CompileMan; db.SubmitChanges(); } else {//新增 Model.License_LicenseManager newModel = new Model.License_LicenseManager(); newModel.LicenseManagerId = item.LicenseManagerId; newModel.ProjectId = data.SubjectProject; newModel.UnitId = unit.UnitId; newModel.LicenseManagerCode = item.LicenseManagerCode; newModel.LicenseManageName = item.LicenseManageName; //newModel.LicenseTypeId = item.LicenseTypeId; newModel.LicenseTypeId = APIDataShareSyncService.GetLicenseTypeId(item.LicenseTypeId, item.LicenseTypeCode, item.LicenseTypeName); //newModel.LicenseManageContents = HttpUtility.HtmlEncode(item.LicenseManageContents); newModel.LicenseManageContents = item.LicenseManageContents; newModel.States = item.States; newModel.StartDate = item.StartDate; newModel.EndDate = item.EndDate; newModel.ApplicantMan = item.ApplicantMan; newModel.IsHighRisk = item.IsHighRisk; newModel.WorkStates = item.WorkStates; newModel.SourceDes = $"{(!string.IsNullOrWhiteSpace(data.ShortUnitName) ? data.ShortUnitName : data.UnitName)}#{item.CompileManName}#单位工程:{item.UnitWorkName}"; newModel.CompileDate = item.CompileDate; //newModel.CompileMan = item.CompileMan; db.License_LicenseManager.InsertOnSubmit(newModel); db.SubmitChanges(); } succ++; //ErrLogInfo.WriteLog($"【作业票定稿接收数据——内容附件】UnitDomain:{data.UnitDomain};AttachFileId:{item.AttachFileId};ToKeyId:{item.ToKeyId};AttachSource:{item.AttachSource};AttachUrl:{item.AttachUrl}"); //附件处理:附件 BLL.FileInsertService.SaveAttachFileRecords(data.UnitDomain, item.AttachFileId, item.ToKeyId, item.AttachSource, item.AttachUrl); //附件处理:关闭附件 BLL.FileInsertService.SaveAttachFileRecords(data.UnitDomain, item.AttachFileId1, item.ToKeyId1, item.AttachSource1, item.AttachUrl1); } catch (Exception ex) { BLL.ErrLogInfo.WriteLog($"【{porject.ProjectName}】作业票定稿数据推送总包失败", ex.Message); continue; } } result = $"推送成功:总数{data.Items.Count()}条,成功{succ}条"; } } } else { result = "推送数据对象为空!"; } return result; } } #endregion } }