IcsFromERPJob
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

485 lines
30 KiB

  1. using NPinyin;
  2. using Quartz;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. namespace ICSSoft.FromERP
  9. {
  10. /// <summary>
  11. /// 工序/工艺路线(锐腾)
  12. /// </summary>
  13. public class IcsOp : IJob
  14. {
  15. private static object key = new object();
  16. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  17. public void Execute(IJobExecutionContext context)
  18. {
  19. try
  20. {
  21. lock (key)
  22. {
  23. log.Info("开始……………………………………………………………………");
  24. Execute();
  25. log.Info("结束……………………………………………………………………");
  26. }
  27. }
  28. catch (Exception ex)
  29. {
  30. log.Error(ex.ToString());
  31. }
  32. }
  33. public void Execute()
  34. {
  35. try
  36. {
  37. string conStr = ICSHelper.GetConnectString();
  38. string Namespace = this.GetType().Namespace;
  39. DataTable dt = ICSHelper.GetERPDB(conStr);
  40. foreach (DataRow dr in dt.Rows)
  41. {
  42. string erpName = ICSHelper.GetConfigString()["ERPDB"];
  43. string TenantId = dr["TenantId"].ToString();
  44. string TenantCode = dr["TenantCode"].ToString();
  45. string ErpId = dr["ErpID"].ToString(); //erpID
  46. string Class = this.GetType().Name + TenantCode;
  47. //erpName = string.Format(erpName, TenantId);
  48. string time = ICSHelper.GetTime(conStr, Namespace, Class, TenantId).ToStringBz();
  49. string sql = @"select DISTINCT EffectiveDate,DisableDate,ITEM.Code+'_'+RoutingVersionCode+'_'+cast(AlternateType as nvarchar) AS ROUTECODE,
  50. ITEM.Code+'_'+RoutingVersionCode+'_'+(case AlternateType when '0' then '' when '1' then '' when '2001' then '' end ) AS RouteDesc,RoutingVersionCode,RoutingVersion,
  51. u9route.CreatedOn as RouteCreateTime,u9route.ModifiedOn as RouteModifiedTime,opu9.Sequence as opseq,
  52. opu9trl.Description+'_'+OPU9.Sequence as U9Opcode,opu9.CreatedOn as opCreateTime,opu9.ModifiedOn as OpModifiedTime,opu9trl.Description as OpDesc,
  53. u9route.ID as routeID,opu9.ID as OPID,work.Code AS SegCode,u9Group.Code as CrewCode,u9route.Lot,u9Line.Code as LineCode,'' as MesOpcode,
  54. case when timeuom.Name='' then cast (opu9.RunTime as decimal(18,3))
  55. when timeuom.Name='秒' then cast (opu9.RunTime/3600 as decimal(18,3))
  56. when timeuom.Name='' then cast (opu9.RunTime/60 as decimal(18,3))
  57. when timeuom.Name='天' then cast (opu9.RunTime/24 as decimal(18,3))
  58. end as Rtime,
  59. case when timeuom.Name='' then cast (opu9.SetupTime as decimal(18,3))
  60. when timeuom.Name='秒' then cast (opu9.SetupTime/3600 as decimal(18,3))
  61. when timeuom.Name='' then cast (opu9.SetupTime/60 as decimal(18,3))
  62. when timeuom.Name='天' then cast (opu9.SetupTime/24 as decimal(18,3))
  63. end as Stime,u9Res.Code AS resCode,u9Restrl.Name as resName,PerResUsage,PerResOutput,
  64. case when IsSubcOperation='0' THEN 'N'
  65. else 'Y' end as IsSubcOperation,ITEM.Code as ItemCode,u9Group.Name AS CREWName,u9Linetrl.Name as LineName,worktrl.Name as SegNAME
  66. from
  67. {2}.DBO.CBO_Routing u9route
  68. LEFT JOIN {2}.DBO.CBO_Routing_Trl u9routetrl on u9routetrl.ID=u9route.ID
  69. LEFT JOIN {2}.DBO.CBO_ItemMaster ITEM ON ITEM.ID=ItemMaster
  70. LEFT JOIN {2}.DBO.CBO_Operation opu9 on opu9.Routing=u9route.ID
  71. LEFT JOIN {2}.DBO.CBO_Operation_Trl opu9trl on opu9.ID=opu9trl.ID
  72. LEFT JOIN {2}.DBO.CBO_StdOperation opstdu9 on opstdu9.id=opu9.StdOperation
  73. LEFT JOIN {2}.DBO.CBO_ShiftGroup u9Group on u9Group.ID=opu9.ShiftGroup
  74. LEFT JOIN {2}.DBO.CBO_WorkCenter work on work.ID=opu9.WorkCenter
  75. LEFT JOIN {2}.DBO.CBO_WorkCenter_Trl worktrl on work.ID=worktrl.ID
  76. LEFT JOIN {2}.DBO.Base_UOM_Trl timeuom on timeuom.ID=opu9.TimeUOM
  77. LEFT JOIN {2}.DBO.CBO_ProductionLine u9Line on u9Line.ID=ProductionLine
  78. LEFT JOIN {2}.DBO.CBO_ProductionLine_Trl u9Linetrl on u9Linetrl.ID=ProductionLine
  79. LEFT JOIN {2}.DBO.CBO_OpResource opRes ON opRes.Operation=opu9.id
  80. LEFT JOIN {2}.DBO.CBO_Resource u9Res on u9Res.ID=opRes.Resource
  81. LEFT JOIN {2}.DBO.CBO_Resource_Trl u9Restrl on u9Restrl.ID=u9Res.ID
  82. WHERE u9route.org='{0}' And u9route.ModifiedOn>='{1}' and u9route.ApproveDate is not null
  83. ";
  84. sql = string.Format(sql, ErpId, time, erpName);
  85. var table = ICSHelper.ExecuteTable(conStr, sql);
  86. if (table.Rows.Count <= 0)
  87. {
  88. log.Info("未查询到工艺路线数据,同步结束");
  89. return;
  90. }
  91. table.AsEnumerable().ToList().ForEach(row =>
  92. {
  93. row["MesOpcode"] = Pinyin.GetPinyin(row["U9Opcode"].ToString()).Replace(" ", "");
  94. });
  95. table.AcceptChanges();
  96. #region 工序
  97. string opsql = "";
  98. int count = 0;
  99. var opObj = table.AsEnumerable().Select(a => new { opcode = a["MesOpcode"].ToString(), opdesc = a["OpDesc"].ToString(), Opseq = a["opseq"].ToString(), u9Opcode = a["U9Opcode"].ToString() }).
  100. Distinct().ToList();
  101. opObj.ForEach(b => {
  102. count++;
  103. if (count != opObj.Count)
  104. opsql += $" SELECT '{b.opcode}' as Opcode,'{b.opdesc}' as Opdesc,'{b.Opseq}' as opseq,'{b.u9Opcode}' as u9Opcode union all ";
  105. else
  106. opsql += $" SELECT '{b.opcode}' as Opcode,'{b.opdesc}' as Opdesc,'{b.Opseq}' as opseq,'{b.u9Opcode}' as u9Opcode";
  107. });
  108. opsql = "select * into #TempOp from (" + opsql + ") a";
  109. Dictionary<string, string> opDic = new Dictionary<string, string>();
  110. opDic.Add("Opcode", "A.OPCODE");
  111. opDic.Add("Opdesc", "A.OPDESC");
  112. opDic.Add("OPNAME", "A.OPDESC");
  113. opDic.Add("OPSEQ", "A.OPSEQ");
  114. opDic.Add("OPCOLLECTION", "'MANUAl'");
  115. opDic.Add("OPCONTROL", "'10000000'");
  116. opDic.Add("ISMaterial", "'N'");
  117. opDic.Add("ISMerge", "'N'");
  118. opDic.Add("OPLevel", "'A'");
  119. opDic.Add("STIME", "0.00");
  120. opDic.Add("RTIME", "0.00");
  121. opDic.Add("Default1", "A.u9Opcode");
  122. opDic.Add("TenantId", "'" + TenantId + "'");
  123. opDic.Add("CreationTime", "GETDATE()");//操作时间
  124. opDic.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人
  125. opDic.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称
  126. opsql += @"
  127. DELETE OP
  128. FROM ICSOP OP
  129. LEFT JOIN
  130. (SELECT opu9trl.Description+'_'+Sequence as Opcode FROM {1}.DBO.CBO_Operation opu9
  131. LEFT JOIN {1}.DBO.CBO_Operation_Trl opu9trl on opu9.ID=opu9trl.ID
  132. WHERE opu9.org='{2}'
  133. ) u9op on u9op.Opcode=OP.Default1
  134. WHERE TenantId='{0}' and u9op.Opcode is null ";
  135. opsql += ICSHelper.InsertSQL("ICSOP", opDic)
  136. + @" #TempOp a
  137. LEFT JOIN ICSOP b on a.OPCODE=b.OPCODE and b.TenantId='{0}'
  138. WHERE 1=1 and b.OPCODE is null";
  139. opsql += " DROP TABLE #TempOp";
  140. opsql = string.Format(opsql, TenantId, erpName, ErpId);
  141. #endregion
  142. #region 途程
  143. String routeSql = "";
  144. string itemRouteSql = "";
  145. count = 0;
  146. 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["RoutingVersion"].ToString(), Lot = a["Lot"].ToString() }).
  147. Distinct().ToList();
  148. RouteObj.ForEach(b => {
  149. count++;
  150. if (count != RouteObj.Count)
  151. 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 union all ";
  152. else
  153. 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";
  154. });
  155. itemRouteSql = routeSql;
  156. routeSql = " select * into #TempRoute from (" + routeSql + ") a";
  157. Dictionary<string, string> routeDic = new Dictionary<string, string>();
  158. routeDic.Add("RouteCode", "A.RouteCode");
  159. routeDic.Add("RouteName", "A.RouteName");
  160. routeDic.Add("RouteDesc", "A.RouteName");
  161. routeDic.Add("RouteType", "'Normal'");
  162. routeDic.Add("EffDate", "a.EffDate");
  163. routeDic.Add("IvlDate", "a.IvlDate");
  164. routeDic.Add("Enabled", "'Y'");
  165. routeDic.Add("TenantId", "'" + TenantId + "'");
  166. routeDic.Add("CreationTime", "GETDATE()");//操作时间
  167. routeDic.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人
  168. routeDic.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称
  169. routeSql += @"
  170. DELETE ROUTE
  171. FROM ICSROUTE ROUTE
  172. LEFT JOIN
  173. (SELECT item.Code+'_'+route.RoutingVersionCode+'_'+cast(AlternateType as nvarchar) as RouteCode FROM {1}.DBO.CBO_Routing route
  174. LEFT JOIN {1}.DBO.CBO_ItemMaster item on route.ItemMaster=item.ID
  175. WHERE route.org='{2}'
  176. ) u9route on u9route.RouteCode=ROUTE.ROUTECODE
  177. WHERE TenantId='{0}' and u9route.RouteCode is null ";
  178. routeSql += ICSHelper.InsertSQL("ICSROUTE", routeDic)
  179. + @"#TempRoute a
  180. LEFT JOIN ICSROUTE b on a.ROUTECODE=b.ROUTECODE and b.TenantId='{0}'
  181. WHERE 1=1 and b.ID is null";
  182. routeSql += " DROP TABLE #TempRoute ";
  183. routeSql = string.Format(routeSql, TenantId, erpName, ErpId);
  184. #endregion
  185. #region 途程关联工序
  186. string routeOpSql = "";
  187. count = 0;
  188. 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() }).
  189. Distinct().ToList();
  190. RouteOpObj.ForEach(b => {
  191. count++;
  192. if (count != RouteOpObj.Count)
  193. 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";
  194. else
  195. 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 ";
  196. });
  197. var itemOpSql = routeOpSql;
  198. routeOpSql = $@"SELECT a.*,B.ID AS ROUTEID,C.ID AS OPID INTO #TempRoute2op FROM ({routeOpSql}) a
  199. LEFT JOIN ICSROUTE b on b.RouteCode=a.RouteCode and b.TenantId='{TenantId}'
  200. LEFT JOIN ICSOP C ON C.OPCODE=A.OPCODE AND C.TenantId='{TenantId}'
  201. ";
  202. Dictionary<string, string> routeOpDic = new Dictionary<string, string>();
  203. routeOpDic.Add("RouteCode", "A.RouteCode");
  204. routeOpDic.Add("OPCODE", "A.OPCODE");
  205. routeOpDic.Add("ROUTEID", "A.ROUTEID");
  206. routeOpDic.Add("OPID", "A.OPID");
  207. routeOpDic.Add("OPSEQ", "a.OPSEQ");
  208. routeOpDic.Add("IsMaterial", "'N'");
  209. routeOpDic.Add("IsMerge", "'N'");
  210. routeOpDic.Add("OpLevel", "'A'");
  211. routeOpDic.Add("OpControlSeq", "a.OPSEQ");
  212. routeOpDic.Add("OPControl", "'10000000'");
  213. routeOpDic.Add("ParallelOp", "a.ParallelOp");
  214. routeOpDic.Add("TenantId", "'" + TenantId + "'");
  215. routeOpDic.Add("CreationTime", "GETDATE()");//操作时间
  216. routeOpDic.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人
  217. routeOpDic.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称
  218. routeOpSql += @"
  219. DELETE ROUTEOP
  220. FROM ICSROUTE2OP ROUTEOP
  221. LEFT JOIN ICSOP OP ON ROUTEOP.OPID=OP.ID
  222. LEFT JOIN
  223. (SELECT item.Code+'_'+u9route.RoutingVersionCode+'_'+cast(u9route.AlternateType as nvarchar) as U9RouteCode,opu9trl.Description+'_'+OPU9.Sequence as u9Opcode
  224. FROM {1}.DBO.CBO_Routing u9route
  225. LEFT JOIN {1}.DBO.CBO_ItemMaster item on u9route.ItemMaster=item.ID
  226. LEFT JOIN {1}.DBO.CBO_Operation opu9 on opu9.Routing=u9route.ID
  227. LEFT JOIN {1}.DBO.CBO_Operation_Trl opu9trl on opu9.ID=opu9trl.ID
  228. WHERE u9route.org='{2}'
  229. ) u9routeOp on u9routeOp.U9RouteCode=ROUTEOP.ROUTECODE AND u9routeOp.u9Opcode=OP.DEFAULT1
  230. WHERE ROUTEOP.TenantId='{0}' and u9routeOp.U9RouteCode is null ";
  231. routeOpSql += ICSHelper.InsertSQL("ICSROUTE2OP", routeOpDic)
  232. + @"#TempRoute2op a
  233. LEFT JOIN ICSROUTE2OP b on a.ROUTECODE=b.ROUTECODE and A.OPCODE=B.OPCODE AND b.TenantId='{0}'
  234. WHERE 1=1 and b.OPCODE is null";
  235. routeOpSql += " DROP TABLE #TempRoute2op";
  236. routeOpSql = string.Format(routeOpSql, TenantId, erpName, ErpId);
  237. #endregion
  238. #region 物料关联途程
  239. itemRouteSql = $@"SELECT A.*,B.ID AS ITEMID,C.ID AS ROUTEID INTO #TempItemRoute
  240. FROM ({itemRouteSql}) A
  241. LEFT JOIN IcsInventory B ON A.ItemCode=B.ItemCode AND B.TenantId='{TenantId}'
  242. LEFT JOIN ICSROUTE C ON C.ROUTECODE=A.ROUTECODE AND C.TenantId='{TenantId}'
  243. ";
  244. itemRouteSql += @"
  245. DELETE ITEMROUTE
  246. FROM IcsItem2Route ITEMROUTE
  247. LEFT JOIN
  248. (SELECT item.Code+'_'+u9route.RoutingVersionCode+ '_'+cast(u9route.AlternateType as nvarchar) as U9RouteCode,
  249. item.Code as itemcode
  250. FROM {1}.DBO.CBO_Routing u9route
  251. LEFT JOIN {1}.DBO.CBO_ItemMaster item on u9route.ItemMaster=item.ID
  252. WHERE u9route.org='{2}'
  253. ) u9routeOp on u9routeOp.U9RouteCode=ITEMROUTE.ROUTECODE AND u9routeOp.ITEMCODE=ITEMROUTE.itemcode
  254. WHERE TenantId='{0}' and u9routeOp.itemcode is null ";
  255. Dictionary<string, string> ItemRouteDic = new Dictionary<string, string>();
  256. ItemRouteDic.Add("RouteCode", "A.RouteCode");
  257. ItemRouteDic.Add("ItemCode", "A.ItemCode");
  258. ItemRouteDic.Add("ROUTEID", "A.ROUTEID");
  259. ItemRouteDic.Add("ITEMID", "A.ITEMID");
  260. ItemRouteDic.Add("IsRef", "'Y'");
  261. ItemRouteDic.Add("Default1", "A.ProductLine");
  262. ItemRouteDic.Add("Default2", "A.ProductLineName");
  263. ItemRouteDic.Add("Default3", "A.LOT");
  264. ItemRouteDic.Add("TenantId", "'" + TenantId + "'");
  265. ItemRouteDic.Add("CreationTime", "GETDATE()");//操作时间
  266. ItemRouteDic.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人
  267. ItemRouteDic.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称
  268. itemRouteSql += ICSHelper.InsertSQL("ICSITEM2ROUTE", ItemRouteDic)
  269. + @" #TempItemRoute a
  270. LEFT JOIN ICSITEM2ROUTE b on a.ROUTECODE=b.ROUTECODE and A.ITEMCODE=B.ITEMCODE AND b.TenantId='{0}'
  271. WHERE 1=1 and b.ID is null ";
  272. itemRouteSql += @" Update A SET IsRef='N' FROM ICSITEM2ROUTE A INNER JOIN
  273. (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
  274. ON B.ITEMCODE=A.ITEMCODE AND B.ROUTECODE<>A.ROUTECODE
  275. ";
  276. itemRouteSql += " DROP TABLE #TempItemRoute";
  277. itemRouteSql = string.Format(itemRouteSql, TenantId, erpName, ErpId);
  278. #endregion
  279. #region 物料关联途程与工序
  280. itemOpSql = $@"Select a.*,B.ID AS RouteId,c.id as OpId,d.id as ItemId,C.DEFAULT1 AS U9OPCODE INTO #TEMPITEMOP from ({itemOpSql})a
  281. LEFT JOIN ICSROUTE b on b.RouteCode=a.RouteCode and b.TenantId='{TenantId}'
  282. LEFT JOIN ICSOP C ON C.OPCODE=A.OPCODE AND C.TenantId='{TenantId}'
  283. LEFT JOIN IcsInventory D ON D.ItemCode=A.ItemCode AND D.TenantId='{TenantId}'
  284. ";
  285. Dictionary<string, string> ItemOpDic = new Dictionary<string, string>();
  286. ItemOpDic.Add("STIME", "A.STIME");
  287. ItemOpDic.Add("RTIME", "A.RTIME");
  288. ItemOpDic.Add("Default1", "A.SEGCODE");
  289. ItemOpDic.Add("Default2", "A.SEGNAME");
  290. ItemOpDic.Add("Default3", "A.CREWCODE");
  291. ItemOpDic.Add("Default4", "A.CREWNAME");
  292. ItemOpDic.Add("LastModificationTime", "GETDATE()");//操作时间
  293. ItemOpDic.Add("LastModifierUserId", "'" + ConstWorkPoint.Muser + "'");//操作人
  294. ItemOpDic.Add("LastModifierUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称
  295. itemOpSql += ICSHelper.UpdateSQL("b", ItemOpDic)
  296. + "#TEMPITEMOP a"
  297. + " INNER JOIN ICSITEMROUTE2OP b on a.ROUTECODE = b.ROUTECODE and A.ITEMCODE = B.ITEMCODE AND A.OPCODE = B.OPCODE AND b.TenantId = '{0}'";
  298. ItemOpDic.Add("RouteCode", "A.RouteCode");
  299. ItemOpDic.Add("ItemCode", "A.ItemCode");
  300. ItemOpDic.Add("OpCode", "A.OpCode");
  301. ItemOpDic.Add("OpSeq", "A.OpSeq");
  302. ItemOpDic.Add("ITEMID", "A.ITEMID");
  303. ItemOpDic.Add("RouteId", "A.RouteId");
  304. ItemOpDic.Add("OpId", "A.OpId");
  305. ItemOpDic.Add("OPCONTROL", "'10000000'");
  306. ItemOpDic.Add("IdMergeRule", "'0'");
  307. ItemOpDic.Add("OpLevel", "'A'");
  308. ItemOpDic.Add("OpControlSeq", "A.OpSeq");
  309. ItemOpDic.Add("TenantId", "'" + TenantId + "'");
  310. ItemOpDic.Add("CreationTime", "GETDATE()");//操作时间
  311. ItemOpDic.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人
  312. ItemOpDic.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称
  313. itemOpSql += @"
  314. DELETE ITEMOP
  315. FROM ICSITEMROUTE2OP ITEMOP
  316. LEFT JOIN ICSOP OP ON ITEMOP.OPID=OP.ID
  317. LEFT JOIN
  318. (SELECT item.Code+'_'+u9route.RoutingVersionCode+ '_'+cast(u9route.AlternateType as nvarchar) as U9RouteCode,
  319. item.Code as itemcode,
  320. opu9trl.Description+'_'+OPU9.Sequence as U9Opcode
  321. FROM {1}.DBO.CBO_Routing u9route
  322. LEFT JOIN {1}.DBO.CBO_Operation opu9 on opu9.Routing=u9route.ID
  323. LEFT JOIN {1}.DBO.CBO_Operation_TRL opu9trl on opu9trl.ID=opu9.ID
  324. LEFT JOIN {1}.DBO.CBO_ItemMaster item on u9route.ItemMaster=item.ID
  325. WHERE u9route.org='{2}'
  326. ) u9ItemOp on u9ItemOp.U9RouteCode=ITEMOP.ROUTECODE AND u9ItemOp.ITEMCODE=ITEMOP.itemcode AND u9ItemOp.U9Opcode=OP.DEFAULT1
  327. WHERE ITEMOP.TenantId='{0}' and u9ItemOp.U9RouteCode is null ";
  328. itemOpSql += ICSHelper.InsertSQL("ICSITEMROUTE2OP", ItemOpDic)
  329. + @" #TEMPITEMOP a
  330. LEFT JOIN ICSITEMROUTE2OP b on a.ROUTECODE=b.ROUTECODE and A.ITEMCODE=B.ITEMCODE AND A.OPCODE=B.OPCODE AND b.TenantId='{0}'
  331. WHERE 1=1 and b.ID is null";
  332. itemOpSql = string.Format(itemOpSql, TenantId, erpName, ErpId);
  333. itemOpSql += " DROP TABLE #TEMPITEMOP";
  334. #endregion
  335. #region 工序资源
  336. string opResSql = "";
  337. count = 0;
  338. 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() }).
  339. Distinct().ToList();
  340. opResObj.ForEach(b => {
  341. count++;
  342. if (count != opResObj.Count)
  343. 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";
  344. else
  345. 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 ";
  346. });
  347. opResSql = $@"SELECT a.*,B.ID AS RouteId,c.id as OpId,d.id as ItemId,C.DEFAULT1 AS U9OPCODE INTO #TempOPRes FROM ({opResSql}) a
  348. LEFT JOIN ICSROUTE b on b.RouteCODE=a.RouteCode and b.TenantId='{TenantId}'
  349. LEFT JOIN ICSOP C ON C.OPCODE=A.OPCODE AND C.TenantId='{TenantId}'
  350. LEFT JOIN IcsInventory D ON D.ItemCode=A.ItemCode AND D.TenantId='{TenantId}'
  351. ";
  352. Dictionary<string, string> opResDic = new Dictionary<string, string>();
  353. opResDic.Add("PerResUsage", "CAST(A.PerResUsage AS DECIMAL)");
  354. opResDic.Add("PerResOutput", "CAST(A.PerResOutput AS DECIMAL)");
  355. opResDic.Add("LastModificationTime", "GETDATE()");//操作时间
  356. opResDic.Add("LastModifierUserId", "'" + ConstWorkPoint.Muser + "'");//操作人
  357. opResDic.Add("LastModifierUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称
  358. opResSql += ICSHelper.UpdateSQL("b", opResDic)
  359. + " #TempOPRes a"
  360. + " 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}'";
  361. opResSql += @"
  362. DELETE OPRES
  363. FROM IcsOpResU9 OPRES
  364. LEFT JOIN ICSOP OP ON OPRES.OPID=OP.ID
  365. LEFT JOIN
  366. (SELECT item.Code+'_'+u9route.RoutingVersionCode+ '_'+cast(u9route.AlternateType as nvarchar) as U9RouteCode,
  367. item.Code as itemcode,
  368. opu9trl.Description+'_'+OPU9.Sequence as U9Opcode,
  369. u9Res.code as resCode
  370. FROM {1}.DBO.CBO_Routing u9route
  371. LEFT JOIN {1}.DBO.CBO_Operation opu9 on opu9.Routing=u9route.ID
  372. LEFT JOIN {1}.DBO.CBO_ItemMaster item on u9route.ItemMaster=item.ID
  373. LEFT JOIN {1}.DBO.CBO_OpResource opRes ON opRes.Operation=opu9.id
  374. LEFT JOIN {1}.DBO.CBO_Operation_TRL opu9trl on opu9trl.ID=opu9.ID
  375. LEFT JOIN {1}.DBO.CBO_Resource u9Res on u9Res.ID=opRes.Resource
  376. WHERE u9route.org='{2}'
  377. ) u9opres on u9opres.U9RouteCode=OPRES.ROUTECODE AND u9opres.ITEMCODE=OPRES.itemcode AND u9opres.U9Opcode=OP.DEFAULT1 and u9opres.rescode=OPRES.U9ResCode
  378. WHERE OPRES.TenantId='{0}' and u9opres.U9Opcode is null ";
  379. opResDic.Add("RouteCode", "A.RouteCode");
  380. opResDic.Add("ItemCode", "A.ItemCode");
  381. opResDic.Add("u9Rescode", "A.Rescode");
  382. opResDic.Add("u9ResName", "A.ResName");
  383. opResDic.Add("OpCode", "A.OpCode");
  384. opResDic.Add("OpSeq", "A.OpSeq");
  385. opResDic.Add("ITEMID", "A.ITEMID");
  386. opResDic.Add("RouteId", "A.RouteId");
  387. opResDic.Add("OpId", "A.OpId");
  388. opResDic.Add("TenantId", "'" + TenantId + "'");
  389. opResDic.Add("CreationTime", "GETDATE()");//操作时间
  390. opResDic.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人
  391. opResDic.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称
  392. opResSql += ICSHelper.InsertSQL("IcsOpResU9", opResDic)
  393. + " #TempOPRes a"
  394. + " 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 ";
  395. opResSql = string.Format(opResSql, TenantId, erpName, ErpId);
  396. opResSql += " DROP TABLE #TempOPRes";
  397. #endregion
  398. log.Info("开始同步工序");
  399. ICSHelper.ExecuteDateNew(conStr, opsql);
  400. log.Info("结束同步工序");
  401. log.Info("开始同步途程");
  402. ICSHelper.ExecuteDateNew(conStr, routeSql);
  403. log.Info("结束同步途程");
  404. log.Info("开始同步途程关联工序");
  405. ICSHelper.ExecuteDateNew(conStr, routeOpSql);
  406. log.Info("结束同步途程关联工序");
  407. log.Info("开始同步物料关联途程");
  408. ICSHelper.ExecuteDateNew(conStr, itemRouteSql);
  409. log.Info("结束同步物料关联途程");
  410. log.Info("开始同步物料关联途程与工序");
  411. ICSHelper.ExecuteDateNew(conStr, itemOpSql);
  412. log.Info("结束同步物料关联途程与工序");
  413. log.Info("开始同步工序关联资源信息");
  414. ICSHelper.ExecuteDateNew(conStr, opResSql);
  415. log.Info("结束同步工序关联资源信息");
  416. var maxTime = table.AsEnumerable().Max(a => Convert.ToDateTime(a["RouteModifiedTime"]));
  417. ICSHelper.UpdateTime(conStr, Namespace, Class, TenantId, maxTime);
  418. }
  419. }
  420. catch (Exception ex)
  421. {
  422. throw ex;
  423. }
  424. }
  425. }
  426. }