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.

519 lines
32 KiB

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