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.

196 lines
11 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 ICSOutsourcingReceive : 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 RCVDetailID,
  48. b.cCode AS cCode,
  49. CAST(a.irowno AS NVARCHAR(50)) AS irowno,
  50. CAST(f.irowno AS NVARCHAR(50)) as SourceSequence,
  51. isnull(a.iNum,0) as Amount,
  52. b.cSource as SourceCode,
  53. '1' as Status,
  54. b.cMaker as CreatePerson,
  55. b.dnmaketime as CreateDateTime,
  56. b.ID as RCVID,
  57. b.cVenCode AS cVenCode,
  58. d.cVenName AS cVenName,
  59. b.cDepCode AS cDepCode,
  60. c.cDepName AS cDepName,
  61. b.cWhCode AS cWhCode,
  62. a.cInvCode AS cInvCode,
  63. a.iQuantity AS iQuantity,
  64. ISNULL(b.dnmodifytime, b.dnmaketime) AS MTIME,
  65. CAST(b.ID AS NVARCHAR(50)) AS ErpID,
  66. b.cOrderCode AS POCode,
  67. CAST(e.ivouchrowno AS NVARCHAR(50)) AS iPOsID,
  68. b.cBusType AS Free1,
  69. isnull(a.cItemCode,'') as ProjectCode,isnull(a.cbatch,'') cBatch,'' version ,'' brand,
  70. (case WHEN inv.bFree1=1 THEN a.cFree1 ELSE '' END) as cFree1,
  71. (case WHEN inv.bFree2=1 THEN a.cFree2 ELSE '' END) as cFree2,
  72. (case WHEN inv.bFree3=1 THEN a.cFree3 ELSE '' END) as cFree3,
  73. (case WHEN inv.bFree4=1 THEN a.cFree4 ELSE '' END) as cFree4,
  74. (case WHEN inv.bFree5=1 THEN a.cFree5 ELSE '' END) as cFree5,
  75. (case WHEN inv.bFree6=1 THEN a.cFree6 ELSE '' END) as cFree6,
  76. (case WHEN inv.bFree7=1 THEN a.cFree7 ELSE '' END) as cFree7,
  77. (case WHEN inv.bFree8=1 THEN a.cFree8 ELSE '' END) as cFree8,
  78. (case WHEN inv.bFree9=1 THEN a.cFree9 ELSE '' END) as cFree9,
  79. (case WHEN inv.bFree10=1 THEN a.cFree10 ELSE '' END) as cFree10,CAST('' AS NVARCHAR(500)) as Colspan
  80. , '{0}' as WorkPoint
  81. INTO #TempPOArrive
  82. FROM {1}.dbo.RdRecords01 a
  83. INNER JOIN {1}.dbo.RdRecord01 b ON a.ID = b.ID
  84. LEFT JOIN {1}.dbo.Department c ON b.cDepCode = c.cDepCode
  85. LEFT JOIN {1}.dbo.Vendor d ON b.cVenCode = d.cVenCode
  86. LEFT JOIN {1}.dbo.Inventory inv ON a.cInvCode = inv.cInvCode
  87. LEFT JOIN {1}.dbo.PO_Podetails e ON a.iPOsID = e.ID
  88. LEFT JOIN {1}.dbo.PU_ArrivalVouchs f on a.iArrsId=f.Autoid
  89. WHERE ISNULL(b.dnmodifytime, b.dnmaketime)>=@LastTime AND b.cBusType IN ('') AND a.iQuantity<0 AND ISNULL(b.cHandler, '')=''
  90. 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,'')
  91. select distinct
  92. Colspan,ProjectCode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,WorkPoint,cBatch,version ,brand
  93. INTO #TempExtension
  94. from #TempPOArrive
  95. IF NOT EXISTS(SELECT * FROM #TempPOArrive)
  96. RETURN";
  97. sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempPOArrive");
  98. Dictionary<string, string> value = new Dictionary<string, string>();
  99. value.Add("ID", "NEWID()");
  100. value.Add("BatchCode", "a.cBatch");
  101. value.Add("version", "a.version");
  102. value.Add("brand", "a.brand");
  103. value.Add("ProjectCode", "a.ProjectCode");
  104. value.Add("cFree1", "a.cFree1");
  105. value.Add("cFree2", "a.cFree2");
  106. value.Add("cFree3", "a.cFree3");
  107. value.Add("cFree4", "a.cFree4");
  108. value.Add("cFree5", "a.cFree5");
  109. value.Add("cFree6", "a.cFree6");
  110. value.Add("cFree7", "a.cFree7");
  111. value.Add("cFree8", "a.cFree8");
  112. value.Add("cFree9", "a.cFree9");
  113. value.Add("cFree10", "a.cFree10");
  114. value.Add("Colspan", "a.Colspan");
  115. value.Add("MTIME", "GETDATE()");
  116. value.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  117. value.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  118. value.Add("WorkPoint", "'" + WorkPoint + "'");
  119. //插入新增数据->ICSExtension表
  120. sql += ICSHelper.InsertSQL("ICSExtension", value)
  121. + @"#TempExtension a
  122. 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
  123. WHERE b.Colspan IS NULL";
  124. sql += "\r\n";
  125. sql += @"--删除数据
  126. --DELETE ICSOutsourcingReceive WHERE RCVDetailID NOT IN (SELECT Autoid FROM {1}.dbo.RdRecords01) AND LEN(RCVDetailID)>0
  127. SELECT a.RCVDetailID INTO #tableICSOutsourcingReceive
  128. FROM ICSOutsourcingReceive a With(NoLock)
  129. LEFT JOIN {1}.dbo.[RdRecords01] b With(NoLock) ON a.RCVDetailID=b.AutoID
  130. WHERE a.WorkPoint='{0}' and b.AutoID IS NULL AND LEN(a.RCVDetailID)>0
  131. DELETE ICSOutsourcingReceive
  132. WHERE WorkPoint='{0}' and RCVDetailID IN ( SELECT RCVDetailID from #tableICSOutsourcingReceive)
  133. ";
  134. Dictionary<string, string> values = new Dictionary<string, string>();
  135. values.Add("RCVCode", "a.cCode");//入库单号
  136. values.Add("Sequence", "a.irowno");//入库单行号
  137. values.Add("SourceCode", "a.SourceCode");//来源单号
  138. values.Add("SourceSequence", "a.SourceSequence");//来源单号行号
  139. values.Add("InvCode", "a.cInvCode");//物料编码
  140. values.Add("Quantity", "ABS(a.iQuantity)");//入库数量
  141. values.Add("Amount", "a.Amount");//辅助数量
  142. values.Add("WHCode", "a.cWhCode");//仓库代码
  143. values.Add("ExtensionID", "c.ID");
  144. values.Add("RCVDetailID", "a.RCVDetailID");
  145. values.Add("RCVID", "a.RCVID");
  146. values.Add("CreateDateTime", "a.CreateDateTime");
  147. values.Add("CreatePerson", "a.CreatePerson");
  148. values.Add("Status", "a.Status");//状态
  149. values.Add("MTIME", "a.MTIME");
  150. values.Add("Type", "2");//类型
  151. //更新存在数据
  152. sql += ICSHelper.UpdateSQL("b", values)
  153. + @" #TempPOArrive a
  154. INNER JOIN ICSOutsourcingReceive b ON a.RCVDetailID=b.RCVDetailID and a.WorkPoint=b.WorkPoint
  155. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  156. WHERE a.WorkPoint='" + WorkPoint + "' ";
  157. values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  158. values.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  159. values.Add("WorkPoint", "'" + WorkPoint + "'");
  160. values.Add("ID", "NewID()");
  161. values.Add("RCVQuantity", "'0'");
  162. sql += "\r\n";
  163. //插入新增数据
  164. sql += ICSHelper.InsertSQL("ICSOutsourcingReceive", values)
  165. + @" #TempPOArrive a
  166. LEFT JOIN ICSOutsourcingReceive b ON a.RCVDetailID=b.RCVDetailID and a.WorkPoint=b.WorkPoint
  167. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  168. WHERE b.RCVID IS NULL and a.WorkPoint='" + WorkPoint + @"'
  169. ";
  170. sql += "DROP TABLE #TempPOArrive";
  171. sql += " DROP TABLE #TempExtension " +
  172. " DROP TABLE #tableICSOutsourcingReceive";
  173. sql = string.Format(sql, WorkPoint, erpName);
  174. ICSHelper.ExecuteDate(conStr, sql);
  175. #endregion
  176. }
  177. }
  178. catch (Exception ex)
  179. {
  180. log.Error(ex.ToString());
  181. }
  182. }
  183. }
  184. }