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.

190 lines
10 KiB

4 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using Quartz;
  7. namespace ICSSoft.FromERP
  8. {
  9. /// <summary>
  10. /// 红字采购入库单
  11. /// </summary>
  12. public class ICSPurchaseReceive : IJob
  13. {
  14. private static object key = new object();
  15. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  16. public void Execute(IJobExecutionContext context)
  17. {
  18. try
  19. {
  20. lock (key)
  21. {
  22. log.Info("开始……………………………………………………………………");
  23. Execute();
  24. log.Info("结束……………………………………………………………………");
  25. }
  26. }
  27. catch (Exception ex)
  28. {
  29. log.Error(ex.ToString());
  30. }
  31. }
  32. public void Execute()
  33. {
  34. try
  35. {
  36. string conStr = ICSHelper.GetConnectString();
  37. string Namespace = this.GetType().Namespace;
  38. //string Class = this.GetType().Name;
  39. DataTable dt = ICSHelper.GetERPDB(conStr);
  40. foreach (DataRow dr in dt.Rows)
  41. {
  42. string erpName = ICSHelper.GetConfigString()["ERPDB"];
  43. string WorkPoint = dr["WorkPointCode"].ToString();
  44. string Class = this.GetType().Name + WorkPoint;
  45. erpName = string.Format(erpName, WorkPoint);
  46. #region SQL
  47. string sql = @" SELECT CAST(a.Autoid AS NVARCHAR(50)) AS ERPAutoid,
  48. b.cCode AS RCVCode,
  49. CAST(a.irowno AS NVARCHAR(50)) AS Sequence,
  50. b.cSource as SourceCode,
  51. CAST(f.irowno AS NVARCHAR(50)) as SourceSequence,
  52. a.cInvCode AS InvCode,
  53. a.iQuantity AS Quantity,
  54. isnull(a.iNum,0) as Amount,
  55. 0 as RCVQuantity,
  56. b.cWhCode AS WhCode,
  57. --b.bredvouch as Type,
  58. '1' as Status,
  59. b.cMaker as CreatePerson,
  60. b.dnmaketime as CreateDateTime,
  61. a.ID as RCVID,
  62. a.AutoID as RCVDetailID,
  63. ISNULL(b.dnmodifytime, b.dnmaketime) AS MTIME,
  64. isnull(a.cItemCode,'') ProjectCode,isnull(a.cbatch,'') cBatch,'' version ,'' brand,
  65. (case WHEN inv.bFree1=1 THEN a.cFree1 ELSE '' END) as cFree1,
  66. (case WHEN inv.bFree2=1 THEN a.cFree2 ELSE '' END) as cFree2,
  67. (case WHEN inv.bFree3=1 THEN a.cFree3 ELSE '' END) as cFree3,
  68. (case WHEN inv.bFree4=1 THEN a.cFree4 ELSE '' END) as cFree4,
  69. (case WHEN inv.bFree5=1 THEN a.cFree5 ELSE '' END) as cFree5,
  70. (case WHEN inv.bFree6=1 THEN a.cFree6 ELSE '' END) as cFree6,
  71. (case WHEN inv.bFree7=1 THEN a.cFree7 ELSE '' END) as cFree7,
  72. (case WHEN inv.bFree8=1 THEN a.cFree8 ELSE '' END) as cFree8,
  73. (case WHEN inv.bFree9=1 THEN a.cFree9 ELSE '' END) as cFree9,
  74. (case WHEN inv.bFree10=1 THEN a.cFree10 ELSE '' END) as cFree10,CAST('' AS NVARCHAR(500)) as Colspan
  75. , '{0}' as WorkPoint
  76. INTO #TempPOArrive
  77. FROM {1}.dbo.RdRecords01 a
  78. INNER JOIN {1}.dbo.RdRecord01 b ON a.ID = b.ID
  79. LEFT JOIN {1}.dbo.Department c ON b.cDepCode = c.cDepCode
  80. LEFT JOIN {1}.dbo.Vendor d ON b.cVenCode = d.cVenCode
  81. LEFT JOIN {1}.dbo.PO_Podetails e ON a.iPOsID = e.ID
  82. LEFT JOIN {1}.dbo.Inventory inv ON a.cInvCode = inv.cInvCode
  83. LEFT JOIN {1}.dbo.PU_ArrivalVouchs f on a.iArrsId=f.Autoid
  84. WHERE ISNULL(b.dnmodifytime, b.dnmaketime)>=@LastTime AND b.cBusType IN ('') AND a.iQuantity<0 AND ISNULL(b.cHandler, '')=''
  85. update #TempPOArrive set Colspan=isNULL(ProjectCode,'')+'~'+isNULL(cBatch,'')+'~'+isNULL(Version,'')+'~'+isNULL(Brand,'')+'~'+isNULL(cFree1,'')+'~'+isnull(cFree2,'')+'~'+isnull(cFree3,'')+'~'+isnull(cFree4,'')+'~'+isnull(cFree5,'')+'~'+isnull(cFree6,'')+'~'+isnull(cFree7,'')+'~'+isnull(cFree8,'')+'~'+isnull(cFree9,'')+'~'+isnull(cFree10,'')
  86. select distinct
  87. Colspan,ProjectCode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,WorkPoint,cBatch,version ,brand
  88. INTO #TempExtension
  89. from #TempPOArrive
  90. IF NOT EXISTS(SELECT * FROM #TempPOArrive)
  91. RETURN";
  92. sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempPOArrive");
  93. Dictionary<string, string> value = new Dictionary<string, string>();
  94. value.Add("ID", "NEWID()");
  95. value.Add("BatchCode", "a.cBatch");
  96. value.Add("version", "a.version");
  97. value.Add("brand", "a.brand");
  98. value.Add("ProjectCode", "a.ProjectCode");
  99. value.Add("cFree1", "a.cFree1");
  100. value.Add("cFree2", "a.cFree2");
  101. value.Add("cFree3", "a.cFree3");
  102. value.Add("cFree4", "a.cFree4");
  103. value.Add("cFree5", "a.cFree5");
  104. value.Add("cFree6", "a.cFree6");
  105. value.Add("cFree7", "a.cFree7");
  106. value.Add("cFree8", "a.cFree8");
  107. value.Add("cFree9", "a.cFree9");
  108. value.Add("cFree10", "a.cFree10");
  109. value.Add("Colspan", "a.Colspan");
  110. value.Add("MTIME", "GETDATE()");
  111. value.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  112. value.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  113. value.Add("WorkPoint", "'" + WorkPoint + "'");
  114. //插入新增数据->ICSExtension表
  115. sql += ICSHelper.InsertSQL("ICSExtension", value)
  116. + @"#TempExtension a
  117. LEFT JOIN ICSExtension b ON a.ProjectCode+'~'+a.cBatch+'~'+a.Version+'~'+a.Brand+'~'+a.cFree1+'~'+a.cFree2+'~'+a.cFree3+'~'+a.cFree4+'~'+a.cFree5+'~'+a.cFree6+'~'+a.cFree7+'~'+a.cFree8+'~'+a.cFree9+'~'+a.cFree10=b.Colspan and a.WorkPoint=b.WorkPoint
  118. WHERE b.Colspan IS NULL";
  119. sql += "\r\n";
  120. sql += @"--删除数据
  121. -- DELETE ICSPurchaseReceive WHERE RCVDetailID NOT IN (SELECT Autoid FROM {1}.dbo.RdRecords01) AND LEN(RCVDetailID)>0
  122. SELECT a.RCVDetailID INTO #tableICSPurchaseReceive
  123. FROM ICSPurchaseReceive a With(NoLock)
  124. LEFT JOIN {1}.dbo.[RdRecords01] b With(NoLock) ON a.RCVDetailID=b.Autoid
  125. WHERE a.WorkPoint='{0}' and b.Autoid IS NULL AND LEN(a.RCVDetailID)>0
  126. DELETE ICSPurchaseReceive
  127. WHERE WorkPoint='{0}' and RCVDetailID IN ( SELECT RCVDetailID from #tableICSPurchaseReceive)
  128. ";
  129. Dictionary<string, string> values = new Dictionary<string, string>();
  130. values.Add("RCVCode", "a.RCVCode");//入库单号
  131. values.Add("Sequence", "a.Sequence");//入库单行号
  132. values.Add("SourceCode", "a.SourceCode");//来源单号
  133. values.Add("SourceSequence", "a.SourceSequence");//来源单号行号
  134. values.Add("InvCode", "a.InvCode");//物料编码
  135. values.Add("Quantity", "ABS(a.Quantity)");//入库数量
  136. values.Add("Amount", "a.Amount");//辅助数量
  137. values.Add("WHCode", "a.WHCode");//仓库代码
  138. values.Add("RCVDetailID", "a.RCVDetailID");
  139. values.Add("RCVID", "a.RCVID");
  140. values.Add("CreateDateTime", "a.CreateDateTime");
  141. values.Add("CreatePerson", "a.CreatePerson");
  142. values.Add("Status", "a.Status");//状态
  143. values.Add("ExtensionID", "c.ID");
  144. values.Add("MTIME", "a.MTIME");
  145. values.Add("Type", "2");//类型
  146. //更新存在数据
  147. sql += ICSHelper.UpdateSQL("b", values)
  148. + @" #TempPOArrive a
  149. INNER JOIN ICSPurchaseReceive b ON a.RCVDetailID=b.RCVDetailID and a.WorkPoint=b.WorkPoint
  150. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  151. WHERE a.WorkPoint='" + WorkPoint + "' ";
  152. values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  153. values.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  154. values.Add("WorkPoint", "'" + WorkPoint + "'");
  155. values.Add("ID", "NewID()");
  156. values.Add("RCVQuantity", "a.RCVQuantity");
  157. sql += "\r\n";
  158. //插入新增数据
  159. sql += ICSHelper.InsertSQL("ICSPurchaseReceive", values)
  160. + @" #TempPOArrive a
  161. LEFT JOIN ICSPurchaseReceive b ON a.RCVDetailID=b.RCVDetailID and a.WorkPoint=b.WorkPoint
  162. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  163. WHERE b.RCVDetailID IS NULL and a.WorkPoint='" + WorkPoint + @"' ";
  164. sql += "DROP TABLE #TempPOArrive";
  165. sql += " DROP TABLE #TempExtension " +
  166. "DROP TABLE #tableICSPurchaseReceive";
  167. sql = string.Format(sql, WorkPoint, erpName);
  168. ICSHelper.ExecuteDate(conStr, sql);
  169. #endregion
  170. }
  171. }
  172. catch (Exception ex)
  173. {
  174. log.Error(ex.ToString());
  175. }
  176. }
  177. }
  178. }