using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ICSSoft.FromERP.Model;
using Newtonsoft.Json;
using Quartz;
namespace ICSSoft.FromERP
{
///
/// 物料档案(威迈)
///
public class SyncItem_WeiMas : 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)
{
var dtNow = new DateTime(2000, 1, 1, 0, 0, 0);//默认开始时间
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);
string sql0 = " SELECT top 1 ModifyDate FROM ICSERPTime where ClassName='" + Class + "'";
var lastDate = ICSHelper.ExecuteScalar(conStr, sql0).ToDateOrNull();
if (!lastDate.HasValue)
{
lastDate = dtNow;
}
string sql = @" select '"+ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' as MTIME into #TempItem ";
sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempItem");
sql += "DROP TABLE #TempItem";
ICSHelper.ExecuteDate(conStr, sql);
var input = new ItemInputDto();
input.BeginTime = lastDate.Value;
input.EndTime = new DateTime(9999, 1, 1, 0, 0, 0);
var inputObj = JsonConvert.SerializeObject(input);
string url = ICSHelper.GetConfigString()["WeiMasErpUrl"];
var result = HttpHelper.HttpClientPost>>(url, inputObj).Result;
if (result.IsSuccess)
{
if (result.Data.Count == 0)
{
return;
}
var insertSql = @"select
*
into #tempFromErp4ItemCode
from
(";
Dictionary dics = new Dictionary();
for (int i = 0; i < result.Data.Count; i++)
{
var item = result.Data[i];
dics.Add("ItemCode", item.ItemCode);
dics.Add("ItemName", item.ItemName);
dics.Add("ItemStd", item.ItemStd);
dics.Add("ItemDesc", item.ItemDesc);
dics.Add("ItemCasNo", item.ItemCasNo);
dics.Add("ItemMolecularFormula", item.ItemMolecularFormula);
dics.Add("ItemAttribute", item.ItemAttribute);
dics.Add("ItemMainCategoryCode", item.ItemMainCategoryCode);
dics.Add("ITEMUnit", item.ITEMUnit);
dics.Add("ITEMMODEL", item.ITEMMODEL);
dics.Add("GR", item.GR);
dics.Add("SGFS", item.SGFS);
dics.Add("TGFS", item.TGFS);
dics.Add("WG", item.WG);
dics.Add("NI", item.NI);
dics.Add("MA", item.MA);
dics.Add("Mtime", item.Mtime.ToString("yyyy-MM-dd HH:mm:ss"));
dics.Add("Other", item.Other);
dics.Add("Free1", item.Free1);
dics.Add("Free2", item.Free2);
dics.Add("Free3", item.Free3);
dics.Add("Free4", item.Free4);
if (i == 0)
{
insertSql += " select ";
}
else
{
insertSql += " union all ";
insertSql += " select ";
}
foreach (var keyValue in dics)
{
insertSql += " '" + keyValue.Value + "' as " + keyValue.Key + " , ";
}
insertSql = insertSql.TrimEnd(',');
}
insertSql += " ) a";
insertSql += @"
insert into IcsInventory
(ItemCode,ItemName,ItemDesc,ItemStd,ItemType,ItemControlType,ItemContype
,ItemAttribute,ItemUnit,ItemMainCategoryCode,ItemModel,ItemCasNo,ItemMolecularFormula,GR,SGFS,TGFS,WG,NI,MA,Other,Default1,Default2,Default3,Default4
,CreationTime,CreatorUserId,CreatorUserName,TenantId)
select a.ItemCode,ItemName,ItemDesc,ItemStd,'itemtype_finishedproduct','item_control_lot','item_control_nocontrol'
,ItemAttribute,ITEMUnit,ItemMainCategoryCode,ITEMMODEL,ItemCasNo,ItemMolecularFormula,GR,SGFS,TGFS,WG,NI,MA,Other,Free1,Free2,Free3,Free4
,Mtime,'job','job','{0}'
from #tempFromErp4ItemCode a
left join IcsInventory b with(nolock) on a.itemcode=b.itemcode
where 1=1
and b.id is null";
insertSql = string.Format(insertSql, TenantId);
ICSHelper.ExecuteDate(conStr, insertSql);
}
else
{
throw new Exception(result.Message);
}
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
}
}