using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using Quartz; namespace ICSSoft.FromERP { /// /// 生产订单备料表 /// public class ICSMOPick : IJob { private static object key = new object(); private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); public void Execute(IJobExecutionContext context) { try { lock (key) { log.Info("开始……………………………………………………………………"); Execute(); log.Info("结束……………………………………………………………………"); } } catch (Exception ex) { log.Error(ex.ToString()); } } public void Execute() { try { string conStr = ICSHelper.GetConnectString(); string Namespace = this.GetType().Namespace; //string Class = this.GetType().Name; DataTable dt = ICSHelper.GetERPDB(conStr); foreach (DataRow dr in dt.Rows) { string erpName = ICSHelper.GetConfigString()["ERPDB"]; string WorkPoint = dr["WorkPointCode"].ToString(); string Class = this.GetType().Name + WorkPoint; erpName = string.Format(erpName, WorkPoint); #region SQL string sql = @" SELECT a.MoCode,b.SortSeq SEQ1,b.InvCode,b.Qty QTY1,c.AllocateId AS PickID,d.AllocateId AS ParentPickID, c.SortSeq SEQ2,c.InvCode INV1,c.Qty QTY2,e.InvCode INV2,c.Qty/d.Factor SETE,isnull(isnull(c.AuxQty,0)/d.Factor,0) ParentAmount,null ParentQuantity into #TempBOM1 FROM {1}.dbo.mom_order a INNER JOIN {1}.dbo.mom_orderdetail b ON a.MoId = b.MoId INNER JOIN {1}.dbo.mom_moallocate c ON b.MoDId = c.MoDId LEFT JOIN {1}.dbo.mom_moallocatesub d ON c.MoallocateSubId = d.MoallocateSubId LEFT JOIN {1}.dbo.mom_moallocate e ON d.AllocateId = e.AllocateId ORDER BY a.MoCode,b.SortSeq,d.AllocateId,c.SortSeq update #TempBOM1 set ParentQuantity=(case when ParentPickID is null then QTY2+isnull((select sum(isnull(SETE,0)) from #TempBOM1 a where a.ParentPickID=b.PickID),0) else (select sum(isnull(SETE,0)) from #TempBOM1 a where b.ParentPickID=a.ParentPickID)+(select QTY2 from #TempBOM1 a where a.PickID=b.ParentPickID) end) from #TempBOM1 b SELECT b.MoDId AS MODetailID , CAST(c.AllocateId AS NVARCHAR(50)) AS PickID, c.InvCode AS InvCode, c.SortSeq AS Sequence, CASE WHEN d.bCutMantissa='1' AND d.bKCCutMantissa='1' THEN CEILING(c.Qty) ELSE c.Qty END AS Quantity, isnull(c.AuxQty,0)AS Amount, 0 as IssueQuantity, c.WhCode as WHCode, (case when bInvQuality=0 then '4' else c.WIPType end) AS SupplyType, 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, isnull(b.CostItemCode,'') ProjectCode,isnull(b.MoLotCode,'') cBatch,'' version ,'' brand, (case WHEN d.bFree1=1 THEN c.Free1 ELSE '' END) as cFree1, (case WHEN d.bFree2=1 THEN c.Free2 ELSE '' END) as cFree2, (case WHEN d.bFree3=1 THEN c.Free3 ELSE '' END) as cFree3, (case WHEN d.bFree4=1 THEN c.Free4 ELSE '' END) as cFree4, (case WHEN d.bFree5=1 THEN c.Free5 ELSE '' END) as cFree5, (case WHEN d.bFree6=1 THEN c.Free6 ELSE '' END) as cFree6, (case WHEN d.bFree7=1 THEN c.Free7 ELSE '' END) as cFree7, (case WHEN d.bFree8=1 THEN c.Free8 ELSE '' END) as cFree8, (case WHEN d.bFree9=1 THEN c.Free9 ELSE '' END) as cFree9, (case WHEN d.bFree10=1 THEN c.Free10 ELSE '' END) as cFree10,CAST('' AS NVARCHAR(500)) as Colspan ,'{0}' as WorkPoint ,(CASE WHEN c.ProductType<>1 then '1' else '0' end ) EATTRIBUTE1,i.ParentPickID,i.ParentQuantity,i.ParentAmount into #TempBOM FROM {1}.dbo.mom_order a INNER JOIN {1}.dbo.mom_orderdetail b ON a.MoId = b.MoId INNER JOIN {1}.dbo.mom_moallocate c ON b.MoDId = c.MoDId INNER JOIN {1}.dbo.Inventory d ON c.InvCode = d.cInvCode LEFT JOIN {1}.dbo.bom_parent e ON b.BomId = e.BomId LEFT JOIN {1}.dbo.bom_bom f ON e.BomId = f.BomId LEFT JOIN {1}.dbo.ComputationUnit unit ON d.cComUnitCode = unit.cComunitCode left join #TempBOM1 i on i.MoCode=a.MoCode and c.AllocateId =i.PickID WHERE ISNULL (b.RelsUser,'') <> '' 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 update #TempBOM 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,'') select distinct Colspan,ProjectCode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,WorkPoint,cBatch,version ,brand INTO #TempExtension from #TempBOM IF NOT EXISTS(SELECT * FROM #TempBOM) RETURN"; sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempBOM"); Dictionary value = new Dictionary(); value.Add("ID", "NEWID()"); value.Add("BatchCode", "a.cBatch"); value.Add("version", "a.version"); value.Add("brand", "a.brand"); value.Add("ProjectCode", "a.ProjectCode"); value.Add("cFree1", "a.cFree1"); value.Add("cFree2", "a.cFree2"); value.Add("cFree3", "a.cFree3"); value.Add("cFree4", "a.cFree4"); value.Add("cFree5", "a.cFree5"); value.Add("cFree6", "a.cFree6"); value.Add("cFree7", "a.cFree7"); value.Add("cFree8", "a.cFree8"); value.Add("cFree9", "a.cFree9"); value.Add("cFree10", "a.cFree10"); value.Add("Colspan", "a.Colspan"); value.Add("MTIME", "GETDATE()"); value.Add("MUSER", "'" + ConstWorkPoint.Muser + "'"); value.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'"); value.Add("WorkPoint", "'" + WorkPoint + "'"); //插入新增数据->ICSExtension表 sql += ICSHelper.InsertSQL("ICSExtension", value) + @"#TempExtension a 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 WHERE b.Colspan IS NULL"; sql += "\r\n"; sql += @"--删除数据 -- DELETE icsMOPick WHERE PickID NOT IN (SELECT AllocateId FROM {1}.dbo.mom_moallocate) SELECT a.PickID INTO #tableicsMOPick FROM icsMOPick a With(NoLock) LEFT JOIN {1}.dbo.[mom_moallocate] b With(NoLock) ON a.PickID=b.AllocateId WHERE a.WorkPoint='{0}' and b.AllocateId IS NULL DELETE icsMOPick WHERE WorkPoint='{0}' and PickID IN ( SELECT PickID from #tableicsMOPick) "; Dictionary values = new Dictionary(); values.Add("MODetailID", "a.MODetailID"); values.Add("PickID", "a.PickID"); values.Add("InvCode", "a.InvCode"); values.Add("Sequence", "a.Sequence"); values.Add("Quantity", "a.Quantity"); values.Add("Amount", "a.Amount"); values.Add("ParentPickID", "a.ParentPickID"); values.Add("ParentQuantity", "a.ParentQuantity"); values.Add("ParentAmount", "a.ParentAmount"); values.Add("WHCode", "a.WHCode"); values.Add("SupplyType", "a.SupplyType"); values.Add("ExtensionID", "c.ID"); values.Add("EATTRIBUTE1", "a.EATTRIBUTE1"); //values.Add("WorkPoint", "a.WorkPoint"); //更新存在数据 sql += ICSHelper.UpdateSQL("b", values) + @" #TempBOM a INNER JOIN icsMOPick b ON a.PickID=b.PickID and a.WorkPoint=b.WorkPoint inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint WHERE a.WorkPoint='" + WorkPoint + "' "; values.Add("ID", "NEWID()");//ID values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'"); values.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'"); values.Add("WorkPoint", "'" + WorkPoint + "'"); values.Add("MTIME", "a.MTIME"); values.Add("IssueQuantity", "a.IssueQuantity"); sql += "\r\n"; //插入新增数据 sql += ICSHelper.InsertSQL("icsMOPick", values) + @" #TempBOM a LEFT JOIN icsMOPick b ON a.PickID=b.PickID and a.WorkPoint=b.WorkPoint inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint WHERE b.PickID IS NULL and a.WorkPoint='" + WorkPoint + @"' "; sql += " DROP TABLE #TempBOM "; sql += " DROP TABLE #TempExtension " + " DROP TABLE #tableicsMOPick"; sql = string.Format(sql, WorkPoint, erpName); log.Info(sql); ICSHelper.ExecuteDate(conStr, sql); #endregion } } catch (Exception ex) { log.Error(ex.ToString()); } } } }