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.

202 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 ICSTransfer : 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 A1.DocNo AS TransferNO,
  49. CAST(A2.DocLineNo AS NVARCHAR(50)) AS Sequence,
  50. A6.Code AS FromWarehouseCode,
  51. --d.LocationCode as FromLocationCode,
  52. A3.Code AS ToWarehouseCode,
  53. '1' AS Status,
  54. A2.ItemInfo_ItemCode AS InvCode,
  55. A2.StoreUOMQty AS Quantity,
  56. 0 as Amount,
  57. '1' AS Type,
  58. A14.Memo as Memo,
  59. A1.CreatedBy as CreatePerson,
  60. A1.CreatedOn as CreateDateTime,
  61. A1.ID as TransferID,
  62. CAST(A2.ID AS NVARCHAR(50)) AS TransferDetailID,
  63. ISNULL(A2.ModifiedOn ,A2.CreatedOn) AS MTIME,
  64. isnull(A12.Code,'') ProjectCode,isnull(A5.LotInfo_LotCode,'') cBatch,'' version ,'' brand,
  65. '' as cFree1,
  66. '' as cFree2,
  67. '' as cFree3,
  68. '' as cFree4,
  69. '' as cFree5,
  70. '' as cFree6,
  71. '' as cFree7,
  72. '' as cFree8,
  73. '' as cFree9,
  74. '' as cFree10,CAST('' AS NVARCHAR(500)) as Colspan
  75. ,A13.Code as WorkPoint
  76. INTO #TempTrans
  77. FROM {1}.dbo.InvDoc_TransferIn A1
  78. LEFT JOIN {1}.dbo.InvDoc_TransINLine A2 ON A2.TransferIn=A1.ID
  79. LEFT JOIN {1}.dbo.CBO_Wh A3 ON A2.TransInWh=A3.ID
  80. LEFT JOIN {1}.dbo.CBO_Wh_Trl A4 ON A4.ID = A3.ID AND A4.SysMLFlag='zh-CN'
  81. LEFT JOIN {1}.dbo.InvDoc_TransINSubLine A5 ON A2.ID=A5.TransINLine
  82. LEFT JOIN {1}.dbo.CBO_Wh A6 ON A5.TransOutWh=A6.ID
  83. LEFT JOIN {1}.dbo.CBO_Wh_Trl A7 ON A7.ID = A6.ID AND A7.SysMLFlag='zh-CN'
  84. LEFT JOIN {1}.dbo.Base_Organization A8 ON A1.Org=A8.ID
  85. LEFT JOIN {1}.dbo.[CBO_Project] as A11 on (A2.[Project] = A11.[ID])
  86. LEFT JOIN {1}.dbo.[CBO_Project] as A12 on (A5.[Project] = A12.[ID])
  87. LEFT JOIN {1}.dbo.Base_Organization A13 ON A1.Org=A13.ID
  88. LEFT JOIN {1}.dbo.InvDoc_TransferIn_Trl A14 ON A14.ID=A1.ID
  89. where ISNULL(A2.ModifiedOn ,A2.CreatedOn)>=@LastTime and A1.Status=1 AND A13.Code='{0}'
  90. 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,'')
  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 #TempTrans
  95. IF NOT EXISTS(SELECT * FROM #TempTrans)
  96. RETURN";
  97. sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempTrans");
  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 += @"--删除数据
  125. -- DELETE ICSTransfer where ISNULL(TransferDetailID,'') NOT IN ( SELECT autoID FROM {1}.dbo.TransVouchs )
  126. SELECT ISNULL(a.TransferDetailID,'') TransferDetailID INTO #tableICSTransfer
  127. FROM ICSTransfer a With(NoLock)
  128. LEFT JOIN {1}.dbo.[InvDoc_TransINLine] b With(NoLock) ON ISNULL(a.TransferDetailID,'')=b.ID
  129. WHERE a.WorkPoint='{0}' and b.ID IS NULL
  130. DELETE ICSTransfer
  131. WHERE WorkPoint='{0}' and ISNULL(TransferDetailID,'') IN ( SELECT TransferDetailID from #tableICSTransfer)
  132. ";
  133. sql += "\r\n";
  134. Dictionary<string, string> values = new Dictionary<string, string>();
  135. values.Add("TransferNO", "a.TransferNO");
  136. values.Add("Sequence", "a.Sequence");
  137. values.Add("FromWarehouseCode", "a.FromWarehouseCode");
  138. //values.Add("FromLocationCode", "a.FromLocationCode");
  139. values.Add("ToWarehouseCode", "a.ToWarehouseCode");
  140. values.Add("Status", "a.Status");
  141. values.Add("InvCode", "a.InvCode");
  142. values.Add("Quantity", "a.Quantity");
  143. values.Add("Amount", "a.Amount");
  144. values.Add("Type", "a.Type");
  145. values.Add("Memo", "a.Memo");
  146. values.Add("CreateDateTime", "a.CreateDateTime");
  147. values.Add("CreatePerson", "a.CreatePerson");
  148. values.Add("TransferID", "a.TransferID");
  149. values.Add("TransferDetailID", "a.TransferDetailID");
  150. values.Add("ExtensionID", "c.ID");
  151. values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  152. values.Add("MUSERNAME", "'" + ConstWorkPoint.Musername + "'");
  153. values.Add("WorkPoint", "'" + WorkPoint + "'");
  154. values.Add("MTIME", "a.MTIME");
  155. //更新存在数据
  156. sql += "\r\n";
  157. sql += ICSHelper.UpdateSQL("b", values)
  158. + @" #TempTrans a
  159. INNER JOIN ICSTransfer b ON a.TransferDetailID=b.TransferDetailID and a.WorkPoint=b.WorkPoint
  160. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  161. WHERE a.WorkPoint='" + WorkPoint + "' ";
  162. values.Add("ID", "newid()");
  163. values.Add("TransferQuantity", "0");
  164. //values.Add("EATTRIBUTE3", "a.EATTRIBUTE3");
  165. //插入新增数据
  166. sql += "\r\n";
  167. sql += ICSHelper.InsertSQL("ICSTransfer", values)
  168. + @" #TempTrans a
  169. LEFT JOIN ICSTransfer b ON a.TransferDetailID=b.TransferDetailID and a.WorkPoint=b.WorkPoint
  170. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  171. WHERE b.TransferDetailID IS NULL and a.WorkPoint='" + WorkPoint + @"' ";
  172. sql += "DROP TABLE #TempTrans";
  173. sql += " DROP TABLE #TempExtension " +
  174. " DROP TABLE #tableICSTransfer";
  175. sql = string.Format(sql, WorkPoint, erpName);
  176. ICSHelper.ExecuteDate(conStr, sql);
  177. #endregion
  178. }
  179. }
  180. catch (Exception ex)
  181. {
  182. log.Error(ex.ToString());
  183. }
  184. }
  185. }
  186. }