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

256 lines
15 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 ICSSoDispatch : 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 * into #TempSO FROM (
  48. SELECT A1.DocNo AS SDNCode,
  49. CAST(A2.DocLineNo AS NVARCHAR(50)) AS Sequence,
  50. ISNULL(A2.SONo,'') AS SOCode,
  51. CAST(A2.SrcDocLineNo AS NVARCHAR(50)) AS SOSequence,
  52. A3.Code AS CusCode,
  53. A4.Name AS CusName,
  54. '1' AS Type,
  55. A5.Code AS InvCode,
  56. A7.Code AS WHCode,
  57. A2.ShipQtyInvAmount AS Quantity,
  58. 0 as Amount,
  59. A1.CreatedBy AS CreatePerson,
  60. A1.CreatedOn as CreateDateTime,
  61. '1' AS Status,
  62. CAST(A1.ID AS NVARCHAR(50)) AS SDNID,
  63. CAST(A2.ID AS NVARCHAR(50)) AS SDNDetailID,
  64. ISNULL(A2.ModifiedOn ,A2.CreatedOn) AS MTIME,
  65. isnull(A1.ShipConfirmDate,getdate()) AS ArriveDate,
  66. isnull(A13.Code,'') ProjectCode,isnull(A2.LotInfo_LotCode,'') cBatch, '' version ,'' brand,
  67. '' as cFree1,
  68. '' as cFree2,
  69. '' as cFree3,
  70. '' as cFree4,
  71. '' as cFree5,
  72. '' as cFree6,
  73. '' as cFree7,
  74. '' as cFree8,
  75. '' as cFree9,
  76. '' as cFree10,CAST('' AS NVARCHAR(500)) as Colspan,
  77. '' as EATTRIBUTE1
  78. ,A11.Code as WorkPoint
  79. FROM {1}.dbo.SM_Ship A1
  80. LEFT JOIN {1}.dbo.SM_ShipLine A2 ON A2.Ship=A1.ID
  81. LEFT JOIN {1}.dbo.CBO_Customer A3 ON A1.OrderBy_Customer=A3.ID
  82. LEFT JOIN {1}.dbo.CBO_Customer_Trl A4 ON A4.ID = A3.ID AND A4.SysMLFlag='zh-CN'
  83. LEFT JOIN {1}.dbo.CBO_ItemMaster A5 ON A2.ItemInfo_ItemID=A5.ID
  84. LEFT JOIN {1}.dbo.CBO_ItemMaster_Trl A6 ON A6.ID = A5.ID AND A6.SysMLFlag='zh-CN'
  85. LEFT JOIN {1}.dbo.CBO_Wh A7 ON A2.WH=A7.ID
  86. LEFT JOIN {1}.dbo.CBO_Wh_Trl A8 ON A8.ID = A7.ID AND A8.SysMLFlag='zh-CN'
  87. LEFT JOIN {1}.dbo.Base_UOM A9 ON A2.InvUom=A9.ID
  88. LEFT JOIN {1}.dbo.Base_UOM_Trl A10 ON A10.ID = A9.ID AND A10.SysMLFlag='zh-CN'
  89. LEFT JOIN {1}.dbo.Base_Organization A11 ON A1.Org=A11.ID
  90. LEFT JOIN {1}.dbo.Base_Organization_Trl A12 ON A12.ID = A11.ID AND A12.SysMLFlag='zh-CN'
  91. LEFT JOIN {1}.dbo.CBO_Project A13 ON A2.Project=A13.ID
  92. WHERE A1.Status=2
  93. UNION ALL
  94. SELECT A1.DocNo AS SDNCode,
  95. CAST(A2.DocLineNo AS NVARCHAR(10)) AS Sequence,
  96. A2.SrcDoc_SrcDocNo AS SOCode,
  97. CAST(A2.SrcDoc_SrcDocLineNo AS NVARCHAR(50)) AS SOSequence,
  98. A1.RtnCustomer_Code AS CusCode,
  99. A1.RtnCustomer_ShortName AS CusName,
  100. '2' AS Type,
  101. A2.ItemInfo_ItemCode AS InvCode,
  102. A6.Code AS WHCode,
  103. A2.ArriveQtyPU AS Quantity,
  104. 0 as Amount,
  105. A1.CreatedBy AS CreatePerson,
  106. A1.CreatedOn as CreateDateTime,
  107. '1' AS Status,
  108. CAST(A1.ID AS NVARCHAR(50)) AS SDNID,
  109. CAST(A2.ID AS NVARCHAR(50)) AS SDNDetailID,
  110. ISNULL(A2.ModifiedOn ,A2.CreatedOn) AS MTIME,
  111. A2.ArrivedTime AS ArriveDate,
  112. isnull(A12.Code,'') ProjectCode,isnull(A13.LotCode,'') cBatch, '' version ,'' brand,
  113. '' as cFree1,
  114. '' as cFree2,
  115. '' as cFree3,
  116. '' as cFree4,
  117. '' as cFree5,
  118. '' as cFree6,
  119. '' as cFree7,
  120. '' as cFree8,
  121. '' as cFree9,
  122. '' as cFree10,CAST('' AS NVARCHAR(500)) as Colspan,
  123. '' as EATTRIBUTE1
  124. ,A10.Code as WorkPoint
  125. FROM {1}.dbo.PM_Receivement A1
  126. LEFT JOIN {1}.dbo.PM_RcvLine A2 ON A2.Receivement=A1.ID
  127. LEFT JOIN {1}.dbo.PM_RcvLine_Trl A3 ON A2.ID=A3.ID AND A3.SysMLFlag='zh-CN'
  128. LEFT JOIN {1}.dbo.PM_RcvDocType A4 ON A1.RcvDocType=A4.ID
  129. LEFT JOIN {1}.dbo.PM_RcvDocType_Trl A5 ON A5.ID = A4.ID AND A5.SysMLFlag='zh-CN'
  130. LEFT JOIN {1}.dbo.CBO_Wh A6 ON A2.Wh=A6.ID
  131. LEFT JOIN {1}.dbo.CBO_Wh_Trl A7 ON A7.ID = A6.ID AND A7.SysMLFlag='zh-CN'
  132. LEFT JOIN {1}.dbo.Base_UOM A8 ON A2.PriceUOM=A8.ID
  133. LEFT JOIN {1}.dbo.Base_UOM_Trl A9 ON A9.ID = A8.ID AND A9.SysMLFlag='zh-CN'
  134. LEFT JOIN {1}.dbo.Base_Organization A10 ON A1.Org=A10.ID
  135. LEFT JOIN {1}.dbo.Base_Organization_Trl A11 ON A11.ID = A10.ID AND A11.SysMLFlag='zh-CN'
  136. LEFT JOIN {1}.dbo.CBO_Project A12 ON A2.Project=A12.ID
  137. LEFT JOIN {1}.dbo.Lot_LotMaster A13 ON A2.InvLot=A13.ID
  138. WHERE A1.ReceivementType=2 AND A1.Status=3
  139. ) t
  140. where t.MTIME>=@LastTime
  141. update #TempSO 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,'')
  142. select distinct
  143. Colspan,ProjectCode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,WorkPoint,cBatch,version ,brand
  144. INTO #TempExtension
  145. from #TempSO
  146. IF NOT EXISTS(SELECT * FROM #TempSO)
  147. RETURN";
  148. sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempSO");
  149. Dictionary<string, string> value = new Dictionary<string, string>();
  150. value.Add("ID", "NEWID()");
  151. value.Add("BatchCode", "a.cBatch");
  152. value.Add("version", "a.version");
  153. value.Add("brand", "a.brand");
  154. value.Add("ProjectCode", "a.ProjectCode");
  155. value.Add("cFree1", "a.cFree1");
  156. value.Add("cFree2", "a.cFree2");
  157. value.Add("cFree3", "a.cFree3");
  158. value.Add("cFree4", "a.cFree4");
  159. value.Add("cFree5", "a.cFree5");
  160. value.Add("cFree6", "a.cFree6");
  161. value.Add("cFree7", "a.cFree7");
  162. value.Add("cFree8", "a.cFree8");
  163. value.Add("cFree9", "a.cFree9");
  164. value.Add("cFree10", "a.cFree10");
  165. value.Add("Colspan", "a.Colspan");
  166. value.Add("MTIME", "GETDATE()");
  167. value.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  168. value.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  169. value.Add("WorkPoint", "'" + WorkPoint + "'");
  170. //插入新增数据->ICSExtension表
  171. sql += ICSHelper.InsertSQL("ICSExtension", value)
  172. + @"#TempExtension a
  173. 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
  174. WHERE b.Colspan IS NULL";
  175. sql += @"--删除数据
  176. -- DELETE ICSSDN WHERE SDNDetailID NOT IN (SELECT CAST(AutoID as VARCHAR) FROM {1}.dbo.DispatchLists)
  177. SELECT * INTO #tableICSSDN FROM(
  178. SELECT a.SDNDetailID FROM ICSSDN a With(NoLock)
  179. LEFT JOIN {1}.dbo.[SM_ShipLine] b With(NoLock) ON a.SDNDetailID=CAST(b.ID as VARCHAR)
  180. WHERE a.WorkPoint='{0}' and b.ID IS NULL
  181. UNION ALL
  182. SELECT a.SDNDetailID FROM ICSSDN a With(NoLock)
  183. LEFT JOIN {1}.dbo.[PM_RcvLine] b With(NoLock) ON a.SDNDetailID=CAST(b.ID as VARCHAR)
  184. WHERE a.WorkPoint='{0}' and b.ID IS NULL
  185. )
  186. DELETE ICSSDN
  187. WHERE WorkPoint='{0}' and SDNDetailID IN ( SELECT SDNDetailID from #tableICSSDN)
  188. ";
  189. Dictionary<string, string> values = new Dictionary<string, string>();
  190. values.Add("SDNCode", "a.SDNCode");
  191. values.Add("Sequence", "a.Sequence");
  192. values.Add("SOCode", "a.SOCode");
  193. values.Add("SOSequence", "a.SOSequence");
  194. values.Add("CusCode", "a.CusCode");
  195. values.Add("CusName", "a.CusName");
  196. values.Add("Type", "a.Type");
  197. values.Add("InvCode", "a.InvCode");
  198. values.Add("WHCode", "a.WHCode");
  199. values.Add("ArriveDate", "a.ArriveDate");
  200. values.Add("Quantity", "a.Quantity");
  201. values.Add("EATTRIBUTE1", "a.EATTRIBUTE1");
  202. values.Add("Amount", "a.Amount");
  203. values.Add("CreatePerson", "a.CreatePerson");
  204. values.Add("CreateDateTime", "a.CreateDateTime");
  205. values.Add("Status", "a.Status");
  206. values.Add("SDNID", "a.SDNID");
  207. values.Add("ExtensionID", "c.ID");
  208. values.Add("SDNDetailID", "a.SDNDetailID");
  209. //更新存在数据
  210. sql += ICSHelper.UpdateSQL("ICSSDN", values)
  211. + @" #TempSO a
  212. INNER JOIN ICSSDN b ON a.SDNDetailID=b.SDNDetailID and a.WorkPoint=b.WorkPoint
  213. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  214. WHERE a.WorkPoint='" + WorkPoint + "' ";
  215. values.Add("MTIME", "a.MTIME");
  216. values.Add("ID", "NEWID()");
  217. values.Add("SDNQuantity", "0");
  218. values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  219. values.Add("MUSERNAME", "'" + ConstWorkPoint.Musername + "'");
  220. values.Add("WorkPoint", "'" + WorkPoint + "'");
  221. //插入新增数据
  222. sql += ICSHelper.InsertSQL("ICSSDN", values)
  223. + @" #TempSO a
  224. LEFT JOIN ICSSDN b ON a.SDNDetailID=b.SDNDetailID and a.WorkPoint=b.WorkPoint
  225. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  226. WHERE b.SDNDetailID IS NULL and a.WorkPoint='" + WorkPoint + @"'
  227. ";
  228. sql += " DROP TABLE #TempSO";
  229. sql += " DROP TABLE #TempExtension" +
  230. " DROP TABLE #tableICSSDN";
  231. sql = string.Format(sql, WorkPoint, erpName);
  232. ICSHelper.ExecuteDate(conStr, sql);
  233. #endregion
  234. }
  235. }
  236. catch (Exception ex)
  237. {
  238. log.Error(ex.ToString());
  239. }
  240. }
  241. }
  242. }