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.

227 lines
13 KiB

using Quartz;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ICSSoft.FromERP
{
/// <summary>
/// Stock information 库存信息 日
/// </summary>
public class YERP_010_BOH : 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 async void Execute()
{
try
{
Configuration config = GetConfig();
var Dates = DateTime.Now;
string conStr = ICSHelper.GetConnectString();
//<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:yerp="http://skch.com/YERP_010_IF_STCK_HST">
// <soapenv:Header/>
// <soapenv:Body>
// <yerp:YERP_010_OA1_MT>
// <!--Zero or more repetitions:-->
// <IF_STCK_HST>
// <IF_STD_DATE>2024-07-01</IF_STD_DATE>
// <!--Optional:-->
// <STCK_STD_DATE>2024-07-01</STCK_STD_DATE>
// <!--Optional:-->
// <WRHS_CD>?</WRHS_CD>
// <ITEM_CD>?</ITEM_CD>
// <!--Optional:-->
// <PRDT_BTCH_NO>?</PRDT_BTCH_NO>
// <STCK_TYPE_CD>?</STCK_TYPE_CD>
// <!--Optional:-->
// <STCK_QTY>?</STCK_QTY>
// <!--Optional:-->
// <STCK_VALUE_CNW>?</STCK_VALUE_CNW>
// <!--Optional:-->
// <STCK_UOM>?</STCK_UOM>
// <!--Optional:-->
// <PACK_MIN_QTY>?</PACK_MIN_QTY>
// <GEN_ROW_CNT>?</GEN_ROW_CNT>
// </IF_STCK_HST>
// </yerp:YERP_010_OA1_MT>
// </soapenv:Body>
//</soapenv:Envelope>
log.Info("上月日期:" + Dates.AddMonths(-1).AddDays(-Dates.Day + 1).ToString("yyyy-MM-dd")); // 2024-07-01
log.Info("本月日期:" + Dates.AddDays(-Dates.Day + 1).ToString("yyyy-MM-dd")); // 2024-08-01
log.Info("本月日期+时分秒:" + Dates.AddDays(-Dates.Day + 1).ToString("yyyy-MM-dd 00:00:00")); // 2024-08-01 00:00:00 日期结果已测试
// 1和2为 当月 汇总数据过滤条件
var beginPeriod1 = "2024-02-01";// Dates.AddDays(-Dates.Day + 1).ToString("yyyy-MM-dd");//"2024-02-01";
var endPeriod2 = "2024-03-01";// Dates.ToString("yyyy-MM-dd");//"2024-03-01";
// 3和4为 上月 汇总数据过滤条件
var beginPeriod3 = "2024-01-01";// Dates.AddMonths(-1).AddDays(-Dates.Day + 1).ToString("yyyy-MM-dd");//"2024-01-01";
var endPeriod4 = "2024-02-01";// Dates.AddDays(-Dates.Day + 1).ToString("yyyy-MM-dd");//"2024-02-01";
string sqls = @"
-- 当月的汇总
select a.cInvCode,cWhCode,
case when sum(ISNULL(iAOutQuantity,0)) <> 0 then SUM(ISNULL(iAOutPrice,0)) / sum(ISNULL(iAOutQuantity,0)) else 0 end as OutAInPrice
,case when SUM(ISNULL(iAInQuantity,0)) <> 0 then (SUM(ISNULL(iAInPrice,0))) / sum(ISNULL(iAInQuantity,0)) else 0 end as InAInPrice
into #STCK_VALUE_CNW_thisMonth
from IA_Subsidiary a
where a.dKeepDate >= N'{0}' and a.dKeepDate <= N'{1}' and cVouType<> N'33' and iMonth<>0
group by a.cInvCode,cWhCode
-- 上个月的汇总
select a.cInvCode,cWhCode,
case when sum(ISNULL(iAOutQuantity,0)) <> 0 then SUM(ISNULL(iAOutPrice,0)) / sum(ISNULL(iAOutQuantity,0)) else 0 end as OutAInPrice
,case when SUM(ISNULL(iAInQuantity,0)) <> 0 then (SUM(ISNULL(iAInPrice,0))) / sum(ISNULL(iAInQuantity,0)) else 0 end as InAInPrice
into #STCK_VALUE_CNW_ListMonth
from IA_Subsidiary a
where a.dKeepDate >= N'{2}' and a.dKeepDate <= N'{3}' and cVouType<> N'33' and iMonth<>0
group by a.cInvCode,cWhCode
-- 有当月的取当月,其次取上个月的 RowIndex排序后只取值为1的
select cInvCode,cWhCode,STCK_VALUE_CNW,Months,row_number() over (partition by cInvCode,cWhCode order by Months desc) as RowIndex
into #STCK_VALUE_CNW
from (
select cInvCode,cWhCode,
case when InAInPrice <> 0 and OutAInPrice <> 0
then
case when InAInPrice <> 0 then InAInPrice else OutAInPrice end
else
0
end as STCK_VALUE_CNW
,1 as Months from #STCK_VALUE_CNW_thisMonth -- 本月为1
union all
select cInvCode,cWhCode,
case when InAInPrice <> 0 and OutAInPrice <> 0
then
case when InAInPrice <> 0 then InAInPrice else OutAInPrice end
else
0
end as STCK_VALUE_CNW
,0 as Months from #STCK_VALUE_CNW_ListMonth -- 上月为0
) a
select convert(nvarchar(10),getdate(),112) IF_STD_DATE,
convert(nvarchar(10),getdate(),112) STCK_STD_DATE,
a.cWhCode WRHS_CD,
a.cInvCode ITEM_CD,
cBatch PRDT_BTCH_NO,
'BOH' STCK_TYPE_CD,
a.iQuantity STCK_QTY,
a.iQuantity * ABS(ISNULL(d.STCK_VALUE_CNW,0)) STCK_VALUE_CNY,
(case when cInvStd is null then null else (CASE WHEN ISNUMERIC(left(cinvstd,3)) = 1 THEN REPLACE(SUBSTRING(cinvstd, 1, CHARINDEX('/', cinvstd) - 1),left(cinvstd,3),'') ELSE (CASE WHEN ISNUMERIC(left(cinvstd,2)) = 1
THEN REPLACE(SUBSTRING(cinvstd, 1, CHARINDEX('/', cinvstd) - 1),left(cinvstd,2),'') ELSE (CASE WHEN ISNUMERIC(left(cinvstd,1)) = 1
THEN REPLACE(SUBSTRING(cinvstd, 1, CHARINDEX('/', cinvstd) - 1),left(cinvstd,1),'') ELSE '' END) END) END) end) STCK_UOM,
cInvStd PACK_MIN_QTY,
COUNT(*) OVER (PARTITION BY getdate()) GEN_ROW_CNT
from CurrentStock a
left join dbo.Inventory c on a.cInvCode=c.cInvCode
left join #STCK_VALUE_CNW d on a.cWhCode = d.cWhCode and a.cInvCode = d.cInvCode and RowIndex = 1
where 1 = 1 and iQuantity > 0
Drop Table #STCK_VALUE_CNW_ListMonth
Drop Table #STCK_VALUE_CNW_thisMonth
Drop Table #STCK_VALUE_CNW";//and dInvCreateDatetime > '{0}' and dInvCreateDatetime < '{1}'
sqls = string.Format(sqls, beginPeriod1, endPeriod2, beginPeriod3, endPeriod4);
log.Info("YERP_010 sql:" + sqls);
DataTable vbsdt = ICSHelper.ExecuteTable(conStr, sqls);
log.Info("YERP_010 sql结果数量" + vbsdt.Rows.Count);
if (vbsdt.Rows.Count > 0)
{
StringBuilder soapRequestData = new StringBuilder();
soapRequestData.Append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:yerp=\"http://skch.com/YERP_010_IF_STCK_HST\">");
soapRequestData.Append("<soapenv:Header/>");
soapRequestData.Append("<soapenv:Body>");
soapRequestData.Append("<yerp:YERP_010_OA1_MT>");
//循环数据
foreach (DataRow itemRow in vbsdt.Rows)
{
soapRequestData.Append("<!--Zero or more repetitions: -->");
soapRequestData.Append("<IF_STCK_HST>");
soapRequestData.Append(itemRow["IF_STD_DATE"] != null ? "<IF_STD_DATE>" + itemRow["IF_STD_DATE"].ToString() + "</IF_STD_DATE>" : "<IF_STD_DATE>" + "</IF_STD_DATE>");
soapRequestData.Append(itemRow["STCK_STD_DATE"] != null ? "<STCK_STD_DATE>" + itemRow["STCK_STD_DATE"].ToString() + "</STCK_STD_DATE>" : "<STCK_STD_DATE>" + "</STCK_STD_DATE>");
soapRequestData.Append(itemRow["WRHS_CD"] != null ? "<WRHS_CD>" + itemRow["WRHS_CD"].ToString() + "</WRHS_CD>" : "<WRHS_CD>" + "</WRHS_CD>");
soapRequestData.Append(itemRow["ITEM_CD"] != null ? "<ITEM_CD>" + itemRow["ITEM_CD"].ToString() + "</ITEM_CD>" : "<ITEM_CD>" + "</ITEM_CD>");
soapRequestData.Append(itemRow["PRDT_BTCH_NO"] != null ? "<PRDT_BTCH_NO>" + itemRow["PRDT_BTCH_NO"].ToString() + "</PRDT_BTCH_NO>" : "<PRDT_BTCH_NO>" + "</PRDT_BTCH_NO>");
soapRequestData.Append(itemRow["STCK_TYPE_CD"] != null ? "<STCK_TYPE_CD>" + itemRow["STCK_TYPE_CD"].ToString() + "</STCK_TYPE_CD>" : "<STCK_TYPE_CD>" + "</STCK_TYPE_CD>");
soapRequestData.Append(itemRow["STCK_QTY"] != null ? "<STCK_QTY>" + itemRow["STCK_QTY"].ToString() + "</STCK_QTY>" : "<STCK_QTY>" + "</STCK_QTY>");
soapRequestData.Append(itemRow["STCK_VALUE_CNY"] != null ? "<STCK_VALUE_CNY>" + itemRow["STCK_VALUE_CNY"].ToString() + "</STCK_VALUE_CNY>" : "<STCK_VALUE_CNY>" + "</STCK_VALUE_CNY>");
soapRequestData.Append(itemRow["STCK_UOM"] != null ? "<STCK_UOM>" + itemRow["STCK_UOM"].ToString() + "</STCK_UOM>" : "<STCK_UOM>" + "</STCK_UOM>");
soapRequestData.Append("<!--Optional:-->");
soapRequestData.Append(itemRow["PACK_MIN_QTY"] != null ? "<PACK_MIN_QTY>" + Regex.Replace(itemRow["PACK_MIN_QTY"].ToString(), @"[^0-9]+", "") + "</PACK_MIN_QTY>" : "<PACK_MIN_QTY>" + "</PACK_MIN_QTY>");
soapRequestData.Append("<!--Optional:-->");
soapRequestData.Append(itemRow["GEN_ROW_CNT"] != null ? "<GEN_ROW_CNT>" + itemRow["GEN_ROW_CNT"].ToString() + "</GEN_ROW_CNT>" : "<GEN_ROW_CNT>" + "</GEN_ROW_CNT>");
soapRequestData.Append("</IF_STCK_HST>");
}
soapRequestData.Append("</yerp:YERP_010_OA1_MT>");
soapRequestData.Append("</soapenv:Body>");
soapRequestData.Append("</soapenv:Envelope>");
string postData = soapRequestData.ToString();
log.Info("YERP_010 xmlRequest:" + postData);
string statusCode;
string resultContent;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
RestOpHelper rop = new RestOpHelper();
rop.Clientp12path = @"C:\Users\Administrator\Desktop\yonyou-cert\wildcard.pfx";
rop.Clientp12PassWord = "Yonyou2024!";
rop.Url = @"https://yerp-proxy-sap-dev.skchemicals.com/XISOAPAdapter/MessageServlet?senderParty=&senderService=YERP_D&receiverParty=&receiverService=&interface=YERP_010_OA1_SI&interfaceNamespace=http://skch.com/YERP_010_IF_STCK_HST";
rop.RequestSAP(rop.Url, 10000, postData, "IF_YERP", "Xtxc355860", out statusCode, out resultContent);
log.Info("YERP_010 statusCode:" + statusCode + "\r\n" + "resultContent:" + resultContent);
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
public static Configuration GetConfig()
{
Assembly assembly = Assembly.GetCallingAssembly();
string path = string.Format("{0}.config", assembly.Location);
if (!File.Exists(path))
{
throw new FileNotFoundException(path + "路径下的文件未找到!");
}
try
{
ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = path;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);
return config;
}
catch (Exception)
{
throw;
}
}
}
}