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

221 lines
13 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 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 (b.ID AS VARCHAR(50)) AS PODetailID,
  49. b.POID AS POID,
  50. a.cPOID AS POCode,
  51. isnull(ss.AutoID ,'')as PRDetailID,
  52. isnull(ss.ID,'') as PRID,
  53. CAST (b.ivouchrowno AS VARCHAR(50)) AS PORow,
  54. a.dPODate AS PODate,
  55. a.cVenCode AS VenCode,
  56. v.cVenName AS VenName,
  57. a.cPersonCode AS PersonCode,
  58. p.cPersonName AS PersonName,
  59. a.cDepCode AS GroupCode,
  60. d.cDepName AS GroupName,
  61. a.cexch_name AS CurrencyName,
  62. a.cPTCode AS SubjectCode,
  63. pt.cPTName AS ProjectType,
  64. b.cInvCode AS InvCode,
  65. b.iQuantity AS Quantity,
  66. a.cMaker AS CreatePerson,
  67. a.cmaketime as CreateDateTime,
  68. b.dArriveDate AS PreArriveDate,
  69. '2' AS Status,
  70. isnull(b.iNum,0) AS Amount,
  71. '1' AS POType,
  72. 'BS' AS ServerModel,
  73. b.iTaxPrice AS Price,
  74. '' AS ORDERNO,
  75. ISNULL(b.cbCloseTime,ISNULL(a.cChangAuditTime,ISNULL(a.cAuditTime, ISNULL(a.cModifyTime, a.cmaketime)))) AS MTIME,
  76. '' AS MEMO,
  77. a.cDefine1 AS WHCode,
  78. a.cdefine13 AS ReMark,
  79. a.cMaker AS DateTime1User,
  80. a.dPODate AS DateTime1,
  81. a.cVerifier AS DateTime2User,
  82. a.cAuditDate AS DateTime2,
  83. CASE WHEN b.cbCloseTime IS NULL THEN '' ELSE '' END AS ISUrgent,
  84. --(CASE WHEN inv.bCheckBatch=1 THEN isnull(b.cBatch,'') ELSE '' END) as cBatch,
  85. isnull(b.cItemCode,'') ProjectCode,'' cBatch,'' version ,'' brand,
  86. (case WHEN inv.bFree1=1 THEN b.cFree1 ELSE '' END) as cFree1,
  87. (case WHEN inv.bFree2=1 THEN b.cFree2 ELSE '' END) as cFree2,
  88. (case WHEN inv.bFree3=1 THEN b.cFree3 ELSE '' END) as cFree3,
  89. (case WHEN inv.bFree4=1 THEN b.cFree4 ELSE '' END) as cFree4,
  90. (case WHEN inv.bFree5=1 THEN b.cFree5 ELSE '' END) as cFree5,
  91. (case WHEN inv.bFree6=1 THEN b.cFree6 ELSE '' END) as cFree6,
  92. (case WHEN inv.bFree7=1 THEN b.cFree7 ELSE '' END) as cFree7,
  93. (case WHEN inv.bFree8=1 THEN b.cFree8 ELSE '' END) as cFree8,
  94. (case WHEN inv.bFree9=1 THEN b.cFree9 ELSE '' END) as cFree9,
  95. (case WHEN inv.bFree10=1 THEN b.cFree10 ELSE '' END) as cFree10,CAST('' AS NVARCHAR(500)) as Colspan
  96. , '{0}' as WorkPoint
  97. INTO #TempPO
  98. FROM {1}.dbo.PO_Pomain a
  99. INNER JOIN {1}.dbo.PO_Podetails b ON a.POID = b.POID
  100. LEFT JOIN {1}.dbo.Vendor v ON a.cVenCode = v.cVenCode
  101. LEFT JOIN {1}.dbo.Department d ON a.cDepCode = d.cDepCode
  102. LEFT JOIN {1}.dbo.Person p ON a.cPersonCode = p.cPersonCode
  103. LEFT JOIN {1}.dbo.PurchaseType pt ON a.cPTCode = pt.cPTCode
  104. LEFT JOIN {1}.dbo.PayCondition pay ON a.cPayCode = pay.cPayCode
  105. LEFT JOIN {1}.dbo.Inventory inv ON b.cInvCode = inv.cInvCode
  106. LEFT JOIN {1}.dbo.ComputationUnit unit ON unit.cComunitCode = inv.cComUnitCode
  107. LEFT JOIN {1}.dbo.PU_AppVouchs ss on b.iAppIds=ss.AutoID
  108. where ISNULL(b.cbCloseTime,ISNULL(a.cChangAuditTime,ISNULL(a.cAuditTime, ISNULL(a.cModifyTime, a.cmaketime))))>=@LastTime
  109. and a.cVerifier IS NOT NULL
  110. 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,'')
  111. select distinct
  112. Colspan,ProjectCode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,WorkPoint,cBatch,version ,brand
  113. INTO #TempExtensionPO
  114. from #TempPO
  115. IF NOT EXISTS(SELECT * FROM #TempPO)
  116. RETURN";
  117. sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempPO");
  118. Dictionary<string, string> value = new Dictionary<string, string>();
  119. value.Add("ID", "NEWID()");
  120. value.Add("BatchCode", "a.cBatch");
  121. value.Add("version", "a.version");
  122. value.Add("brand", "a.brand");
  123. value.Add("ProjectCode", "a.ProjectCode");
  124. value.Add("cFree1", "a.cFree1");
  125. value.Add("cFree2", "a.cFree2");
  126. value.Add("cFree3", "a.cFree3");
  127. value.Add("cFree4", "a.cFree4");
  128. value.Add("cFree5", "a.cFree5");
  129. value.Add("cFree6", "a.cFree6");
  130. value.Add("cFree7", "a.cFree7");
  131. value.Add("cFree8", "a.cFree8");
  132. value.Add("cFree9", "a.cFree9");
  133. value.Add("cFree10", "a.cFree10");
  134. value.Add("Colspan", "a.Colspan");
  135. value.Add("MTIME", "GETDATE()");
  136. value.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  137. value.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  138. value.Add("WorkPoint", "'" + WorkPoint + "'");
  139. //插入新增数据->ICSExtension表
  140. sql += ICSHelper.InsertSQL("ICSExtension", value)
  141. + @"#TempExtensionPO a
  142. 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
  143. WHERE b.Colspan IS NULL";
  144. sql += @"--删除数据
  145. -- DELETE ICSPurchaseOrder WHERE PODetailID NOT IN (SELECT CAST(ID AS VARCHAR) FROM {1}.dbo.[PO_Podetails])
  146. SELECT a.PODetailID INTO #tableICSPurchaseOrder
  147. FROM ICSPurchaseOrder a With(NoLock)
  148. LEFT JOIN {1}.dbo.[PO_Podetails] b With(NoLock) ON a.PODetailID=CAST(b.ID AS VARCHAR)
  149. WHERE a.WorkPoint='{0}' and b.ID IS NULL
  150. DELETE ICSPurchaseOrder
  151. WHERE WorkPoint='{0}' and PODetailID IN ( SELECT PODetailID from #tableICSPurchaseOrder)
  152. ";
  153. Dictionary<string, string> values = new Dictionary<string, string>();
  154. values.Add("POID", "a.POID");
  155. values.Add("POCode", "a.POCode");//采购订单号
  156. values.Add("Sequence", "a.PORow");//采购订单行号
  157. values.Add("VenCode", "a.VenCode");//供应商代码
  158. values.Add("DepCode", "a.GroupCode");//部门代码
  159. values.Add("PersonCode", "a.PersonCode");//业务员代码
  160. values.Add("InvCode", "a.InvCode");//物料代码
  161. values.Add("Quantity", "a.Quantity");//数量
  162. values.Add("Amount", "a.Amount");//辅计量
  163. values.Add("UnitPrice", "a.Price");//价格
  164. values.Add("Currency", "a.CurrencyName");//币种
  165. values.Add("Status", "a.Status");
  166. values.Add("CreatePerson", "a.CreatePerson");//创建人
  167. values.Add("PlanArriveDate", "a.PreArriveDate");//预计到货日期
  168. values.Add("CreateDateTime", "a.CreateDateTime");//业务员名称
  169. values.Add("PRDetailID", "a.PRDetailID");
  170. values.Add("PRID", "a.PRID");
  171. values.Add("ExtensionID", "c.ID");
  172. values.Add("PODetailID", "a.PODetailID");
  173. //values.Add("POID", "a.POID");
  174. values.Add("MTIME", "a.MTIME");
  175. //更新存在数据
  176. sql += ICSHelper.UpdateSQL("b", values)
  177. + @" #TempPO a
  178. INNER JOIN ICSPurchaseOrder b ON a.PODetailID+a.POType=b.PODetailID+b.POType and a.WorkPoint=b.WorkPoint
  179. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  180. WHERE a.WorkPoint='" + WorkPoint + "' ";
  181. //values.Add("PODetailID", "a.Free2");
  182. //values.Add("POType", "a.POType");
  183. //values.Add("ServerModel", "a.ServerModel");
  184. values.Add("ID", "NEWID()");//ID
  185. values.Add("POType", "a.POType");//采购类型
  186. values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");//操作人
  187. values.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");//操作人姓名
  188. values.Add("WorkPoint", "'" + WorkPoint + "'");//站点
  189. values.Add("EATTRIBUTE1", "''");//站点
  190. values.Add("ReleaseState", "'0'");//发布状态
  191. //插入新增数据
  192. sql += ICSHelper.InsertSQL("ICSPurchaseOrder", values)
  193. + @" #TempPO a
  194. LEFT JOIN ICSPurchaseOrder b ON a.PODetailID=b.PODetailID and a.WorkPoint=b.WorkPoint
  195. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  196. WHERE b.PODetailID IS NULL and a.WorkPoint='" + WorkPoint + @"' ";
  197. sql += " DROP TABLE #TempPO";
  198. sql += " DROP TABLE #TempExtensionPO " +
  199. " DROP TABLE #tableICSPurchaseOrder ";
  200. sql = string.Format(sql, WorkPoint, erpName);
  201. ICSHelper.ExecuteDate(conStr, sql);
  202. #endregion
  203. }
  204. }
  205. catch (Exception ex)
  206. {
  207. log.Error(ex.ToString());
  208. }
  209. }
  210. }
  211. }