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.

236 lines
14 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 ICSPO : 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. CAST (A1.ID AS VARCHAR(50)) AS PODetailID,
  49. A.ID AS POID,
  50. A.DocNO AS POCode,
  51. isnull(A1.SrcDocInfo_SrcDoc_EntityID ,'')as PRDetailID,
  52. isnull(A1.SrcDocInfo_SrcDocLine_EntityID,'') as PRID,
  53. CAST (A1.DocLineNo AS VARCHAR(50)) AS PORow,
  54. A.BusinessDate AS PODate,
  55. A2.Code AS VenCode,
  56. A3.Name AS VenName,
  57. A6.Code AS PersonCode,
  58. A7.Name AS PersonName,
  59. A8.Code AS GroupCode,
  60. A9.Name AS GroupName,
  61. A5.Name AS CurrencyName,
  62. '' AS SubjectCode,
  63. '' AS ProjectType,
  64. A1.ItemInfo_ItemCode AS InvCode,
  65. A1.ReqQtyTU AS Quantity,
  66. A.CreatedBy AS CreatePerson,
  67. A.CreatedOn as CreateDateTime,
  68. A20.PlanArriveDate AS PreArriveDate,
  69. '2' AS Status,
  70. 0 AS Amount,
  71. '1' AS POType,
  72. 'BS' AS ServerModel,
  73. A1.NetFinallyPriceTC AS Price,
  74. ISNULL(A28.Code,'') AS ORDERNO,
  75. ISNULL(A1.ModifiedOn ,A1.CreatedOn) AS MTIME,
  76. A16.Description AS MEMO,
  77. '' AS WHCode,
  78. A1.DescFlexSegments_PubDescSeg4 AS ReMark,
  79. A.CreatedBy AS DateTime1User,
  80. A.BusinessDate AS DateTime1,
  81. A.ApprovedBy AS DateTime2User,
  82. A.ApprovedOn AS DateTime2,
  83. A.POPriority AS ISUrgent,
  84. --(CASE WHEN inv.bCheckBatch=1 THEN isnull(b.cBatch,'') ELSE '' END) as cBatch,
  85. isnull(A28.Code,'') ProjectCode,'' cBatch,'' version ,'' brand,
  86. '' as cFree1,
  87. '' as cFree2,
  88. '' as cFree3,
  89. '' as cFree4,
  90. '' as cFree5,
  91. '' as cFree6,
  92. '' as cFree7,
  93. '' as cFree8,
  94. '' as cFree9,
  95. '' as cFree10,CAST('' AS NVARCHAR(500)) as Colspan
  96. ,A30.Code as WorkPoint
  97. INTO #TempPO
  98. FROM {1}.dbo.PM_PurchaseOrder A
  99. inner join {1}.dbo.PM_POLine A1 on A.ID=A1.PurchaseOrder
  100. left join {1}.dbo.CBO_Supplier A2 on A.Supplier_Supplier=A2.ID
  101. left join {1}.dbo.CBO_Supplier_Trl A3 on (A3.SysMLFlag='zh-CN') and (A2.ID=A3.ID)
  102. left join {1}.dbo.Base_Currency A4 on (A.TC = A4.[ID])
  103. left join {1}.dbo.[Base_Currency_Trl] A5 on (A5.SysMlFlag = 'zh-CN') and (A4.[ID] = A5.[ID])
  104. left join {1}.dbo.[CBO_Operators] as A6 on (A.PurOper = A6.[ID])
  105. left join {1}.dbo.[CBO_Operators_Trl] as A7 on (A7.SysMlFlag = 'zh-CN') and (A6.[ID] = A7.[ID])
  106. left join {1}.dbo.CBO_Department as A8 on (A.PurDept = A8.[ID])
  107. left join {1}.dbo.[CBO_Department_Trl] as A9 on (A9.SysMlFlag = 'zh-CN') and (A8.[ID] = A9.[ID])
  108. left join {1}.dbo.PM_PODocType A12 on A.DocumentType=A12.ID
  109. left join {1}.dbo.PM_PODocType_Trl A13 on A12.ID=A13.ID and A13.SysMLFlag='zh-CN'
  110. left join {1}.dbo.Base_User A14 on A14.Name=A.CreatedBy
  111. LEFT JOIN {1}.dbo.PM_POMemo A15 ON A15.PurchaseOrder=A.ID
  112. LEFT JOIN {1}.dbo.PM_POMemo_Trl A16 ON A15.ID=A16.ID AND A16.SysMLFlag='zh-CN'
  113. left join {1}.dbo.[CBO_ItemMaster] as A22 on (A1.[ItemInfo_ItemID] = A22.[ID])
  114. left join {1}.dbo.[Base_UOM] as A24 on (A1.TradeUOM = A24.[ID])
  115. left join {1}.dbo.[Base_UOM_Trl] as A25 on (A25.SysMlFlag = 'zh-CN') and (A24.[ID] = A25.[ID])
  116. left join {1}.dbo.[Base_UOM] as A26 on (A1.PriceUOM = A26.[ID])
  117. left join {1}.dbo.[Base_UOM_Trl] as A27 on (A27.SysMlFlag = 'zh-CN') and (A26.[ID] = A27.[ID])
  118. left join {1}.dbo.CBO_Project as A28 on A1.Project =A28.ID
  119. left join {1}.dbo.CBO_Project_Trl as A29 on (A29.SysMLFlag='zh-CN') and (A28.ID=A29.ID)
  120. LEFT JOIN (SELECT POLine,MAX(PlanArriveDate) AS PlanArriveDate FROM {1}.dbo.PM_POShipLine GROUP BY POLine) A20 ON A1.ID=A20.POLine
  121. LEFT JOIN {1}.dbo.Base_Organization A30 ON A.Org=A30.ID
  122. where A.Status='2' AND A30.Code='{0}' and ISNULL(A1.ModifiedOn ,A1.CreatedOn)>=@LastTime
  123. update #TempPO 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,'')
  124. select distinct
  125. Colspan,ProjectCode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,WorkPoint,cBatch,version ,brand
  126. INTO #TempExtensionPO
  127. from #TempPO
  128. IF NOT EXISTS(SELECT * FROM #TempPO)
  129. RETURN";
  130. sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempPO");
  131. Dictionary<string, string> value = new Dictionary<string, string>();
  132. value.Add("ID", "NEWID()");
  133. value.Add("BatchCode", "a.cBatch");
  134. value.Add("version", "a.version");
  135. value.Add("brand", "a.brand");
  136. value.Add("ProjectCode", "a.ProjectCode");
  137. value.Add("cFree1", "a.cFree1");
  138. value.Add("cFree2", "a.cFree2");
  139. value.Add("cFree3", "a.cFree3");
  140. value.Add("cFree4", "a.cFree4");
  141. value.Add("cFree5", "a.cFree5");
  142. value.Add("cFree6", "a.cFree6");
  143. value.Add("cFree7", "a.cFree7");
  144. value.Add("cFree8", "a.cFree8");
  145. value.Add("cFree9", "a.cFree9");
  146. value.Add("cFree10", "a.cFree10");
  147. value.Add("Colspan", "a.Colspan");
  148. value.Add("MTIME", "GETDATE()");
  149. value.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  150. value.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  151. value.Add("WorkPoint", "'" + WorkPoint + "'");
  152. //插入新增数据->ICSExtension表
  153. sql += ICSHelper.InsertSQL("ICSExtension", value)
  154. + @"#TempExtensionPO a
  155. 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
  156. WHERE b.Colspan IS NULL";
  157. sql += @"--删除数据
  158. -- DELETE ICSPurchaseOrder WHERE PODetailID NOT IN (SELECT CAST(ID AS VARCHAR) FROM {1}.dbo.[PO_Podetails])
  159. SELECT a.PODetailID INTO #tableICSPurchaseOrder
  160. FROM ICSPurchaseOrder a With(NoLock)
  161. LEFT JOIN {1}.dbo.[PM_POLine] b With(NoLock) ON a.PODetailID=CAST(b.ID AS VARCHAR)
  162. WHERE a.WorkPoint='{0}' and b.ID IS NULL
  163. DELETE ICSPurchaseOrder
  164. WHERE WorkPoint='{0}' and PODetailID IN ( SELECT PODetailID from #tableICSPurchaseOrder)
  165. ";
  166. Dictionary<string, string> values = new Dictionary<string, string>();
  167. values.Add("POID", "a.POID");
  168. values.Add("POCode", "a.POCode");//采购订单号
  169. values.Add("Sequence", "a.PORow");//采购订单行号
  170. values.Add("VenCode", "a.VenCode");//供应商代码
  171. values.Add("DepCode", "a.GroupCode");//部门代码
  172. values.Add("PersonCode", "a.PersonCode");//业务员代码
  173. values.Add("InvCode", "a.InvCode");//物料代码
  174. values.Add("Quantity", "a.Quantity");//数量
  175. values.Add("Amount", "a.Amount");//辅计量
  176. values.Add("UnitPrice", "a.Price");//价格
  177. values.Add("Currency", "a.CurrencyName");//币种
  178. values.Add("Status", "a.Status");
  179. values.Add("CreatePerson", "a.CreatePerson");//创建人
  180. values.Add("PlanArriveDate", "a.PreArriveDate");//预计到货日期
  181. values.Add("CreateDateTime", "a.CreateDateTime");//业务员名称
  182. values.Add("PRDetailID", "a.PRDetailID");
  183. values.Add("PRID", "a.PRID");
  184. values.Add("ExtensionID", "c.ID");
  185. values.Add("PODetailID", "a.PODetailID");
  186. //values.Add("POID", "a.POID");
  187. values.Add("MTIME", "a.MTIME");
  188. //更新存在数据
  189. sql += ICSHelper.UpdateSQL("b", values)
  190. + @" #TempPO a
  191. INNER JOIN ICSPurchaseOrder b ON a.PODetailID+a.POType=b.PODetailID+b.POType and a.WorkPoint=b.WorkPoint
  192. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  193. WHERE a.WorkPoint='" + WorkPoint + "' ";
  194. //values.Add("PODetailID", "a.Free2");
  195. //values.Add("POType", "a.POType");
  196. //values.Add("ServerModel", "a.ServerModel");
  197. values.Add("ID", "NEWID()");//ID
  198. values.Add("POType", "a.POType");//采购类型
  199. values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");//操作人
  200. values.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");//操作人姓名
  201. values.Add("WorkPoint", "'" + WorkPoint + "'");//站点
  202. values.Add("EATTRIBUTE1", "''");//站点
  203. values.Add("ReleaseState", "'0'");//发布状态
  204. //插入新增数据
  205. sql += ICSHelper.InsertSQL("ICSPurchaseOrder", values)
  206. + @" #TempPO a
  207. LEFT JOIN ICSPurchaseOrder b ON a.PODetailID=b.PODetailID and a.WorkPoint=b.WorkPoint
  208. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  209. WHERE b.PODetailID IS NULL and a.WorkPoint='" + WorkPoint + @"' ";
  210. sql += " DROP TABLE #TempPO";
  211. sql += " DROP TABLE #TempExtensionPO " +
  212. " DROP TABLE #tableICSPurchaseOrder ";
  213. sql = string.Format(sql, WorkPoint, erpName);
  214. ICSHelper.ExecuteDate(conStr, sql);
  215. #endregion
  216. }
  217. }
  218. catch (Exception ex)
  219. {
  220. log.Error(ex.ToString());
  221. }
  222. }
  223. }
  224. }