纽威
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.

192 lines
10 KiB

  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 ICSTransferSQ : 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.cTVCode AS TransferNO,
  49. CAST(b.irowno AS NVARCHAR(50)) AS Sequence,
  50. a.cOWhCode AS FromWarehouseCode,
  51. --d.LocationCode as FromLocationCode,
  52. a.cIWhCode AS ToWarehouseCode,
  53. CASE WHEN ISNULL(a.cVerifyPerson , '')='' THEN '1' ELSE '2' END AS Status,
  54. b.cInvCode AS InvCode,
  55. b.iTVQuantity AS Quantity,
  56. isnull(b.iTVNum,0) as Amount,
  57. CASE WHEN ISNULL(a.cVerifyPerson, '')='' THEN '1' ELSE '2' END AS Type,
  58. a.cTVMemo as Memo,
  59. a.cMaker as CreatePerson,
  60. a.dnmaketime as CreateDateTime,
  61. a.ID as TransferID,
  62. CAST(b.autoID AS NVARCHAR(50)) AS TransferDetailID,
  63. ISNULL(a.dnmodifytime, a.dnmaketime) AS MTIME,
  64. isnull(b.cItemCode,'') ProjectCode,isnull(b.cMoLotCode,'') cBatch,'' version ,'' brand,
  65. (case WHEN inv.bFree1=1 THEN isnull(b.cFree1,'') ELSE '' END) as cFree1,
  66. (case WHEN inv.bFree2=1 THEN isnull(b.cFree2,'') ELSE '' END) as cFree2,
  67. (case WHEN inv.bFree3=1 THEN isnull(b.cFree3,'') ELSE '' END) as cFree3,
  68. (case WHEN inv.bFree4=1 THEN isnull(b.cFree4,'') ELSE '' END) as cFree4,
  69. (case WHEN inv.bFree5=1 THEN isnull(b.cFree5,'') ELSE '' END) as cFree5,
  70. (case WHEN inv.bFree6=1 THEN isnull(b.cFree6,'') ELSE '' END) as cFree6,
  71. (case WHEN inv.bFree7=1 THEN isnull(b.cFree7,'') ELSE '' END) as cFree7,
  72. (case WHEN inv.bFree8=1 THEN isnull(b.cFree8,'') ELSE '' END) as cFree8,
  73. (case WHEN inv.bFree9=1 THEN isnull(b.cFree9,'') ELSE '' END) as cFree9,
  74. (case WHEN inv.bFree10=1 THEN isnull(b.cFree10,'') ELSE '' END) as cFree10,CAST('' AS NVARCHAR(500)) as Colspan
  75. , '{0}' as WorkPoint
  76. INTO #TempTrans
  77. FROM {1}.dbo.ST_AppTransVouch a
  78. INNER JOIN {1}.dbo.ST_AppTransVouchs b ON a.cTVCode = b.cTVCode
  79. LEFT JOIN {1}.dbo.Inventory inv ON b.cInvCode = inv.cInvCode
  80. WHERE ISNULL(a.dnmodifytime, a.dnmaketime)>=@LastTime
  81. update #TempTrans 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,'')
  82. select distinct
  83. Colspan,ProjectCode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,WorkPoint,cBatch,version ,brand
  84. INTO #TempExtension
  85. from #TempTrans
  86. IF NOT EXISTS(SELECT * FROM #TempTrans)
  87. RETURN";
  88. sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempTrans");
  89. Dictionary<string, string> value = new Dictionary<string, string>();
  90. value.Add("ID", "NEWID()");
  91. value.Add("BatchCode", "a.cBatch");
  92. value.Add("version", "a.version");
  93. value.Add("brand", "a.brand");
  94. value.Add("ProjectCode", "a.ProjectCode");
  95. value.Add("cFree1", "a.cFree1");
  96. value.Add("cFree2", "a.cFree2");
  97. value.Add("cFree3", "a.cFree3");
  98. value.Add("cFree4", "a.cFree4");
  99. value.Add("cFree5", "a.cFree5");
  100. value.Add("cFree6", "a.cFree6");
  101. value.Add("cFree7", "a.cFree7");
  102. value.Add("cFree8", "a.cFree8");
  103. value.Add("cFree9", "a.cFree9");
  104. value.Add("cFree10", "a.cFree10");
  105. value.Add("Colspan", "a.Colspan");
  106. value.Add("MTIME", "GETDATE()");
  107. value.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  108. value.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  109. value.Add("WorkPoint", "'" + WorkPoint + "'");
  110. //插入新增数据->ICSExtension表
  111. sql += ICSHelper.InsertSQL("ICSExtension", value)
  112. + @"#TempExtension a
  113. 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
  114. WHERE b.Colspan IS NULL";
  115. sql += @"--删除数据
  116. -- DELETE ICSTransferApplication where ISNULL(TransferDetailID,'') NOT IN ( SELECT autoID FROM {1}.dbo.ST_AppTransVouchs )
  117. SELECT ISNULL(a.TransferDetailID,'') TransferDetailID INTO #tableICSTransfer
  118. FROM ICSTransferApplication a With(NoLock)
  119. LEFT JOIN {1}.dbo.[ST_AppTransVouchs] b With(NoLock) ON ISNULL(a.TransferDetailID,'')=b.autoID
  120. WHERE a.WorkPoint='{0}' and b.autoID IS NULL
  121. DELETE ICSTransferApplication
  122. WHERE WorkPoint='{0}' and ISNULL(TransferDetailID,'') IN ( SELECT TransferDetailID from #tableICSTransfer)
  123. ";
  124. sql += "\r\n";
  125. Dictionary<string, string> values = new Dictionary<string, string>();
  126. values.Add("TransferNO", "a.TransferNO");
  127. values.Add("Sequence", "a.Sequence");
  128. values.Add("FromWarehouseCode", "a.FromWarehouseCode");
  129. //values.Add("FromLocationCode", "a.FromLocationCode");
  130. values.Add("ToWarehouseCode", "a.ToWarehouseCode");
  131. values.Add("Status", "a.Status");
  132. values.Add("InvCode", "a.InvCode");
  133. values.Add("Quantity", "a.Quantity");
  134. values.Add("Amount", "a.Amount");
  135. values.Add("Type", "a.Type");
  136. values.Add("Memo", "a.Memo");
  137. values.Add("CreateDateTime", "a.CreateDateTime");
  138. values.Add("CreatePerson", "a.CreatePerson");
  139. values.Add("TransferID", "a.TransferID");
  140. values.Add("TransferDetailID", "a.TransferDetailID");
  141. values.Add("ExtensionID", "c.ID");
  142. values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  143. values.Add("MUSERNAME", "'" + ConstWorkPoint.Musername + "'");
  144. values.Add("WorkPoint", "'" + WorkPoint + "'");
  145. values.Add("MTIME", "a.MTIME");
  146. //更新存在数据
  147. sql += "\r\n";
  148. sql += ICSHelper.UpdateSQL("b", values)
  149. + @" #TempTrans a
  150. INNER JOIN ICSTransferApplication b ON a.TransferDetailID=b.TransferDetailID and a.WorkPoint=b.WorkPoint
  151. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  152. WHERE a.WorkPoint='" + WorkPoint + "' ";
  153. values.Add("ID", "newid()");
  154. values.Add("TransferQuantity", "0");
  155. //values.Add("EATTRIBUTE3", "a.EATTRIBUTE3");
  156. //插入新增数据
  157. sql += "\r\n";
  158. sql += ICSHelper.InsertSQL("ICSTransferApplication", values)
  159. + @" #TempTrans a
  160. LEFT JOIN ICSTransferApplication b ON a.TransferDetailID=b.TransferDetailID and a.WorkPoint=b.WorkPoint
  161. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  162. WHERE b.TransferDetailID IS NULL and a.WorkPoint='" + WorkPoint + @"' ";
  163. sql += "DROP TABLE #TempTrans";
  164. sql += " DROP TABLE #TempExtension " +
  165. " DROP TABLE #tableICSTransfer";
  166. sql = string.Format(sql, WorkPoint, erpName);
  167. ICSHelper.ExecuteDate(conStr, sql);
  168. #endregion
  169. }
  170. }
  171. catch (Exception ex)
  172. {
  173. log.Error(ex.ToString());
  174. }
  175. }
  176. }
  177. }