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

218 lines
13 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years 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 ICSMo : 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 A.ID as ID,
  49. A.DocNO as MOCODE,
  50. 1 as Sequence,
  51. A7.Code as InvCode,
  52. CASE WHEN A.IsStartMO != 1 THEN
  53. (SELECT TOP 1 TotalStartQty FROM {1}.dbo.MO_MOStartInfo WHERE MO=A.ID ORDER BY StartDatetime DESC)
  54. ELSE (SELECT TOP 1 ActualStartQty FROM {1}.dbo.MO_MOStartInfo WHERE MO=A.ID ORDER BY StartDatetime DESC) END as Quantity,
  55. A.CreatedBy as CreatePerson,
  56. A.CreatedOn as CreateDateTime,
  57. 0 as Amount,
  58. 0 as RCVQuantity,
  59. A13.Code as DepCode,
  60. A22.Code as WHCode,
  61. '1' as MOStatus,
  62. A2.Description as MOMemo,
  63. '2' as ERPStatus,
  64. A.StartDate as StartDate,
  65. A.CompleteDate as DueDate,
  66. A.ID as MOID,
  67. A.ID as MODetailID,
  68. ISNULL(A.ModifiedOn,A.BusinessCreatedOn) as MTIME,
  69. isnull(A11.Code,'') ProjectCode,isnull(A19.LotCode,'') 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. ,A12.Code as WorkPoint
  81. ,'0' AS EATTRIBUTE1
  82. INTO #TempMO
  83. from {1}.dbo.MO_MO as A
  84. left join {1}.dbo.[MO_MOMemo] as A1 on (A.[ID] = A1.[MO])
  85. left join {1}.dbo.[MO_MOMemo_Trl] as A2 on (A2.SysMlFlag = 'zh-CN') and (A1.[ID] = A2.[ID])
  86. left join {1}.dbo.[MO_MODocType] as A3 on (A.[MODocType] = A3.[ID])
  87. left join {1}.dbo.[MO_MODocType_Trl] as A4 on (A4.SysMlFlag = 'zh-CN') and (A3.[ID] = A4.[ID])
  88. left join {1}.dbo.[MO_MOTaskPlan] as A5 on (A.[ID] = A5.[MO])
  89. left join {1}.dbo.[Base_User] as A6 on (A.ModifiedBy = A6.Code)
  90. left join {1}.dbo.[CBO_ItemMaster] as A7 on (A.[ItemMaster] = A7.[ID])
  91. left join {1}.dbo.[MO_MO_Trl] as A8 on (A8.SysMlFlag = 'zh-CN') and (A.[ID] = A8.[ID])
  92. left join {1}.dbo.[CBO_BOMVersion] as A9 on (A.[BOMVersion] = A9.[ID])
  93. left join {1}.dbo.[CBO_ItemMaster_Trl] as A10 on (A10.SysMlFlag = 'zh-CN') and (A7.[ID] = A10.[ID])
  94. left join {1}.dbo.[CBO_Project] as A11 on (A.[Project] = A11.[ID])
  95. left join {1}.dbo.[Base_Organization] as A12 on (A.[Org] = A12.[ID])
  96. left join {1}.dbo.[CBO_Department] as A13 on (A.[Department] = A13.[ID])
  97. left join {1}.dbo.[CBO_Department_Trl] as A14 on (A14.SysMlFlag = 'zh-CN') and (A13.[ID] = A14.[ID])
  98. left join {1}.dbo. MO_MO As A16 on A16.[MODocType]=1001603110990023 and A.[Project]=A16.Project and A.ItemMaster=A16.ItemMaster
  99. left join {1}.dbo.[CBO_ItemMaster] as A17 on (A16.[ItemMaster] = A17.[ID])
  100. left join {1}.dbo.[CBO_ItemMaster_Trl] as A18 on (A18.SysMlFlag = 'zh-CN') and (A17.[ID] = A16.[ID])
  101. left join {1}.dbo.[MO_MOLot] as A19 on (A16.[ID] = A19.[MO])
  102. left join (select * from (select ROW_NUMBER() over( partition by dept order by code desc ) row,* from {1}.dbo.CBO_Operators with(nolock) ) bb
  103. where bb.row='1' )as A20 on A20.dept=A.Department and a20.Org=a.Org
  104. left join {1}.dbo.[MO_IssueDoc] as A21 on A21.[SourceDoc_SrcDocNo]=A.DocNo
  105. LEFT JOIN {1}.dbo.[CBO_Wh] AS A22 ON A22.ID=A.CompleteWh
  106. where (A.DocState=2 OR A.DocState=3) and A.[BusinessType] != 50 and (A.[BusinessType] != 52)
  107. and (A.[BusinessType] != 2417) and (A.[IsMultiRouting] != 1) and A.[ItemMaster] is not null --and (A.[IsStartMO] != 1)
  108. and ISNULL(A.ModifiedOn,A.BusinessCreatedOn)>=@LastTime AND A12.Code='{0}'
  109. update #TempMO 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,'')
  110. select distinct
  111. Colspan,ProjectCode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,WorkPoint,cBatch,version ,brand
  112. INTO #TempExtensionMO
  113. from #TempMO
  114. IF NOT EXISTS(SELECT * FROM #TempMO)
  115. RETURN";
  116. sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempMO");
  117. Dictionary<string, string> value = new Dictionary<string, string>();
  118. value.Add("ID", "NEWID()");
  119. value.Add("BatchCode", "a.cBatch");
  120. value.Add("version", "a.version");
  121. value.Add("brand", "a.brand");
  122. value.Add("ProjectCode", "a.ProjectCode");
  123. value.Add("cFree1", "a.cFree1");
  124. value.Add("cFree2", "a.cFree2");
  125. value.Add("cFree3", "a.cFree3");
  126. value.Add("cFree4", "a.cFree4");
  127. value.Add("cFree5", "a.cFree5");
  128. value.Add("cFree6", "a.cFree6");
  129. value.Add("cFree7", "a.cFree7");
  130. value.Add("cFree8", "a.cFree8");
  131. value.Add("cFree9", "a.cFree9");
  132. value.Add("cFree10", "a.cFree10");
  133. value.Add("Colspan", "a.Colspan");
  134. value.Add("MTIME", "GETDATE()");
  135. value.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  136. value.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  137. value.Add("WorkPoint", "'" + WorkPoint + "'");
  138. //插入新增数据->ICSExtension表
  139. sql += ICSHelper.InsertSQL("ICSExtension", value)
  140. + @"#TempExtensionMO a
  141. 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
  142. WHERE b.Colspan IS NULL";
  143. sql += @"--删除数据
  144. -- DELETE ICSMO WHERE MODetailID NOT IN (SELECT CAST(MoDId AS VARCHAR) FROM {1}.dbo.[mom_orderdetail])
  145. SELECT a.MODetailID INTO #tableICSMO
  146. FROM ICSMO a With(NoLock)
  147. LEFT JOIN {1}.dbo.[MO_MO] b With(NoLock) ON a.MODetailID=b.ID
  148. WHERE a.WorkPoint='{0}' and b.ID IS NULL
  149. DELETE ICSMO
  150. WHERE WorkPoint='{0}' and MODetailID IN ( SELECT MODetailID from #tableICSMO)
  151. ";
  152. Dictionary<string, string> values = new Dictionary<string, string>();
  153. values.Add("MOCODE", "a.MOCODE");//工单号
  154. values.Add("Sequence", "a.Sequence");//工单行号
  155. values.Add("InvCode", "a.InvCode");//物料代码
  156. values.Add("Quantity", "a.Quantity");//数量
  157. values.Add("Amount", "a.Amount");//辅机数量
  158. values.Add("DepCode", "a.DepCode");//辅机数量
  159. values.Add("WHCode", "a.WHCode");//仓库
  160. values.Add("StartDate", "a.StartDate");//开工日
  161. values.Add("DueDate", "a.DueDate");//完工日
  162. values.Add("MOSTATUS", "a.MOStatus");//生产订单状态
  163. values.Add("MOMemo", "a.MOMemo");//生产订单状态
  164. values.Add("CreatePerson", "a.CreatePerson");//生产订单状态
  165. values.Add("CreateDateTime", "a.CreateDateTime");//生产订单状态
  166. values.Add("ERPStatus", "a.ERPStatus");//生产订单状态
  167. // values.Add("ERPStatus", "a.MOSTATUS");//生产订单状态
  168. values.Add("MOID", "a.MoId");//生产订单状态
  169. values.Add("MODetailID", "a.MODetailID");//生产订单状态
  170. values.Add("ExtensionID", "c.ID");//生产订单状态
  171. //values.Add("MODetailID", "a.MOSTATUS");//生产订单状态
  172. // values.Add("ExtensionID", "a.MOSTATUS");//生产订单状态
  173. values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  174. values.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  175. values.Add("WorkPoint", "'" + WorkPoint + "'");
  176. values.Add("MTIME", "a.MTIME");//生产订单状态
  177. values.Add("EATTRIBUTE1", "a.EATTRIBUTE1");
  178. //更新存在数据
  179. sql += ICSHelper.UpdateSQL("b", values)
  180. + @" #TempMO a
  181. INNER JOIN ICSMO b ON a.MODetailID=b.MODetailID and a.WorkPoint=b.WorkPoint
  182. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  183. WHERE a.WorkPoint='" + WorkPoint + "' ";
  184. values.Add("ID", "NEWID()");
  185. values.Add("RCVQuantity", "a.RCVQuantity");//辅机数量
  186. //插入新增数据
  187. sql += ICSHelper.InsertSQL("ICSMO", values)
  188. + @" #TempMO a
  189. LEFT JOIN ICSMO b ON a.MODetailID=b.MODetailID and a.WorkPoint=b.WorkPoint
  190. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  191. WHERE b.ID IS NULL and a.WorkPoint='" + WorkPoint + @"'
  192. ";
  193. sql += "DROP TABLE #TempMO";
  194. sql += " DROP TABLE #TempExtensionMO " +
  195. " DROP TABLE #tableICSMO ";
  196. sql = string.Format(sql, WorkPoint, erpName);
  197. ICSHelper.ExecuteDate(conStr, sql);
  198. #endregion
  199. }
  200. }
  201. catch (Exception ex)
  202. {
  203. log.Error(ex.ToString());
  204. }
  205. }
  206. }
  207. }