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

3 months ago
3 months ago
2 months ago
3 months ago
3 months ago
2 months ago
3 months ago
2 months ago
3 months ago
3 months ago
  1. using Quartz;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Threading.Tasks;
  13. namespace ICSSoft.FromERP
  14. {
  15. /// <summary>
  16. /// Stock information 库存信息 日
  17. /// </summary>
  18. public class YERP_010_BOH : IJob
  19. {
  20. private static object key = new object();
  21. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  22. public void Execute(IJobExecutionContext context)
  23. {
  24. try
  25. {
  26. lock (key)
  27. {
  28. log.Info("开始……………………………………………………………………");
  29. Execute();
  30. log.Info("结束……………………………………………………………………");
  31. }
  32. }
  33. catch (Exception ex)
  34. {
  35. log.Error(ex.ToString());
  36. }
  37. }
  38. public async void Execute()
  39. {
  40. try
  41. {
  42. Configuration config = GetConfig();
  43. var Dates = DateTime.Now;
  44. string conStr = ICSHelper.GetConnectString();
  45. //<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:yerp="http://skch.com/YERP_010_IF_STCK_HST">
  46. // <soapenv:Header/>
  47. // <soapenv:Body>
  48. // <yerp:YERP_010_OA1_MT>
  49. // <!--Zero or more repetitions:-->
  50. // <IF_STCK_HST>
  51. // <IF_STD_DATE>2024-07-01</IF_STD_DATE>
  52. // <!--Optional:-->
  53. // <STCK_STD_DATE>2024-07-01</STCK_STD_DATE>
  54. // <!--Optional:-->
  55. // <WRHS_CD>?</WRHS_CD>
  56. // <ITEM_CD>?</ITEM_CD>
  57. // <!--Optional:-->
  58. // <PRDT_BTCH_NO>?</PRDT_BTCH_NO>
  59. // <STCK_TYPE_CD>?</STCK_TYPE_CD>
  60. // <!--Optional:-->
  61. // <STCK_QTY>?</STCK_QTY>
  62. // <!--Optional:-->
  63. // <STCK_VALUE_CNW>?</STCK_VALUE_CNW>
  64. // <!--Optional:-->
  65. // <STCK_UOM>?</STCK_UOM>
  66. // <!--Optional:-->
  67. // <PACK_MIN_QTY>?</PACK_MIN_QTY>
  68. // <GEN_ROW_CNT>?</GEN_ROW_CNT>
  69. // </IF_STCK_HST>
  70. // </yerp:YERP_010_OA1_MT>
  71. // </soapenv:Body>
  72. //</soapenv:Envelope>
  73. log.Info("上月日期:" + Dates.AddMonths(-1).AddDays(-Dates.Day + 1).ToString("yyyy-MM-dd")); // 2024-07-01
  74. log.Info("本月日期:" + Dates.AddDays(-Dates.Day + 1).ToString("yyyy-MM-dd")); // 2024-08-01
  75. log.Info("本月日期+时分秒:" + Dates.AddDays(-Dates.Day + 1).ToString("yyyy-MM-dd 00:00:00")); // 2024-08-01 00:00:00 日期结果已测试
  76. // 1和2为 当月 汇总数据过滤条件
  77. var beginPeriod1 = Dates.AddDays(-Dates.Day + 1).ToString("yyyy-MM-dd");//"2024-02-01";
  78. var endPeriod2 = Dates.ToString("yyyy-MM-dd");//"2024-03-01";
  79. // 3和4为 上月 汇总数据过滤条件
  80. var beginPeriod3 = Dates.AddMonths(-1).AddDays(-Dates.Day + 1).ToString("yyyy-MM-dd");//"2024-01-01";
  81. var endPeriod4 = Dates.AddDays(-Dates.Day).ToString("yyyy-MM-dd");//"2024-02-01";
  82. string sqls = @"
  83. --
  84. select a.cInvCode,cWhCode,
  85. case when sum(ISNULL(iAOutQuantity,0)) <> 0 then SUM(ISNULL(iAOutPrice,0)) / sum(ISNULL(iAOutQuantity,0)) else 0 end as OutAInPrice
  86. ,case when SUM(ISNULL(iAInQuantity,0)) <> 0 then (SUM(ISNULL(iAInPrice,0))) / sum(ISNULL(iAInQuantity,0)) else 0 end as InAInPrice
  87. into #STCK_VALUE_CNW_thisMonth
  88. from IA_Subsidiary a
  89. where a.dKeepDate >= N'{0}' and a.dKeepDate <= N'{1}' and cVouType<> N'33' and iMonth<>0
  90. group by a.cInvCode,cWhCode
  91. --
  92. select a.cInvCode,cWhCode,
  93. case when sum(ISNULL(iAOutQuantity,0)) <> 0 then SUM(ISNULL(iAOutPrice,0)) / sum(ISNULL(iAOutQuantity,0)) else 0 end as OutAInPrice
  94. ,case when SUM(ISNULL(iAInQuantity,0)) <> 0 then (SUM(ISNULL(iAInPrice,0))) / sum(ISNULL(iAInQuantity,0)) else 0 end as InAInPrice
  95. into #STCK_VALUE_CNW_ListMonth
  96. from IA_Subsidiary a
  97. where a.dKeepDate >= N'{2}' and a.dKeepDate <= N'{3}' and cVouType<> N'33' and iMonth<>0
  98. group by a.cInvCode,cWhCode
  99. -- RowIndex排序后只取值为1的
  100. select cInvCode,cWhCode,STCK_VALUE_CNW,Months,row_number() over (partition by cInvCode,cWhCode order by Months desc) as RowIndex
  101. into #STCK_VALUE_CNW
  102. from (
  103. select cInvCode,cWhCode,
  104. case when InAInPrice <> 0 and OutAInPrice <> 0
  105. then
  106. case when InAInPrice <> 0 then InAInPrice else OutAInPrice end
  107. else
  108. 0
  109. end as STCK_VALUE_CNW
  110. ,1 as Months from #STCK_VALUE_CNW_thisMonth -- 1
  111. union all
  112. select cInvCode,cWhCode,
  113. case when InAInPrice <> 0 and OutAInPrice <> 0
  114. then
  115. case when InAInPrice <> 0 then InAInPrice else OutAInPrice end
  116. else
  117. 0
  118. end as STCK_VALUE_CNW
  119. ,0 as Months from #STCK_VALUE_CNW_ListMonth -- 0
  120. ) a
  121. select convert(nvarchar(10),getdate(),112) IF_STD_DATE,
  122. convert(nvarchar(10),getdate(),112) STCK_STD_DATE,
  123. a.cWhCode WRHS_CD,
  124. a.cInvCode ITEM_CD,
  125. cBatch PRDT_BTCH_NO,
  126. 'BOH' STCK_TYPE_CD,
  127. CAST(a.iQuantity as decimal(10,3)) STCK_QTY,
  128. CAST(a.iQuantity * ABS(ISNULL(d.STCK_VALUE_CNW,0)) as decimal(10,3)) STCK_VALUE_CNY,
  129. (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
  130. THEN LOWER(REPLACE(SUBSTRING(cinvstd, 1, CHARINDEX('/', cinvstd) - 1),left(cinvstd,2),'')) ELSE (CASE WHEN ISNUMERIC(left(cinvstd,1)) = 1
  131. THEN LOWER(REPLACE(SUBSTRING(cinvstd, 1, CHARINDEX('/', cinvstd) - 1),left(cinvstd,1),'')) ELSE '' END) END) END) end) STCK_UOM,
  132. cInvStd PACK_MIN_QTY,
  133. COUNT(*) OVER (PARTITION BY getdate()) GEN_ROW_CNT
  134. from CurrentStock a
  135. left join dbo.Inventory c on a.cInvCode=c.cInvCode
  136. left join #STCK_VALUE_CNW d on a.cWhCode = d.cWhCode and a.cInvCode = d.cInvCode and RowIndex = 1
  137. where 1 = 1 and iQuantity > 0 and ISNULL(d.STCK_VALUE_CNW,0) > 0
  138. Drop Table #STCK_VALUE_CNW_ListMonth
  139. Drop Table #STCK_VALUE_CNW_thisMonth
  140. Drop Table #STCK_VALUE_CNW";//and dInvCreateDatetime > '{0}' and dInvCreateDatetime < '{1}'
  141. sqls = string.Format(sqls, beginPeriod1, endPeriod2, beginPeriod3, endPeriod4);
  142. log.Info("YERP_010 sql:" + sqls);
  143. DataTable vbsdt = ICSHelper.ExecuteTable(conStr, sqls);
  144. log.Info("YERP_010 sql结果数量" + vbsdt.Rows.Count);
  145. if (vbsdt.Rows.Count > 0)
  146. {
  147. StringBuilder soapRequestData = new StringBuilder();
  148. soapRequestData.Append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:yerp=\"http://skch.com/YERP_010_IF_STCK_HST\">");
  149. soapRequestData.Append("<soapenv:Header/>");
  150. soapRequestData.Append("<soapenv:Body>");
  151. soapRequestData.Append("<yerp:YERP_010_OA1_MT>");
  152. //循环数据
  153. foreach (DataRow itemRow in vbsdt.Rows)
  154. {
  155. soapRequestData.Append("<!--Zero or more repetitions: -->");
  156. soapRequestData.Append("<IF_STCK_HST>");
  157. soapRequestData.Append(itemRow["IF_STD_DATE"] != null ? "<IF_STD_DATE>" + itemRow["IF_STD_DATE"].ToString() + "</IF_STD_DATE>" : "<IF_STD_DATE>" + "</IF_STD_DATE>");
  158. soapRequestData.Append(itemRow["STCK_STD_DATE"] != null ? "<STCK_STD_DATE>" + itemRow["STCK_STD_DATE"].ToString() + "</STCK_STD_DATE>" : "<STCK_STD_DATE>" + "</STCK_STD_DATE>");
  159. soapRequestData.Append(itemRow["WRHS_CD"] != null ? "<WRHS_CD>" + itemRow["WRHS_CD"].ToString() + "</WRHS_CD>" : "<WRHS_CD>" + "</WRHS_CD>");
  160. soapRequestData.Append(itemRow["ITEM_CD"] != null ? "<ITEM_CD>" + itemRow["ITEM_CD"].ToString() + "</ITEM_CD>" : "<ITEM_CD>" + "</ITEM_CD>");
  161. soapRequestData.Append(itemRow["PRDT_BTCH_NO"] != null ? "<PRDT_BTCH_NO>" + itemRow["PRDT_BTCH_NO"].ToString() + "</PRDT_BTCH_NO>" : "<PRDT_BTCH_NO>" + "</PRDT_BTCH_NO>");
  162. soapRequestData.Append(itemRow["STCK_TYPE_CD"] != null ? "<STCK_TYPE_CD>" + itemRow["STCK_TYPE_CD"].ToString() + "</STCK_TYPE_CD>" : "<STCK_TYPE_CD>" + "</STCK_TYPE_CD>");
  163. soapRequestData.Append(itemRow["STCK_QTY"] != null ? "<STCK_QTY>" + itemRow["STCK_QTY"].ToString() + "</STCK_QTY>" : "<STCK_QTY>" + "</STCK_QTY>");
  164. soapRequestData.Append(itemRow["STCK_VALUE_CNY"] != null ? "<STCK_VALUE_CNY>" + itemRow["STCK_VALUE_CNY"].ToString() + "</STCK_VALUE_CNY>" : "<STCK_VALUE_CNY>" + "</STCK_VALUE_CNY>");
  165. soapRequestData.Append(itemRow["STCK_UOM"] != null ? "<STCK_UOM>" + itemRow["STCK_UOM"].ToString() + "</STCK_UOM>" : "<STCK_UOM>" + "</STCK_UOM>");
  166. soapRequestData.Append("<!--Optional:-->");
  167. 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>");
  168. soapRequestData.Append("<!--Optional:-->");
  169. soapRequestData.Append(itemRow["GEN_ROW_CNT"] != null ? "<GEN_ROW_CNT>" + itemRow["GEN_ROW_CNT"].ToString() + "</GEN_ROW_CNT>" : "<GEN_ROW_CNT>" + "</GEN_ROW_CNT>");
  170. soapRequestData.Append("</IF_STCK_HST>");
  171. }
  172. soapRequestData.Append("</yerp:YERP_010_OA1_MT>");
  173. soapRequestData.Append("</soapenv:Body>");
  174. soapRequestData.Append("</soapenv:Envelope>");
  175. string postData = soapRequestData.ToString();
  176. log.Info("YERP_010 xmlRequest:" + postData);
  177. string statusCode;
  178. string resultContent;
  179. ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
  180. RestOpHelper rop = new RestOpHelper();
  181. rop.Clientp12path = @"C:\Users\Administrator\Desktop\yonyou-cert\wildcard.pfx";
  182. rop.Clientp12PassWord = "Yonyou2024!";
  183. 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";
  184. rop.RequestSAP(rop.Url, 10000, postData, "IF_YERP", "Kfiw282082", out statusCode, out resultContent);
  185. log.Info("YERP_010 statusCode:" + statusCode + "\r\n" + "resultContent:" + resultContent);
  186. }
  187. }
  188. catch (Exception ex)
  189. {
  190. log.Error(ex.ToString());
  191. }
  192. }
  193. public static Configuration GetConfig()
  194. {
  195. Assembly assembly = Assembly.GetCallingAssembly();
  196. string path = string.Format("{0}.config", assembly.Location);
  197. if (!File.Exists(path))
  198. {
  199. throw new FileNotFoundException(path + "路径下的文件未找到!");
  200. }
  201. try
  202. {
  203. ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
  204. configFile.ExeConfigFilename = path;
  205. Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);
  206. return config;
  207. }
  208. catch (Exception)
  209. {
  210. throw;
  211. }
  212. }
  213. }
  214. }