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.

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