using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using Quartz; namespace ICSSoft.FromERP { /// /// 工单 /// public class ICSMo : 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.ID as ID, A.DocNO as MOCODE, 1 as Sequence, A7.Code as InvCode, CASE WHEN A.IsStartMO != 1 THEN (SELECT TOP 1 TotalStartQty FROM {1}.dbo.MO_MOStartInfo WHERE MO=A.ID ORDER BY StartDatetime DESC) ELSE (SELECT TOP 1 ActualStartQty FROM {1}.dbo.MO_MOStartInfo WHERE MO=A.ID ORDER BY StartDatetime DESC) END as Quantity, A.CreatedBy as CreatePerson, A.CreatedOn as CreateDateTime, 0 as Amount, 0 as RCVQuantity, A13.Code as DepCode, A22.Code as WHCode, '1' as MOStatus, A2.Description as MOMemo, '2' as ERPStatus, A.StartDate as StartDate, A.CompleteDate as DueDate, A.ID as MOID, A.ID as MODetailID, ISNULL(A.ModifiedOn,A.BusinessCreatedOn) as MTIME, isnull(A11.Code,'') ProjectCode,isnull(A19.LotCode,'') cBatch,'' version ,'' brand, '' as cFree1, '' as cFree2, '' as cFree3, '' as cFree4, '' as cFree5, '' as cFree6, '' as cFree7, '' as cFree8, '' as cFree9, '' as cFree10,CAST('' AS NVARCHAR(500)) as Colspan ,A12.Code as WorkPoint ,'0' AS EATTRIBUTE1 INTO #TempMO from {1}.dbo.MO_MO as A left join {1}.dbo.[MO_MOMemo] as A1 on (A.[ID] = A1.[MO]) left join {1}.dbo.[MO_MOMemo_Trl] as A2 on (A2.SysMlFlag = 'zh-CN') and (A1.[ID] = A2.[ID]) left join {1}.dbo.[MO_MODocType] as A3 on (A.[MODocType] = A3.[ID]) left join {1}.dbo.[MO_MODocType_Trl] as A4 on (A4.SysMlFlag = 'zh-CN') and (A3.[ID] = A4.[ID]) left join {1}.dbo.[MO_MOTaskPlan] as A5 on (A.[ID] = A5.[MO]) left join {1}.dbo.[Base_User] as A6 on (A.ModifiedBy = A6.Code) left join {1}.dbo.[CBO_ItemMaster] as A7 on (A.[ItemMaster] = A7.[ID]) left join {1}.dbo.[MO_MO_Trl] as A8 on (A8.SysMlFlag = 'zh-CN') and (A.[ID] = A8.[ID]) left join {1}.dbo.[CBO_BOMVersion] as A9 on (A.[BOMVersion] = A9.[ID]) left join {1}.dbo.[CBO_ItemMaster_Trl] as A10 on (A10.SysMlFlag = 'zh-CN') and (A7.[ID] = A10.[ID]) left join {1}.dbo.[CBO_Project] as A11 on (A.[Project] = A11.[ID]) left join {1}.dbo.[Base_Organization] as A12 on (A.[Org] = A12.[ID]) left join {1}.dbo.[CBO_Department] as A13 on (A.[Department] = A13.[ID]) left join {1}.dbo.[CBO_Department_Trl] as A14 on (A14.SysMlFlag = 'zh-CN') and (A13.[ID] = A14.[ID]) left join {1}.dbo. MO_MO As A16 on A16.[MODocType]=1001603110990023 and A.[Project]=A16.Project and A.ItemMaster=A16.ItemMaster left join {1}.dbo.[CBO_ItemMaster] as A17 on (A16.[ItemMaster] = A17.[ID]) left join {1}.dbo.[CBO_ItemMaster_Trl] as A18 on (A18.SysMlFlag = 'zh-CN') and (A17.[ID] = A16.[ID]) left join {1}.dbo.[MO_MOLot] as A19 on (A16.[ID] = A19.[MO]) left join (select * from (select ROW_NUMBER() over( partition by dept order by code desc ) row,* from {1}.dbo.CBO_Operators with(nolock) ) bb where bb.row='1' )as A20 on A20.dept=A.Department and a20.Org=a.Org left join {1}.dbo.[MO_IssueDoc] as A21 on A21.[SourceDoc_SrcDocNo]=A.DocNo LEFT JOIN {1}.dbo.[CBO_Wh] AS A22 ON A22.ID=A.CompleteWh where (A.DocState=2 OR A.DocState=3) and A.[BusinessType] != 50 and (A.[BusinessType] != 52) and (A.[BusinessType] != 2417) and (A.[IsMultiRouting] != 1) and A.[ItemMaster] is not null --and (A.[IsStartMO] != 1) and ISNULL(A.ModifiedOn,A.BusinessCreatedOn)>=@LastTime AND A12.Code='{0}' 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,'') select distinct Colspan,ProjectCode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,WorkPoint,cBatch,version ,brand INTO #TempExtensionMO from #TempMO IF NOT EXISTS(SELECT * FROM #TempMO) RETURN"; sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempMO"); 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) + @"#TempExtensionMO 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 += @"--删除数据 -- DELETE ICSMO WHERE MODetailID NOT IN (SELECT CAST(MoDId AS VARCHAR) FROM {1}.dbo.[mom_orderdetail]) SELECT a.MODetailID INTO #tableICSMO FROM ICSMO a With(NoLock) LEFT JOIN {1}.dbo.[MO_MO] b With(NoLock) ON a.MODetailID=b.ID WHERE a.WorkPoint='{0}' and b.ID IS NULL DELETE ICSMO WHERE WorkPoint='{0}' and MODetailID IN ( SELECT MODetailID from #tableICSMO) "; Dictionary values = new Dictionary(); values.Add("MOCODE", "a.MOCODE");//工单号 values.Add("Sequence", "a.Sequence");//工单行号 values.Add("InvCode", "a.InvCode");//物料代码 values.Add("Quantity", "a.Quantity");//数量 values.Add("Amount", "a.Amount");//辅机数量 values.Add("DepCode", "a.DepCode");//辅机数量 values.Add("WHCode", "a.WHCode");//仓库 values.Add("StartDate", "a.StartDate");//开工日 values.Add("DueDate", "a.DueDate");//完工日 values.Add("MOSTATUS", "a.MOStatus");//生产订单状态 values.Add("MOMemo", "a.MOMemo");//生产订单状态 values.Add("CreatePerson", "a.CreatePerson");//生产订单状态 values.Add("CreateDateTime", "a.CreateDateTime");//生产订单状态 values.Add("ERPStatus", "a.ERPStatus");//生产订单状态 // values.Add("ERPStatus", "a.MOSTATUS");//生产订单状态 values.Add("MOID", "a.MoId");//生产订单状态 values.Add("MODetailID", "a.MODetailID");//生产订单状态 values.Add("ExtensionID", "c.ID");//生产订单状态 //values.Add("MODetailID", "a.MOSTATUS");//生产订单状态 // values.Add("ExtensionID", "a.MOSTATUS");//生产订单状态 values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'"); values.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'"); values.Add("WorkPoint", "'" + WorkPoint + "'"); values.Add("MTIME", "a.MTIME");//生产订单状态 values.Add("EATTRIBUTE1", "a.EATTRIBUTE1"); //更新存在数据 sql += ICSHelper.UpdateSQL("b", values) + @" #TempMO a INNER JOIN ICSMO b ON a.MODetailID=b.MODetailID 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()"); values.Add("RCVQuantity", "a.RCVQuantity");//辅机数量 //插入新增数据 sql += ICSHelper.InsertSQL("ICSMO", values) + @" #TempMO a LEFT JOIN ICSMO b ON a.MODetailID=b.MODetailID and a.WorkPoint=b.WorkPoint inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint WHERE b.ID IS NULL and a.WorkPoint='" + WorkPoint + @"' "; sql += "DROP TABLE #TempMO"; sql += " DROP TABLE #TempExtensionMO " + " DROP TABLE #tableICSMO "; sql = string.Format(sql, WorkPoint, erpName); ICSHelper.ExecuteDate(conStr, sql); #endregion } } catch (Exception ex) { log.Error(ex.ToString()); } } } }