using Quartz;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
namespace ICSSoft.FromERP
{
///
/// 物料主分类(来源U8)
///
public class IcsMainCategoryCodeFromU8 : 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.cInvCCode as ItemMainCategoryCode
,b.cInvCCode as ItemMainCategoryName
,'' as ItemMainCategoryDesc
,getdate() MTime
,p.parentCode
INTO #TempItemCategory
from {1}.dbo.[InventoryClass] a with(nolock)
left join
(
select cc.cInvCCode,cc_p.cInvCCode parentCode
from {1}.dbo.InventoryClass cc with(nolock)
left join {1}.dbo.InventoryClass cc_p with(nolock) on cc.cInvCCode like cc_p.cInvCCode+'%'
and cc.iInvCGrade=cc_p.iInvCGrade+1
) p ON a.cInvCCode=p.cInvCCode
where 1=1
and A.ModifiedOn>=@LastTime
ORDER BY A.cInvCCode
IF NOT EXISTS(SELECT * FROM #TempItemCategory)
RETURN";
sql = string.Format(sql, ErpId, erpName);
sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempItemCategory");
sql += @"--删除数据
SELECT a.ItemMainCategoryCode INTO #tableItemCategory
FROM IcsMainCategoryCode a With(NoLock)
LEFT JOIN {1}.dbo.[InventoryClass] b With(NoLock) ON a.ItemMainCategoryCode=b.cInvCCode
WHERE b.cInvCCode is null
and a.TenantId='{0}'
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 + "' ";
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
";
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());
}
}
}
}