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.

210 lines
11 KiB

3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 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. /// 工单(来源U8)
  11. /// </summary>
  12. public class ICSMoFromU8 : 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. string erpNames = ICSHelper.GetConfigString()["Workpoint_ERPDB"];
  41. List<string> DBS = erpNames.Split(',').ToList();
  42. List<WorkpointERPDBLink> ListDB = new List<WorkpointERPDBLink>();
  43. foreach (string DB in DBS)
  44. {
  45. if (DB.Split('|') != null && DB.Split('|').Count() > 1)
  46. {
  47. WorkpointERPDBLink wd = new WorkpointERPDBLink();
  48. wd.Workpoint = DB.Split('|')[0];
  49. wd.ERPDB = DB.Split('|')[1];
  50. ListDB.Add(wd);
  51. }
  52. }
  53. foreach (DataRow dr in dt.Rows)
  54. {
  55. // string erpName = ICSHelper.GetConfigString()["ERPDB"];
  56. string TenantId = dr["TenantId"].ToString();//mes 组织
  57. string TenantCode = dr["TenantCode"].ToString();
  58. string ErpId = dr["ErpID"].ToString(); //erpID
  59. string Class = this.GetType().Name + TenantCode;
  60. WorkpointERPDBLink dblink = ListDB.FindLast(a => a.Workpoint == TenantCode);
  61. if (dblink == null)
  62. {
  63. return;
  64. }
  65. string erpName = dblink.ERPDB;
  66. //erpName = string.Format(erpName, TenantId);
  67. #region SQL
  68. string sql = @"
  69. select CAST(b.MoDId as VARCHAR(50)) as ID,
  70. a.MoCode as MOCODE,
  71. b.SortSeq as Sequence,
  72. b.InvCode as InvCode,
  73. b.Qty as Quantity,
  74. a.CreateUser as CreatePerson,
  75. a.CreateTime as CreateDateTime,
  76. isnull(b.AuxQty,0) as Amount,
  77. isnull(b.QualifiedInQty,0) as RCVQuantity,
  78. b.MDeptCode as DepCode,
  79. b.WhCode as WHCode,
  80. case when b.CloseUser is not null then 'mostatus_initial' else 'mostatus_close' end as MOStatus,
  81. b.Remark as MOMemo,
  82. case when b.CloseUser is not null then '3' else '2' end as ERPStatus,
  83. c.StartDate as StartDate,
  84. c.DueDate as DueDate,
  85. a.MoId as MOID,
  86. b.MoDId as MODetailID,
  87. CASE WHEN ISNULL(a.ModifyTime, a.CreateTime)>
  88. ISNULL(b.CloseTime,ISNULL(b.RelsTime,ISNULL(a.ModifyTime, a.CreateTime)))
  89. THEN ISNULL(a.ModifyTime, a.CreateTime)
  90. ELSE ISNULL(b.CloseTime,ISNULL(b.RelsTime,ISNULL(a.ModifyTime, a.CreateTime))) END as MTIME,
  91. isnull(b.CostItemCode,'') ProjectCode,isnull(b.MoLotCode,'') cBatch,'' version ,'' brand,
  92. (case WHEN inv.bFree1=1 THEN isnull(b.Free1,'') ELSE '' END) as cFree1,
  93. (case WHEN inv.bFree2=1 THEN isnull(b.Free2,'') ELSE '' END) as cFree2,
  94. (case WHEN inv.bFree3=1 THEN isnull(b.Free3,'') ELSE '' END) as cFree3,
  95. (case WHEN inv.bFree4=1 THEN isnull(b.Free4,'') ELSE '' END) as cFree4,
  96. (case WHEN inv.bFree5=1 THEN isnull(b.Free5,'') ELSE '' END) as cFree5,
  97. (case WHEN inv.bFree6=1 THEN isnull(b.Free6,'') ELSE '' END) as cFree6,
  98. (case WHEN inv.bFree7=1 THEN isnull(b.Free7,'') ELSE '' END) as cFree7,
  99. (case WHEN inv.bFree8=1 THEN isnull(b.Free8,'') ELSE '' END) as cFree8,
  100. (case WHEN inv.bFree9=1 THEN isnull(b.Free9,'') ELSE '' END) as cFree9,
  101. (case WHEN inv.bFree10=1 THEN isnull(b.Free10,'') ELSE '' END) as cFree10,
  102. CAST('' AS NVARCHAR(500)) as Colspan
  103. INTO #TempMO
  104. FROM {1}.dbo.mom_order a
  105. INNER JOIN {1}.dbo.mom_orderdetail b ON a.MoId = b.MoId
  106. INNER JOIN {1}.dbo.mom_morder c ON b.MoDId = c.MoDId
  107. LEFT JOIN {1}.dbo.Inventory inv ON b.InvCode = inv.cInvCode
  108. LEFT JOIN {1}.dbo.Customer cus ON b.CustCode = cus.cCusCode
  109. LEFT JOIN {1}.dbo.SO_SODetails so ON b.OrderDId = so.iSOsID
  110. where ISNULL (b.RelsUser,'') <> ''
  111. and CASE WHEN ISNULL(a.ModifyTime, a.CreateTime)>ISNULL(b.CloseTime,ISNULL(b.RelsTime,ISNULL(a.ModifyTime, a.CreateTime))) THEN ISNULL(a.ModifyTime, a.CreateTime) ELSE ISNULL(b.CloseTime,ISNULL(b.RelsTime,ISNULL(a.ModifyTime, a.CreateTime))) END>=@LastTime
  112. IF NOT EXISTS(SELECT * FROM #TempMO)
  113. begin
  114. DROP TABLE #TempMO
  115. RETURN
  116. end";
  117. sql = string.Format(sql, ErpId, erpName);
  118. sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempMO");
  119. Dictionary<string, string> value = new Dictionary<string, string>();
  120. sql += @"--删除数据
  121. SELECT a.ID INTO #tableICSMO
  122. FROM ICSMO a With(NoLock)
  123. LEFT JOIN {1}.dbo.[mom_order] b With(NoLock) ON a.Mocode=b.MoCode
  124. left JOIN {1}.dbo.mom_orderdetail b2 ON b.MoId = b2.MoId AND A.MoSeq=B2.SortSeq
  125. WHERE a.TenantId='{0}' and B2.SortSeq IS NULL
  126. DELETE FROM ICSMO
  127. WHERE TenantId='{0}' and ID IN ( SELECT ID from #tableICSMO )
  128. ";
  129. Dictionary<string, string> values = new Dictionary<string, string>();
  130. values.Add("MoCode", "a.MOCODE");//工单号
  131. values.Add("MoSeq", "a.Sequence");//工单行号
  132. values.Add("ItemCode", "a.InvCode");//物料代码
  133. values.Add("MoPlanQty", "a.Quantity");//数量
  134. values.Add("Amount", "a.Amount");//辅机数量
  135. values.Add("DepCode", "a.DepCode");//辅机数量
  136. values.Add("SegCode", "a.DepCode");//辅机数量
  137. values.Add("WHCode", "a.WHCode");//仓库
  138. values.Add("MoPlanStartDate", "a.StartDate");//开工日
  139. values.Add("MoPlanEndDate", "a.DueDate");//完工日
  140. values.Add("MOMemo", "a.MOMemo");//生产订单状态
  141. values.Add("MoUser", "a.CreatePerson");//生产订单状态
  142. values.Add("MODOWNDATE", "a.CreateDateTime");//生产订单状态
  143. values.Add("MOID", "a.MoId");//生产订单状态
  144. values.Add("MODetailID", "a.MODetailID");//生产订单状态
  145. values.Add("LastModifierUserId", "'" + ConstWorkPoint.Muser + "'");
  146. values.Add("LastModifierUserName", "'" + ConstWorkPoint.Musername + "'");
  147. values.Add("LastModificationTime", "a.Mtime");//操作时间
  148. //更新存在数据
  149. sql += ICSHelper.UpdateSQL("b", values)
  150. + @" #TempMO a
  151. INNER JOIN ICSMO b ON a.Mocode=b.Mocode AND A.Sequence=B.MoSeq
  152. WHERE b.TenantId='{0}' ";
  153. // values.Add("ID", "NEWID()");
  154. values.Add("MoType", "'normal'");//辅机数量
  155. values.Add("MOVER", "'1'");//辅机数量
  156. values.Add("ProjectCode", "a.ProjectCode");//辅机数量
  157. values.Add("MOINPUTQTY", "0");//辅机数量
  158. values.Add("MOSCRAPQTY", "0");//辅机数量
  159. values.Add("MOACTQTY", "0");//辅机数量
  160. values.Add("CollectModel", "'SS'");//辅机数量
  161. values.Add("CollectType", "'collecttype_one'");//辅机数量
  162. values.Add("MoBom", "'1'");//辅机数量
  163. values.Add("CreationTime", "a.MTime");//操作人
  164. values.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人
  165. values.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称
  166. values.Add("TenantId", "'" + TenantId + "'");//站点
  167. values.Add("MOSTATUS", "a.MOStatus");//生产订单状态
  168. //插入新增数据
  169. sql += ICSHelper.InsertSQL("ICSMO", values)
  170. + @" #TempMO a
  171. LEFT JOIN ICSMO b on a.Mocode=b.Mocode AND A.Sequence=B.MoSeq and b.TenantId='{0}'
  172. WHERE 1=1 and b.MoCode is null ";
  173. sql += "DROP TABLE #TempMO";
  174. sql += " DROP TABLE #tableICSMO ";
  175. sql = string.Format(sql, TenantId, erpName, ErpId);
  176. //return;
  177. ICSHelper.ExecuteDate(conStr, sql);
  178. #endregion
  179. }
  180. }
  181. catch (Exception ex)
  182. {
  183. log.Error(ex.ToString());
  184. }
  185. }
  186. }
  187. }