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

2 years 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 ICSMOIssue : 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 IssueCode,
  49. B.LineNum as Sequence,
  50. CASE WHEN A.IssueType=0 THEN C.DocNo WHEN A.IssueType=1 THEN E.DocNo END as SourceCode,
  51. CASE WHEN A.IssueType=0 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. '1' as Status,
  58. A.CreatedBy as CreatePerson,
  59. A.CreatedOn as CreateDateTime,
  60. A.ID as IssueID,
  61. B.ID as IssueDetailID ,
  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 ,'' AppLyDetailID
  76. INTO #TempRd
  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=1) AND A.DocState=3 AND H.Code='{0}' AND ISNULL(B.ModifiedOn,B.CreatedOn)>=@LastTime
  87. update #TempRd 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 #TempRd
  92. IF NOT EXISTS(SELECT * FROM #TempRd)
  93. RETURN";
  94. sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempRd");
  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 ICSMOIssue where ISNULL(IssueDetailID,'') NOT IN ( SELECT autoID FROM {1}.dbo.rdrecords11 )
  123. SELECT ISNULL(a.IssueDetailID,'') IssueDetailID INTO #tableICSMOIssue
  124. FROM ICSMOIssue a With(NoLock)
  125. LEFT JOIN {1}.dbo.[MO_IssueDocLine] b With(NoLock) ON ISNULL(a.IssueDetailID,'')=b.ID
  126. WHERE a.WorkPoint='{0}' and b.ID IS NULL
  127. DELETE ICSMOIssue
  128. WHERE WorkPoint='{0}' and IssueDetailID IN ( SELECT IssueDetailID from #tableICSMOIssue)
  129. ";
  130. sql += "\r\n";
  131. Dictionary<string, string> values = new Dictionary<string, string>();
  132. values.Add("IssueCode", "a.IssueCode");
  133. values.Add("Sequence", "a.Sequence");
  134. values.Add("SourceCode", "a.SourceCode");
  135. values.Add("SourceSequence", "a.SourceSequence");
  136. values.Add("InvCode", "a.InvCode");
  137. values.Add("Quantity", "a.Quantity");
  138. values.Add("Amount", "a.Amount");
  139. values.Add("WHCode", "a.WHCode");
  140. values.Add("Type", "a.Type");
  141. values.Add("Status", "a.Status");
  142. values.Add("CreatePerson", "a.CreatePerson");
  143. values.Add("CreateDateTime", "a.CreateDateTime");
  144. values.Add("IssueID", "a.IssueID");
  145. values.Add("IssueDetailID", "a.IssueDetailID");
  146. values.Add("PickID", "a.PickID");
  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("WorkPoint", "'" + WorkPoint + "'");
  152. values.Add("MTIME", "a.MTIME");
  153. //更新存在数据
  154. sql += "\r\n";
  155. sql += ICSHelper.UpdateSQL("b", values)
  156. + @" #TempRd a
  157. INNER JOIN ICSMOIssue b ON a.IssueDetailID=b.IssueDetailID and a.WorkPoint=b.WorkPoint
  158. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  159. WHERE a.WorkPoint='" + WorkPoint + "' ";
  160. //values.Add("EATTRIBUTE3", "a.EATTRIBUTE3");
  161. values.Add("ID", "newid()");
  162. values.Add("IssueQuantity", "0");
  163. //插入新增数据
  164. sql += "\r\n";
  165. sql += ICSHelper.InsertSQL("ICSMOIssue", values)
  166. + @" #TempRd a
  167. LEFT JOIN ICSMOIssue b ON a.IssueDetailID=b.IssueDetailID and a.WorkPoint=b.WorkPoint
  168. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  169. WHERE b.IssueDetailID IS NULL and a.WorkPoint='" + WorkPoint + @"'
  170. ";
  171. sql += " DROP TABLE #TempRd ";
  172. sql += " DROP TABLE #TempExtension " +
  173. " DROP TABLE #tableICSMOIssue";
  174. sql = string.Format(sql, WorkPoint, erpName);
  175. ICSHelper.ExecuteDate(conStr, sql);
  176. #endregion
  177. }
  178. }
  179. catch (Exception ex)
  180. {
  181. log.Error(ex.ToString());
  182. }
  183. }
  184. }
  185. }