using NFine.Data.Extensions; using Quartz; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; namespace ICSSoft.FromERP { public class IcsAutoMo2Lot4JWX : 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 conERPStr = ICSHelper.GetERPConnectString(); string conStr = ICSHelper.GetConnectString(); string Namespace = this.GetType().Namespace; //string Class = this.GetType().Name; // List itemCodeList = new List() { "A2001", "A2002", "KA10", "A2003" }; DataTable dt = ICSHelper.GetERPDB(conStr); foreach (DataRow dr in dt.Rows) { string erpName = ICSHelper.GetConfigString()["ERPDB"]; string TenantId = dr["TenantId"].ToString(); string TenantCode = dr["TenantCode"].ToString(); var sql0 = @"select RuleCode,a.Prefix,a.Suffix,a.DateCode,a.SerialLength,a.RuleSeq from SysLabelRule a with(nolock) where RuleCode='Mo2Lot' and TenantId='" + TenantId + "' "; var ruleDt = ICSHelper.ExecuteTable(conStr, sql0); if (ruleDt == null || ruleDt.Rows.Count == 0) { return; } string sql1 = @" select a.MoCode,a.MoSeq ,a.Id ,a.MoPlanQty from IcsMo a with(nolock) where exists ( select ItemCode from IcsManufacturingHead t with(nolock) where (ContractNo like 'NK%' OR ContractNo like 'FW%') and t.ItemCode=a.ItemCode ) and a.MoPlanQty>0 and not exists (select MoCode from IcsMo2Lot with(nolock) where MoCode=a.MoCode) and TenantId='" + TenantId + "' "; var itemDt = ICSHelper.ExecuteTable(conStr, sql1);//生产工单 if (itemDt != null && itemDt.Rows.Count > 0) { string sql2 = ""; foreach (DataRow dr2 in itemDt.Rows) { var qty = dr2["MoPlanQty"].ToInt(); var sql4 = ""; //更新工单表 sql2 += @" update icsmo set MoStatus='mostatus_release',LastModificationTime=GETDATE(),LastModifierUserId='c65321b94c804dc26eb93a0ba67c8a2a',LastModifierUserName='xusc' where Id=" + dr2["Id"].ToInt64() + " and MoStatus='mostatus_initial' and TenantId='" + TenantId + "' "; for (int i = 0; i < qty; i++) { //查询序列号 var sql3 = @" EXEC Addins_GetSerialCode '" + TenantId + "','IcsMo2Lot','Lotno','" + ruleDt.Rows[0]["Prefix"].ToString() + "','" + ruleDt.Rows[0]["Suffix"].ToString() + "','" + DateTime.Now.ToString(ruleDt.Rows[0]["DateCode"].ToString()) + "'," + ruleDt.Rows[0]["SerialLength"].ToInt() + ",'" + ruleDt.Rows[0]["RuleSeq"].ToStringExt() + "'"; var lotno = ICSHelper.ExecuteScalar(conStr, sql3).ToStringExt(); sql4 += @" insert into IcsMo2Lot (MoId,MoCode,MoSeq,Lotno,LotSeq,LotQty,LotStatus,PrintTimes,TenantId,CreationTime,CreatorUserId,CreatorUserName) select " + dr2["Id"].ToInt64() + ",'" + dr2["MoCode"].ToStringExt() + "'," + dr2["MoSeq"].ToInt64() + ",'" + lotno + "',1," + dr2["MoPlanQty"].ToDecimal() + ",'lotstatus_new',0,'" + TenantId + "',GETDATE(),'c65321b94c804dc26eb93a0ba67c8a2a','xusc'"; } ICSHelper.ExecuteDate(conStr, sql4);//批量更新批次(单个工单) } ICSHelper.ExecuteDate(conStr, sql2);//批量更新工单 } } } catch (Exception ex) { log.Error(ex.ToString()); } } } }