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 { /// /// Stock information 库存信息 月 /// public class YERP_010_EOH : 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(); // // // // // // // 2024-07-01 // // 2024-07-01 // // ? // ? // // ? // ? // // ? // // ? // // ? // // ? // ? // // // // //数量和金额相反的例子 //select //SUM(ISNULL(iAInPrice,0)) as SumAInPrice, //SUM(ISNULL(iAOutPrice,0)) as SumAOutPrice, //sum(ISNULL(iAInQuantity,0)) as SumAInQuantity, //sum(ISNULL(iAOutQuantity,0)) as SumAOutQuantity //from IA_Subsidiary a where a.cWhCode = 'CC001' and cInvCode = '103012' //group by cWhCode,cInvCode // 过滤时间为截止本月月初 var lastMonth = Dates.AddDays(-Dates.Day + 1).ToString("yyyy-MM-dd");//"2024-01-01"; // 月库存统计:每个月14号,查询上月截止到月末的库存数据累计数量和金额 因为期初数量本来就是统计历史收发数量,所以期初数量+收发数量 = 收发数量总和,金额同理 string sqls = @" select *,COUNT(*) OVER (PARTITION BY getdate()) GEN_ROW_CNT from (select cWhCode,a.cInvCode,convert(nvarchar(10),getdate(),112) IF_STD_DATE, convert(nvarchar(10),getdate(),112) STCK_STD_DATE, a.cWhCode WRHS_CD, a.cInvCode ITEM_CD,c.cInvName, cBatchia PRDT_BTCH_NO, 'EOH' STCK_TYPE_CD, CAST(SUM(ISNULL(a.iAInQuantity,0)) - SUM(ISNULL(a.iAOutQuantity,0)) as decimal(10,3)) STCK_QTY, CAST(SUM(ISNULL(iAInPrice,0)) - SUM(ISNULL(iAOutPrice,0)) as decimal(10,3)) STCK_VALUE_CNY, (case when cInvStd is null then null else (CASE WHEN ISNUMERIC(left(cinvstd,3)) = 1 THEN LOWER(REPLACE(SUBSTRING(cinvstd, 1, CHARINDEX('/', cinvstd) - 1),left(cinvstd,3),'')) ELSE (CASE WHEN ISNUMERIC(left(cinvstd,2)) = 1 THEN LOWER(REPLACE(SUBSTRING(cinvstd, 1, CHARINDEX('/', cinvstd) - 1),left(cinvstd,2),'')) ELSE (CASE WHEN ISNUMERIC(left(cinvstd,1)) = 1 THEN LOWER(REPLACE(SUBSTRING(cinvstd, 1, CHARINDEX('/', cinvstd) - 1),left(cinvstd,1),'')) ELSE '' END) END) END) end) STCK_UOM, cInvStd PACK_MIN_QTY from IA_Subsidiary a left join dbo.Inventory c on a.cInvCode=c.cInvCode where a.dKeepDate < N'{0}' and a.cVouType<> N'33' and ISNULL(a.iMonth,0)<>0 and ISNULL(a.cBatchia,'') != '' group by a.cInvCode,a.cWhCode,a.cBatchia,c.cInvName,c.cinvstd) a where STCK_QTY > 0 and STCK_VALUE_CNY > 0"; sqls = string.Format(sqls, lastMonth); 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(""); soapRequestData.Append(""); soapRequestData.Append(""); soapRequestData.Append(""); //循环数据 STCK_STD_DATE STCK_TYPE_CD STCK_VALUE_CNW foreach (DataRow itemRow in vbsdt.Rows) { soapRequestData.Append(""); soapRequestData.Append(""); soapRequestData.Append(itemRow["IF_STD_DATE"] != null ? "" + itemRow["IF_STD_DATE"].ToString() + "" : "" + ""); soapRequestData.Append(itemRow["STCK_STD_DATE"] != null ? "" + itemRow["STCK_STD_DATE"].ToString() + "" : "" + ""); soapRequestData.Append(itemRow["WRHS_CD"] != null ? "" + itemRow["WRHS_CD"].ToString() + "" : "" + ""); soapRequestData.Append(itemRow["ITEM_CD"] != null ? "" + itemRow["ITEM_CD"].ToString() + "" : "" + ""); soapRequestData.Append(itemRow["PRDT_BTCH_NO"] != null ? "" + itemRow["PRDT_BTCH_NO"].ToString() + "" : "" + ""); soapRequestData.Append(itemRow["STCK_TYPE_CD"] != null ? "" + itemRow["STCK_TYPE_CD"].ToString() + "" : "" + ""); soapRequestData.Append(itemRow["STCK_QTY"] != null ? "" + itemRow["STCK_QTY"].ToString() + "" : "" + ""); soapRequestData.Append(itemRow["STCK_VALUE_CNY"] != null ? "" + itemRow["STCK_VALUE_CNY"].ToString() + "" : "" + ""); soapRequestData.Append(itemRow["STCK_UOM"] != null ? "" + itemRow["STCK_UOM"].ToString() + "" : "" + ""); soapRequestData.Append(""); soapRequestData.Append(itemRow["PACK_MIN_QTY"] != null ? "" + Regex.Replace(itemRow["PACK_MIN_QTY"].ToString(), @"[^0-9]+", "") + "" : "" + ""); soapRequestData.Append(""); soapRequestData.Append(itemRow["GEN_ROW_CNT"] != null ? "" + itemRow["GEN_ROW_CNT"].ToString() + "" : "" + ""); soapRequestData.Append(""); } soapRequestData.Append(""); soapRequestData.Append(""); soapRequestData.Append(""); 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-prod.skchemicals.com/XISOAPAdapter/MessageServlet?senderParty=&senderService=YERP_P&receiverParty=&receiverService=&interface=YERP_010_OA1_SI&interfaceNamespace=http://skch.com/YERP_010_IF_STCK_HST"; rop.RequestSAP(rop.Url, 10000, postData, "IF_YERP", "Kfiw282082", 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; } } } }