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.

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