using Quartz; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; namespace ICSSoft.FromERP { /// /// 物料主分类(凯尔克) /// public class IcsMainCategoryCode : 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 conERPStr = ICSHelper.GetERPConnectString(); string conStr = ICSHelper.GetConnectString(); string Namespace = this.GetType().Namespace; //string Class = this.GetType().Name; DataTable dt = ICSHelper.GetERPDB(conStr); foreach (DataRow dr in dt.Rows) { string erpName = ICSHelper.GetConfigString()["ERPDB"]; string TenantId = dr["TenantId"].ToString();//mes 组织 string TenantCode = dr["TenantCode"].ToString(); string ErpId = dr["ErpID"].ToString(); //erpID string Class = this.GetType().Name + TenantCode; erpName = string.Format(erpName, TenantId); #region SQL string sql = @" select a.code as ItemMainCategoryCode ,b.Name as ItemMainCategoryName ,b.Description as ItemMainCategoryDesc ,a.ModifiedOn MTime ,I.id as Org ,p.parentCode INTO #TempItemCategory from {1}.dbo.[CBO_Category] a left join {1}.dbo.CBO_CategoryType catt on catt.id=a.CategorySystem left join {1}.dbo.[CBO_Category_Trl] b on a.id=b.id AND b.SysMLFlag='zh-CN' left join {1}.dbo.[Base_Organization] AS I ON ( A.[Org] = I.[ID] ) left join {1}.dbo.[Base_Organization_Trl] AS J ON ( I.[ID] = J.[ID] AND J.SysMLFlag='zh-CN') left join ( SELECT a2.code parentCode ,a3.name parentName , A .code , A1.[Name] , a.level+1 层级 FROM {1}.dbo.Base_DefineValue AS A LEFT JOIN {1}.dbo.[Base_DefineValue_Trl] AS A1 ON(A1.SysMlFlag = 'zh-CN') AND(A.[ID] = A1.[ID]) left join {1}.dbo.Base_DefineValue a2 on a2.id=a.ParentNode and a2.Effective_IsEffective=1 and a2.Effective_DisableDate>= CAST ( CAST ( GETDATE( ) AS DATE ) AS VARCHAR ( 10 ) ) + ' 00:00:00' AND a2.Effective_EffectiveDate< CAST ( CAST ( dateadd( DAY, 1, GETDATE( ) ) AS DATE ) AS VARCHAR ( 10 ) ) + ' 00:00:00' LEFT JOIN {1}.dbo.[Base_DefineValue_Trl] AS A3 ON(A3.SysMlFlag = 'zh-CN') AND(A2.[ID] = A3.[ID]) WHERE 1=1 and A.[ValueSetDef] = (SELECT ID FROM {1}.dbo.Base_ValueSetDef_trl DD WHERE name = '库存分类' ) and a.Effective_IsEffective=1 and a.Effective_DisableDate>= CAST ( CAST ( GETDATE( ) AS DATE ) AS VARCHAR ( 10 ) ) + ' 00:00:00' AND a.Effective_EffectiveDate< CAST ( CAST ( dateadd( DAY, 1, GETDATE( ) ) AS DATE ) AS VARCHAR ( 10 ) ) + ' 00:00:00' --order by a.level+1, a2.code,A .code ) p ON a.code=p.code where 1=1 and A.ModifiedOn>=@LastTime AND I.id={0} and catt.IsStock='1' ORDER BY A.Code IF NOT EXISTS(SELECT * FROM #TempItemCategory) RETURN"; sql = string.Format(sql, ErpId, erpName); sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempItemCategory"); sql += @"--删除数据 -- DELETE ICSINVENTORY WHERE ItemCode NOT IN (SELECT cInvCode FROM {1}.dbo.[CBO_ItemMaster]) SELECT a.ItemMainCategoryCode INTO #tableItemCategory FROM IcsMainCategoryCode a With(NoLock) LEFT JOIN {1}.dbo.[CBO_Category] b With(NoLock) ON a.ItemMainCategoryCode=b.Code WHERE b.code is null and a.TenantId='{0}' AND b.Org={2} DELETE IcsMainCategoryCode WHERE TenantId='{0}' and ItemMainCategoryCode IN ( SELECT ItemMainCategoryCode from #tableItemCategory) "; Dictionary values = new Dictionary(); values.Add("ItemMainCategoryCode", "a.ItemMainCategoryCode");//物料编号 values.Add("ItemMainCategoryName", "a.ItemMainCategoryNAME");//物料名称 values.Add("ItemMainCategoryDesc", "a.ItemMainCategoryDesc");//物料描述 values.Add("LastModificationTime", "a.MTime");//操作时间 values.Add("PCode", "a.parentCode");//操作人 //更新存在数据 sql += ICSHelper.UpdateSQL("b", values) + @" #TempItemCategory a INNER JOIN IcsMainCategoryCode b ON a.ItemMainCategoryCode=b.ItemMainCategoryCode WHERE b.TenantId='" + TenantId + "' and a.Org={2}"; values.Add("CreationTime", "a.MTime");//操作人 values.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人 values.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称 values.Add("TenantId", "'" + TenantId + "'");//站点 //插入新增数据 sql += ICSHelper.InsertSQL("IcsMainCategoryCode", values) + @" #TempItemCategory a LEFT JOIN IcsMainCategoryCode b ON a.ItemMainCategoryCode=b.ItemMainCategoryCode and b.TenantId='{0}' WHERE 1=1 and b.ItemMainCategoryCode is null and a.Org={2} "; sql += " DROP TABLE #TempItemCategory " + " DROP TABLE #tableItemCategory "; sql = string.Format(sql, TenantId, erpName,ErpId); // return; ICSHelper.ExecuteDate(conStr, sql); #endregion } } catch (Exception ex) { log.Error(ex.ToString()); } } } }