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.

213 lines
11 KiB

2 weeks 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_XinHJ : 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. b.MoLotCode ,b.Free1 ,b.Free2
  104. INTO #TempMO
  105. FROM {1}.dbo.mom_order a
  106. INNER JOIN {1}.dbo.mom_orderdetail b ON a.MoId = b.MoId
  107. INNER JOIN {1}.dbo.mom_morder c ON b.MoDId = c.MoDId
  108. LEFT JOIN {1}.dbo.Inventory inv ON b.InvCode = inv.cInvCode
  109. LEFT JOIN {1}.dbo.Customer cus ON b.CustCode = cus.cCusCode
  110. LEFT JOIN {1}.dbo.SO_SODetails so ON b.OrderDId = so.iSOsID
  111. where ISNULL (b.RelsUser,'') <> ''
  112. 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
  113. IF NOT EXISTS(SELECT * FROM #TempMO)
  114. begin
  115. DROP TABLE #TempMO
  116. RETURN
  117. end";
  118. sql = string.Format(sql, ErpId, erpName);
  119. sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempMO");
  120. Dictionary<string, string> value = new Dictionary<string, string>();
  121. sql += @"--删除数据
  122. SELECT a.ID INTO #tableICSMO
  123. FROM ICSMO a With(NoLock)
  124. LEFT JOIN {1}.dbo.[mom_order] b With(NoLock) ON a.Mocode=b.MoCode
  125. left JOIN {1}.dbo.mom_orderdetail b2 ON b.MoId = b2.MoId AND A.MoSeq=B2.SortSeq
  126. WHERE a.TenantId='{0}' and B2.SortSeq IS NULL
  127. DELETE FROM ICSMO
  128. WHERE TenantId='{0}' and ID IN ( SELECT ID from #tableICSMO )
  129. ";
  130. Dictionary<string, string> values = new Dictionary<string, string>();
  131. values.Add("MoCode", "a.MOCODE");//工单号
  132. values.Add("MoSeq", "a.Sequence");//工单行号
  133. values.Add("ItemCode", "a.InvCode");//物料代码
  134. values.Add("MoPlanQty", "a.Quantity");//数量
  135. values.Add("Amount", "a.Amount");
  136. values.Add("DepCode", "a.DepCode");
  137. values.Add("SegCode", "a.DepCode");
  138. values.Add("WHCode", "a.WHCode");//仓库
  139. values.Add("MoPlanStartDate", "a.StartDate");//开工日
  140. values.Add("MoPlanEndDate", "a.DueDate");//完工日
  141. values.Add("MOMemo", "a.MOMemo");//生产订单状态
  142. values.Add("MoUser", "a.CreatePerson");//生产订单状态
  143. values.Add("MODOWNDATE", "a.CreateDateTime");//生产订单状态
  144. values.Add("MOID", "a.MoId");//生产订单状态
  145. values.Add("MODetailID", "a.MODetailID");//生产订单状态
  146. values.Add("LastModifierUserId", "'" + ConstWorkPoint.Muser + "'");
  147. values.Add("LastModifierUserName", "'" + ConstWorkPoint.Musername + "'");
  148. values.Add("LastModificationTime", "a.Mtime");//操作时间
  149. values.Add("Default1", "a.生产批号");//完工日
  150. values.Add("Default2", "a.零件版本");//完工日
  151. values.Add("Default3", "a.图纸版本");//完工日
  152. //更新存在数据
  153. sql += ICSHelper.UpdateSQL("b", values)
  154. + @" #TempMO a
  155. INNER JOIN ICSMO b ON a.Mocode=b.Mocode AND A.Sequence=B.MoSeq
  156. WHERE b.TenantId='{0}' ";
  157. // values.Add("ID", "NEWID()");
  158. values.Add("MoType", "'normal'");//辅机数量
  159. values.Add("MOVER", "'1'");//辅机数量
  160. values.Add("ProjectCode", "a.ProjectCode");//辅机数量
  161. values.Add("MOINPUTQTY", "0");//辅机数量
  162. values.Add("MOSCRAPQTY", "0");//辅机数量
  163. values.Add("MOACTQTY", "0");//辅机数量
  164. values.Add("CollectModel", "'SS'");//辅机数量
  165. values.Add("CollectType", "'collecttype_one'");//辅机数量
  166. values.Add("MoBom", "'1'");//辅机数量
  167. values.Add("CreationTime", "a.MTime");//操作人
  168. values.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人
  169. values.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称
  170. values.Add("TenantId", "'" + TenantId + "'");//站点
  171. values.Add("MOSTATUS", "a.MOStatus");//生产订单状态
  172. //插入新增数据
  173. sql += ICSHelper.InsertSQL("ICSMO", values)
  174. + @" #TempMO a
  175. LEFT JOIN ICSMO b on a.Mocode=b.Mocode AND A.Sequence=B.MoSeq and b.TenantId='{0}'
  176. WHERE 1=1 and b.MoCode is null ";
  177. sql += "DROP TABLE #TempMO";
  178. sql += " DROP TABLE #tableICSMO ";
  179. sql = string.Format(sql, TenantId, erpName, ErpId);
  180. //return;
  181. ICSHelper.ExecuteDate(conStr, sql);
  182. #endregion
  183. }
  184. }
  185. catch (Exception ex)
  186. {
  187. log.Error(ex.ToString());
  188. }
  189. }
  190. }
  191. }