using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Quartz;
namespace ICSSoft.FromERP
{
///
/// 采购订单
///
public class ICSPORed : 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.MODetailsID AS VARCHAR(50)) AS Free2,
b.MOID AS Free1,
c.cCode AS POCode,
CAST (b.iVouchRowNo AS VARCHAR(50)) AS PORow,
c.dDate AS PODate,
c.cVenCode AS VenCode,
v.cVenName AS VenName,
c.cPersonCode AS PersonCode,
p.cPersonName AS PersonName,
c.cDepCode AS GroupCode,
q.cDepName AS GroupName,
c.cexch_name AS CurrencyName,
c.cPTCode AS SubjectCode,
pt.cPTName AS ProjectType,
b.cInvCode AS InvCode,
b.iQuantity AS Quantity,
c.cMaker AS CreatePerson,
b.dArriveDate AS PreArriveDate,
c.cState AS Status,
'委外' AS POType,
'CS' AS ServerModel,
b.iTaxPrice AS Price,
'' AS ORDERNO,
ISNULL(b.dbCloseTime,ISNULL(c.dChangeVerifyTime,ISNULL(c.dVerifyTime, ISNULL(c.dModifyTime, c.dCreateTime)))) AS MTIME,
'' AS MEMO,
'' AS WHCode,
c.cdefine13 AS ReMark,
c.cMaker AS DateTime1User,
c.dDate AS DateTime1,
c.cVerifier AS DateTime2User,
c.dVerifyDate AS DateTime2,
CASE WHEN b.dbCloseTime 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
, '{0}' as WorkPoint
INTO #TempPO
FROM {1}.dbo.OM_MODetails b
LEFT JOIN {1}.dbo.OM_MOMain c ON b.MOID = c.MOID
LEFT JOIN {1}.dbo.Inventory d ON b.cInvCode = d.cInvCode
LEFT JOIN {1}.dbo.PurchaseType pt ON c.cPTCode = pt.cPTCode
LEFT JOIN {1}.dbo.ComputationUnit g ON d.cComUnitCode = g.cComunitCode
LEFT JOIN {1}.dbo.Vendor v ON c.cVenCode = v.cVenCode
LEFT JOIN {1}.dbo.Department q ON c.cDepCode = q.cDepCode
LEFT JOIN {1}.dbo.Person p ON c.cPersonCode = p.cPersonCode
LEFT JOIN {1}.dbo.PU_AppVouchs ss on b.iAppIds=ss.AutoID
WHERE c.cverifier IS NOT NULL AND ISNULL(b.dbCloseTime,ISNULL(c.dChangeVerifyTime,ISNULL(c.dVerifyTime, ISNULL(c.dModifyTime, c.dCreateTime))))>=@LastTime
select distinct
ProjectCode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,WorkPoint,cBatch,version ,brand
INTO #TempExtension
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.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");
value.Add("MTIME", "GETDATE()");
value.Add("MUSER", "'" + ConstWorkPoint.Muser + "'");
value.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");
value.Add("WorkPoint", "'" + WorkPoint + "'");
//插入新增数据->ICSExtension表
sql += ICSHelper.InsertSQL("ICSExtension", value)
+ @"#TempPO 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+POType NOT IN (SELECT CAST(ID AS VARCHAR)+'采购' FROM {1}.dbo.[PO_Podetails] UNION ALL SELECT CAST(MODetailsID AS VARCHAR)+'委外' FROM {1}.dbo.[OM_MODetails])
SELECT a.PODetailID+a.POType ID INTO #tableICSPurchaseOrder
FROM ICSPurchaseOrder a With(NoLock)
LEFT JOIN (SELECT CAST(ID AS VARCHAR)+'采购' str1
FROM {1}.dbo.[PO_Podetails] With(NoLock)
UNION ALL
SELECT CAST(MODetailsID AS VARCHAR)+'委外' str1
FROM {1}.dbo.[OM_MODetails] With(NoLock)
) b ON a.PODetailID+a.POType=b.str1
WHERE a.WorkPoint='{0}' and b.str1 IS NULL
DELETE ICSPurchaseOrder
WHERE WorkPoint='{0}' and PODetailID+POType IN ( SELECT ID 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("POType", "a.ProjectType");//采购类型
values.Add("InvCode", "a.InvCode");//物料代码
values.Add("Quantity", "a.Quantity");//数量
values.Add("UnitPrice", "a.Price");//价格
values.Add("Currency", "a.CurrencyName");//币种
values.Add("Status", "a.Status");
values.Add("CreatePerson", "a.CreatePerson");//创建人
values.Add("ReleaseState", "'未发布'");//发布状态
values.Add("PlanArriveDate", "a.PreArriveDate");//预计到货日期
//values.Add("PersonCode", "a.PersonName");//业务员名称
values.Add("PRDetailID", "a.PRDetailID");
values.Add("PRID", "a.PRID");
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.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=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("MUSER", "'" + ConstWorkPoint.Muser + "'");//操作人
values.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");//操作人姓名
values.Add("WorkPoint", "'" + WorkPoint + "'");//站点
//插入新增数据
sql += ICSHelper.InsertSQL("ICSPurchaseOrder", values)
+ @" #TempPO a
LEFT JOIN ICSPurchaseOrder b ON a.PODetailID+a.POType=b.PODetailID+b.POType and a.WorkPoint=b.WorkPoint
inner join ICSExtension c 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=c.Colspan and a.WorkPoint=c.WorkPoint
WHERE b.PODetailID IS NULL and a.WorkPoint='" + WorkPoint + @"' ";
sql += "DROP TABLE #TempPO";
sql += " DROP TABLE #TempExtension " +
" DROP TABLE #tableICSPurchaseOrder";
sql = string.Format(sql, WorkPoint, erpName);
ICSHelper.ExecuteDate(conStr, sql);
#endregion
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
}
}