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

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