using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using System.Data;
namespace ICSSoft.FromERP
{
///
/// 物料
///
public class ICSInventory : 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 WorkPoint = dr["WorkPointCode"].ToString();
string Class = this.GetType().Name + WorkPoint;
erpName = string.Format(erpName, WorkPoint);
#region SQL
string sql = @"select A.ID AS ID,
A.Code AS INVCODE,
dbo.GetReplaceStr(A.Name) AS INVNAME,
dbo.GetReplaceStr(A.SPECS) AS INVSTD,
D.Name AS InvUnit,
G.Code as ClassCode,
H.Name as ClassName,
1 as InvRate,
1 as EffectiveDays,
ISNULL(CONVERT(NVARCHAR(50),A.ModifiedOn,21),'') as MTIME,
K.Code as WorkPoint
INTO #TempINV
from {1}.dbo.[CBO_ItemMaster] AS A
LEFT JOIN {1}.dbo.[CBO_ItemMaster_Trl] AS B ON ( A.[ID] = B.[ID] AND B.SysMLFlag='zh-CN')
LEFT JOIN {1}.dbo.[Base_UOM] AS C ON ( A.[CostUOM] = C.[ID] )
LEFT JOIN {1}.dbo.[Base_UOM_Trl] AS D ON ( C.[ID] = D.[ID] AND D.SysMLFlag='zh-CN')
LEFT JOIN {1}.dbo.[Base_UOM] AS E ON ( A.[InventoryUOM] = E.[ID] )
LEFT JOIN {1}.dbo.[Base_UOM_Trl] AS F ON ( E.[ID] = F.[ID] AND F.SysMLFlag='zh-CN')
LEFT JOIN {1}.dbo.[CBO_Category] AS G ON ( A.[MainItemCategory] = G.[ID] )
LEFT JOIN {1}.dbo.[CBO_Category_Trl] AS H ON ( G.[ID] = H.[ID] AND H.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 {1}.dbo.Base_Organization K ON A.Org=K.ID
LEFT JOIN {1}.dbo.CBO_InventoryInfo L on A.id=L.ItemMaster
LEFT JOIN {1}.dbo.CBO_Wh M on L.warehouse=M.id and A.org=M.org
LEFT JOIN {1}.dbo.Base_DefineValue N ON N.Code=A.DescFlexField_PrivateDescSeg5 AND N.ValueSetDef='1002204070000769'
LEFT JOIN {1}.dbo.Base_DefineValue_Trl O ON O.ID=N.ID
LEFT JOIN {1}.dbo.CBO_Category_Trl P ON P.ID=A.MainItemCategory
LEFT JOIN {1}.dbo.CBO_MfgInfo Q ON Q.ItemMaster=A.ID
where A.ModifiedOn>=@LastTime
ORDER BY A.Code
IF NOT EXISTS(SELECT * FROM #TempINV)
RETURN";
sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempINV");
sql += @"--删除数据
-- DELETE ICSINVENTORY WHERE InvCode NOT IN (SELECT cInvCode FROM {1}.dbo.[CBO_ItemMaster])
SELECT a.INVCODE INTO #tableICSINVENTORY
FROM ICSINVENTORY a With(NoLock)
LEFT JOIN {1}.dbo.[CBO_ItemMaster] b With(NoLock) ON a.ID=b.ID
WHERE a.WorkPoint='{0}' and b.ID IS NULL
DELETE ICSINVENTORY
WHERE WorkPoint='{0}' and INVCODE IN ( SELECT INVCODE from #tableICSINVENTORY) ";
Dictionary values = new Dictionary();
values.Add("INVCODE", "a.INVCODE");//物料编号
values.Add("InvName", "a.INVNAME");//物料名称
values.Add("INVDESC", "a.INVDESC");//物料描述
values.Add("InvStd", "a.INVSTD");//规格型号
values.Add("InvUnit", "a.InvUnit");//主计量单位
values.Add("ClassCode", "a.ClassCode");//大类编码
values.Add("ClassName", "a.ClassName");//大类编码
values.Add("InvRate", "a.InvRate");
values.Add("InvVersion", "''");
values.Add("InvBrand", "''");
values.Add("InvIQC", "0");//大类编码
values.Add("InvFQC", "0");//大类编码
values.Add("EffectiveEnable", "0");//大类名称
values.Add("EffectiveDays", "a.EffectiveDays");//大类名称
values.Add("BatchEnable", "0");//大类名称
values.Add("LotEnable", "0");//大类名称
values.Add("PrintEnable", "0");//大类名称
values.Add("MTIME", "a.MTIME");//操作时间
//更新存在数据
sql += ICSHelper.UpdateSQL("b", values)
+ @" #TempINV a
INNER JOIN ICSINVENTORY b ON a.INVCODE=b.INVCODE and a.WorkPoint=b.WorkPoint
WHERE a.WorkPoint='" + WorkPoint + "' ";
values.Add("ID", "a.ID");//唯一标识
values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");//操作人
values.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");//操作人名称
values.Add("WorkPoint", "'" + WorkPoint + "'");//站点
//插入新增数据
sql += ICSHelper.InsertSQL("ICSINVENTORY", values)
+ @" #TempINV a
LEFT JOIN ICSINVENTORY b ON a.INVCODE=b.INVCODE and a.WorkPoint=b.WorkPoint
WHERE b.INVCODE IS NULL and a.WorkPoint='" + WorkPoint + "' ";
sql += " DROP TABLE #TempINV " +
" DROP TABLE #tableICSINVENTORY ";
sql = string.Format(sql, WorkPoint, erpName);
ICSHelper.ExecuteDate(conStr, sql);
#endregion
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
}
}