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.

181 lines
9.7 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 ICSReturn : 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 = @"
  48. select a.cCODE as ReturnCode,
  49. a.cdepcode as DepCode,
  50. isnull(b.cwhcode,'') as WHCode ,
  51. b.cinvcode as InvCode,
  52. b.iquantity as Quantity,
  53. Row_Number() OVER (PARTITION BY cCODE ORDER BY b.cinvcode desc) as Sequence,
  54. ISNULL(a.dCloseDate ,ISNULL(a.dmDate ,a.dVeriDate)) AS MTIME,
  55. a.iStatus as Status,
  56. a.cMaker as CreatePerson ,
  57. a.dmDate as CreateDateTime,
  58. a.ID as ReturnID,
  59. b.AutoID as ReturnDetailID,
  60. '' ProjectCode,isnull(b.cbatch,'') cBatch,'' version ,'' brand,
  61. (case WHEN inv.bFree1=1 THEN b.cFree1 ELSE '' END) as cFree1,
  62. (case WHEN inv.bFree2=1 THEN b.cFree2 ELSE '' END) as cFree2,
  63. (case WHEN inv.bFree3=1 THEN b.cFree3 ELSE '' END) as cFree3,
  64. (case WHEN inv.bFree4=1 THEN b.cFree4 ELSE '' END) as cFree4,
  65. (case WHEN inv.bFree5=1 THEN b.cFree5 ELSE '' END) as cFree5,
  66. (case WHEN inv.bFree6=1 THEN b.cFree6 ELSE '' END) as cFree6,
  67. (case WHEN inv.bFree7=1 THEN b.cFree7 ELSE '' END) as cFree7,
  68. (case WHEN inv.bFree8=1 THEN b.cFree8 ELSE '' END) as cFree8,
  69. (case WHEN inv.bFree9=1 THEN b.cFree9 ELSE '' END) as cFree9,
  70. (case WHEN inv.bFree10=1 THEN b.cFree10 ELSE '' END) as cFree10,CAST('' AS NVARCHAR(500)) as Colspan
  71. , '{0}' as WorkPoint
  72. INTO #Return
  73. from {1}.dbo.HY_DZ_BorrowOutBack a
  74. inner join {1}.dbo.HY_DZ_BorrowOutBacks b on a.ID =b.ID
  75. LEFT JOIN {1}.dbo.Inventory inv ON b.cInvCode = inv.cInvCode
  76. WHERE ISNULL(a.cHandler, '')<>'' AND a.dVeriDate >=@LastTime
  77. update #Return 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,'')
  78. select distinct
  79. Colspan,ProjectCode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,WorkPoint,cBatch,version ,brand
  80. INTO #TempExtension
  81. from #Return
  82. IF NOT EXISTS(SELECT * FROM #Return)
  83. RETURN";
  84. sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#Return");
  85. Dictionary<string, string> value = new Dictionary<string, string>();
  86. value.Add("ID", "NEWID()");
  87. value.Add("BatchCode", "a.cBatch");
  88. value.Add("version", "a.version");
  89. value.Add("brand", "a.brand");
  90. value.Add("ProjectCode", "a.ProjectCode");
  91. value.Add("cFree1", "a.cFree1");
  92. value.Add("cFree2", "a.cFree2");
  93. value.Add("cFree3", "a.cFree3");
  94. value.Add("cFree4", "a.cFree4");
  95. value.Add("cFree5", "a.cFree5");
  96. value.Add("cFree6", "a.cFree6");
  97. value.Add("cFree7", "a.cFree7");
  98. value.Add("cFree8", "a.cFree8");
  99. value.Add("cFree9", "a.cFree9");
  100. value.Add("cFree10", "a.cFree10");
  101. value.Add("Colspan", "a.Colspan");
  102. value.Add("MTIME", "GETDATE()");
  103. value.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  104. value.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  105. value.Add("WorkPoint", "'" + WorkPoint + "'");
  106. //插入新增数据->ICSExtension表
  107. sql += ICSHelper.InsertSQL("ICSExtension", value)
  108. + @"#TempExtension a
  109. 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
  110. WHERE b.Colspan IS NULL";
  111. sql += @"--删除数据
  112. --DELETE ICSReturn WHERE ReturnDetailID NOT IN ( SELECT AutoID FROM {1}.dbo.HY_DZ_BorrowOutBacks)
  113. SELECT a.ReturnDetailID INTO #tableICSSDN
  114. FROM ICSReturn a With(NoLock)
  115. LEFT JOIN {1}.dbo.[HY_DZ_BorrowOutBacks] b With(NoLock) ON a.ReturnDetailID=b.AutoID
  116. WHERE a.WorkPoint='{0}' and b.AutoID IS NULL
  117. DELETE ICSReturn
  118. WHERE WorkPoint='{0}' and ReturnDetailID IN ( SELECT ReturnDetailID from #tableICSSDN)
  119. ";
  120. Dictionary<string, string> values = new Dictionary<string, string>();
  121. values.Add("ReturnCode", "a.ReturnCode");
  122. values.Add("DepCode", "a.DepCode");
  123. values.Add("WHCode", "a.WHCode");
  124. values.Add("InvCode", "a.InvCode");
  125. values.Add("Quantity", "a.Quantity");
  126. values.Add("Amount", "0");
  127. values.Add("Sequence", "a.Sequence");
  128. values.Add("Status", "a.Status ");
  129. values.Add("CreatePerson", "a.CreatePerson");
  130. values.Add("CreateDateTime", "a.CreateDateTime");
  131. values.Add("ReturnID", "a.ReturnID");
  132. values.Add("ReturnDetailID", "a.ReturnDetailID");
  133. values.Add("ExtensionID", "c.ID");
  134. values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  135. values.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  136. values.Add("WorkPoint", "'" + WorkPoint + "'");
  137. values.Add("MTIME", "a.MTIME");
  138. //更新存在数据
  139. sql += ICSHelper.UpdateSQL("ICSReturn", values)
  140. + @" #Return a
  141. INNER JOIN ICSReturn b ON a.ReturnDetailID=b.ReturnDetailID and a.WorkPoint=b.WorkPoint
  142. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  143. WHERE a.WorkPoint='" + WorkPoint + "' ";
  144. values.Add("ID", "NEWID()");
  145. values.Add("ReturnQuantity", "0");
  146. //values.Add("ErpAutoID", "a.ErpAutoID");
  147. //values.Add("ErpID", "a.ErpID");
  148. //values.Add("iFQuantity", "a.iFQuantity");
  149. //插入新增数据
  150. sql += ICSHelper.InsertSQL("ICSReturn", values)
  151. + @" #Return a
  152. LEFT JOIN ICSReturn b ON a.ReturnDetailID=b.ReturnDetailID and a.WorkPoint=b.WorkPoint
  153. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  154. WHERE b.ReturnDetailID IS NULL and a.WorkPoint='" + WorkPoint + @"'
  155. ";
  156. sql += "DROP TABLE #Return";
  157. sql += " DROP TABLE #TempExtension " +
  158. " DROP TABLE #tableICSSDN";
  159. sql = string.Format(sql, WorkPoint, erpName);
  160. ICSHelper.ExecuteDate(conStr, sql);
  161. #endregion
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. log.Error(ex.ToString());
  167. }
  168. }
  169. }
  170. }