using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Quartz;
namespace ICSSoft.FromERP
{
///
/// 产品Bom(凯尔克)
///
public class IcsSBom : 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 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();
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 distinct A2.[ID] as 料品ID,
A2.[Code] as 料品编码,
A2.Name as 料品名称,
A4.Description as 料品描述,
A3.ID as 子阶料ID,
A3.[Code] as 子阶料编码,
A3.[Name] as 子阶料名称,
A5.Description as 子阶料描述,
A.FixedScrap as 固定损耗,
isnull( A6.[Code],'') as 项目号,
isnull( A.[ECNCode],'') as 工程变更号,
A.[UsageQty] as 子阶料计量数量,
A.[ItemVersionCode] as 子阶料物料版本,
A.[EffectiveDate] as 生效日期,
isnull( A.IsSpecialUseItem,'') as 管控类型,
A.[DisableDate] as 失效日期,
A.[Sequence] as 次序,
A.[IsEffective] as 有效,
A1.[BOMVersionCode] as 母料版本号,
A.[OperationNum] as 工序号
,I.id as Org
,A.ModifiedOn as MTime
,A.ID as ErpDetailID
INTO #TempBOM
from {1}.dbo.CBO_BOMComponent as A
left join {1}.dbo.[CBO_BOMMaster] as A1 on (A.[BOMMaster] = A1.[ID])
left join {1}.dbo.[CBO_ItemMaster] as A2 on (A1.[ItemMaster] = A2.[ID])
left join {1}.dbo.[CBO_ItemMaster_Trl] as A4 on (A4.SysMlFlag = 'zh-CN') and (A2.[ID] = A4.[ID])
left join {1}.dbo.[CBO_ItemMaster] as A3 on (A.[ItemMaster] = A3.[ID])
left join {1}.dbo.[CBO_ItemMaster_Trl] as A5 on (A5.SysMlFlag = 'zh-CN') and (A3.[ID] = A5.[ID])
left join {1}.dbo.[CBO_Project] as A6 on (A.[CompProject] = A6.[ID])
LEFT JOIN {1}.dbo.[Base_Organization] AS I ON ( A1.[Org] = I.[ID] )
LEFT JOIN {1}.dbo.[Base_Organization_Trl] AS J ON ( I.[ID] = J.[ID] AND J.SysMLFlag='zh-CN')
where 1=1 and A1.AlternateType=0 and IsEffective=1 and A.ModifiedOn>=@LastTime AND I.id={0} and A.ComponentType=0
IF NOT EXISTS(SELECT * FROM #TempBOM)
RETURN";
sql = string.Format(sql, ErpId, erpName);
sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempBOM");
sql += "\r\n";
sql += @"--删除数据
-- DELETE ICSSBOM WHERE moOpComponentId NOT IN (SELECT OpComponentId FROM {1}.dbo.bom_opcomponent)
SELECT a.ErpId INTO #tableICSSBOM
FROM ICSSBOM a With(NoLock)
LEFT JOIN {1}.dbo.[CBO_BOMComponent] b With(NoLock) ON a.ErpId=b.Id
LEFT JOIN {1}.dbo.CBO_BOMMaster c With(NoLock) ON b.BOMMaster=c.Id
WHERE b.id is null
and a.TenantId='{0}'
AND c.Org={2}
DELETE ICSSBOM
WHERE TenantId='{0}' and ErpId IN ( SELECT ErpId from #tableICSSBOM)
";
Dictionary values = new Dictionary();
values.Add("SBOMVER", "a.母料版本号");
values.Add("ITEMCODE", "a.料品编码");
values.Add("SBITEMCODE", "a.子阶料编码");
values.Add("SBItemName", "a.子阶料名称");
//values.Add("SBItemDesc", "a.子阶料描述");
values.Add("SBSITEMCODE", "a.子阶料编码");
values.Add("SBITEMQTY", "a.子阶料计量数量");
values.Add("SEQ", "1");
values.Add("SBItemECN", "a.工程变更号");
values.Add("SBItemStatus", "'Y'");
values.Add("SBItemEfftime", "a.生效日期");
values.Add("SBItemInvtime", "a.失效日期");
values.Add("SBItemProject", "a.项目号");
values.Add("SBItemSeq", "a.次序");
values.Add("Location", "a.工序号");
values.Add("LastModificationTime", "a.Mtime");//操作时间
values.Add("LastModifierUserId", "'" + ConstWorkPoint.Muser + "'");//操作人
values.Add("LastModifierUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称
values.Add("SBItemContype", "c.ItemContype");
//更新存在数据
sql += ICSHelper.UpdateSQL("b", values)
+ @" #TempBOM a
INNER JOIN ICSSBOM b ON a.ErpDetailID=b.ErpId
left join IcsInventory c on b.SBItemCode=c.ItemCode and b.TenantId=c.TenantId
left join IcsMainCategoryCode d on c.ItemMainCategoryCode=d.ItemMainCategoryCode and c.TenantId=d.TenantId
WHERE b.TenantId='{0}' and a.Org={2} ";
values.Add("ErpId", "convert(nvarchar(100), a.ErpDetailID)");
values.Add("CreationTime", "a.MTime");//操作人
values.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人
values.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称
values.Add("TenantId", "'" + TenantId + "'");//站点
sql += "\r\n";
//插入新增数据
sql += ICSHelper.InsertSQL("ICSSBOM", values)
+ @" #TempBOM a
LEFT JOIN ICSSBOM b ON a.ErpDetailID=b.ErpId and b.TenantId='{0}'
left join IcsInventory c on a.子阶料编码=c.ItemCode and c.TenantId='{0}'
left join IcsMainCategoryCode d on c.ItemMainCategoryCode=d.ItemMainCategoryCode and d.TenantId='{0}'
WHERE 1=1 and b.id is null
and a.Org={2} ";
sql += " DROP TABLE #TempBOM " +
"DROP TABLE #tableICSSBOM";
sql = string.Format(sql, TenantId, erpName, ErpId);
//return;
ICSHelper.ExecuteDate(conStr, sql);
#endregion
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
}
}