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.

203 lines
10 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 ICSMOApply : 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
  48. A.DocNo as ApplyCode,
  49. B.LineNum as Sequence,
  50. CASE WHEN A.IssueType=0 OR A.IssueType=3 THEN C.DocNo WHEN A.IssueType=1 THEN E.DocNo END as SourceCode,
  51. CASE WHEN A.IssueType=0 OR A.IssueType=3 THEN 1 WHEN A.IssueType=1 THEN D.LineNum END as SourceSequence,
  52. F.Code as InvCode,
  53. B.IssuedQty as Quantity,
  54. 0 as Amount,
  55. G.Code as WHCode,
  56. CASE WHEN A.IssueType=0 THEN '1' WHEN A.IssueType=1 THEN '2' END as Type,
  57. '2' as Status,
  58. A.CreatedBy as CreatePerson,
  59. A.CreatedOn as CreateDateTime,
  60. A.ID as ApplyID,
  61. B.ID as ApplyDetailID ,
  62. ISNULL(B.ModifiedOn,B.CreatedOn) AS MTIME,
  63. isnull(I.Code,'') ProjectCode,isnull(B.LotNo,'') cBatch,'' version ,'' brand,
  64. '' as cFree1,
  65. '' as cFree2,
  66. '' as cFree3,
  67. '' as cFree4,
  68. '' as cFree5,
  69. '' as cFree6,
  70. '' as cFree7,
  71. '' as cFree8,
  72. '' as cFree9,
  73. '' as cFree10,CAST('' AS NVARCHAR(500)) as Colspan
  74. ,H.Code as WorkPoint
  75. ,B.MOPick PickID
  76. INTO #TempPick
  77. from {1}.dbo.MO_IssueDoc A
  78. LEFT JOIN {1}.dbo.MO_IssueDocLine B ON IssueDoc=A.ID
  79. LEFT JOIN {1}.dbo.MO_MO C ON C.ID=B.MO
  80. LEFT JOIN {1}.dbo.MO_IssueDocLine D ON D.ID=B.SourceIssueDocLine
  81. LEFT JOIN {1}.dbo.MO_IssueDoc E ON E.ID=D.IssueDoc
  82. LEFT JOIN {1}.dbo.CBO_ItemMaster F ON F.ID=B.Item
  83. LEFT JOIN {1}.dbo.CBO_Wh G ON G.ID=B.Wh
  84. LEFT JOIN {1}.dbo.Base_Organization H ON H.ID=A.Org
  85. LEFT JOIN {1}.dbo.CBO_Project I ON I.ID=B.Project
  86. where (A.IssueType=0 OR A.IssueType=3) AND A.DocState=3 AND H.Code='{0}' AND ISNULL(B.ModifiedOn,B.CreatedOn)>=@LastTime
  87. update #TempPick 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,'')
  88. select distinct
  89. Colspan,ProjectCode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,WorkPoint,cBatch,version ,brand
  90. INTO #TempExtension
  91. from #TempPick
  92. IF NOT EXISTS(SELECT * FROM #TempPick)
  93. RETURN";
  94. sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempPick");
  95. Dictionary<string, string> value = new Dictionary<string, string>();
  96. value.Add("ID", "NEWID()");
  97. value.Add("BatchCode", "a.cBatch");
  98. value.Add("version", "a.version");
  99. value.Add("brand", "a.brand");
  100. value.Add("ProjectCode", "a.ProjectCode");
  101. value.Add("cFree1", "a.cFree1");
  102. value.Add("cFree2", "a.cFree2");
  103. value.Add("cFree3", "a.cFree3");
  104. value.Add("cFree4", "a.cFree4");
  105. value.Add("cFree5", "a.cFree5");
  106. value.Add("cFree6", "a.cFree6");
  107. value.Add("cFree7", "a.cFree7");
  108. value.Add("cFree8", "a.cFree8");
  109. value.Add("cFree9", "a.cFree9");
  110. value.Add("cFree10", "a.cFree10");
  111. value.Add("Colspan", "a.Colspan");
  112. value.Add("MTIME", "GETDATE()");
  113. value.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  114. value.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  115. value.Add("WorkPoint", "'" + WorkPoint + "'");
  116. //插入新增数据->ICSExtension表
  117. sql += ICSHelper.InsertSQL("ICSExtension", value)
  118. + @"#TempExtension a
  119. 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
  120. WHERE b.Colspan IS NULL";
  121. sql += @"--删除数据
  122. -- DELETE ICSMOApply WHERE ApplyDetailID IN (SELECT ApplyDetailID FROM ICSMOApply a left join {1}.dbo.MaterialAppVouchs b on a.ApplyDetailID=b.AutoID where b.AutoID is null) AND LEN(ApplyDetailID)>0
  123. SELECT a.ApplyDetailID INTO #tableICSMOApply
  124. FROM ICSMOApply a With(NoLock)
  125. LEFT JOIN {1}.dbo.[MO_IssueDocLine] b With(NoLock) ON a.ApplyDetailID=b.ID
  126. WHERE a.WorkPoint='{0}' and b.ID IS NULL
  127. DELETE ICSMOApply
  128. WHERE WorkPoint='{0}' and ApplyDetailID IN ( SELECT ApplyDetailID from #tableICSMOApply)
  129. ";
  130. Dictionary<string, string> values = new Dictionary<string, string>();
  131. values.Add("ApplyCode", "a.ApplyCode");
  132. values.Add("Sequence", "a.Sequence");
  133. values.Add("SourceCode", "a.SourceCode");
  134. values.Add("SourceSequence", "a.SourceSequence");
  135. values.Add("InvCode", "a.InvCode");
  136. //values.Add("SubInvCode", "a.SubInvCode");
  137. values.Add("Quantity", "a.Quantity");
  138. values.Add("Amount", "0");
  139. values.Add("WHCode", "a.WHCode");
  140. values.Add("Type", "1");
  141. values.Add("Status", "a.Status");
  142. //values.Add("VoucherDate", "a.VoucherDate");
  143. //values.Add("EATTRIBUTE", "a.EATTRIBUTE");
  144. values.Add("CreatePerson", "a.CreatePerson");
  145. values.Add("CreateDateTime", "a.CreateDateTime");
  146. values.Add("ApplyID", "a.ApplyID");
  147. values.Add("ApplyDetailID", "a.ApplyDetailID");
  148. values.Add("ExtensionID", "c.ID");
  149. values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  150. values.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  151. values.Add("MTIME", "a.MTIME");
  152. //values.Add("MoveType", "a.MoveType");
  153. // values.Add("ORDERNO", "a.ORDERNO");
  154. values.Add("WorkPoint", "'" + WorkPoint + "'");
  155. //values.Add("WorkPoint", "a.WorkPoint");
  156. // values.Add("cBatch", "a.cBatch");
  157. //values.Add("DepCode", "a.DepCode");
  158. //更新存在数据
  159. sql += ICSHelper.UpdateSQL("b", values)
  160. + @" #TempPick a
  161. INNER JOIN ICSMOApply b ON a.ApplyDetailID=b.ApplyDetailID and a.WorkPoint=b.WorkPoint
  162. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  163. WHERE a.WorkPoint='" + WorkPoint + "' ";
  164. values.Add("ID", "NEWID()");
  165. values.Add("IssueQuantity", "'0'");
  166. //插入新增数据
  167. sql += ICSHelper.InsertSQL("ICSMOApply", values)
  168. + @" #TempPick a
  169. LEFT JOIN ICSMOApply b ON a.ApplyDetailID=b.ApplyDetailID and a.WorkPoint=b.WorkPoint
  170. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  171. WHERE b.ID IS NULL and a.WorkPoint='" + WorkPoint + "' ";
  172. sql += " DROP TABLE #TempPick ";
  173. sql += " DROP TABLE #TempExtension " +
  174. " DROP TABLE #tableICSMOApply";
  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. }