using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using Quartz; namespace ICSSoft.FromERP { /// /// 工单(来源U8) /// public class ICSMoFromU8 : 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 TenantId = dr["TenantId"].ToString();//mes 组织 string TenantCode = dr["TenantCode"].ToString(); string ErpId = dr["ErpID"].ToString(); //erpID string Class = this.GetType().Name + TenantCode; erpName = string.Format(erpName, TenantId); #region SQL string sql = @" select CAST(b.MoDId as VARCHAR(50)) as ID, a.MoCode as MOCODE, b.SortSeq as Sequence, b.InvCode as InvCode, b.Qty as Quantity, a.CreateUser as CreatePerson, a.CreateTime as CreateDateTime, isnull(b.AuxQty,0) as Amount, isnull(b.QualifiedInQty,0) as RCVQuantity, b.MDeptCode as DepCode, b.WhCode as WHCode, case when b.CloseUser is not null then 'mostatus_initial' else 'mostatus_close' end as MOStatus, b.Remark as MOMemo, case when b.CloseUser is not null then '3' else '2' end as ERPStatus, c.StartDate as StartDate, c.DueDate as DueDate, a.MoId as MOID, b.MoDId as MODetailID, 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 inv.bFree1=1 THEN isnull(b.Free1,'') ELSE '' END) as cFree1, (case WHEN inv.bFree2=1 THEN isnull(b.Free2,'') ELSE '' END) as cFree2, (case WHEN inv.bFree3=1 THEN isnull(b.Free3,'') ELSE '' END) as cFree3, (case WHEN inv.bFree4=1 THEN isnull(b.Free4,'') ELSE '' END) as cFree4, (case WHEN inv.bFree5=1 THEN isnull(b.Free5,'') ELSE '' END) as cFree5, (case WHEN inv.bFree6=1 THEN isnull(b.Free6,'') ELSE '' END) as cFree6, (case WHEN inv.bFree7=1 THEN isnull(b.Free7,'') ELSE '' END) as cFree7, (case WHEN inv.bFree8=1 THEN isnull(b.Free8,'') ELSE '' END) as cFree8, (case WHEN inv.bFree9=1 THEN isnull(b.Free9,'') ELSE '' END) as cFree9, (case WHEN inv.bFree10=1 THEN isnull(b.Free10,'') ELSE '' END) as cFree10, CAST('' AS NVARCHAR(500)) as Colspan INTO #TempMO FROM {1}.dbo.mom_order a INNER JOIN {1}.dbo.mom_orderdetail b ON a.MoId = b.MoId INNER JOIN {1}.dbo.mom_morder c ON b.MoDId = c.MoDId LEFT JOIN {1}.dbo.Inventory inv ON b.InvCode = inv.cInvCode LEFT JOIN {1}.dbo.Customer cus ON b.CustCode = cus.cCusCode LEFT JOIN {1}.dbo.SO_SODetails so ON b.OrderDId = so.iSOsID 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 IF NOT EXISTS(SELECT * FROM #TempMO) begin DROP TABLE #TempMO RETURN end"; sql = string.Format(sql, ErpId, erpName); sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempMO"); Dictionary value = new Dictionary(); sql += @"--删除数据 SELECT a.ID INTO #tableICSMO FROM ICSMO a With(NoLock) LEFT JOIN {1}.dbo.[mom_order] b With(NoLock) ON a.Mocode=b.MoCode AND A.MoSeq=B.SortSeq WHERE a.TenantId='{0}' and b.MoCode IS NULL DELETE FROM ICSMO WHERE TenantId='{0}' and ID IN ( SELECT ID from #tableICSMO ) "; Dictionary values = new Dictionary(); values.Add("MoCode", "a.MOCODE");//工单号 values.Add("MoSeq", "a.Sequence");//工单行号 values.Add("ItemCode", "a.InvCode");//物料代码 values.Add("MoPlanQty", "a.Quantity");//数量 values.Add("Amount", "a.Amount");//辅机数量 values.Add("DepCode", "a.DepCode");//辅机数量 values.Add("SegCode", "a.DepCode");//辅机数量 values.Add("WHCode", "a.WHCode");//仓库 values.Add("MoPlanStartDate", "a.StartDate");//开工日 values.Add("MoPlanEndDate", "a.DueDate");//完工日 values.Add("MOMemo", "a.MOMemo");//生产订单状态 values.Add("MoUser", "a.CreatePerson");//生产订单状态 values.Add("MODOWNDATE", "a.CreateDateTime");//生产订单状态 values.Add("MOID", "a.MoId");//生产订单状态 values.Add("MODetailID", "a.MODetailID");//生产订单状态 values.Add("LastModifierUserId", "'" + ConstWorkPoint.Muser + "'"); values.Add("LastModifierUserName", "'" + ConstWorkPoint.Musername + "'"); values.Add("LastModificationTime", "a.Mtime");//操作时间 //更新存在数据 sql += ICSHelper.UpdateSQL("b", values) + @" #TempMO a INNER JOIN ICSMO b ON a.Mocode=b.Mocode AND A.Sequence=B.MoSeq WHERE b.TenantId='{0}' "; // values.Add("ID", "NEWID()"); values.Add("MoType", "'normal'");//辅机数量 values.Add("MOVER", "'1'");//辅机数量 values.Add("ProjectCode", "a.ProjectCode");//辅机数量 values.Add("MOINPUTQTY", "0");//辅机数量 values.Add("MOSCRAPQTY", "0");//辅机数量 values.Add("MOACTQTY", "0");//辅机数量 values.Add("CollectModel", "'SS'");//辅机数量 values.Add("CollectType", "'collecttype_one'");//辅机数量 values.Add("MoBom", "'1'");//辅机数量 values.Add("CreationTime", "a.MTime");//操作人 values.Add("CreatorUserId", "'" + ConstWorkPoint.Muser + "'");//操作人 values.Add("CreatorUserName", "'" + ConstWorkPoint.Musername + "'");//操作人名称 values.Add("TenantId", "'" + TenantId + "'");//站点 values.Add("MOSTATUS", "a.MOStatus");//生产订单状态 //插入新增数据 sql += ICSHelper.InsertSQL("ICSMO", values) + @" #TempMO a LEFT JOIN ICSMO b on a.Mocode=b.Mocode AND A.Sequence=B.MoSeq and b.TenantId='{0}' WHERE 1=1 and b.MoCode is null "; sql += "DROP TABLE #TempMO"; sql += " DROP TABLE #tableICSMO "; sql = string.Format(sql, TenantId, erpName, ErpId); //return; ICSHelper.ExecuteDate(conStr, sql); #endregion } } catch (Exception ex) { log.Error(ex.ToString()); } } } }