using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Quartz;
namespace ICSSoft.FromERP
{
///
/// 领料单
///
public class ICSMOApply : 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 CAST(b.ID as VARCHAR(50)) AS ApplyID ,
b.cCode AS ApplyCode ,
CAST(a.AutoID as VARCHAR(50)) AS ApplyDetailID ,
CAST(a.irowno as VARCHAR(50)) AS Sequence ,
ISNULL(a.cmocode,'') AS SourceCode ,
a.imoseq AS SourceSequence ,
ISNULL(a.cInvCode,'') AS InvCode ,
CASE WHEN e.bCutMantissa='1' AND e.bKCCutMantissa='1' THEN CEILING(a.iQuantity) ELSE a.iQuantity END AS Quantity ,
a.fOutQuantity AS DemandQuantity,
ISNULL(a.cWhCode,'') AS WHCode ,
e.cComUnitCode AS ComUnitCode ,
'2' AS Status ,
b.cMaker AS MUSER ,
'' AS MUSERName ,
b.dModifyDate AS VoucherDate ,
b.cMaker as CreatePerson,
b.dnmaketime as CreateDateTime,
ISNULL(b.dnverifytime,ISNULL(b.dnmodifytime, b.dnmaketime)) AS MTIME ,
'1' AS MoveType ,
'' AS ORDERNO ,
'' AS DepCode,
isnull(a.cItemCode,'') ProjectCode,isnull(a.cbatch,'') cBatch,'' version ,'' brand,
(case WHEN e.bFree1=1 THEN a.cFree1 ELSE '' END) as cFree1,
(case WHEN e.bFree2=1 THEN a.cFree2 ELSE '' END) as cFree2,
(case WHEN e.bFree3=1 THEN a.cFree3 ELSE '' END) as cFree3,
(case WHEN e.bFree4=1 THEN a.cFree4 ELSE '' END) as cFree4,
(case WHEN e.bFree5=1 THEN a.cFree5 ELSE '' END) as cFree5,
(case WHEN e.bFree6=1 THEN a.cFree6 ELSE '' END) as cFree6,
(case WHEN e.bFree7=1 THEN a.cFree7 ELSE '' END) as cFree7,
(case WHEN e.bFree8=1 THEN a.cFree8 ELSE '' END) as cFree8,
(case WHEN e.bFree9=1 THEN a.cFree9 ELSE '' END) as cFree9,
(case WHEN e.bFree10=1 THEN a.cFree10 ELSE '' END) as cFree10,CAST('' AS NVARCHAR(500)) as Colspan
,'{0}' as WorkPoint
INTO #TempPick
FROM {1}.dbo.MaterialAppVouchs a
INNER JOIN {1}.dbo.MaterialAppVouch b ON a.ID = b.ID
LEFT JOIN {1}.dbo.mom_orderdetail c ON a.iMPoIds = c.MoDId
LEFT JOIN {1}.dbo.mom_order d ON c.MoId = d.MoId
LEFT JOIN {1}.dbo.Inventory e ON a.cInvCode = e.cInvCode
WHERE ISNULL(b.cHandler,'')<> ''and b.cSource='生产订单'
AND ISNULL(b.dnverifytime,ISNULL(b.dnmodifytime, b.dnmaketime))>=@LastTime
update #TempPick 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 #TempExtension
from #TempPick
IF NOT EXISTS(SELECT * FROM #TempPick)
RETURN";
sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempPick");
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)
+ @"#TempExtension 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 ICSMOApply WHERE ApplyDetailID IN (SELECT ApplyDetailID FROM ICSMOApply a left join {1}.dbo.MaterialAppVouchs b on a.ApplyDetailID=b.AutoID where b.AutoID is null) AND LEN(ApplyDetailID)>0
SELECT a.ApplyDetailID INTO #tableICSMOApply
FROM ICSMOApply a With(NoLock)
LEFT JOIN {1}.dbo.[MaterialAppVouchs] b With(NoLock) ON a.ApplyDetailID=b.AutoID
WHERE a.WorkPoint='{0}' and b.AutoID IS NULL AND LEN(a.ApplyDetailID)>0
DELETE ICSMOApply
WHERE WorkPoint='{0}' and ApplyDetailID IN ( SELECT ApplyDetailID from #tableICSMOApply)
";
Dictionary values = new Dictionary();
values.Add("ApplyCode", "a.ApplyCode");
values.Add("Sequence", "a.Sequence");
values.Add("SourceCode", "a.SourceCode");
values.Add("SourceSequence", "a.SourceSequence");
values.Add("InvCode", "a.InvCode");
//values.Add("SubInvCode", "a.SubInvCode");
values.Add("Quantity", "a.Quantity");
values.Add("Amount", "0");
values.Add("WHCode", "a.WHCode");
values.Add("Type", "1");
values.Add("Status", "a.Status");
//values.Add("VoucherDate", "a.VoucherDate");
//values.Add("EATTRIBUTE", "a.EATTRIBUTE");
values.Add("CreatePerson", "a.CreatePerson");
values.Add("CreateDateTime", "a.CreateDateTime");
values.Add("ApplyID", "a.ApplyID");
values.Add("ApplyDetailID", "a.ApplyDetailID");
values.Add("ExtensionID", "c.ID");
values.Add("MUSER", "a.MUSER");
values.Add("MUSERName", "a.MUSERName");
values.Add("MTIME", "a.MTIME");
//values.Add("MoveType", "a.MoveType");
// values.Add("ORDERNO", "a.ORDERNO");
values.Add("WorkPoint", "'" + WorkPoint + "'");
//values.Add("WorkPoint", "a.WorkPoint");
// values.Add("cBatch", "a.cBatch");
//values.Add("DepCode", "a.DepCode");
//更新存在数据
sql += ICSHelper.UpdateSQL("b", values)
+ @" #TempPick a
INNER JOIN ICSMOApply b ON a.ApplyDetailID=b.ApplyDetailID 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("IssueQuantity", "'0'");
//插入新增数据
sql += ICSHelper.InsertSQL("ICSMOApply", values)
+ @" #TempPick a
LEFT JOIN ICSMOApply b ON a.ApplyDetailID=b.ApplyDetailID 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 #TempPick ";
sql += " DROP TABLE #TempExtension " +
" DROP TABLE #tableICSMOApply";
sql = string.Format(sql, WorkPoint, erpName);
ICSHelper.ExecuteDate(conStr, sql);
#endregion
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
}
}