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

205 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 ICSDeliveryNotice : 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 = @"
  48. select A1.DocNO as DNCode,
  49. CAST(A2.DocLineNo AS NVARCHAR(50)) as Sequence,
  50. A1.Supplier_Code as VenCode ,
  51. A3.Code as DepCode ,
  52. CASE WHEN A1.ReceivementType=0 THEN '1' ELSE '2' END AS DNType,
  53. A2.ItemInfo_ItemCode as InvCode,
  54. Case When A2.SplitFlag=2 Then (SELECT ArriveQtyCU FROM {1}.dbo.PM_RcvLine
  55. WHERE Receivement=A1.ID AND DocLineNo=A2.DocLineNo AND SplitFlag=1)
  56. ELSE A2.ArriveQtyCU END as Quantity ,
  57. 0 as Amount,
  58. '' as Currency,
  59. '' as UnitPrice,
  60. A1.CreatedBy as CreatePerson,
  61. A1.CreatedOn as CreateDateTime,
  62. 1 as Status,
  63. ISNULL(A2.ModifiedOn ,A2.CreatedOn) as MTIME,
  64. A1.ID as DNID,
  65. isnull(A2.SrcDoc_SrcDoc_EntityID,'') as POID,
  66. isnull(A2.SrcDoc_SrcDocLine_EntityID,'') as PODetailID,
  67. cast(A2.ID as varchar(100)) as DNDetailID,
  68. isnull(A7.Code,'') ProjectCode,isnull(A2.InvLotCode,'') cBatch,'' version ,'' brand,
  69. '' as cFree1,
  70. '' as cFree2,
  71. '' as cFree3,
  72. '' as cFree4,
  73. '' as cFree5,
  74. '' as cFree6,
  75. '' as cFree7,
  76. '' as cFree8,
  77. '' as cFree9,
  78. '' as cFree10,CAST('' AS NVARCHAR(500)) as Colspan,
  79. A6.Code as WorkPoint,
  80. b.cWhCode as WHCode
  81. INTO #ICSDelive
  82. FROM {1}.dbo.PM_Receivement A1
  83. LEFT JOIN {1}.dbo.PM_RcvLine A2 ON A2.Receivement=A1.ID
  84. LEFT JOIN {1}.dbo.CBO_Department A3 ON A2.RcvDept=A3.ID
  85. LEFT JOIN {1}.dbo.CBO_Department_Trl A4 ON A4.ID = A3.ID AND A4.SysMLFlag='zh-CN'
  86. LEFT JOIN {1}.dbo.CBO_Wh A5 ON A2.Wh=A5.ID
  87. LEFT JOIN {1}.dbo.Base_Organization A6 ON A1.Org=A6.ID
  88. LEFT JOIN {1}.dbo.CBO_Project A7 ON A7.ID=A2.Project
  89. LEFT JOIN {1}.dbo.PM_RcvDocType A8 ON A1.RcvDocType=A8.ID
  90. WHERE (A1.ReceivementType=0 OR A1.ReceivementType=1) AND A2.Status='3'
  91. AND (A2.QCQCConclusion=-1 OR A2.QCQCConclusion=0 OR A2.QCQCConclusion=1 OR A2.QCQCConclusion=5
  92. OR A2.QCQCConclusion=6 OR A2.QCQCConclusion=7 OR A2.QCQCConclusion=12)
  93. and ISNULL(A2.ModifiedOn ,A2.CreatedOn)>=@LastTime
  94. update #ICSDelive 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,'')
  95. select distinct
  96. Colspan,ProjectCode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,WorkPoint,cBatch,version ,brand
  97. INTO #TempExtension
  98. from #ICSDelive
  99. IF NOT EXISTS(SELECT * FROM #ICSDelive)
  100. RETURN";
  101. sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#ICSDelive");
  102. Dictionary<string, string> value = new Dictionary<string, string>();
  103. value.Add("ID", "NEWID()");
  104. value.Add("BatchCode", "a.cBatch");
  105. value.Add("version", "a.version");
  106. value.Add("brand", "a.brand");
  107. value.Add("ProjectCode", "a.ProjectCode");
  108. value.Add("cFree1", "a.cFree1");
  109. value.Add("cFree2", "a.cFree2");
  110. value.Add("cFree3", "a.cFree3");
  111. value.Add("cFree4", "a.cFree4");
  112. value.Add("cFree5", "a.cFree5");
  113. value.Add("cFree6", "a.cFree6");
  114. value.Add("cFree7", "a.cFree7");
  115. value.Add("cFree8", "a.cFree8");
  116. value.Add("cFree9", "a.cFree9");
  117. value.Add("cFree10", "a.cFree10");
  118. value.Add("Colspan", "a.Colspan");
  119. value.Add("MTIME", "GETDATE()");
  120. value.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  121. value.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  122. value.Add("WorkPoint", "'" + WorkPoint + "'");
  123. //插入新增数据->ICSExtension表
  124. sql += ICSHelper.InsertSQL("ICSExtension", value)
  125. + @"#TempExtension a
  126. 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
  127. WHERE b.Colspan IS NULL";
  128. sql += @"--删除数据
  129. --DELETE ICSDeliveryNotice WHERE DNDetailID NOT IN ( SELECT AutoID FROM {1}.dbo.PU_ArrivalVouchs)and ICSDeliveryNotice.EATTRIBUTE1='1' and ASNCode is NULL
  130. SELECT a.DNDetailID INTO #tableICSDeliveryNotice
  131. FROM ICSDeliveryNotice a With(NoLock)
  132. LEFT JOIN {1}.dbo.[PM_RcvLine] b With(NoLock) ON a.DNDetailID=b.ID
  133. WHERE a.WorkPoint='{0}' and b.ID IS NULL and a.EATTRIBUTE1='1' and a.ASNCode is NULL
  134. DELETE ICSDeliveryNotice
  135. WHERE WorkPoint='{0}' and DNDetailID IN ( SELECT DNDetailID from #tableICSDeliveryNotice)
  136. ";
  137. Dictionary<string, string> values = new Dictionary<string, string>();
  138. values.Add("DNCode", "a.DNCode");
  139. values.Add("Sequence", "a.Sequence");
  140. values.Add("VenCode", "a.VenCode");
  141. values.Add("DepCode", "a.DepCode");
  142. values.Add("DNType", "a.DNType");
  143. values.Add("InvCode", "a.InvCode");
  144. values.Add("Quantity", "abs(a.Quantity)");
  145. values.Add("UnitPrice", "a.UnitPrice ");
  146. values.Add("Currency", "a.Currency");
  147. values.Add("Status", "a.Status");
  148. values.Add("WHCode", "a.WHCode");
  149. values.Add("CreatePerson", "a.CreatePerson");
  150. values.Add("CreateDateTime", "a.CreateDateTime");
  151. values.Add("POID", "a.POID");
  152. values.Add("PODetailID", "a.PODetailID");
  153. values.Add("DNDetailID", "a.DNDetailID");
  154. values.Add("DNID", "a.DNID");
  155. values.Add("ExtensionID", "c.ID");
  156. values.Add("EATTRIBUTE1", "''");
  157. //更新存在数据
  158. sql += ICSHelper.UpdateSQL("ICSDeliveryNotice", values)
  159. + @" #ICSDelive a
  160. INNER JOIN ICSDeliveryNotice b ON a.DNDetailID=b.DNDetailID and a.WorkPoint=b.WorkPoint
  161. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  162. WHERE a.WorkPoint='" + WorkPoint + "' ";
  163. values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  164. values.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  165. values.Add("WorkPoint", "'" + WorkPoint + "'");
  166. values.Add("MTIME", "a.MTIME");
  167. values.Add("ID", "NEWID()");
  168. values.Add("RCVQuantity", "0");
  169. //values.Add("ErpAutoID", "a.ErpAutoID");
  170. //values.Add("ErpID", "a.ErpID");
  171. //values.Add("iFQuantity", "a.iFQuantity");
  172. //插入新增数据
  173. sql += ICSHelper.InsertSQL("ICSDeliveryNotice", values)
  174. + @" #ICSDelive a
  175. LEFT JOIN ICSDeliveryNotice b ON a.DNDetailID=b.DNDetailID and a.WorkPoint=b.WorkPoint
  176. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  177. WHERE b.DNDetailID IS NULL and a.WorkPoint='" + WorkPoint + "' ";
  178. sql += "DROP TABLE #ICSDelive ";
  179. sql += " DROP TABLE #TempExtension " +
  180. " DROP TABLE #tableICSDeliveryNotice";
  181. sql = string.Format(sql, WorkPoint, erpName);
  182. ICSHelper.ExecuteDate(conStr, sql);
  183. #endregion
  184. }
  185. }
  186. catch (Exception ex)
  187. {
  188. log.Error(ex.ToString());
  189. }
  190. }
  191. }
  192. }