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
13 KiB

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