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.

192 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 ICSOApply : 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. b.cCode AS ApplyCode,
  49. isnull(a.comcode,'' )as SourceCode,
  50. isnull(c.iVouchRowNo,'') as SourceSequence,
  51. a.cInvCode as InvCode,
  52. isnull(a.irowno,'') as Sequence,
  53. a.iQuantity as Quantity,
  54. isnull(a.fOutQuantity,0) as IssueQuantity,
  55. isnull(a.cWhCode,'') as WHCode,
  56. '2' as Status,
  57. b.cMaker as CreatePerson,
  58. b.dnmaketime as CreateDateTime,
  59. isnull(b.id,'') as ApplyID,
  60. isnull(a.AutoID,'') as ApplyDetailID,
  61. ISNULL(b.dnverifytime,ISNULL(b.dnmodifytime, b.dnmaketime)) AS MTIME,
  62. isnull(a.cItemCode,'') ProjectCode,isnull(a.cbatch,'') cBatch,'' version ,'' brand,
  63. (case WHEN e.bFree1=1 THEN a.cFree1 ELSE '' END) as cFree1,
  64. (case WHEN e.bFree2=1 THEN a.cFree2 ELSE '' END) as cFree2,
  65. (case WHEN e.bFree3=1 THEN a.cFree3 ELSE '' END) as cFree3,
  66. (case WHEN e.bFree4=1 THEN a.cFree4 ELSE '' END) as cFree4,
  67. (case WHEN e.bFree5=1 THEN a.cFree5 ELSE '' END) as cFree5,
  68. (case WHEN e.bFree6=1 THEN a.cFree6 ELSE '' END) as cFree6,
  69. (case WHEN e.bFree7=1 THEN a.cFree7 ELSE '' END) as cFree7,
  70. (case WHEN e.bFree8=1 THEN a.cFree8 ELSE '' END) as cFree8,
  71. (case WHEN e.bFree9=1 THEN a.cFree9 ELSE '' END) as cFree9,
  72. (case WHEN e.bFree10=1 THEN a.cFree10 ELSE '' END) as cFree10,CAST('' AS NVARCHAR(500)) as Colspan
  73. ,'{0}' as WorkPoint
  74. into #TempPick
  75. FROM {1}.dbo.MaterialAppVouchs a
  76. INNER JOIN {1}.dbo.MaterialAppVouch b ON a.ID = b.ID
  77. LEFT JOIN {1}.dbo.OM_MODetails c ON a.iOMoDID = c.MODetailsID
  78. LEFT JOIN {1}.dbo.OM_MOMain d ON c.MOID = d.MOID
  79. LEFT JOIN {1}.dbo.Inventory e ON a.cInvCode = e.cInvCode
  80. WHERE ISNULL(b.cHandler,'')<> ''and b.cSource=''
  81. AND ISNULL(b.dnverifytime,ISNULL(b.dnmodifytime, b.dnmaketime))>=@LastTime
  82. 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,'')
  83. select distinct
  84. Colspan,ProjectCode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,WorkPoint,cBatch,version ,brand
  85. INTO #TempExtension
  86. from #TempPick
  87. IF NOT EXISTS(SELECT * FROM #TempPick)
  88. RETURN";
  89. sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempPick");
  90. Dictionary<string, string> value = new Dictionary<string, string>();
  91. value.Add("ID", "NEWID()");
  92. value.Add("BatchCode", "a.cBatch");
  93. value.Add("version", "a.version");
  94. value.Add("brand", "a.brand");
  95. value.Add("ProjectCode", "a.ProjectCode");
  96. value.Add("cFree1", "a.cFree1");
  97. value.Add("cFree2", "a.cFree2");
  98. value.Add("cFree3", "a.cFree3");
  99. value.Add("cFree4", "a.cFree4");
  100. value.Add("cFree5", "a.cFree5");
  101. value.Add("cFree6", "a.cFree6");
  102. value.Add("cFree7", "a.cFree7");
  103. value.Add("cFree8", "a.cFree8");
  104. value.Add("cFree9", "a.cFree9");
  105. value.Add("cFree10", "a.cFree10");
  106. value.Add("Colspan", "a.Colspan");
  107. value.Add("MTIME", "GETDATE()");
  108. value.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  109. value.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  110. value.Add("WorkPoint", "'" + WorkPoint + "'");
  111. //插入新增数据->ICSExtension表
  112. sql += ICSHelper.InsertSQL("ICSExtension", value)
  113. + @"#TempExtension a
  114. 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
  115. WHERE b.Colspan IS NULL";
  116. sql += @"--删除数据
  117. -- DELETE ICSOApply WHERE ApplyID NOT IN (SELECT ApplyID FROM {1}.dbo.[MaterialAppVouchs]) AND LEN(ApplyID)>0
  118. SELECT a.ApplyDetailID INTO #tableICSOApply
  119. FROM ICSOApply a With(NoLock)
  120. LEFT JOIN {1}.dbo.[MaterialAppVouchs] b With(NoLock) ON a.ApplyDetailID=b.AutoID
  121. WHERE a.WorkPoint='{0}' and b.AutoID IS NULL AND LEN(a.ApplyDetailID)>0
  122. DELETE ICSOApply
  123. WHERE WorkPoint='{0}' and ApplyDetailID IN ( SELECT ApplyDetailID from #tableICSOApply)
  124. ";
  125. Dictionary<string, string> values = new Dictionary<string, string>();
  126. values.Add("ApplyCode", "a.ApplyCode");
  127. values.Add("Sequence", "a.Sequence");
  128. values.Add("SourceCode", "a.SourceCode");
  129. values.Add("SourceSequence", "a.SourceSequence");
  130. values.Add("InvCode", "a.InvCode");
  131. values.Add("Quantity", "a.Quantity");
  132. values.Add("Amount", "0");
  133. values.Add("WHCode", "a.WHCode");
  134. values.Add("Type", "'领料'");
  135. values.Add("Status", "a.Status");
  136. values.Add("CreatePerson", "a.CreatePerson");
  137. values.Add("CreateDateTime", "a.CreateDateTime");
  138. values.Add("ApplyID", "a.ApplyID");
  139. values.Add("ApplyDetailID", "a.ApplyDetailID");
  140. values.Add("ExtensionID", "c.ID");
  141. values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  142. values.Add("MUSERName", "'" + ConstWorkPoint.Muser + "'");
  143. values.Add("MTIME", "a.MTIME");
  144. values.Add("WorkPoint", "'" + WorkPoint + "'");
  145. //values.Add("VoucherNO", "a.VoucherNO");
  146. //values.Add("Reservation", "a.Reservation");
  147. //values.Add("ReservationRow", "a.ReservationRow");
  148. //更新存在数据
  149. sql += ICSHelper.UpdateSQL("b", values)
  150. + @" #TempPick a
  151. INNER JOIN ICSOApply b ON a.ApplyDetailID=b.ApplyDetailID and a.WorkPoint=b.WorkPoint
  152. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  153. WHERE a.WorkPoint='" + WorkPoint + "' ";
  154. values.Add("ID", "NEWID()");
  155. values.Add("IssueQuantity", "a.IssueQuantity");
  156. //values.Add("HasQuantity", "'0'");
  157. //values.Add("Sign", "'1'");
  158. //values.Add("Did", "a.Did");
  159. //values.Add("WorkPoint", "'" + WorkPoint + "'");
  160. //插入新增数据
  161. sql += ICSHelper.InsertSQL("ICSOApply", values)
  162. + @" #TempPick a
  163. LEFT JOIN ICSOApply b ON a.ApplyDetailID=b.ApplyDetailID and a.WorkPoint=b.WorkPoint
  164. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  165. WHERE b.ID IS NULL and a.WorkPoint='" + WorkPoint + @"'
  166. ";
  167. sql += " DROP TABLE #TempPick ";
  168. sql += " DROP TABLE #TempExtension " +
  169. " DROP TABLE #tableICSOApply";
  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. }