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 IcsAutoMo2Lot : 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);
Dictionary dis = new Dictionary() {
{ "A2001", "ZP01_Route" },
{ "A2002", "ZP02_Route" },
{ "KA10", "ZP03_Route" },
{ "A2003", "ZP04_Route" },
};
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;
}
foreach (var item in dis)
{
string sql1 = @" select a.MoCode,a.MoSeq, a.Id,a.MoPlanQty from IcsMo a with(nolock)
where a.ItemCode like '" + item.Key+ "%' " +
" and not exists (select MoCode from IcsMo2Lot with(nolock) where MoCode=a.MoCode)" +
" and a.mocode like 'MOSCZP%' " +
" and a.MoPlanQty=1 " +
" and a.TenantId='" + TenantId+"' ";
var itemDt = ICSHelper.ExecuteTable(conStr, sql1);
if (itemDt != null && itemDt.Rows.Count > 0)
{
string sql2 = "";
foreach (DataRow dr2 in itemDt.Rows)
{
//更新工单表
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 + "' ";
//查询序列号
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();
sql2 += @"
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, sql2);
}
}
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
}
}