using ICSSoft.FromERP.Model; using Newtonsoft.Json; using Quartz; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ICSSoft.FromERP { /// /// 工单工时统计(锐腾) /// public class SyncMoHour_Rhyton : 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) { var dtNowBegin = new DateTime(2000, 1, 1, 0, 0, 0);//默认开始时间 var dtNow = DateTime.Now; 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); string sql0 = " SELECT top 1 ModifyDate FROM ICSERPTime where ClassName='" + Class + "'"; var lastDate = ICSHelper.ExecuteScalar(conStr, sql0).ToDateOrNull(); if (!lastDate.HasValue) { lastDate = dtNowBegin; } string sql = @" if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#tempSyncMoHour_Rhyton') and type='U') DROP TABLE #tempSyncMoHour_Rhyton select a.* ,(a.PTime+cast( a.ManualTime as decimal(18,6) )) as MachiningTotalTime ,case when (a.EqpCalcType='1'and a.ResQty>0) then (a.PTime+cast( a.ManualTime as decimal(18,6) ))/a.ResQty else 0 end as EqpTotalTime ,(a.PTime+cast( a.ManualTime as decimal(18,6) )-a.TotalTime) as HourDiff into #tempSyncMoHour_Rhyton from ( select a.MoCode ,a.ItemCode ,a.OpCode ,a.MoPlanQty ,a.STime ,a.Rtime ,a.ResQty ,((a.STime*a.MoPlanQty)+a.Rtime) as TotalTime ,cast( cast( isnull( a.ProduceTime,0) as decimal(18,6)) /cast( 3600 as decimal(18,6)) as decimal(18,6)) as PTime ,a.ManualTime ,a.Status ,a.TenantId ,a.CreationTime MTIME ,a.CreatorUserId ,a.CreatorUserName ,a.EqpCalcType from ( select a.MoCode ,a.ItemCode --,b.ItemName ,e.OpCode ,e.OpSeq --,e2.OPName ,a.MoPlanQty ,isnull( h1.Stime,0) STime ,isnull( h1.Rtime,0) RTime ,isnull(h1.ResQty,0) ResQty ,isnull( g.ProduceTime,0) ProduceTime ,isnull(f.RecordingHours,0) as ManualTime --,0.00 as AlterTime ,'No' as Status ,'{0}' as TenantId ,GETDATE() CreationTime ,'job' as CreatorUserId ,'job' CreatorUserName ,h1.EqpCalcType from IcsMo a with(nolock) join IcsInventory b with(nolock) on a.ItemCode=b.ItemCode and a.TenantId=b.TenantId join IcsMo2Route d with(nolock) on a.MoCode=d.MoCode and d.IsMRoute='Y' and a.TenantId=d.TenantId join IcsMoRoute2Op e with(nolock) on d.MoCode=e.MoCode and d.RouteCode=e.RouteCode and d.TenantId=e.TenantId join IcsOP e2 with(nolock) on e.OpCode=e2.OPCode and e.TenantId=e2.TenantId left join ( select sum(RecordingHours) RecordingHours, t.MoCode,t.OpCode from IcsManualRecording t with(nolock) group by t.MoCode,t.OpCode ) f on a.MoCode=f.MoCode and e.OpCode=f.OpCode left join (select t.MoCode,t.OpCode,SUM( datediff(SS,t.BeginTime,t.EndTime)) ProduceTime from IcsLotOnWipDetail t with(nolock) where t.CollectStatus='End' and t.IsHistory='N' and t.CollectForm='Standard' group by t.MoCode,t.OpCode ) g on a.MoCode=g.MoCode and e.OpCode=g.OpCode left join IcsItem2Route h with(nolock) on b.ItemCode=h.ItemCode and d.RouteCode=h.RouteCode and b.TenantId=h.TenantId left join IcsItemRoute2Op h1 with(nolock) on h.ItemCode=h1.ItemCode and h.RouteCode=h1.RouteCode and e.OpCode=h1.OpCode and h.TenantId=h1.TenantId left join IcsMoHourData h2 with(nolock) on a.mocode=h2.mocode and e.OpCode=h2.OpCode where 1=1 and a.MoStatus='mostatus_close' and a.Default6='0' and a.TenantId='{0}' and e.OpControl='10000000' and h2.Status='No' ) a where 1=1 ) a insert into IcsMoHourData ( MoCode ,ItemCode ,OpCode ,Qty ,STime ,Rtime ,TotalTime ,RealTime ,ManualTime ,MachiningTotalTime ,EqpTotalTime ,ResQty ,HourDiff ,Status ,TenantId ,CreationTime ,CreatorUserId ,CreatorUserName ) select a.MoCode ,a.ItemCode ,a.OpCode ,a.MoPlanQty ,a.STime ,a.Rtime ,a.TotalTime ,a.PTime ,a.ManualTime ,a.MachiningTotalTime ,a.EqpTotalTime ,a.ResQty ,a.HourDiff ,a.Status ,a.TenantId ,a.MTIME ,a.CreatorUserId ,a.CreatorUserName from #tempSyncMoHour_Rhyton a left join IcsMoHourData b on a.MoCode=b.MoCode and a.OpCode=b.OpCode where b.Id is null update IcsMoHourData set STime=a.STime ,Rtime=a.Rtime ,TotalTime=a.TotalTime ,RealTime=a.PTime ,ManualTime=a.ManualTime ,MachiningTotalTime=a.MachiningTotalTime ,EqpTotalTime=a.EqpTotalTime ,ResQty=a.ResQty ,HourDiff=a.HourDiff from #tempSyncMoHour_Rhyton a join IcsMoHourData b on a.MoCode=b.MoCode and a.OpCode=b.OpCode where 1=1 and b.Status='No' "; sql = string.Format(sql, TenantId); // sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#tempSyncMoHour_Rhyton"); // sql += "DROP TABLE #tempSyncCas_Jinyang"; // sql += @""; ICSHelper.ExecuteDate(conStr, sql); } } catch (Exception ex) { log.Error(ex.ToString()); } } } }