纽威
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.

199 lines
12 KiB

  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 WorkPoint = dr["WorkPointCode"].ToString();
  44. string Class = this.GetType().Name + WorkPoint;
  45. erpName = string.Format(erpName, WorkPoint);
  46. #region SQL
  47. string sql = @"
  48. select CAST(b.MoDId as VARCHAR(50)) as ID,
  49. a.MoCode as MOCODE,
  50. b.SortSeq as Sequence,
  51. b.InvCode as InvCode,
  52. b.Qty as Quantity,
  53. a.CreateUser as CreatePerson,
  54. a.CreateTime as CreateDateTime,
  55. isnull(b.AuxQty,0) as Amount,
  56. isnull(b.QualifiedInQty,0) as RCVQuantity,
  57. b.MDeptCode as DepCode,
  58. b.WhCode as WHCode,
  59. '1' as MOStatus,
  60. b.Remark as MOMemo,
  61. '2' as ERPStatus,
  62. c.StartDate as StartDate,
  63. c.DueDate as DueDate,
  64. a.MoId as MOID,
  65. b.MoDId as MODetailID,
  66. 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 as MTIME,
  67. isnull(b.CostItemCode,'') ProjectCode,isnull(b.MoLotCode,'') cBatch,'' version ,'' brand,
  68. (case WHEN inv.bFree1=1 THEN b.Free1 ELSE '' END) as cFree1,
  69. (case WHEN inv.bFree2=1 THEN b.Free2 ELSE '' END) as cFree2,
  70. (case WHEN inv.bFree3=1 THEN b.Free3 ELSE '' END) as cFree3,
  71. (case WHEN inv.bFree4=1 THEN b.Free4 ELSE '' END) as cFree4,
  72. (case WHEN inv.bFree5=1 THEN b.Free5 ELSE '' END) as cFree5,
  73. (case WHEN inv.bFree6=1 THEN b.Free6 ELSE '' END) as cFree6,
  74. (case WHEN inv.bFree7=1 THEN b.Free7 ELSE '' END) as cFree7,
  75. (case WHEN inv.bFree8=1 THEN b.Free8 ELSE '' END) as cFree8,
  76. (case WHEN inv.bFree9=1 THEN b.Free9 ELSE '' END) as cFree9,
  77. (case WHEN inv.bFree10=1 THEN b.Free10 ELSE '' END) as cFree10,CAST('' AS NVARCHAR(500)) as Colspan
  78. ,'{0}' as WorkPoint
  79. ,(CASE WHEN b.MoClass<>1 then '1' else '0' end ) EATTRIBUTE1
  80. INTO #TempMO
  81. FROM {1}.dbo.mom_order a
  82. INNER JOIN {1}.dbo.mom_orderdetail b ON a.MoId = b.MoId
  83. INNER JOIN {1}.dbo.mom_morder c ON b.MoDId = c.MoDId
  84. --LEFT JOIN {1}.dbo.mom_orderdetail_extradefine d ON b.MoDId = d.MoDId
  85. LEFT JOIN {1}.dbo.Inventory inv ON b.InvCode = inv.cInvCode
  86. LEFT JOIN {1}.dbo.Customer cus ON b.CustCode = cus.cCusCode
  87. LEFT JOIN {1}.dbo.SO_SODetails so ON b.OrderDId = so.iSOsID
  88. --LEFT JOIN {1}.dbo.CusInvContrapose cusInv ON cusinv.cCusCode = b.CustCode AND b.InvCode = cusInv.cInvCode
  89. where ISNULL (b.RelsUser,'') <> ''
  90. 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
  91. 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,'')
  92. select distinct
  93. Colspan,ProjectCode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,WorkPoint,cBatch,version ,brand
  94. INTO #TempExtensionMO
  95. from #TempMO
  96. IF NOT EXISTS(SELECT * FROM #TempMO)
  97. RETURN";
  98. sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempMO");
  99. Dictionary<string, string> value = new Dictionary<string, string>();
  100. value.Add("ID", "NEWID()");
  101. value.Add("BatchCode", "a.cBatch");
  102. value.Add("version", "a.version");
  103. value.Add("brand", "a.brand");
  104. value.Add("ProjectCode", "a.ProjectCode");
  105. value.Add("cFree1", "a.cFree1");
  106. value.Add("cFree2", "a.cFree2");
  107. value.Add("cFree3", "a.cFree3");
  108. value.Add("cFree4", "a.cFree4");
  109. value.Add("cFree5", "a.cFree5");
  110. value.Add("cFree6", "a.cFree6");
  111. value.Add("cFree7", "a.cFree7");
  112. value.Add("cFree8", "a.cFree8");
  113. value.Add("cFree9", "a.cFree9");
  114. value.Add("cFree10", "a.cFree10");
  115. value.Add("Colspan", "a.Colspan");
  116. value.Add("MTIME", "GETDATE()");
  117. value.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  118. value.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  119. value.Add("WorkPoint", "'" + WorkPoint + "'");
  120. //插入新增数据->ICSExtension表
  121. sql += ICSHelper.InsertSQL("ICSExtension", value)
  122. + @"#TempExtensionMO a
  123. 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.WorkPoint=b.WorkPoint
  124. WHERE b.Colspan IS NULL";
  125. sql += @"--删除数据
  126. -- DELETE ICSMO WHERE MODetailID NOT IN (SELECT CAST(MoDId AS VARCHAR) FROM {1}.dbo.[mom_orderdetail])
  127. SELECT a.MODetailID INTO #tableICSMO
  128. FROM ICSMO a With(NoLock)
  129. LEFT JOIN {1}.dbo.[mom_orderdetail] b With(NoLock) ON a.MODetailID=CAST(b.MoDId AS VARCHAR)
  130. WHERE a.WorkPoint='{0}' and b.MoDId IS NULL
  131. DELETE ICSMO
  132. WHERE WorkPoint='{0}' and MODetailID IN ( SELECT MODetailID from #tableICSMO)
  133. ";
  134. Dictionary<string, string> values = new Dictionary<string, string>();
  135. values.Add("MOCODE", "a.MOCODE");//工单号
  136. values.Add("Sequence", "a.Sequence");//工单行号
  137. values.Add("InvCode", "a.InvCode");//物料代码
  138. values.Add("Quantity", "a.Quantity");//数量
  139. values.Add("Amount", "a.Amount");//辅机数量
  140. values.Add("DepCode", "a.DepCode");//辅机数量
  141. values.Add("WHCode", "a.WHCode");//仓库
  142. values.Add("StartDate", "a.StartDate");//开工日
  143. values.Add("DueDate", "a.DueDate");//完工日
  144. values.Add("MOSTATUS", "a.MOStatus");//生产订单状态
  145. values.Add("MOMemo", "a.MOMemo");//生产订单状态
  146. values.Add("CreatePerson", "a.CreatePerson");//生产订单状态
  147. values.Add("CreateDateTime", "a.CreateDateTime");//生产订单状态
  148. values.Add("ERPStatus", "a.ERPStatus");//生产订单状态
  149. // values.Add("ERPStatus", "a.MOSTATUS");//生产订单状态
  150. values.Add("MOID", "a.MoId");//生产订单状态
  151. values.Add("MODetailID", "a.MODetailID");//生产订单状态
  152. values.Add("ExtensionID", "c.ID");//生产订单状态
  153. //values.Add("MODetailID", "a.MOSTATUS");//生产订单状态
  154. // values.Add("ExtensionID", "a.MOSTATUS");//生产订单状态
  155. values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
  156. values.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
  157. values.Add("WorkPoint", "'" + WorkPoint + "'");
  158. values.Add("MTIME", "a.MTIME");//生产订单状态
  159. values.Add("EATTRIBUTE1", "a.EATTRIBUTE1");
  160. //更新存在数据
  161. sql += ICSHelper.UpdateSQL("b", values)
  162. + @" #TempMO a
  163. INNER JOIN ICSMO b ON a.MODetailID=b.MODetailID and a.WorkPoint=b.WorkPoint
  164. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  165. WHERE a.WorkPoint='" + WorkPoint + "' ";
  166. values.Add("ID", "NEWID()");
  167. values.Add("RCVQuantity", "a.RCVQuantity");//辅机数量
  168. //插入新增数据
  169. sql += ICSHelper.InsertSQL("ICSMO", values)
  170. + @" #TempMO a
  171. LEFT JOIN ICSMO b ON a.MODetailID=b.MODetailID and a.WorkPoint=b.WorkPoint
  172. inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
  173. WHERE b.ID IS NULL and a.WorkPoint='" + WorkPoint + @"'
  174. ";
  175. sql += "DROP TABLE #TempMO";
  176. sql += " DROP TABLE #TempExtensionMO " +
  177. " DROP TABLE #tableICSMO ";
  178. sql = string.Format(sql, WorkPoint, erpName);
  179. ICSHelper.ExecuteDate(conStr, sql);
  180. #endregion
  181. }
  182. }
  183. catch (Exception ex)
  184. {
  185. log.Error(ex.ToString());
  186. }
  187. }
  188. }
  189. }