using Quartz;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
namespace ICSSoft.FromERP
{
///
/// 委外采购订单
///
public class ICSOutsourcingOrder : 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 OODetailID,
b.MOID AS OOID,
c.cCode AS OOCode,
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.dCreateTime as CreateDateTime,
'2' AS Status,
isnull(b.iNum,0) AS Amount,
'1' AS Type,
'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,
isnull(b.cItemCode,'') ProjectCode,'' cBatch,'' version ,'' brand,
(case WHEN d.bFree1=1 THEN isnull(b.cFree1,'') ELSE '' END) as cFree1,
(case WHEN d.bFree2=1 THEN isnull(b.cFree2,'') ELSE '' END) as cFree2,
(case WHEN d.bFree3=1 THEN isnull(b.cFree3,'') ELSE '' END) as cFree3,
(case WHEN d.bFree4=1 THEN isnull(b.cFree4,'') ELSE '' END) as cFree4,
(case WHEN d.bFree5=1 THEN isnull(b.cFree5,'') ELSE '' END) as cFree5,
(case WHEN d.bFree6=1 THEN isnull(b.cFree6,'') ELSE '' END) as cFree6,
(case WHEN d.bFree7=1 THEN isnull(b.cFree7,'') ELSE '' END) as cFree7,
(case WHEN d.bFree8=1 THEN isnull(b.cFree8,'') ELSE '' END) as cFree8,
(case WHEN d.bFree9=1 THEN isnull(b.cFree9,'') ELSE '' END) as cFree9,
(case WHEN d.bFree10=1 THEN isnull(b.cFree10,'') ELSE '' END) as cFree10,CAST('' AS NVARCHAR(500)) as Colspan
, '{0}' as WorkPoint
INTO #TempPOWW
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
WHERE c.cverifier IS NOT NULL AND ISNULL(b.dbCloseTime,ISNULL(c.dChangeVerifyTime,ISNULL(c.dVerifyTime, ISNULL(c.dModifyTime, c.dCreateTime))))>=@LastTime
and c.cVerifier IS NOT NULL --AND ISNULL(b.cbCloser,'')=''
update #TempPOWW 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 #TempPOWW
IF NOT EXISTS(SELECT * FROM #TempPOWW)
RETURN";
sql = ICSHelper.Time(Namespace, Class, WorkPoint, sql, "#TempPOWW");
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 ICSOutsourcingOrder WHERE OODetailID NOT IN ( SELECT CAST(MODetailsID AS VARCHAR) FROM {1}.dbo.[OM_MODetails])
SELECT a.OODetailID INTO #tableICSOutsourcingOrder
FROM ICSOutsourcingOrder a With(NoLock)
LEFT JOIN {1}.dbo.[OM_MODetails] b With(NoLock) ON a.OODetailID= CAST(b.MODetailsID AS VARCHAR)
WHERE a.WorkPoint='{0}' and b.MODetailsID IS NULL
DELETE ICSOutsourcingOrder
WHERE WorkPoint='{0}' and OODetailID IN ( SELECT OODetailID from #tableICSOutsourcingOrder)
";
Dictionary values = new Dictionary();
values.Add("OOCode", "a.OOCode");//采购订单号
values.Add("Sequence", "a.PORow");//采购订单行号
values.Add("VenCode", "a.VenCode");//供应商代码
values.Add("Amount", "a.Amount");//辅计量
values.Add("DepCode", "a.GroupCode");//部门代码
values.Add("PersonCode", "a.PersonCode");//业务员代码
values.Add("Type", "a.Type");//采购类型
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("PlanArriveDate", "a.PreArriveDate");//预计到货日期
values.Add("CreateDateTime", "a.CreateDateTime");//业务员名称
values.Add("OODetailID", "a.OODetailID");
values.Add("OOID", "a.OOID");
values.Add("ExtensionID", "c.ID");
//values.Add("POID", "a.POID");
values.Add("MTIME", "a.MTIME");
//更新存在数据
sql += ICSHelper.UpdateSQL("b", values)
+ @" #TempPOWW a
INNER JOIN ICSOutsourcingOrder b ON a.OODetailID+a.Type=b.OODetailID+b.Type 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("MUSER", "'" + ConstWorkPoint.Muser + "'");//操作人
values.Add("MUSERName", "'" + ConstWorkPoint.Musername + "'");//操作人姓名
values.Add("WorkPoint", "'" + WorkPoint + "'");//站点
values.Add("EATTRIBUTE1", "''");//站点
values.Add("ReleaseState", "'0'");//发布状态
//插入新增数据
sql += ICSHelper.InsertSQL("ICSOutsourcingOrder", values)
+ @" #TempPOWW a
LEFT JOIN ICSOutsourcingOrder b ON a.OODetailID+a.Type=b.OODetailID+b.Type and a.WorkPoint=b.WorkPoint
inner join ICSExtension c on a.Colspan=c.Colspan and a.WorkPoint=c.WorkPoint
WHERE b.OODetailID IS NULL and a.WorkPoint='" + WorkPoint + @"' ";
sql += " DROP TABLE #TempPOWW";
sql += " DROP TABLE #TempExtension " +
" DROP TABLE #tableICSOutsourcingOrder";
sql = string.Format(sql, WorkPoint, erpName);
ICSHelper.ExecuteDate(conStr, sql);
#endregion
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
}
}