using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Quartz;
namespace ICSSoft.FromERP
{
///
/// 材料出库单
///
public class ICSMOIssue : 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
A.DocNo as IssueCode,
B.LineNum as Sequence,
CASE WHEN A.IssueType=0 THEN C.DocNo WHEN A.IssueType=1 THEN E.DocNo END as SourceCode,
CASE WHEN A.IssueType=0 THEN 1 WHEN A.IssueType=1 THEN D.LineNum END as SourceSequence,
F.Code as InvCode,
B.IssuedQty as Quantity,
0 as Amount,
G.Code as WHCode,
CASE WHEN A.IssueType=0 THEN '1' WHEN A.IssueType=1 THEN '2' END as Type,
'1' as Status,
A.CreatedBy as CreatePerson,
A.CreatedOn as CreateDateTime,
A.ID as IssueID,
B.ID as IssueDetailID ,
ISNULL(B.ModifiedOn,B.CreatedOn) AS MTIME,
isnull(I.Code,'') ProjectCode,isnull(B.LotNo,'') cBatch,'' version ,'' brand,
'' as cFree1,
'' as cFree2,
'' as cFree3,
'' as cFree4,
'' as cFree5,
'' as cFree6,
'' as cFree7,
'' as cFree8,
'' as cFree9,
'' as cFree10,CAST('' AS NVARCHAR(500)) as Colspan
,H.Code as WorkPoint
,B.MOPick PickID ,'' AppLyDetailID
INTO #TempRd
from {1}.dbo.MO_IssueDoc A
LEFT JOIN {1}.dbo.MO_IssueDocLine B ON IssueDoc=A.ID
LEFT JOIN {1}.dbo.MO_MO C ON C.ID=B.MO
LEFT JOIN {1}.dbo.MO_IssueDocLine D ON D.ID=B.SourceIssueDocLine
LEFT JOIN {1}.dbo.MO_IssueDoc E ON E.ID=D.IssueDoc
LEFT JOIN {1}.dbo.CBO_ItemMaster F ON F.ID=B.Item
LEFT JOIN {1}.dbo.CBO_Wh G ON G.ID=B.Wh
LEFT JOIN {1}.dbo.Base_Organization H ON H.ID=A.Org
LEFT JOIN {1}.dbo.CBO_Project I ON I.ID=B.Project
where (A.IssueType=1 OR A.IssueType=2) AND A.DocState=3 AND ISNULL(B.ModifiedOn,B.CreatedOn)>=@LastTime
update #TempRd 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 #TempRd
IF NOT EXISTS(SELECT * FROM #TempRd)
RETURN";
sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempRd");
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 ICSMOIssue where ISNULL(IssueDetailID,'') NOT IN ( SELECT autoID FROM {1}.dbo.rdrecords11 )
SELECT ISNULL(a.IssueDetailID,'') IssueDetailID INTO #tableICSMOIssue
FROM ICSMOIssue a With(NoLock)
LEFT JOIN {1}.dbo.[MO_IssueDocLine] b With(NoLock) ON ISNULL(a.IssueDetailID,'')=b.ID
WHERE a.WorkPoint='{0}' and b.ID IS NULL
DELETE ICSMOIssue
WHERE WorkPoint='{0}' and IssueDetailID IN ( SELECT IssueDetailID from #tableICSMOIssue)
";
sql += "\r\n";
Dictionary values = new Dictionary();
values.Add("IssueCode", "a.IssueCode");
values.Add("Sequence", "a.Sequence");
values.Add("SourceCode", "a.SourceCode");
values.Add("SourceSequence", "a.SourceSequence");
values.Add("InvCode", "a.InvCode");
values.Add("Quantity", "a.Quantity");
values.Add("Amount", "a.Amount");
values.Add("WHCode", "a.WHCode");
values.Add("Type", "a.Type");
values.Add("Status", "a.Status");
values.Add("CreatePerson", "a.CreatePerson");
values.Add("CreateDateTime", "a.CreateDateTime");
values.Add("IssueID", "a.IssueID");
values.Add("IssueDetailID", "a.IssueDetailID");
values.Add("PickID", "a.PickID");
values.Add("AppLyDetailID", "a.AppLyDetailID");
values.Add("ExtensionID", "c.ID");
values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
values.Add("MUSERNAME", "'" + ConstWorkPoint.Musername + "'");
values.Add("WorkPoint", "'" + WorkPoint + "'");
values.Add("MTIME", "a.MTIME");
//更新存在数据
sql += "\r\n";
sql += ICSHelper.UpdateSQL("b", values)
+ @" #TempRd a
INNER JOIN ICSMOIssue b ON a.IssueDetailID=b.IssueDetailID 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("EATTRIBUTE3", "a.EATTRIBUTE3");
values.Add("ID", "newid()");
values.Add("IssueQuantity", "0");
//插入新增数据
sql += "\r\n";
sql += ICSHelper.InsertSQL("ICSMOIssue", values)
+ @" #TempRd a
LEFT JOIN ICSMOIssue b ON a.IssueDetailID=b.IssueDetailID and a.WorkPoint=b.WorkPoint
inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
WHERE b.IssueDetailID IS NULL and a.WorkPoint='" + WorkPoint + @"'
";
sql += " DROP TABLE #TempRd ";
sql += " DROP TABLE #TempExtension " +
" DROP TABLE #tableICSMOIssue";
sql = string.Format(sql, WorkPoint, erpName);
ICSHelper.ExecuteDate(conStr, sql);
#endregion
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
}
}