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.

221 lines
13 KiB

4 months ago
4 months ago
4 months ago
4 months ago
3 months ago
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 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 TenantId = dr["TenantId"].ToString();//mes 组织
  44. string TenantCode = dr["TenantCode"].ToString();
  45. string ErpId = dr["ErpID"].ToString(); //erpID
  46. string Class = this.GetType().Name + TenantCode;
  47. erpName = string.Format(erpName, TenantId);
  48. #region SQL
  49. string sql = @"
  50. select A.ID as ID,
  51. A.DocNO as MOCODE,
  52. 1 as Sequence,
  53. A7.Code as InvCode,
  54. CASE WHEN A.IsStartMO != 1 THEN
  55. (SELECT TOP 1 TotalStartQty FROM {1}.dbo.MO_MOStartInfo WHERE MO=A.ID ORDER BY StartDatetime DESC)
  56. ELSE (SELECT TOP 1 ActualStartQty FROM {1}.dbo.MO_MOStartInfo WHERE MO=A.ID ORDER BY StartDatetime DESC) END as Quantity,
  57. A.CreatedBy as CreatePerson,
  58. A.CreatedOn as CreateDateTime,
  59. 0 as Amount,
  60. 0 as RCVQuantity,
  61. A13.Code as DepCode,
  62. A22.Code as WHCode,
  63. 'mostatus_initial' as MOStatus,
  64. A2.Description as MOMemo,
  65. '2' as ERPStatus,
  66. A.StartDate as StartDate,
  67. A.CompleteDate as DueDate,
  68. A.ID as MOID,
  69. A.ID as MODetailID,
  70. ISNULL(A.ModifiedOn,A.BusinessCreatedOn) as MTIME,
  71. isnull(A11.Code,'') ProjectCode,
  72. --isnull(A19.LotCode,'') cBatch,
  73. '' version ,'' brand,
  74. '' as cFree1,
  75. '' as cFree2,
  76. '' as cFree3,
  77. '' as cFree4,
  78. '' as cFree5,
  79. '' as cFree6,
  80. '' as cFree7,
  81. '' as cFree8,
  82. '' as cFree9,
  83. '' as cFree10,CAST('' AS NVARCHAR(500)) as Colspan
  84. ,A12.Code as TenantId
  85. ,A12.id as Org
  86. ,a.DescFlexField_PrivateDescSeg1 AS EATTRIBUTE1
  87. ,(
  88. select b1.code from {1}.dbo.UBF_Sys_ExtEnumType a1
  89. left join {1}.dbo.UBF_Sys_ExtEnumValue b1 on a1.ID=b1.ExtEnumType
  90. left join {1}.dbo.UBF_Sys_ExtEnumValue_Trl c1 on b1.id=c1.ID
  91. WHERE a1.Code like 'UFIDA.U9.CBO.Enums.DemandCodeEnum'
  92. and b1.evalue=a.DemandCode
  93. and b1.code not like 'K10%'
  94. ) as NeedType
  95. ,A4.Name as MoDocType
  96. ,case when a.ActualCompleteDate is null then ''
  97. else Convert(varchar(10),a.ActualCompleteDate,120 ) end as ActualCompleteDate
  98. INTO #TempMO
  99. from {1}.dbo.MO_MO as A With(NoLock)
  100. left join {1}.dbo.[MO_MOMemo] as A1 With(NoLock) on (A.[ID] = A1.[MO])
  101. left join {1}.dbo.[MO_MOMemo_Trl] as A2 With(NoLock) on (A2.SysMlFlag = 'zh-CN') and (A1.[ID] = A2.[ID])
  102. left join {1}.dbo.[MO_MODocType] as A3 With(NoLock) on (A.[MODocType] = A3.[ID])
  103. left join {1}.dbo.[MO_MODocType_Trl] as A4 With(NoLock) on (A4.SysMlFlag = 'zh-CN') and (A3.[ID] = A4.[ID])
  104. left join {1}.dbo.[Base_User] as A6 With(NoLock) on (A.ModifiedBy = A6.Code)
  105. left join {1}.dbo.[CBO_ItemMaster] as A7 With(NoLock) on (A.[ItemMaster] = A7.[ID])
  106. left join {1}.dbo.[MO_MO_Trl] as A8 With(NoLock) on (A8.SysMlFlag = 'zh-CN') and (A.[ID] = A8.[ID])
  107. left join {1}.dbo.[CBO_BOMVersion] as A9 With(NoLock) on (A.[BOMVersion] = A9.[ID])
  108. left join {1}.dbo.[CBO_ItemMaster_Trl] as A10 With(NoLock) on (A10.SysMlFlag = 'zh-CN') and (A7.[ID] = A10.[ID])
  109. left join {1}.dbo.[CBO_Project] as A11 With(NoLock) on (A.[Project] = A11.[ID])
  110. left join {1}.dbo.[Base_Organization] as A12 With(NoLock) on (A.[Org] = A12.[ID])
  111. left join {1}.dbo.[CBO_Department] as A13 With(NoLock) on (A.[Department] = A13.[ID])
  112. left join {1}.dbo.[CBO_Department_Trl] as A14 With(NoLock) on (A14.SysMlFlag = 'zh-CN') and (A13.[ID] = A14.[ID])
  113. LEFT JOIN {1}.dbo.[CBO_Wh] AS A22 With(NoLock) ON A22.ID=A.CompleteWh
  114. where (A.DocState=2 OR A.DocState=3) and A.[BusinessType] != 50 and (A.[BusinessType] != 52)
  115. and (A.[BusinessType] != 2417) and (A.[IsMultiRouting] != 1) and A.[ItemMaster] is not null --and (A.[IsStartMO] != 1)
  116. and ISNULL(A.ModifiedOn,A.BusinessCreatedOn)>=@LastTime AND A12.id='{0}'
  117. --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,'')
  118. -- select distinct
  119. -- Colspan,ProjectCode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,TenantId,cBatch,version ,brand
  120. -- INTO #TempExtensionMO
  121. -- from #TempMO
  122. IF NOT EXISTS(SELECT * FROM #TempMO)
  123. begin
  124. DROP TABLE #TempMO
  125. RETURN
  126. end";
  127. sql = string.Format(sql, ErpId, erpName);
  128. sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempMO");
  129. Dictionary<string, string> value = new Dictionary<string, string>();
  130. //插入新增数据->ICSExtension表
  131. //sql += ICSHelper.InsertSQL("ICSExtension", value)
  132. // + @"#TempExtensionMO a
  133. // 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.TenantId=b.TenantId
  134. // WHERE b.Colspan IS NULL";
  135. sql += @"--删除数据
  136. -- DELETE ICSMO WHERE MODetailID NOT IN (SELECT CAST(MoDId AS VARCHAR) FROM {1}.dbo.[mom_orderdetail])
  137. SELECT a.MoCode INTO #tableICSMO
  138. FROM ICSMO a With(NoLock)
  139. LEFT JOIN {1}.dbo.[MO_MO] b With(NoLock) ON a.Mocode=b.DocNO
  140. WHERE a.TenantId='{0}' and b.DocNO IS NULL AND b.Org={2}
  141. DELETE ICSMO
  142. WHERE TenantId='{0}' and mocode IN ( SELECT MoCode from #tableICSMO)
  143. ";
  144. Dictionary<string, string> values = new Dictionary<string, string>();
  145. values.Add("MoCode", "a.MOCODE");//工单号
  146. values.Add("MoSeq", "a.Sequence");//工单行号
  147. values.Add("ItemCode", "a.InvCode");//物料代码
  148. values.Add("MoPlanQty", "a.Quantity");//数量
  149. values.Add("Amount", "a.Amount");//辅机数量
  150. values.Add("DepCode", "a.DepCode");//辅机数量
  151. values.Add("SegCode", "a.DepCode");//辅机数量
  152. values.Add("WHCode", "a.WHCode");//仓库
  153. values.Add("MoPlanStartDate", "a.StartDate");//开工日
  154. values.Add("MoPlanEndDate", "a.DueDate");//完工日
  155. values.Add("MOMemo", "a.MOMemo");//生产订单状态
  156. values.Add("MoUser", "a.CreatePerson");//生产订单状态
  157. values.Add("MODOWNDATE", "a.CreateDateTime");//生产订单状态
  158. values.Add("MOID", "a.MoId");//生产订单状态
  159. values.Add("MODetailID", "a.MODetailID");//生产订单状态
  160. values.Add("LastModifierUserId", "'" + ConstWorkPoint.Muser + "'");
  161. values.Add("LastModifierUserName", "'" + ConstWorkPoint.Musername + "'");
  162. values.Add("LastModificationTime", "a.Mtime");//操作时间
  163. values.Add("Default1", "a.EATTRIBUTE1");
  164. values.Add("Default2", "a.NeedType");
  165. values.Add("DocType", "a.MoDocType");
  166. values.Add("Default3", "a.ActualCompleteDate");
  167. //更新存在数据
  168. sql += ICSHelper.UpdateSQL("b", values)
  169. + @" #TempMO a
  170. INNER JOIN ICSMO b ON a.Mocode=b.Mocode
  171. WHERE b.TenantId='{0}' and a.Org={2} ";
  172. // values.Add("ID", "NEWID()");
  173. values.Add("MoType", "'normal'");//辅机数量
  174. values.Add("MOVER", "'1'");//辅机数量
  175. values.Add("ProjectCode", "a.ProjectCode");//辅机数量
  176. values.Add("MOINPUTQTY", "0");//辅机数量
  177. values.Add("MOSCRAPQTY", "0");//辅机数量
  178. values.Add("MOACTQTY", "0");//辅机数量
  179. values.Add("CollectModel", "'SS'");//辅机数量
  180. values.Add("CollectType", "'collecttype_one'");//辅机数量
  181. values.Add("MoBom", "'1'");//辅机数量
  182. values.Add("CreationTime", "a.MTime");//操作人
  183. values.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人
  184. values.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称
  185. values.Add("TenantId", "'" + TenantId + "'");//站点
  186. values.Add("MOSTATUS", "a.MOStatus");//生产订单状态
  187. //插入新增数据
  188. sql += ICSHelper.InsertSQL("ICSMO", values)
  189. + @" #TempMO a
  190. LEFT JOIN ICSMO b on a.Mocode=b.Mocode and b.TenantId='{0}'
  191. WHERE 1=1 and b.MoCode is null and a.Org={2} ";
  192. sql += "DROP TABLE #TempMO";
  193. sql += " DROP TABLE #tableICSMO ";
  194. sql = string.Format(sql, TenantId, erpName, ErpId);
  195. //return;
  196. ICSHelper.ExecuteDate(conStr, sql);
  197. #endregion
  198. }
  199. }
  200. catch (Exception ex)
  201. {
  202. log.Error(ex.ToString());
  203. }
  204. }
  205. }
  206. }