using cuidian.Common;
using cuidian.Sql;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WdtUtils;
using WdtUtils.Load;
using WdtUtils.Proxy;
namespace WangToTempDb
{ ///
/// 旺店通销售订单上传中间库
///
public class TradeUpLoad : BaseUpLoad
{
//private string __sql = @"if not exists (select * from SO_SOMain as a inner join SO_SODetails as b on a.ID=b.ID where a.ID= '{0}') insert into SO_SOMain(ID,cSOCode,dDate,cCusCode,cCusName,iTaxRate,cPersonCode,cVerifier,cexch_name,direction,cSTCode,cDepCode,bReturnFlag,iVTid,cBusType) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}') insert into SO_SODetails(ID,cInvCode,iQuantity,dPreDate,iTaxRate,iQuotedPrice,iSOsID) values('{0}','{15}','{16}','{17}','{18}','{19}','{0}')";
private string __sql = @"if not exists (select ID from SO_SOMain where ID= {0}) insert into SO_SOMain(ID,cSOCode,dDate,cCusCode,cCusName,iTaxRate,cPersonCode,cVerifier,cexch_name,direction,cSTCode,cDepCode,bReturnFlag,iVTid,cBusType,platform_id,ccontactname,cDefine1,cDefine2,cDefine11) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}')";
//避免只能插入一条子表数据
private string sql = @" if not exists (select ID from SO_SODetails where ID= {0}) ";
private string newSql = @"insert into SO_SODetails(ID,cInvCode,iQuantity,dPreDate,iTaxRate,iQuotedPrice,iSOsID,iNatMoney) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')";
protected override Result _LoadFirstPage()
{
TradeProxy tmp = new TradeProxy();
Result rtn = _load.Load(_args, ref tmp);
_proxy = tmp;
return rtn;
}
protected override void _SetLoader()
{
_load = new TradeLoad();
}
protected override void _ImportData()
{
_operType = "插入销售订单中间表数据";
Trade[] entitys = ((TradeProxy)_proxy).trades;
if (entitys == null || entitys.Length == 0) return;
StringBuilder sb = new StringBuilder();
foreach (Trade item in entitys)
{
item.salesman_id = _ConvertStr(item.salesman_id);//业务员ID
item.fullname = _ConvertStr(item.fullname);//业务员姓名
//销售部门默认取101,销售类型默认取MX,业务类型默认取普通销售
//sb.AppendLine(string.Format(__sql, new object[] { item.trade_id, item.trade_no, item.created, item.customer_no, item.buyer_nick, item.tax_rate, item.salesman_id, item.checker_id, item.currency, 1, "MX", 101, 0, 95, "普通销售", item.goods_list[0].goods_no, item.goods_list[0].num, item.goods_list[0].created, item.goods_list[0].tax_rate, item.goods_list[0].price }));
sb.AppendLine(string.Format(__sql, new object[] { item.trade_id, _ConvertStr(item.trade_no), item.created, _ConvertStr(item.customer_no), _ConvertStr(item.buyer_nick),_ConvertNotStr( item.tax_rate), item.salesman_id, _ConvertStr(item.checker_id), _ConvertStr(item.currency), 1, _ConvertStr(AppSeting.Instance.GetValue("ecSTCode")), _ConvertStr(AppSeting.Instance.GetValue("ecDepCode")), 0, 95, _ConvertStr("普通销售"), _ConvertStr(item.platform_id), _ConvertStr(item.fullname), _ConvertStr(item.trade_type), _ConvertStr(item.shop_no),_ConvertStr(item.shop_name) }));
sb.AppendLine(string.Format(sql, new object[] { item.trade_id }));
Goods_List[] gl = item.goods_list;
sb.AppendLine("begin");
foreach (Goods_List ty in gl)
{
// sb.AppendLine(string.Format(newSql , new object[] { item.trade_id, ty.goods_no, ty.num, ty.created, ty.tax_rate, ty.price, ty.trade_id }));
sb.AppendLine(string.Format(newSql, new object[] { item.trade_id, _ConvertStr(ty.spec_no), _ConvertNotStr(ty.num), ty.created, _ConvertNotStr(ty.tax_rate), _ConvertNotStr(ty.price), ty.trade_id, _ConvertNotStr(ty.order_price) }));
}
sb.AppendLine("end");
_InsertIntoTempDb(sb.ToString(), item.trade_no);
}
}
protected override void _DeleteDup()
{
_operType = "删除销售订单中间表重复数据";
//string sql = "delete a from {0}..SO_SOMain a,SO_SODetails b,{1}..SO_SOMain c,SO_SODetails d where a.ID = b.ID and b.ID=c.ID and c.ID=d.ID";
string sql1 = "delete a from {0}..SO_SOMain a, {1}..SO_SOMain b where a.ID = b.cdefine5";
string sql2 = "delete a from {0}..SO_SODetails a, {1}..SO_SODetails 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);
}
}
}