using cuidian.Common;
using cuidian.Sql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WdtUtils;
using WdtUtils.Load;
using WdtUtils.Proxy;
namespace WangToTempDb
{ ///
/// 旺店通其他入库单上传中间库
///
public class StockinUpLoad : BaseUpLoad
{
//private string __sql = @" if not exists (select * from RdRecord08 as a inner join RdRecords08 as b on a.ID=b.ID where a.ID= '{0}') insert into RdRecord08(ID,cCode,cWhCode,bRdFlag,dDate,dnmaketime,cMemo,dVeriDate,cMaker,cVouchType,cSource,bIsSTQc,bTransFlag,direction) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}') insert into RdRecords08(AutoID,ID,cInvCode,iFlag,iQuantity,iUnitCost) values('{14}','{0}','{15}','{16}','{17}','{18}')";
private string __sql = @"if not exists (select ID from RdRecord08 where ID= {0}) insert into RdRecord08(ID,cCode,cWhCode,bRdFlag,dDate,dnmaketime,cMemo,cMaker,cVouchType,cSource,bIsSTQc,bTransFlag,direction,cDefine15) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}')";
//避免只能插入一条子表数据
private string sql = @" if not exists (select ID from RdRecords08 where ID= {0}) ";
private string newSql = @"insert into RdRecords08(ID,AutoID,cInvCode,iFlag,iQuantity,iUnitCost,iPUnitCost) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')";
protected override Result _LoadFirstPage()
{
StockinProxy tmp = new StockinProxy();
Result rtn = _load.Load(_args, ref tmp);
_proxy = tmp;
return rtn;
}
protected override void _SetLoader()
{
_load = new StockinLoad();
}
protected override void _ImportData()
{
_operType = "插入其他入库单中间表数据";
Stockin_List[] entitys = ((StockinProxy)_proxy).stockin_list;
StringBuilder sb = new StringBuilder();
if (entitys == null || entitys.Length == 0) return;
foreach (Stockin_List item in entitys)
{
if (item.remark.Contains("来源U8") || (!item.order_type.Equals("2") && !item.order_type.Equals("4") && !item.order_type.Equals("6")&& !item.order_type.Equals("7")) || item.remark.Contains("库存管理"))//
{
continue;
}
//sb.AppendLine(string.Format(__sql, new object[] { item.stockin_id,item.order_no, item.warehouse_no,1,item.stockin_time,item.created_time,item.remark,item.check_time,item.operator_name,"08", "库存",0,0,1, item.details_list[0].rec_id, item.details_list[0].goods_no,0,item.details_list[0].goods_count,item.details_list[0].src_price }));
//item.warehouse_no = (item.warehouse_no.Length > 10) ? item.warehouse_no.Substring(0, 10) : item.warehouse_no;
sb.AppendLine(string.Format(__sql, new object[] { item.stockin_id, _ConvertStr(item.order_no), _ConvertStr(item.warehouse_no), 1, item.stockin_time, item.created_time, _ConvertStr(item.remark), _ConvertStr(item.operator_name), _ConvertStr("08"), _ConvertStr("库存"), 0, 0, 1, _ConvertNotStr(item.order_type) }));//cVouchType单据类型编码 ,cSource零售来源单据号,bIsSTQc库存期初标志,bTransFlag是否传递
sb.AppendLine(string.Format(sql, new object[] { item.stockin_id}));
Details_List[] dl = item.details_list;
sb.AppendLine("begin");
foreach (Details_List ty in dl)
{
sb.AppendLine(string.Format(newSql, new object[] { item.stockin_id, ty.rec_id, _ConvertStr(ty.spec_no), 0,_ConvertNotStr( ty.goods_count),_ConvertNotStr( ty.cost_price),_ConvertNotStr(ty.total_cost) }));
}
sb.AppendLine("end");
_InsertIntoTempDb(sb.ToString(), item.order_no);
}
}
protected override void _DeleteDup()
{
//_operType = "删除其他入库单中间表重复数据";
////string sql = "delete a from {0}..RdRecord08 a,RdRecords08 b,{1}..RdRecord08 c,RdRecords08 d where a.ID = b.ID and b.ID=c.ID and c.ID=d.ID";
//string sql1 = "delete a from {0}..RdRecord08 a,{1}..RdRecord08 b where a.ID=b.cdefine5";
//string sql2 = "delete a from {0}..RdRecords08 a,{1}..RdRecords08 b where a.ID=b.cdefine27";
//DbUtils.ExecuteNonQuery(string.Format(sql1, _tempDb.Database, _targetDb.Database), _tempDb);
//DbUtils.ExecuteNonQuery(string.Format(sql2, _tempDb.Database, _targetDb.Database), _tempDb);
}
protected override void _LoadNextPage()
{
_proxy = _load.Load(_args);
}
}
}