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.

219 lines
12 KiB

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