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 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());
}
}
// public void Execute_bak20201201()
// {
// try
// {
// string conStr = ICSHelper.GetConnectString();
// string Namespace = this.GetType().Namespace;
// string Class = this.GetType().Name;
// #region SQL
// string sql = @" SELECT g.cInvDefine1,--存货自定义项1
// a.Version,--Version 版本号
// a.VersionDesc,--VersionDesc 版本说明
// a.VersionEffDate,--VersionEffDate 版本生效日
// a.VersionEndDate,--VersionEndDate 版本失效日
// c.InvCode as cInvCode ,--存货编码
// f.InvCode as SubcInvCode ,--存货编码
// e.WIPType, --WIPType WIP属性(1入库/2工序/3领料/4虚拟)
// d.SortSeq, --SortSeq 序号
// d.BaseQtyN, --AuxBaseQtyN 辅助基本用量
// d.BaseQtyD, --BaseQtyD 基本用量-分母
// d.ByproductFlag, --ByproductFlag 是否联副产品
// d.ProductType, --ProductType 产出类型(1:空/2:联产品/3:副产品)
// d.CompScrap, --CompScrap 子件损耗率
// a.CreateDate,a.CreateUser,a.ModifyDate,a.ModifyUser,a.cCurrentAuditor,--当前审批人
// a.RelsTime as cAuditDate, --审核时间
// i.ReplaceFlag,--是否替换料
// i.OpComponentId,-- 子件Id
// i.Sequence ReplaceSequence,--替代序号
// K.InvCode as ReplaceInvCode ,--替代料编码
// i.Factor ReplaceFactor,--替代比例
// i.EffBegDate ReplaceEffBegDate,--生效日期
// i.EffEndDate ReplaceEffEndDate,--失效日期
// ISNULL(a.ModifyDate, a.CreateDate) AS MTIME
// into #TempBOM
// FROM {1}.dbo.bom_bom a -- BOM资料
// INNER JOIN {1}.dbo.bom_parent b on a.BomId = b.BomId --BOM母件资料
// INNER JOIN {1}.dbo.bas_part c on b.ParentId = c.PartId --物料表
// INNER JOIN {1}.dbo.bom_opcomponent d on a.BomId=d.BomId --BOM子件资料
// INNER JOIN {1}.dbo.bas_part f on d.ComponentId = f.PartId
// INNER JOIN {1}.dbo.bom_opcomponentopt e ON d.OptionsId=e.OptionsId -- 子件选项资料
// left JOIN {1}.dbo.bom_opcomponentsub i ON d.OpComponentId =i.OpComponentId --子件替代料
// left join {1}.dbo.inventory g on c.InvCode=g.cInvCode --存货档案
// LEFT JOIN {1}.dbo.bas_part K ON K.PartId=i.PartId
//
// WHERE ISNULL(a.ModifyDate, a.CreateDate)>=@LastTime
// IF NOT EXISTS(SELECT * FROM #TempBOM)
// RETURN";
// sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempBOM");
// sql += "\r\n";
// sql += @"--删除数据
// DELETE ICSBOM WHERE cInvCode NOT IN (SELECT InvCode FROM {1}.dbo.bas_part) ";
// Dictionary values = new Dictionary();
// values.Add("Version", "a.Version");
// values.Add("VersionDesc", "a.VersionDesc");
// values.Add("VersionEffDate", "a.VersionEffDate");
// values.Add("VersionEndDate", "a.VersionEndDate");
// values.Add("cInvCode", "a.cInvCode");
// values.Add("SubcInvCode", "a.SubcInvCode");
// values.Add("WIPType", "a.WIPType");
// values.Add("SortSeq", "a.SortSeq");
// values.Add("BaseQtyN", "a.BaseQtyN");
// values.Add("BaseQtyD", "a.BaseQtyD");
// values.Add("ByproductFlag", "a.ByproductFlag");
// values.Add("ProductType", "a.ProductType");
// values.Add("CompScrap", "a.CompScrap");
// values.Add("CreateDate", "a.CreateDate");
// values.Add("CreateUser", "a.CreateUser");
// values.Add("ModifyDate", "a.ModifyDate");
// values.Add("ModifyUser", "a.ModifyUser");
// values.Add("cCurrentAuditor", "a.cCurrentAuditor");
// values.Add("cAuditDate", "a.cAuditDate");
// values.Add("ReplaceFlag", "a.ReplaceFlag");
// values.Add("OpComponentId", "a.OpComponentId");
// values.Add("ReplaceSequence", "a.ReplaceSequence");
// values.Add("ReplaceInvCode", "a.ReplaceInvCode");
// values.Add("ReplaceFactor", "a.ReplaceFactor");
// values.Add("ReplaceEffBegDate", "a.ReplaceEffBegDate");
// values.Add("ReplaceEffEndDate", "a.ReplaceEffEndDate");
// values.Add("MTIME", "a.MTIME");
// //更新存在数据
// sql += ICSHelper.UpdateSQL("b", values)
// + @" #TempBOM a
// INNER JOIN ICSBOM b ON a.cInvCode=b.cInvCode ";
// values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
// values.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
// values.Add("TenantId", "'" + TenantId + "'");
// sql += "\r\n";
// //插入新增数据
// sql += ICSHelper.InsertSQL("ICSBOM", values)
// + @" #TempBOM a
// LEFT JOIN ICSBOM b ON a.cInvCode=b.cInvCode
// WHERE b.cInvCode IS NULL
// ";
// sql += "DROP TABLE #TempBOM";
// sql = string.Format(sql, ICSHelper.GetConfigString()["ERPDB"]);
// ICSHelper.ExecuteDate(conStr, sql);
// #endregion
// }
// catch (Exception ex)
// {
// log.Error(ex.ToString());
// }
// }
}
}