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

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