using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Quartz;
namespace ICSSoft.FromERP
{
///
/// 采购订单
///
public class ICSPO : 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 PODetailID,
b.POID AS POID,
a.cPOID AS POCode,
isnull(ss.AutoID ,'')as PRDetailID,
isnull(ss.ID,'') as PRID,
CAST (b.ivouchrowno AS VARCHAR(50)) AS PORow,
a.dPODate AS PODate,
a.cVenCode AS VenCode,
v.cVenName AS VenName,
a.cPersonCode AS PersonCode,
p.cPersonName AS PersonName,
a.cDepCode AS GroupCode,
d.cDepName AS GroupName,
a.cexch_name AS CurrencyName,
a.cPTCode AS SubjectCode,
pt.cPTName AS ProjectType,
b.cInvCode AS InvCode,
b.iQuantity AS Quantity,
a.cMaker AS CreatePerson,
a.cmaketime as CreateDateTime,
b.dArriveDate AS PreArriveDate,
'2' AS Status,
isnull(b.iNum,0) AS Amount,
'1' AS POType,
'BS' AS ServerModel,
b.iTaxPrice AS Price,
'' AS ORDERNO,
ISNULL(b.cbCloseTime,ISNULL(a.cChangAuditTime,ISNULL(a.cAuditTime, ISNULL(a.cModifyTime, a.cmaketime)))) AS MTIME,
'' AS MEMO,
a.cDefine1 AS WHCode,
a.cdefine13 AS ReMark,
a.cMaker AS DateTime1User,
a.dPODate AS DateTime1,
a.cVerifier AS DateTime2User,
a.cAuditDate AS DateTime2,
CASE WHEN b.cbCloseTime IS NULL THEN '' ELSE '关闭' END AS ISUrgent,
--(CASE WHEN inv.bCheckBatch=1 THEN isnull(b.cBatch,'') ELSE '' END) as cBatch,
isnull(b.cItemCode,'') ProjectCode,'' cBatch,'' version ,'' brand,
(case WHEN inv.bFree1=1 THEN b.cFree1 ELSE '' END) as cFree1,
(case WHEN inv.bFree2=1 THEN b.cFree2 ELSE '' END) as cFree2,
(case WHEN inv.bFree3=1 THEN b.cFree3 ELSE '' END) as cFree3,
(case WHEN inv.bFree4=1 THEN b.cFree4 ELSE '' END) as cFree4,
(case WHEN inv.bFree5=1 THEN b.cFree5 ELSE '' END) as cFree5,
(case WHEN inv.bFree6=1 THEN b.cFree6 ELSE '' END) as cFree6,
(case WHEN inv.bFree7=1 THEN b.cFree7 ELSE '' END) as cFree7,
(case WHEN inv.bFree8=1 THEN b.cFree8 ELSE '' END) as cFree8,
(case WHEN inv.bFree9=1 THEN b.cFree9 ELSE '' END) as cFree9,
(case WHEN inv.bFree10=1 THEN b.cFree10 ELSE '' END) as cFree10,CAST('' AS NVARCHAR(500)) as Colspan
, '{0}' as WorkPoint
INTO #TempPO
FROM {1}.dbo.PO_Pomain a
INNER JOIN {1}.dbo.PO_Podetails b ON a.POID = b.POID
LEFT JOIN {1}.dbo.Vendor v ON a.cVenCode = v.cVenCode
LEFT JOIN {1}.dbo.Department d ON a.cDepCode = d.cDepCode
LEFT JOIN {1}.dbo.Person p ON a.cPersonCode = p.cPersonCode
LEFT JOIN {1}.dbo.PurchaseType pt ON a.cPTCode = pt.cPTCode
LEFT JOIN {1}.dbo.PayCondition pay ON a.cPayCode = pay.cPayCode
LEFT JOIN {1}.dbo.Inventory inv ON b.cInvCode = inv.cInvCode
LEFT JOIN {1}.dbo.ComputationUnit unit ON unit.cComunitCode = inv.cComUnitCode
LEFT JOIN {1}.dbo.PU_AppVouchs ss on b.iAppIds=ss.AutoID
where ISNULL(b.cbCloseTime,ISNULL(a.cChangAuditTime,ISNULL(a.cAuditTime, ISNULL(a.cModifyTime, a.cmaketime))))>=@LastTime
and a.cVerifier IS NOT NULL
update #TempPO 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 #TempExtensionPO
from #TempPO
IF NOT EXISTS(SELECT * FROM #TempPO)
RETURN";
sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempPO");
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)
+ @"#TempExtensionPO 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 ICSPurchaseOrder WHERE PODetailID NOT IN (SELECT CAST(ID AS VARCHAR) FROM {1}.dbo.[PO_Podetails])
SELECT a.PODetailID INTO #tableICSPurchaseOrder
FROM ICSPurchaseOrder a With(NoLock)
LEFT JOIN {1}.dbo.[PO_Podetails] b With(NoLock) ON a.PODetailID=CAST(b.ID AS VARCHAR)
WHERE a.WorkPoint='{0}' and b.ID IS NULL
DELETE ICSPurchaseOrder
WHERE WorkPoint='{0}' and PODetailID IN ( SELECT PODetailID from #tableICSPurchaseOrder)
";
Dictionary values = new Dictionary();
values.Add("POID", "a.POID");
values.Add("POCode", "a.POCode");//采购订单号
values.Add("Sequence", "a.PORow");//采购订单行号
values.Add("VenCode", "a.VenCode");//供应商代码
values.Add("DepCode", "a.GroupCode");//部门代码
values.Add("PersonCode", "a.PersonCode");//业务员代码
values.Add("InvCode", "a.InvCode");//物料代码
values.Add("Quantity", "a.Quantity");//数量
values.Add("Amount", "a.Amount");//辅计量
values.Add("UnitPrice", "a.Price");//价格
values.Add("Currency", "a.CurrencyName");//币种
values.Add("Status", "a.Status");
values.Add("CreatePerson", "a.CreatePerson");//创建人
values.Add("PlanArriveDate", "a.PreArriveDate");//预计到货日期
values.Add("CreateDateTime", "a.CreateDateTime");//业务员名称
values.Add("PRDetailID", "a.PRDetailID");
values.Add("PRID", "a.PRID");
values.Add("ExtensionID", "c.ID");
values.Add("PODetailID", "a.PODetailID");
//values.Add("POID", "a.POID");
values.Add("MTIME", "a.MTIME");
//更新存在数据
sql += ICSHelper.UpdateSQL("b", values)
+ @" #TempPO a
INNER JOIN ICSPurchaseOrder b ON a.PODetailID+a.POType=b.PODetailID+b.POType 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("PODetailID", "a.Free2");
//values.Add("POType", "a.POType");
//values.Add("ServerModel", "a.ServerModel");
values.Add("ID", "NEWID()");//ID
values.Add("POType", "a.POType");//采购类型
values.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");//操作人
values.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");//操作人姓名
values.Add("WorkPoint", "'" + WorkPoint + "'");//站点
values.Add("EATTRIBUTE1", "''");//站点
values.Add("ReleaseState", "'0'");//发布状态
//插入新增数据
sql += ICSHelper.InsertSQL("ICSPurchaseOrder", values)
+ @" #TempPO a
LEFT JOIN ICSPurchaseOrder b ON a.PODetailID=b.PODetailID and a.WorkPoint=b.WorkPoint
inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
WHERE b.PODetailID IS NULL and a.WorkPoint='" + WorkPoint + @"' ";
sql += " DROP TABLE #TempPO";
sql += " DROP TABLE #TempExtensionPO " +
" DROP TABLE #tableICSPurchaseOrder ";
sql = string.Format(sql, WorkPoint, erpName);
ICSHelper.ExecuteDate(conStr, sql);
#endregion
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
}
}