using NPinyin; using Quartz; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; namespace ICSSoft.FromERP { /// /// 工序/工艺路线(锐腾) /// public class IcsOp : IJob { private static object key = new object(); private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); public void Execute(IJobExecutionContext context) { try { lock (key) { log.Info("开始……………………………………………………………………"); Execute(); log.Info("结束……………………………………………………………………"); } } catch (Exception ex) { log.Error(ex.ToString()); } } public void Execute() { try { string conStr = ICSHelper.GetConnectString(); string Namespace = this.GetType().Namespace; DataTable dt = ICSHelper.GetERPDB(conStr); foreach (DataRow dr in dt.Rows) { string erpName = ICSHelper.GetConfigString()["ERPDB"]; string TenantId = dr["TenantId"].ToString(); string TenantCode = dr["TenantCode"].ToString(); string ErpId = dr["ErpID"].ToString(); //erpID string Class = this.GetType().Name + TenantCode; //erpName = string.Format(erpName, TenantId); string time = ICSHelper.GetTime(conStr, Namespace, Class, TenantId).ToStringBz(); string sql = @"select DISTINCT EffectiveDate,DisableDate,ITEM.Code+'_'+RoutingVersionCode+'_'+cast(AlternateType as nvarchar) AS ROUTECODE, case AlternateType when '0' then '主制造' when '1' then '委外' when '2001' then '试制' end as AlternateType, ITEM.Code+'_'+RoutingVersionCode+'_'+(case AlternateType when '0' then '主制造' when '1' then '委外' when '2001' then '试制' end ) AS RouteDesc,RoutingVersionCode,RoutingVersion, u9route.CreatedOn as RouteCreateTime,u9route.ModifiedOn as RouteModifiedTime,opu9.Sequence as opseq, opu9trl.Description+'_'+OPU9.Sequence as U9Opcode,opu9.CreatedOn as opCreateTime,opu9.ModifiedOn as OpModifiedTime,opu9trl.Description as OpDesc, u9route.ID as routeID,opu9.ID as OPID,work.Code AS SegCode,u9Group.Code as CrewCode,u9route.Lot,u9Line.Code as LineCode,'' as MesOpcode, case when timeuom.Name='小时' then cast (opu9.RunTime as decimal(18,3)) when timeuom.Name='秒' then cast (opu9.RunTime/3600 as decimal(18,3)) when timeuom.Name='分钟' then cast (opu9.RunTime/60 as decimal(18,3)) when timeuom.Name='天' then cast (opu9.RunTime/24 as decimal(18,3)) end as Rtime, case when timeuom.Name='小时' then cast (opu9.SetupTime as decimal(18,3)) when timeuom.Name='秒' then cast (opu9.SetupTime/3600 as decimal(18,3)) when timeuom.Name='分钟' then cast (opu9.SetupTime/60 as decimal(18,3)) when timeuom.Name='天' then cast (opu9.SetupTime/24 as decimal(18,3)) end as Stime,u9Res.Code AS resCode,u9Restrl.Name as resName,PerResUsage,PerResOutput, case when IsSubcOperation='0' THEN 'N' else 'Y' end as IsSubcOperation,ITEM.Code as ItemCode,u9Group.Name AS CREWName,u9Linetrl.Name as LineName,worktrl.Name as SegNAME from {2}.DBO.CBO_Routing u9route LEFT JOIN {2}.DBO.CBO_Routing_Trl u9routetrl on u9routetrl.ID=u9route.ID LEFT JOIN {2}.DBO.CBO_ItemMaster ITEM ON ITEM.ID=ItemMaster LEFT JOIN {2}.DBO.CBO_Operation opu9 on opu9.Routing=u9route.ID LEFT JOIN {2}.DBO.CBO_Operation_Trl opu9trl on opu9.ID=opu9trl.ID LEFT JOIN {2}.DBO.CBO_StdOperation opstdu9 on opstdu9.id=opu9.StdOperation LEFT JOIN {2}.DBO.CBO_ShiftGroup u9Group on u9Group.ID=opu9.ShiftGroup LEFT JOIN {2}.DBO.CBO_WorkCenter work on work.ID=opu9.WorkCenter LEFT JOIN {2}.DBO.CBO_WorkCenter_Trl worktrl on work.ID=worktrl.ID LEFT JOIN {2}.DBO.Base_UOM_Trl timeuom on timeuom.ID=opu9.TimeUOM LEFT JOIN {2}.DBO.CBO_ProductionLine u9Line on u9Line.ID=ProductionLine LEFT JOIN {2}.DBO.CBO_ProductionLine_Trl u9Linetrl on u9Linetrl.ID=ProductionLine LEFT JOIN {2}.DBO.CBO_OpResource opRes ON opRes.Operation=opu9.id LEFT JOIN {2}.DBO.CBO_Resource u9Res on u9Res.ID=opRes.Resource LEFT JOIN {2}.DBO.CBO_Resource_Trl u9Restrl on u9Restrl.ID=u9Res.ID WHERE u9route.org='{0}' And u9route.ModifiedOn>='{1}' and u9route.ApproveDate is not null "; sql = string.Format(sql, ErpId, time, erpName); var table = ICSHelper.ExecuteTable(conStr, sql); if (table.Rows.Count <= 0) { log.Info("未查询到工艺路线数据,同步结束"); return; } table.AsEnumerable().ToList().ForEach(row => { row["MesOpcode"] = Pinyin.GetPinyin(row["U9Opcode"].ToString()).Replace(" ", ""); }); table.AcceptChanges(); #region 工序 string opsql = ""; int count = 0; var opObj = table.AsEnumerable().Select(a => new { opcode = a["MesOpcode"].ToString(), opdesc = a["OpDesc"].ToString(), Opseq = a["opseq"].ToString(), u9Opcode = a["U9Opcode"].ToString() }). Distinct().ToList(); opObj.ForEach(b => { count++; if (count != opObj.Count) opsql += $" SELECT '{b.opcode}' as Opcode,'{b.opdesc}' as Opdesc,'{b.Opseq}' as opseq,'{b.u9Opcode}' as u9Opcode union all "; else opsql += $" SELECT '{b.opcode}' as Opcode,'{b.opdesc}' as Opdesc,'{b.Opseq}' as opseq,'{b.u9Opcode}' as u9Opcode"; }); opsql = "select * into #TempOp from (" + opsql + ") a"; Dictionary opDic = new Dictionary(); opDic.Add("Opcode", "A.OPCODE"); opDic.Add("Opdesc", "A.OPDESC"); opDic.Add("OPNAME", "A.OPDESC"); opDic.Add("OPSEQ", "A.OPSEQ"); opDic.Add("OPCOLLECTION", "'MANUAl'"); opDic.Add("OPCONTROL", "'10000000'"); opDic.Add("ISMaterial", "'N'"); opDic.Add("ISMerge", "'N'"); opDic.Add("OPLevel", "'A'"); opDic.Add("STIME", "0.00"); opDic.Add("RTIME", "0.00"); opDic.Add("Default1", "A.u9Opcode"); opDic.Add("TenantId", "'" + TenantId + "'"); opDic.Add("CreationTime", "GETDATE()");//操作时间 opDic.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人 opDic.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称 opsql += @" DELETE OP FROM ICSOP OP LEFT JOIN (SELECT opu9trl.Description+'_'+Sequence as Opcode FROM {1}.DBO.CBO_Operation opu9 LEFT JOIN {1}.DBO.CBO_Operation_Trl opu9trl on opu9.ID=opu9trl.ID WHERE opu9.org='{2}' ) u9op on u9op.Opcode=OP.Default1 WHERE TenantId='{0}' and u9op.Opcode is null "; opsql += ICSHelper.InsertSQL("ICSOP", opDic) + @" #TempOp a LEFT JOIN ICSOP b on a.OPCODE=b.OPCODE and b.TenantId='{0}' WHERE 1=1 and b.OPCODE is null"; opsql += " DROP TABLE #TempOp"; opsql = string.Format(opsql, TenantId, erpName, ErpId); #endregion #region 途程 String routeSql = ""; string itemRouteSql = ""; count = 0; var RouteObj = table.AsEnumerable().Select(a => new { RouteCode = a["ROUTECODE"].ToString(), RouteName = a["RouteDesc"].ToString(), EffDate = ((DateTime)a["EffectiveDate"]).ToStringBz(), IvlDate = ((DateTime)a["DisableDate"]).ToStringBz(), ProductLine = a["LineCode"].ToString(), ProductLineName = a["LineName"].ToString(), ItemCode = a["ItemCode"].ToString(), RouteVersion = a["RoutingVersionCode"].ToString(), Lot = a["Lot"].ToString(), AlternateType = a["AlternateType"].ToString() }). Distinct().ToList(); RouteObj.ForEach(b => { count++; if (count != RouteObj.Count) routeSql += $" SELECT '{b.RouteCode}' as RouteCode,'{b.RouteName}' as RouteName,'{b.EffDate}' as EffDate,'{b.IvlDate}' as IvlDate,'{b.ItemCode}' as ItemCode,'{b.Lot}' AS Lot,'{b.ProductLine}' as ProductLine,'{b.ProductLineName}' as ProductLineName,'{b.RouteVersion}' as RouteVersion,'{b.AlternateType}' as AlternateType union all "; else routeSql += $" SELECT '{b.RouteCode}' as RouteCode,'{b.RouteName}' as RouteName,'{b.EffDate}' as EffDate,'{b.IvlDate}' as IvlDate,'{b.ItemCode}' as ItemCode,'{b.Lot}' AS Lot,'{b.ProductLine}' as ProductLine,'{b.ProductLineName}' as ProductLineName,'{b.RouteVersion}' as RouteVersion,'{b.AlternateType}' as AlternateType"; }); itemRouteSql = routeSql; routeSql = " select * into #TempRoute from (" + routeSql + ") a"; Dictionary routeDic = new Dictionary(); routeDic.Add("RouteCode", "A.RouteCode"); routeDic.Add("RouteName", "A.RouteName"); routeDic.Add("RouteDesc", "A.RouteName"); routeDic.Add("RouteType", "'Normal'"); routeDic.Add("EffDate", "a.EffDate"); routeDic.Add("IvlDate", "a.IvlDate"); routeDic.Add("Default4", "a.RouteVersion"); routeDic.Add("Default5", "a.AlternateType"); routeDic.Add("Enabled", "'Y'"); routeDic.Add("TenantId", "'" + TenantId + "'"); routeDic.Add("CreationTime", "GETDATE()");//操作时间 routeDic.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人 routeDic.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称 routeSql += @" DELETE ROUTE FROM ICSROUTE ROUTE LEFT JOIN (SELECT item.Code+'_'+route.RoutingVersionCode+'_'+cast(AlternateType as nvarchar) as RouteCode FROM {1}.DBO.CBO_Routing route LEFT JOIN {1}.DBO.CBO_ItemMaster item on route.ItemMaster=item.ID WHERE route.org='{2}' ) u9route on u9route.RouteCode=ROUTE.ROUTECODE WHERE TenantId='{0}' and u9route.RouteCode is null "; routeSql += ICSHelper.InsertSQL("ICSROUTE", routeDic) + @"#TempRoute a LEFT JOIN ICSROUTE b on a.ROUTECODE=b.ROUTECODE and b.TenantId='{0}' WHERE 1=1 and b.ID is null"; routeSql += " DROP TABLE #TempRoute "; routeSql = string.Format(routeSql, TenantId, erpName, ErpId); #endregion #region 途程关联工序 string routeOpSql = ""; count = 0; var RouteOpObj = table.AsEnumerable().Select(a => new { RouteCode = a["ROUTECODE"].ToString(), Opcode = a["MesOpcode"].ToString(), Opseq = a["opseq"].ToString(), ParallelOp = a["IsSubcOperation"].ToString(), ItemCode = a["ItemCode"].ToString(), RouteVersion = a["RoutingVersion"].ToString(), Stime = a["Stime"].ToString(), Rtime = a["Rtime"].ToString(), SEGCODE = a["SegCode"].ToString(), SegName = a["SegNAME"].ToString(), CrewCode = a["CrewCode"].ToString(), CREWName = a["CREWName"].ToString() }). Distinct().ToList(); RouteOpObj.ForEach(b => { count++; if (count != RouteOpObj.Count) routeOpSql += $" SELECT '{b.RouteCode}' as RouteCode,'{b.Opcode}' as Opcode,'{b.Opseq}' as Opseq,'{b.ParallelOp}' as ParallelOp,'{b.ItemCode}' as ItemCode,{b.Rtime} as Rtime,{b.Stime} as Stime,'{b.SEGCODE}' as SEGCODE,'{b.SegName}' as SegName,'{b.CrewCode}' as CrewCode,'{b.CREWName}' as CREWName union all"; else routeOpSql += $" SELECT '{b.RouteCode}' as RouteCode,'{b.Opcode}' as Opcode,'{b.Opseq}' as Opseq,'{b.ParallelOp}' as ParallelOp,'{b.ItemCode}' as ItemCode,{b.Rtime} as Rtime,{b.Stime} as Stime,'{b.SEGCODE}' as SEGCODE,'{b.SegName}' as SegName,'{b.CrewCode}' as CrewCode,'{b.CREWName}' as CREWName "; }); var itemOpSql = routeOpSql; routeOpSql = $@"SELECT a.*,B.ID AS ROUTEID,C.ID AS OPID INTO #TempRoute2op FROM ({routeOpSql}) a LEFT JOIN ICSROUTE b on b.RouteCode=a.RouteCode and b.TenantId='{TenantId}' LEFT JOIN ICSOP C ON C.OPCODE=A.OPCODE AND C.TenantId='{TenantId}' "; Dictionary routeOpDic = new Dictionary(); routeOpDic.Add("RouteCode", "A.RouteCode"); routeOpDic.Add("OPCODE", "A.OPCODE"); routeOpDic.Add("ROUTEID", "A.ROUTEID"); routeOpDic.Add("OPID", "A.OPID"); routeOpDic.Add("OPSEQ", "a.OPSEQ"); routeOpDic.Add("IsMaterial", "'N'"); routeOpDic.Add("IsMerge", "'N'"); routeOpDic.Add("OpLevel", "'A'"); routeOpDic.Add("OpControlSeq", "a.OPSEQ"); routeOpDic.Add("OPControl", "'10000000'"); routeOpDic.Add("ParallelOp", "a.ParallelOp"); routeOpDic.Add("TenantId", "'" + TenantId + "'"); routeOpDic.Add("CreationTime", "GETDATE()");//操作时间 routeOpDic.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人 routeOpDic.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称 routeOpSql += @" DELETE ROUTEOP FROM ICSROUTE2OP ROUTEOP LEFT JOIN ICSOP OP ON ROUTEOP.OPID=OP.ID LEFT JOIN (SELECT item.Code+'_'+u9route.RoutingVersionCode+'_'+cast(u9route.AlternateType as nvarchar) as U9RouteCode,opu9trl.Description+'_'+OPU9.Sequence as u9Opcode FROM {1}.DBO.CBO_Routing u9route LEFT JOIN {1}.DBO.CBO_ItemMaster item on u9route.ItemMaster=item.ID LEFT JOIN {1}.DBO.CBO_Operation opu9 on opu9.Routing=u9route.ID LEFT JOIN {1}.DBO.CBO_Operation_Trl opu9trl on opu9.ID=opu9trl.ID WHERE u9route.org='{2}' ) u9routeOp on u9routeOp.U9RouteCode=ROUTEOP.ROUTECODE AND u9routeOp.u9Opcode=OP.DEFAULT1 WHERE ROUTEOP.TenantId='{0}' and u9routeOp.U9RouteCode is null "; routeOpSql += ICSHelper.InsertSQL("ICSROUTE2OP", routeOpDic) + @"#TempRoute2op a LEFT JOIN ICSROUTE2OP b on a.ROUTECODE=b.ROUTECODE and A.OPCODE=B.OPCODE AND b.TenantId='{0}' WHERE 1=1 and b.OPCODE is null"; routeOpSql += " DROP TABLE #TempRoute2op"; routeOpSql = string.Format(routeOpSql, TenantId, erpName, ErpId); #endregion #region 物料关联途程 itemRouteSql = $@"SELECT A.*,B.ID AS ITEMID,C.ID AS ROUTEID INTO #TempItemRoute FROM ({itemRouteSql}) A LEFT JOIN IcsInventory B ON A.ItemCode=B.ItemCode AND B.TenantId='{TenantId}' LEFT JOIN ICSROUTE C ON C.ROUTECODE=A.ROUTECODE AND C.TenantId='{TenantId}' "; itemRouteSql += @" DELETE ITEMROUTE FROM IcsItem2Route ITEMROUTE LEFT JOIN (SELECT item.Code+'_'+u9route.RoutingVersionCode+ '_'+cast(u9route.AlternateType as nvarchar) as U9RouteCode, item.Code as itemcode FROM {1}.DBO.CBO_Routing u9route LEFT JOIN {1}.DBO.CBO_ItemMaster item on u9route.ItemMaster=item.ID WHERE u9route.org='{2}' ) u9routeOp on u9routeOp.U9RouteCode=ITEMROUTE.ROUTECODE AND u9routeOp.ITEMCODE=ITEMROUTE.itemcode WHERE TenantId='{0}' and u9routeOp.itemcode is null "; Dictionary ItemRouteDic = new Dictionary(); ItemRouteDic.Add("RouteCode", "A.RouteCode"); ItemRouteDic.Add("ItemCode", "A.ItemCode"); ItemRouteDic.Add("ROUTEID", "A.ROUTEID"); ItemRouteDic.Add("ITEMID", "A.ITEMID"); ItemRouteDic.Add("IsRef", "'Y'"); ItemRouteDic.Add("Default1", "A.ProductLine"); ItemRouteDic.Add("Default2", "A.ProductLineName"); ItemRouteDic.Add("Default3", "A.LOT"); ItemRouteDic.Add("Default4", "a.RouteVersion"); ItemRouteDic.Add("TenantId", "'" + TenantId + "'"); ItemRouteDic.Add("CreationTime", "GETDATE()");//操作时间 ItemRouteDic.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人 ItemRouteDic.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称 //itemRouteSql += @" Update A SET IsRef='N' FROM ICSITEM2ROUTE A INNER JOIN // (SELECT A.ROUTECODE,A.ITEMCODE FROM #TempItemRoute A LEFT JOIN ICSITEM2ROUTE B ON a.ROUTECODE=b.ROUTECODE and A.ITEMCODE=B.ITEMCODE AND b.TenantId='{0}' Where b.ID is null)B // ON B.ITEMCODE=A.ITEMCODE AND B.ROUTECODE<>A.ROUTECODE // "; itemRouteSql += ICSHelper.InsertSQL("ICSITEM2ROUTE", ItemRouteDic) + @" #TempItemRoute a LEFT JOIN ICSITEM2ROUTE b on a.ROUTECODE=b.ROUTECODE and A.ITEMCODE=B.ITEMCODE AND b.TenantId='{0}' WHERE 1=1 and b.ID is null "; itemRouteSql += " DROP TABLE #TempItemRoute"; itemRouteSql = string.Format(itemRouteSql, TenantId, erpName, ErpId); #endregion #region 物料关联途程与工序 itemOpSql = $@"Select a.*,B.ID AS RouteId,c.id as OpId,d.id as ItemId,C.DEFAULT1 AS U9OPCODE INTO #TEMPITEMOP from ({itemOpSql})a LEFT JOIN ICSROUTE b on b.RouteCode=a.RouteCode and b.TenantId='{TenantId}' LEFT JOIN ICSOP C ON C.OPCODE=A.OPCODE AND C.TenantId='{TenantId}' LEFT JOIN IcsInventory D ON D.ItemCode=A.ItemCode AND D.TenantId='{TenantId}' "; Dictionary ItemOpDic = new Dictionary(); ItemOpDic.Add("STIME", "A.STIME"); ItemOpDic.Add("RTIME", "A.RTIME"); ItemOpDic.Add("Default1", "A.SEGCODE"); ItemOpDic.Add("Default2", "A.SEGNAME"); ItemOpDic.Add("Default3", "A.CREWCODE"); ItemOpDic.Add("Default4", "A.CREWNAME"); ItemOpDic.Add("LastModificationTime", "GETDATE()");//操作时间 ItemOpDic.Add("LastModifierUserId", "'" + ConstWorkPoint.Muser + "'");//操作人 ItemOpDic.Add("LastModifierUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称 itemOpSql += ICSHelper.UpdateSQL("b", ItemOpDic) + "#TEMPITEMOP a" + " INNER JOIN ICSITEMROUTE2OP b on a.ROUTECODE = b.ROUTECODE and A.ITEMCODE = B.ITEMCODE AND A.OPCODE = B.OPCODE AND b.TenantId = '{0}'"; ItemOpDic.Add("RouteCode", "A.RouteCode"); ItemOpDic.Add("ItemCode", "A.ItemCode"); ItemOpDic.Add("OpCode", "A.OpCode"); ItemOpDic.Add("OpSeq", "A.OpSeq"); ItemOpDic.Add("ITEMID", "A.ITEMID"); ItemOpDic.Add("RouteId", "A.RouteId"); ItemOpDic.Add("OpId", "A.OpId"); ItemOpDic.Add("OPCONTROL", "'10000000'"); ItemOpDic.Add("IdMergeRule", "'0'"); ItemOpDic.Add("OpLevel", "'A'"); ItemOpDic.Add("OpControlSeq", "A.OpSeq"); ItemOpDic.Add("TenantId", "'" + TenantId + "'"); ItemOpDic.Add("CreationTime", "GETDATE()");//操作时间 ItemOpDic.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人 ItemOpDic.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称 itemOpSql += @" DELETE ITEMOP FROM ICSITEMROUTE2OP ITEMOP LEFT JOIN ICSOP OP ON ITEMOP.OPID=OP.ID LEFT JOIN (SELECT item.Code+'_'+u9route.RoutingVersionCode+ '_'+cast(u9route.AlternateType as nvarchar) as U9RouteCode, item.Code as itemcode, opu9trl.Description+'_'+OPU9.Sequence as U9Opcode FROM {1}.DBO.CBO_Routing u9route LEFT JOIN {1}.DBO.CBO_Operation opu9 on opu9.Routing=u9route.ID LEFT JOIN {1}.DBO.CBO_Operation_TRL opu9trl on opu9trl.ID=opu9.ID LEFT JOIN {1}.DBO.CBO_ItemMaster item on u9route.ItemMaster=item.ID WHERE u9route.org='{2}' ) u9ItemOp on u9ItemOp.U9RouteCode=ITEMOP.ROUTECODE AND u9ItemOp.ITEMCODE=ITEMOP.itemcode AND u9ItemOp.U9Opcode=OP.DEFAULT1 WHERE ITEMOP.TenantId='{0}' and u9ItemOp.U9RouteCode is null "; itemOpSql += ICSHelper.InsertSQL("ICSITEMROUTE2OP", ItemOpDic) + @" #TEMPITEMOP a LEFT JOIN ICSITEMROUTE2OP b on a.ROUTECODE=b.ROUTECODE and A.ITEMCODE=B.ITEMCODE AND A.OPCODE=B.OPCODE AND b.TenantId='{0}' WHERE 1=1 and b.ID is null"; itemOpSql = string.Format(itemOpSql, TenantId, erpName, ErpId); itemOpSql += " DROP TABLE #TEMPITEMOP"; #endregion #region 工序资源 string opResSql = ""; count = 0; var opResObj = table.AsEnumerable().Where(a => a["resCode"].ToString() != "").Select(a => new { RouteCode = a["ROUTECODE"].ToString(), Opcode = a["MesOpcode"].ToString(), Opseq = a["opseq"].ToString(), ItemCode = a["ItemCode"].ToString(), resCode = a["resCode"].ToString(), resName = a["resName"].ToString(), PerResUsage = a["PerResUsage"].ToString(), PerResOutput = a["PerResOutput"].ToString() }). Distinct().ToList(); opResObj.ForEach(b => { count++; if (count != opResObj.Count) opResSql += $" SELECT '{b.RouteCode}' as RouteCode,'{b.Opcode}' as Opcode,'{b.Opseq}' as Opseq,'{b.resCode}' as resCode,'{b.ItemCode}' as ItemCode,'{b.resName}' as resName,'{b.PerResUsage}' as PerResUsage,'{b.PerResOutput}' as PerResOutput union all"; else opResSql += $" SELECT '{b.RouteCode}' as RouteCode,'{b.Opcode}' as Opcode,'{b.Opseq}' as Opseq,'{b.resCode}' as resCode,'{b.ItemCode}' as ItemCode,'{b.resName}' as resName,'{b.PerResUsage}' as PerResUsage,'{b.PerResOutput}' as PerResOutput "; }); opResSql = $@"SELECT a.*,B.ID AS RouteId,c.id as OpId,d.id as ItemId,C.DEFAULT1 AS U9OPCODE INTO #TempOPRes FROM ({opResSql}) a LEFT JOIN ICSROUTE b on b.RouteCODE=a.RouteCode and b.TenantId='{TenantId}' LEFT JOIN ICSOP C ON C.OPCODE=A.OPCODE AND C.TenantId='{TenantId}' LEFT JOIN IcsInventory D ON D.ItemCode=A.ItemCode AND D.TenantId='{TenantId}' "; Dictionary opResDic = new Dictionary(); opResDic.Add("PerResUsage", "CAST(A.PerResUsage AS DECIMAL)"); opResDic.Add("PerResOutput", "CAST(A.PerResOutput AS DECIMAL)"); opResDic.Add("LastModificationTime", "GETDATE()");//操作时间 opResDic.Add("LastModifierUserId", "'" + ConstWorkPoint.Muser + "'");//操作人 opResDic.Add("LastModifierUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称 opResSql += ICSHelper.UpdateSQL("b", opResDic) + " #TempOPRes a" + " INNER JOIN IcsOpResU9 b on a.ROUTECODE = b.ROUTECODE and A.ITEMCODE = B.ITEMCODE AND A.OPCODE = B.OPCODE AND A.ResCode=B.U9ResCode AND b.TenantId = '{0}'"; opResSql += @" DELETE OPRES FROM IcsOpResU9 OPRES LEFT JOIN ICSOP OP ON OPRES.OPID=OP.ID LEFT JOIN (SELECT item.Code+'_'+u9route.RoutingVersionCode+ '_'+cast(u9route.AlternateType as nvarchar) as U9RouteCode, item.Code as itemcode, opu9trl.Description+'_'+OPU9.Sequence as U9Opcode, u9Res.code as resCode FROM {1}.DBO.CBO_Routing u9route LEFT JOIN {1}.DBO.CBO_Operation opu9 on opu9.Routing=u9route.ID LEFT JOIN {1}.DBO.CBO_ItemMaster item on u9route.ItemMaster=item.ID LEFT JOIN {1}.DBO.CBO_OpResource opRes ON opRes.Operation=opu9.id LEFT JOIN {1}.DBO.CBO_Operation_TRL opu9trl on opu9trl.ID=opu9.ID LEFT JOIN {1}.DBO.CBO_Resource u9Res on u9Res.ID=opRes.Resource WHERE u9route.org='{2}' ) u9opres on u9opres.U9RouteCode=OPRES.ROUTECODE AND u9opres.ITEMCODE=OPRES.itemcode AND u9opres.U9Opcode=OP.DEFAULT1 and u9opres.rescode=OPRES.U9ResCode WHERE OPRES.TenantId='{0}' and u9opres.U9Opcode is null "; opResDic.Add("RouteCode", "A.RouteCode"); opResDic.Add("ItemCode", "A.ItemCode"); opResDic.Add("u9Rescode", "A.Rescode"); opResDic.Add("u9ResName", "A.ResName"); opResDic.Add("OpCode", "A.OpCode"); opResDic.Add("OpSeq", "A.OpSeq"); opResDic.Add("ITEMID", "A.ITEMID"); opResDic.Add("RouteId", "A.RouteId"); opResDic.Add("OpId", "A.OpId"); opResDic.Add("TenantId", "'" + TenantId + "'"); opResDic.Add("CreationTime", "GETDATE()");//操作时间 opResDic.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人 opResDic.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称 opResSql += ICSHelper.InsertSQL("IcsOpResU9", opResDic) + " #TempOPRes a" + " LEFT JOIN IcsOpResU9 b on a.ROUTECODE = b.ROUTECODE and A.ITEMCODE = B.ITEMCODE AND A.OPCODE = B.OPCODE AND A.ResCode=B.U9ResCode AND b.TenantId = '{0}' WHERE B.ID IS NULL "; opResSql = string.Format(opResSql, TenantId, erpName, ErpId); opResSql += " DROP TABLE #TempOPRes"; #endregion string updateRef =@" UPDATE A SET IsRef = 'N' FROM IcsItem2Route A LEFT JOIN IcsRoute C ON A.RouteId = C.Id LEFT JOIN ( select MAX(SUBSTRING(A.default4, 2, len(A.default4) - 1)) AS[VerSion], ItemCode, B.Default5, A.TenantId from IcsItem2Route A LEFT JOIN IcsRoute B ON A.RouteId = B.Id GROUP BY ItemCode, B.Default5, A.TenantId HAVING(NOT EXISTS(SELECT IcsItem2Route.ID FROM IcsItem2Route LEFT JOIN IcsRoute ON IcsItem2Route.RouteId = IcsRoute.Id WHERE IcsItem2Route.ItemCode = A.ItemCode AND B.Default5 <> IcsRoute.Default5) OR B.Default5 = '主制造'))B on B.Default5 = C.Default5 AND b.ItemCode = a.ItemCode and b.VerSion = SUBSTRING(A.default4, 2, len(A.default4) - 1) AND B.TenantId = A.TenantId WHERE B.ItemCode IS NULL"; log.Info("开始同步工序"); ICSHelper.ExecuteDateNew(conStr, opsql); log.Info("结束同步工序"); log.Info("开始同步途程"); ICSHelper.ExecuteDateNew(conStr, routeSql); log.Info("结束同步途程"); log.Info("开始同步途程关联工序"); ICSHelper.ExecuteDateNew(conStr, routeOpSql); log.Info("结束同步途程关联工序"); log.Info("开始同步物料关联途程"); ICSHelper.ExecuteDateNew(conStr, itemRouteSql); log.Info("结束同步物料关联途程"); log.Info("开始同步物料关联途程与工序"); ICSHelper.ExecuteDateNew(conStr, itemOpSql); log.Info("结束同步物料关联途程与工序"); log.Info("开始同步工序关联资源信息"); ICSHelper.ExecuteDateNew(conStr, opResSql); log.Info("结束同步工序关联资源信息"); log.Info("开始更新默认途程"); ICSHelper.ExecuteDateNew(conStr, updateRef); log.Info("结束更新默认途程"); var maxTime = table.AsEnumerable().Max(a => Convert.ToDateTime(a["RouteModifiedTime"])); ICSHelper.UpdateTime(conStr, Namespace, Class, TenantId, maxTime); } } catch (Exception ex) { throw ex; } } } }