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.

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