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.

209 lines
12 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 ICSMOPick : 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
  49. a.MoCode,b.SortSeq SEQ1,b.InvCode,b.Qty QTY1,c.AllocateId AS PickID,d.AllocateId AS ParentPickID,
  50. c.SortSeq SEQ2,c.InvCode INV1,c.Qty QTY2,e.InvCode INV2,c.Qty/d.Factor SETE,isnull(isnull(c.AuxQty,0)/d.Factor,0) ParentAmount,null ParentQuantity
  51. into #TempBOM1
  52. FROM {1}.dbo.mom_order a
  53. INNER JOIN {1}.dbo.mom_orderdetail b ON a.MoId = b.MoId
  54. INNER JOIN {1}.dbo.mom_moallocate c ON b.MoDId = c.MoDId
  55. LEFT JOIN {1}.dbo.mom_moallocatesub d ON c.MoallocateSubId = d.MoallocateSubId
  56. LEFT JOIN {1}.dbo.mom_moallocate e ON d.AllocateId = e.AllocateId
  57. ORDER BY a.MoCode,b.SortSeq,d.AllocateId,c.SortSeq
  58. update #TempBOM1 set ParentQuantity=(case when ParentPickID is null then QTY2+isnull((select sum(isnull(SETE,0)) from #TempBOM1 a where a.ParentPickID=b.PickID),0)
  59. else
  60. (select sum(isnull(SETE,0)) from #TempBOM1 a where b.ParentPickID=a.ParentPickID)+(select QTY2 from #TempBOM1 a where a.PickID=b.ParentPickID) end)
  61. from #TempBOM1 b
  62. SELECT b.MoDId AS MODetailID ,
  63. CAST(c.AllocateId AS NVARCHAR(50)) AS PickID,
  64. c.InvCode AS InvCode,
  65. c.SortSeq AS Sequence,
  66. CASE WHEN d.bCutMantissa='1' AND d.bKCCutMantissa='1' THEN CEILING(c.Qty) ELSE c.Qty END AS Quantity,
  67. isnull(c.AuxQty,0)AS Amount,
  68. 0 as IssueQuantity,
  69. c.WhCode as WHCode,
  70. (case when bInvQuality=0 then '4' else c.WIPType end) AS SupplyType,
  71. CASE WHEN ISNULL(a.ModifyTime, a.CreateTime)>ISNULL(b.CloseTime,ISNULL(b.RelsTime,ISNULL(a.ModifyTime, a.CreateTime))) THEN ISNULL(a.ModifyTime, a.CreateTime) ELSE ISNULL(b.CloseTime,ISNULL(b.RelsTime,ISNULL(a.ModifyTime, a.CreateTime))) END as MTIME,
  72. isnull(b.CostItemCode,'') ProjectCode,isnull(b.MoLotCode,'') cBatch,'' version ,'' brand,
  73. (case WHEN d.bFree1=1 THEN c.Free1 ELSE '' END) as cFree1,
  74. (case WHEN d.bFree2=1 THEN c.Free2 ELSE '' END) as cFree2,
  75. (case WHEN d.bFree3=1 THEN c.Free3 ELSE '' END) as cFree3,
  76. (case WHEN d.bFree4=1 THEN c.Free4 ELSE '' END) as cFree4,
  77. (case WHEN d.bFree5=1 THEN c.Free5 ELSE '' END) as cFree5,
  78. (case WHEN d.bFree6=1 THEN c.Free6 ELSE '' END) as cFree6,
  79. (case WHEN d.bFree7=1 THEN c.Free7 ELSE '' END) as cFree7,
  80. (case WHEN d.bFree8=1 THEN c.Free8 ELSE '' END) as cFree8,
  81. (case WHEN d.bFree9=1 THEN c.Free9 ELSE '' END) as cFree9,
  82. (case WHEN d.bFree10=1 THEN c.Free10 ELSE '' END) as cFree10,CAST('' AS NVARCHAR(500)) as Colspan
  83. ,'{0}' as WorkPoint
  84. ,(CASE WHEN c.ProductType<>1 then '1' else '0' end ) EATTRIBUTE1,i.ParentPickID,i.ParentQuantity,i.ParentAmount
  85. into #TempBOM
  86. FROM {1}.dbo.mom_order a
  87. INNER JOIN {1}.dbo.mom_orderdetail b ON a.MoId = b.MoId
  88. INNER JOIN {1}.dbo.mom_moallocate c ON b.MoDId = c.MoDId
  89. INNER JOIN {1}.dbo.Inventory d ON c.InvCode = d.cInvCode
  90. LEFT JOIN {1}.dbo.bom_parent e ON b.BomId = e.BomId
  91. LEFT JOIN {1}.dbo.bom_bom f ON e.BomId = f.BomId
  92. LEFT JOIN {1}.dbo.ComputationUnit unit ON d.cComUnitCode = unit.cComunitCode
  93. left join #TempBOM1 i on i.MoCode=a.MoCode and c.AllocateId =i.PickID
  94. WHERE ISNULL (b.RelsUser,'') <> ''
  95. and CASE WHEN ISNULL(a.ModifyTime, a.CreateTime)>ISNULL(b.CloseTime,ISNULL(b.RelsTime,ISNULL(a.ModifyTime, a.CreateTime))) THEN ISNULL(a.ModifyTime, a.CreateTime) ELSE ISNULL(b.CloseTime,ISNULL(b.RelsTime,ISNULL(a.ModifyTime, a.CreateTime))) END>=@LastTime
  96. update #TempBOM 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,'')
  97. select distinct
  98. Colspan,ProjectCode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,WorkPoint,cBatch,version ,brand
  99. INTO #TempExtension
  100. from #TempBOM
  101. IF NOT EXISTS(SELECT * FROM #TempBOM)
  102. RETURN";
  103. sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempBOM");
  104. Dictionary<string, string> value = new Dictionary<string, string>();
  105. value.Add("ID", "NEWID()");
  106. value.Add("BatchCode", "a.cBatch");
  107. value.Add("version", "a.version");
  108. value.Add("brand", "a.brand");
  109. value.Add("ProjectCode", "a.ProjectCode");
  110. value.Add("cFree1", "a.cFree1");
  111. value.Add("cFree2", "a.cFree2");
  112. value.Add("cFree3", "a.cFree3");
  113. value.Add("cFree4", "a.cFree4");
  114. value.Add("cFree5", "a.cFree5");
  115. value.Add("cFree6", "a.cFree6");
  116. value.Add("cFree7", "a.cFree7");
  117. value.Add("cFree8", "a.cFree8");
  118. value.Add("cFree9", "a.cFree9");
  119. value.Add("cFree10", "a.cFree10");
  120. value.Add("Colspan", "a.Colspan");
  121. value.Add("MTIME", "GETDATE()");
  122. value.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  123. value.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  124. value.Add("WorkPoint", "'" + WorkPoint + "'");
  125. //插入新增数据->ICSExtension表
  126. sql += ICSHelper.InsertSQL("ICSExtension", value)
  127. + @"#TempExtension a
  128. 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
  129. WHERE b.Colspan IS NULL";
  130. sql += "\r\n";
  131. sql += @"--删除数据
  132. -- DELETE icsMOPick WHERE PickID NOT IN (SELECT AllocateId FROM {1}.dbo.mom_moallocate)
  133. SELECT a.PickID INTO #tableicsMOPick
  134. FROM icsMOPick a With(NoLock)
  135. LEFT JOIN {1}.dbo.[mom_moallocate] b With(NoLock) ON a.PickID=b.AllocateId
  136. WHERE a.WorkPoint='{0}' and b.AllocateId IS NULL
  137. DELETE icsMOPick
  138. WHERE WorkPoint='{0}' and PickID IN ( SELECT PickID from #tableicsMOPick)
  139. ";
  140. Dictionary<string, string> values = new Dictionary<string, string>();
  141. values.Add("MODetailID", "a.MODetailID");
  142. values.Add("PickID", "a.PickID");
  143. values.Add("InvCode", "a.InvCode");
  144. values.Add("Sequence", "a.Sequence");
  145. values.Add("Quantity", "a.Quantity");
  146. values.Add("Amount", "a.Amount");
  147. values.Add("ParentPickID", "a.ParentPickID");
  148. values.Add("ParentQuantity", "a.ParentQuantity");
  149. values.Add("ParentAmount", "a.ParentAmount");
  150. values.Add("WHCode", "a.WHCode");
  151. values.Add("SupplyType", "a.SupplyType");
  152. values.Add("ExtensionID", "c.ID");
  153. values.Add("EATTRIBUTE1", "a.EATTRIBUTE1");
  154. //values.Add("WorkPoint", "a.WorkPoint");
  155. //更新存在数据
  156. sql += ICSHelper.UpdateSQL("b", values)
  157. + @" #TempBOM a
  158. INNER JOIN icsMOPick b ON a.PickID=b.PickID and a.WorkPoint=b.WorkPoint
  159. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  160. WHERE a.WorkPoint='" + WorkPoint + "' ";
  161. values.Add("ID", "NEWID()");//ID
  162. values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  163. values.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  164. values.Add("WorkPoint", "'" + WorkPoint + "'");
  165. values.Add("MTIME", "a.MTIME");
  166. values.Add("IssueQuantity", "a.IssueQuantity");
  167. sql += "\r\n";
  168. //插入新增数据
  169. sql += ICSHelper.InsertSQL("icsMOPick", values)
  170. + @" #TempBOM a
  171. LEFT JOIN icsMOPick b ON a.PickID=b.PickID and a.WorkPoint=b.WorkPoint
  172. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  173. WHERE b.PickID IS NULL and a.WorkPoint='" + WorkPoint + @"'
  174. ";
  175. sql += " DROP TABLE #TempBOM ";
  176. sql += " DROP TABLE #TempExtension " +
  177. " DROP TABLE #tableicsMOPick";
  178. sql = string.Format(sql, WorkPoint, erpName);
  179. log.Info(sql);
  180. ICSHelper.ExecuteDate(conStr, sql);
  181. #endregion
  182. }
  183. }
  184. catch (Exception ex)
  185. {
  186. log.Error(ex.ToString());
  187. }
  188. }
  189. }
  190. }