IcsFromERPJob
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
9.4 KiB

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ICSSoft.FromERP.Extension;
using ICSSoft.FromERP.Model;
using Newtonsoft.Json;
using Quartz;
namespace ICSSoft.FromERP
{
/// <summary>
/// 销售订单(威迈)
/// </summary>
public class SyncSO_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 dtNowBegin = new DateTime(2000, 1, 1, 0, 0, 0);//默认开始时间
var dtNow = DateTime.Now;
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 = dtNowBegin;
}
string sql = @" select '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' as MTIME into #TempSaleOrder ";
sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempSaleOrder");
sql += "DROP TABLE #TempSaleOrder";
ICSHelper.ExecuteDate(conStr, sql);
// return;
#region 获取Token
var tokenUrl = ICSHelper.GetConfigString()["WeiMasTokenUrl"];
var inputTokenDto = new TokenInputDto();
inputTokenDto.route = "sj_token";
inputTokenDto.grant_type = "password";
inputTokenDto.client_id = ICSHelper.GetConfigString()["app_id"];
inputTokenDto.client_secret = ICSHelper.GetConfigString()["app_secret"];
inputTokenDto.timestamp = dtNow.ToMillisecondsTimeStamp();
inputTokenDto.username = "api_token";
inputTokenDto.sign = ICSHelper.ApiSign(inputTokenDto.client_id, inputTokenDto.client_secret, inputTokenDto.timestamp);
inputTokenDto.clientExplorer = "";
var tokenDto = HttpHelper.PostForm<TokenDto, TokenInputDto>(tokenUrl, inputTokenDto).Result;
if (string.IsNullOrEmpty(tokenDto.access_token))
{
throw new Exception(" no access_token ");
}
// return;
#endregion
#region 获取子公司列表
//var companyUrl = ICSHelper.GetConfigString()["WeiMasOrgUrl"] + @"/EnterpriseService/FromWeMec/GetSubCompanyList";
//var companyDto = HttpHelper.HttpClientGet<WeiMasResult<List<SubCompanyDto>>>(companyUrl, "", tokenDto.access_token).Result;
//if (companyDto.Data == null || companyDto.Data.Count == 0)
//{
// throw new Exception(" 获取子公司列表失败 ");
//}
// return;
#endregion
var input = new QuerySOInputDto();
input.CustomerId = ICSHelper.GetConfigString()["WeiMasOrgCustomerId"];
input.BeginTime = dtNow.AddDays(-90);
input.EndTime = dtNow;
var inputObj = JsonConvert.SerializeObject(input);
string url = ICSHelper.GetConfigString()["Doc_SOUrl"];
var result = HttpHelper.HttpClientPost<WeiMasResult<List<SODto>>>(url, inputObj, token: tokenDto.access_token).Result;
if (result.Status==200)
{
if (result.Data.Count == 0)
{
return;
}
var insertSql = @"
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#tempFromErp4SaleOrder') and type='U')
DROP TABLE #tempFromErp4SaleOrder
select
*
into #tempFromErp4SaleOrder
from
(";
Dictionary<string, string> dics = new Dictionary<string, string>();
for (int i = 0; i < result.Data.Count; i++)
{
var item = result.Data[i];
dics.Clear();
dics.Add("SOCODE", item.SOCODE);
dics.Add("SOTYPE", item.SOTYPE);
dics.Add("SOORDERDATE", item.SOORDERDATE.ToString("yyyy-MM-dd HH:mm:ss"));
dics.Add("RequestedDeliveryDate", item.RequestedDeliveryDate.ToString("yyyy-MM-dd HH:mm:ss"));
dics.Add("CUSCODE", item.CUSCODE);
dics.Add("CUSNAME", item.CUSNAME);
dics.Add("SOLineNo", item.SOLineNo.ToString());
dics.Add("ITEMCODE", item.ITEMCODE);
dics.Add("CASNo", item.CASNo);
dics.Add("Qty", item.Qty.ToString());
dics.Add("tax", item.Tax.ToString());
dics.Add("SQCode", item.SQCode==null?"": item.SQCode.Trim());
dics.Add("SQLineNo", item.SQLineNo == null ? "" : item.SQLineNo.Trim());
dics.Add("ProduceArea", item.ITEMCODE.Substring(item.ITEMCODE.Length-2,2));
dics.Add("Mtime", item.Mtime.ToString("yyyy-MM-dd HH:mm:ss"));
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 IcsSaleOrder
(SoCode,SoType,[SoOrderDate],[RequestedDeliveryDate],[CusCode],[CusName],[SoLineNo],[ItemCode],[CASNo],[Qty]
,[Tax],[SqCode],[SqLineNo],ProduceArea
,Default1,Default2,Default3,Default4
,CreationTime,CreatorUserId,CreatorUserName,TenantId)
select a.SoCode,a.SoType,a.SOORDERDATE,a.RequestedDeliveryDate,a.CUSCODE,a.CUSNAME,a.SOLineNo ,a.ITEMCODE,a.CASNo,a.Qty
,a.tax,a.SQCode,a.SQLineNo
,a.ProduceArea
, a.Free1,a.Free2,a.Free3,a.Free4
,Mtime,'job','job','{0}'
from #tempFromErp4SaleOrder a
left join IcsSaleOrder b with(nolock) on a.SoCode=b.SoCode and a.SoLineNo=b.SoLineNo
where 1=1
and b.id is null
update IcsSaleOrder
set
SoType=a.SoType,SoOrderDate=a.SoOrderDate,RequestedDeliveryDate=a.RequestedDeliveryDate,CusCode=a.CusCode,CusName=a.CusName
,SoLineNo=a.SoLineNo
,ItemCode=a.ItemCode,CASNo=a.CASNo,Qty=a.Qty,Tax=a.Tax,SqCode=a.SqCode,SqLineNo=a.SqLineNo
,Default1=a.Free1,Default2=a.Free2,Default3=a.Free3,Default4=a.Free4
,LastModificationTime=a.Mtime,LastModifierUserId='job',LastModifierUserName='job'
from #tempFromErp4SaleOrder a
left join IcsSaleOrder b with(nolock) on a.SoCode=b.SoCode and a.SoLineNo=b.SoLineNo
where 1=1
and b.id is not null
";
insertSql = string.Format(insertSql, TenantId);
// insertSql += "DROP TABLE #tempFromErp4Vendor";
ICSHelper.ExecuteDate(conStr, insertSql);
}
else
{
throw new Exception(result.Message);
}
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
}
}