using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Quartz;
namespace ICSSoft.FromERP
{
///
/// 杂收杂发(其他出入)
///
public class ICSOtherOutIn : 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.AutoID AS NVARCHAR(50)) AS ErpAutoID,
CAST(a.ID AS NVARCHAR(50)) AS ErpID,
a.cCode AS cCode,
a.dDate AS VoucherDate,
a.cWhCode AS cWhCode,
b.cInvCode AS cInvCode,
b.iQuantity AS iQuantity,
ISNULL(b.iFQuantity,0) AS iFQuantity,
ISNULL(b.cBatch,'') AS cBatch,
'入' AS TransType,
CAST(b.irowno AS NVARCHAR(50)) AS irowno,
'' AS Status,
ISNULL(a.dnverifytime, a.dnmaketime) AS MTIME
,'{0}' as WorkPoint
INTO #TempPick
FROM {1}.dbo.RdRecord08 a
LEFT JOIN {1}.dbo.rdrecords08 b ON a.ID=b.ID
WHERE ISNULL(a.cHandler, '')='' AND ISNULL(a.dnverifytime, a.dnmaketime)>=@LastTime
UNION ALL
SELECT
CAST(b.AutoID AS NVARCHAR(50)) AS ErpAutoID,
CAST(a.ID AS NVARCHAR(50)) AS ErpID,
a.cCode AS cCode,
a.dDate AS VoucherDate,
a.cWhCode AS cWhCode,
b.cInvCode AS cInvCode,
b.iQuantity AS iQuantity,
ISNULL(b.iFQuantity,0) AS iFQuantity,
ISNULL(b.cBatch,'') AS cBatch,
'出' AS TransType,
CAST(b.irowno AS NVARCHAR(50)) AS irowno,
'' AS Status,
ISNULL(a.dnverifytime, a.dnmaketime) AS MTIME
,'{0}' as WorkPoint
FROM {1}.dbo.RdRecord09 a
LEFT JOIN {1}.dbo.rdrecords09 b ON b.ID=a.ID
WHERE ISNULL(a.cHandler, '')='' AND ISNULL(a.dnverifytime, a.dnmaketime)>=@LastTime
IF NOT EXISTS(SELECT * FROM #TempPick)
RETURN";
sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempPick");
sql += @"--删除数据
--DELETE ICSOtherOutIn WHERE ErpAutoID+TransType NOT IN (SELECT CAST(AutoID AS VARCHAR)+'入' FROM {1}.dbo.rdrecords08
--UNION ALL
--SELECT CAST(AutoID AS VARCHAR)+'出' FROM {1}.dbo.rdrecords09)
SELECT a.ErpAutoID+a.TransType ID INTO #tableICSOtherOutIn
FROM ICSOtherOutIn a With(NoLock)
LEFT JOIN (SELECT CAST(AutoID AS VARCHAR)+'入' str1
FROM {1}.dbo.[rdrecords08] With(NoLock)
UNION ALL
SELECT CAST(AutoID AS VARCHAR)+'出' str1
FROM {1}.dbo.[rdrecords09] With(NoLock)
) b ON a.ErpAutoID+a.TransType=b.str1
WHERE a.WorkPoint='{0}' and b.str1 IS NULL
DELETE ICSOtherOutIn
WHERE WorkPoint='{0}' and ErpAutoID+TransType IN ( SELECT ID from #tableICSOtherOutIn)
";
Dictionary values = new Dictionary();
values.Add("cCode", "a.cCode");
values.Add("VoucherDate", "a.VoucherDate");
values.Add("cWhCode", "a.cWhCode");
values.Add("cInvCode", "a.cInvCode");
values.Add("iQuantity", "a.iQuantity");
values.Add("cBatch", "a.cBatch");
values.Add("TransType", "a.TransType");
values.Add("irowno", "a.irowno");
values.Add("Status", "a.Status");
values.Add("MTIME", "a.MTIME");
//更新存在数据
sql += ICSHelper.UpdateSQL("b", values)
+ @" #TempPick a
INNER JOIN ICSOtherOutIn b ON a.ErpAutoID+a.TransType=b.ErpAutoID+a.TransType and a.WorkPoint=b.WorkPoint
WHERE a.WorkPoint='" + WorkPoint + "' ";
values.Add("ID", "NEWID()");
values.Add("ErpAutoID", "a.ErpAutoID");
values.Add("ErpID", "a.ErpID");
values.Add("iFQuantity", "a.iFQuantity");
values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
values.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
values.Add("WorkPoint", "'" + WorkPoint + "'");
//插入新增数据
sql += ICSHelper.InsertSQL("ICSOtherOutIn", values)
+ @" #TempPick a
LEFT JOIN ICSOtherOutIn b ON a.ErpAutoID+a.TransType=b.ErpAutoID+a.TransType and a.WorkPoint=b.WorkPoint
WHERE b.ID IS NULL and a.WorkPoint='" + WorkPoint + @"'
";
sql += "DROP TABLE #TempPick " +
" DROP TABLE #tableICSOtherOutIn";
sql = string.Format(sql, WorkPoint, erpName);
ICSHelper.ExecuteDate(conStr, sql);
#endregion
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
}
}