using ICSSoft.FromERP.Model;
using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ICSSoft.FromERP
{
///
/// 数采上传(金杨)
///
public class SyncCas_Jinyang : 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.GetOldERPDB(conStr);
foreach (DataRow dr in dt.Rows)
{
var dtNowBegin = new DateTime(2000, 1, 1, 0, 0, 0);//默认开始时间
var dtNow = DateTime.Now;
string erpName = ICSHelper.GetConfigString()["ERPDB"];
string WorkPoint = dr["WorkPointCode"].ToString();
string Class = this.GetType().Name + WorkPoint;
erpName = string.Format(erpName, WorkPoint);
//string sql0 = " SELECT top 1 ModifyDate FROM ICSERPTime where ClassName='" + Class + "'";
//var lastDate = ICSHelper.ExecuteScalar(conStr, sql0).ToDateOrNull();
//if (!lastDate.HasValue)
//{
// lastDate = dtNowBegin;
//}
// string sql = @" select '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' as MTIME into #TempCas ";
string sql = @"if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#tempSyncCas_Jinyang') and type='U')
DROP TABLE #tempSyncCas_Jinyang
select * ,a.CreateTime as MTime into #tempSyncCas_Jinyang from [ICSDomeAmb] a with( nolock)
where a.CreateTime>@LastTime
and free1 <>'15' and Free2 not like '%e%' and free1<>'0'
and LotNO <>''
select
a.*
from
(
select a.*
,row_number() over(partition by a.EqCode order by a.CreateTime desc) RowSeq
from #tempSyncCas_Jinyang a
) a
where a.RowSeq=1
order by a.EqCode ";
// sql = string.Format(sql, ErpId, erpName);
sql = ICSHelper.Time4Old(Namespace, Class, WorkPoint, sql, "#tempSyncCas_Jinyang");
// sql += "DROP TABLE #tempSyncCas_Jinyang";
// sql += @"";
var resultDT = ICSHelper.ExecuteTable(conStr, sql);
List list = new List();
if (resultDT == null)
{
return;
}
var insertSql = "";
foreach (DataRow item in resultDT.Rows)
{
var input = new CapInputDto();
input.WorkshopID = "6";
input.EquipmentID = item["EqCode"].ToStringExt();
input.ProductNo = item["InvStd "].ToStringExt();
input.LotNo = item["LotNO"].ToStringExt();
input.CollectionTime = item["CreateTime"].ToString();
input.ParamValue_Resistance = item["Free2"].ToDecimal();
input.ParamValue_Height = item["Free1"].ToDecimal();
input.Msg = "";
list.Add(input);
insertSql += @"
INSERT INTO [dbo].[ICSDomeAmb_cus]
([CreateTime]
,IPAddress
,UserCode
,[LotNO]
,[InvStd ]
,[EqCode]
,[Free1]
,[Free2]
)
values ('" + item["CreateTime"].ToString() + "','"+ item["IPAddress"].ToString() + "','"+ item["UserCode"].ToStringExt() + "','" + input.LotNo + "','" + input.ProductNo + "','" + input.EquipmentID + "','" + item["Free1"].ToStringExt() + "','"+ item["Free2"].ToStringExt() + "')";
}
if (list.Count == 0)
{
log.Info("没有数据行");
return;
}
var inputObj = JsonConvert.SerializeObject(list);
//string url = ICSHelper.GetConfigString()["JinyangCapUrl"]+ @"?deipaaskeyauth=a5P1RTL4380zd9jpb57qXx63rdynUHN2";
string url = ICSHelper.GetConfigString()["JinyangCapUrl"];
var result = HttpHelper.HttpClientPost(url, inputObj, new Dictionary() { { "deipaaskeyauth", "a5P1RTL4380zd9jpb57qXx63rdynUHN2" } }).Result;
// var result = HttpHelper.HttpClientPost(url, inputObj).Result;
if (result.Result == "OK")
{
ICSHelper.ExecuteDate(conStr, insertSql);
}
else
{
throw new Exception(result.Result);
}
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
}
}