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.

270 lines
14 KiB

4 months ago
4 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using Quartz;
  7. namespace ICSSoft.FromERP
  8. {
  9. /// <summary>
  10. /// 产品Bom(凯尔克)
  11. /// </summary>
  12. public class IcsSBom : IJob
  13. {
  14. private static object key = new object();
  15. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  16. public void Execute(IJobExecutionContext context)
  17. {
  18. try
  19. {
  20. lock (key)
  21. {
  22. log.Info("开始……………………………………………………………………");
  23. Execute();
  24. log.Info("结束……………………………………………………………………");
  25. }
  26. }
  27. catch (Exception ex)
  28. {
  29. log.Error(ex.ToString());
  30. }
  31. }
  32. public void Execute()
  33. {
  34. try
  35. {
  36. string conStr = ICSHelper.GetConnectString();
  37. string Namespace = this.GetType().Namespace;
  38. //string Class = this.GetType().Name;
  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. #region SQL
  49. string sql = @" select distinct A2.[ID] as 料品ID,
  50. A2.[Code] as ,
  51. A2.Name as ,
  52. A4.Description as ,
  53. A3.ID as ID,
  54. A3.[Code] as ,
  55. A3.[Name] as ,
  56. A5.Description as ,
  57. A.FixedScrap as ,
  58. isnull( A6.[Code],'') as ,
  59. isnull( A.[ECNCode],'') as ,
  60. A.[UsageQty] as ,
  61. A.[ItemVersionCode] as ,
  62. A.[EffectiveDate] as ,
  63. isnull( A.IsSpecialUseItem,'') as ,
  64. A.[DisableDate] as ,
  65. A.[Sequence] as ,
  66. A.[IsEffective] as ,
  67. A1.[BOMVersionCode] as ,
  68. A.[OperationNum] as
  69. ,I.id as Org
  70. ,A.ModifiedOn as MTime
  71. ,A.ID as ErpDetailID
  72. INTO #TempBOM
  73. from {1}.dbo.CBO_BOMComponent as A
  74. left join {1}.dbo.[CBO_BOMMaster] as A1 on (A.[BOMMaster] = A1.[ID])
  75. left join {1}.dbo.[CBO_ItemMaster] as A2 on (A1.[ItemMaster] = A2.[ID])
  76. left join {1}.dbo.[CBO_ItemMaster_Trl] as A4 on (A4.SysMlFlag = 'zh-CN') and (A2.[ID] = A4.[ID])
  77. left join {1}.dbo.[CBO_ItemMaster] as A3 on (A.[ItemMaster] = A3.[ID])
  78. left join {1}.dbo.[CBO_ItemMaster_Trl] as A5 on (A5.SysMlFlag = 'zh-CN') and (A3.[ID] = A5.[ID])
  79. left join {1}.dbo.[CBO_Project] as A6 on (A.[CompProject] = A6.[ID])
  80. LEFT JOIN {1}.dbo.[Base_Organization] AS I ON ( A1.[Org] = I.[ID] )
  81. LEFT JOIN {1}.dbo.[Base_Organization_Trl] AS J ON ( I.[ID] = J.[ID] AND J.SysMLFlag='zh-CN')
  82. where 1=1 and IsEffective=1 and A.ModifiedOn>=@LastTime AND I.id={0} and A.ComponentType=0
  83. IF NOT EXISTS(SELECT * FROM #TempBOM)
  84. RETURN";
  85. sql = string.Format(sql, ErpId, erpName);
  86. sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempBOM");
  87. sql += "\r\n";
  88. sql += @"--删除数据
  89. -- DELETE ICSSBOM WHERE moOpComponentId NOT IN (SELECT OpComponentId FROM {1}.dbo.bom_opcomponent)
  90. SELECT a.ErpId INTO #tableICSSBOM
  91. FROM ICSSBOM a With(NoLock)
  92. LEFT JOIN {1}.dbo.[CBO_BOMComponent] b With(NoLock) ON a.ErpId=b.Id
  93. LEFT JOIN {1}.dbo.CBO_BOMMaster c With(NoLock) ON b.BOMMaster=c.Id
  94. WHERE b.id is null
  95. and a.TenantId='{0}'
  96. AND c.Org={2}
  97. DELETE ICSSBOM
  98. WHERE TenantId='{0}' and ErpId IN ( SELECT ErpId from #tableICSSBOM)
  99. ";
  100. Dictionary<string, string> values = new Dictionary<string, string>();
  101. values.Add("SBOMVER", "a.母料版本号");
  102. values.Add("ITEMCODE", "a.料品编码");
  103. values.Add("SBITEMCODE", "a.子阶料编码");
  104. values.Add("SBItemName", "a.子阶料名称");
  105. //values.Add("SBItemDesc", "a.子阶料描述");
  106. values.Add("SBSITEMCODE", "a.子阶料编码");
  107. values.Add("SBITEMQTY", "a.子阶料计量数量");
  108. values.Add("SEQ", "1");
  109. values.Add("SBItemECN", "a.工程变更号");
  110. values.Add("SBItemStatus", "'Y'");
  111. values.Add("SBItemEfftime", "a.生效日期");
  112. values.Add("SBItemInvtime", "a.失效日期");
  113. values.Add("SBItemProject", "a.项目号");
  114. values.Add("SBItemSeq", "a.次序");
  115. values.Add("Location", "a.工序号");
  116. values.Add("LastModificationTime", "a.Mtime");//操作时间
  117. values.Add("LastModifierUserId", "'" + ConstWorkPoint.Muser + "'");//操作人
  118. values.Add("LastModifierUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称
  119. values.Add("SBItemContype", "c.ItemContype");
  120. //更新存在数据
  121. sql += ICSHelper.UpdateSQL("b", values)
  122. + @" #TempBOM a
  123. INNER JOIN ICSSBOM b ON a.ErpDetailID=b.ErpId
  124. left join IcsInventory c on b.SBItemCode=c.ItemCode and b.TenantId=c.TenantId
  125. left join IcsMainCategoryCode d on c.ItemMainCategoryCode=d.ItemMainCategoryCode and c.TenantId=d.TenantId
  126. WHERE b.TenantId='{0}' and a.Org={2} ";
  127. values.Add("ErpId", "convert(nvarchar(100), a.ErpDetailID)");
  128. values.Add("CreationTime", "a.MTime");//操作人
  129. values.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人
  130. values.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称
  131. values.Add("TenantId", "'" + TenantId + "'");//站点
  132. sql += "\r\n";
  133. //插入新增数据
  134. sql += ICSHelper.InsertSQL("ICSSBOM", values)
  135. + @" #TempBOM a
  136. LEFT JOIN ICSSBOM b ON a.ErpDetailID=b.ErpId and b.TenantId='{0}'
  137. left join IcsInventory c on a.=c.ItemCode and c.TenantId='{0}'
  138. left join IcsMainCategoryCode d on c.ItemMainCategoryCode=d.ItemMainCategoryCode and d.TenantId='{0}'
  139. WHERE 1=1 and b.id is null
  140. and a.Org={2} ";
  141. sql += " DROP TABLE #TempBOM " +
  142. "DROP TABLE #tableICSSBOM";
  143. sql = string.Format(sql, TenantId, erpName, ErpId);
  144. // return;
  145. ICSHelper.ExecuteDate(conStr, sql);
  146. #endregion
  147. }
  148. }
  149. catch (Exception ex)
  150. {
  151. log.Error(ex.ToString());
  152. }
  153. }
  154. // public void Execute_bak20201201()
  155. // {
  156. // try
  157. // {
  158. // string conStr = ICSHelper.GetConnectString();
  159. // string Namespace = this.GetType().Namespace;
  160. // string Class = this.GetType().Name;
  161. // #region SQL
  162. // string sql = @" SELECT g.cInvDefine1,--存货自定义项1
  163. // a.Version,--Version 版本号
  164. // a.VersionDesc,--VersionDesc 版本说明
  165. // a.VersionEffDate,--VersionEffDate 版本生效日
  166. // a.VersionEndDate,--VersionEndDate 版本失效日
  167. // c.InvCode as cInvCode ,--存货编码
  168. // f.InvCode as SubcInvCode ,--存货编码
  169. // e.WIPType, --WIPType WIP属性(1入库/2工序/3领料/4虚拟)
  170. // d.SortSeq, --SortSeq 序号
  171. // d.BaseQtyN, --AuxBaseQtyN 辅助基本用量
  172. // d.BaseQtyD, --BaseQtyD 基本用量-分母
  173. // d.ByproductFlag, --ByproductFlag 是否联副产品
  174. // d.ProductType, --ProductType 产出类型(1:空/2:联产品/3:副产品)
  175. // d.CompScrap, --CompScrap 子件损耗率
  176. // a.CreateDate,a.CreateUser,a.ModifyDate,a.ModifyUser,a.cCurrentAuditor,--当前审批人
  177. // a.RelsTime as cAuditDate, --审核时间
  178. // i.ReplaceFlag,--是否替换料
  179. // i.OpComponentId,-- 子件Id
  180. // i.Sequence ReplaceSequence,--替代序号
  181. // K.InvCode as ReplaceInvCode ,--替代料编码
  182. // i.Factor ReplaceFactor,--替代比例
  183. // i.EffBegDate ReplaceEffBegDate,--生效日期
  184. // i.EffEndDate ReplaceEffEndDate,--失效日期
  185. // ISNULL(a.ModifyDate, a.CreateDate) AS MTIME
  186. // into #TempBOM
  187. // FROM {1}.dbo.bom_bom a -- BOM资料
  188. // INNER JOIN {1}.dbo.bom_parent b on a.BomId = b.BomId --BOM母件资料
  189. // INNER JOIN {1}.dbo.bas_part c on b.ParentId = c.PartId --物料表
  190. // INNER JOIN {1}.dbo.bom_opcomponent d on a.BomId=d.BomId --BOM子件资料
  191. // INNER JOIN {1}.dbo.bas_part f on d.ComponentId = f.PartId
  192. // INNER JOIN {1}.dbo.bom_opcomponentopt e ON d.OptionsId=e.OptionsId -- 子件选项资料
  193. // left JOIN {1}.dbo.bom_opcomponentsub i ON d.OpComponentId =i.OpComponentId --子件替代料
  194. // left join {1}.dbo.inventory g on c.InvCode=g.cInvCode --存货档案
  195. // LEFT JOIN {1}.dbo.bas_part K ON K.PartId=i.PartId
  196. //
  197. // WHERE ISNULL(a.ModifyDate, a.CreateDate)>=@LastTime
  198. // IF NOT EXISTS(SELECT * FROM #TempBOM)
  199. // RETURN";
  200. // sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempBOM");
  201. // sql += "\r\n";
  202. // sql += @"--删除数据
  203. // DELETE ICSBOM WHERE cInvCode NOT IN (SELECT InvCode FROM {1}.dbo.bas_part) ";
  204. // Dictionary<string, string> values = new Dictionary<string, string>();
  205. // values.Add("Version", "a.Version");
  206. // values.Add("VersionDesc", "a.VersionDesc");
  207. // values.Add("VersionEffDate", "a.VersionEffDate");
  208. // values.Add("VersionEndDate", "a.VersionEndDate");
  209. // values.Add("cInvCode", "a.cInvCode");
  210. // values.Add("SubcInvCode", "a.SubcInvCode");
  211. // values.Add("WIPType", "a.WIPType");
  212. // values.Add("SortSeq", "a.SortSeq");
  213. // values.Add("BaseQtyN", "a.BaseQtyN");
  214. // values.Add("BaseQtyD", "a.BaseQtyD");
  215. // values.Add("ByproductFlag", "a.ByproductFlag");
  216. // values.Add("ProductType", "a.ProductType");
  217. // values.Add("CompScrap", "a.CompScrap");
  218. // values.Add("CreateDate", "a.CreateDate");
  219. // values.Add("CreateUser", "a.CreateUser");
  220. // values.Add("ModifyDate", "a.ModifyDate");
  221. // values.Add("ModifyUser", "a.ModifyUser");
  222. // values.Add("cCurrentAuditor", "a.cCurrentAuditor");
  223. // values.Add("cAuditDate", "a.cAuditDate");
  224. // values.Add("ReplaceFlag", "a.ReplaceFlag");
  225. // values.Add("OpComponentId", "a.OpComponentId");
  226. // values.Add("ReplaceSequence", "a.ReplaceSequence");
  227. // values.Add("ReplaceInvCode", "a.ReplaceInvCode");
  228. // values.Add("ReplaceFactor", "a.ReplaceFactor");
  229. // values.Add("ReplaceEffBegDate", "a.ReplaceEffBegDate");
  230. // values.Add("ReplaceEffEndDate", "a.ReplaceEffEndDate");
  231. // values.Add("MTIME", "a.MTIME");
  232. // //更新存在数据
  233. // sql += ICSHelper.UpdateSQL("b", values)
  234. // + @" #TempBOM a
  235. // INNER JOIN ICSBOM b ON a.cInvCode=b.cInvCode ";
  236. // values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  237. // values.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  238. // values.Add("TenantId", "'" + TenantId + "'");
  239. // sql += "\r\n";
  240. // //插入新增数据
  241. // sql += ICSHelper.InsertSQL("ICSBOM", values)
  242. // + @" #TempBOM a
  243. // LEFT JOIN ICSBOM b ON a.cInvCode=b.cInvCode
  244. // WHERE b.cInvCode IS NULL
  245. // ";
  246. // sql += "DROP TABLE #TempBOM";
  247. // sql = string.Format(sql, ICSHelper.GetConfigString()["ERPDB"]);
  248. // ICSHelper.ExecuteDate(conStr, sql);
  249. // #endregion
  250. // }
  251. // catch (Exception ex)
  252. // {
  253. // log.Error(ex.ToString());
  254. // }
  255. // }
  256. }
  257. }