using cuidian.Common;
using cuidian.Sql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WangToTempDb;
using WdtUtils;
using WdtUtils.Load;
using WdtUtils.Proxy;
namespace WangToTempDb
{ ///
/// 旺店通销售发货单上传中间库
///
public class DispatchListSendUpLoad : BaseUpLoad
{
//private string __sql = @"if not exists (select * from DispatchList a inner join DispatchLists b on a.DLID=b.DLID where a.DLID= '{0}')
//insert into DispatchList(DLID,cDLCode,cCusCode,cCusName,cPersonCode,dDate,cexch_name,iTaxRate,direction,cVouchType,cSTCode,cDepCode,iExchRate,bFirst,bReturnFlag,bSettleAll,iVTid) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}')
//insert into DispatchLists(DLID,iDLsID,cWhCode,cInvCode,iQuantity,iTaxRate,iTaxUnitPrice,cMemo,bSettleAll,bQANeedCheck,bQAUrgency,bQAChecking,bQAChecked,bcosting) values('{0}','{0}','{17}','{18}','{19}','{20}','{21}','{22}','{23}','{24}','{25}','{26}','{27}','{28}')";
private string __sql = @"if not exists (select DLID from DispatchList where DLID= {0}) insert into DispatchList(DLID,cDLCode,cCusCode,cCusName,cPersonCode,direction,cDefine1,cDefine3,cDefine2,cDefine11,cDefine12,bReturnFlag,cDepCode,cDefine10,cSourceCode,ddate,cVouchType ) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','MX')";
//避免只能插入一条子表数据
private string sql = @" if not exists (select DLID from DispatchLists where DLID= {0}) ";
private string newSql = @"insert into DispatchLists(DLID,iDLsID,cWhCode,cInvCode,iQuantity,iTaxRate,iTaxUnitPrice,cMemo,cCusInvCode,iQuotedPrice,cDefine28,cDefine29) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}')";
protected override Result _LoadFirstPage()
{
DispatchListProxy tmp = new DispatchListProxy();
Result rtn = _load.Load(_args, ref tmp);
_proxy = tmp;
return rtn;
}
protected override void _SetLoader()
{
_load = new DispatchLisSendtLoad();
}
protected override void _ImportData()
{
_operType = "插入销售出库中间表数据";
Stockout_List[] entitys = ((DispatchListProxy)_proxy).stockout_list;
if (entitys == null || entitys.Length == 0) return;
StringBuilder sb = new StringBuilder();
foreach (Stockout_List item in entitys)
{
DbUtils.ExecuteNonQuery(string.Format("update wtu..dispatchlist set ddate='{0}' where cDLCode='{1}'", item.consign_time, item.order_no), ConnectionUtils.Instance.GetConnection());
string deptcode = DbUtils.ExecuteScalar(string.Format(@" select cDepCode from {0}..person where cPersonName='{1}' ",
ConnectionUtils.Instance.GetConnection().Database, item.salesman_name),
ConnectionUtils.Instance.GetConnection("TempDB")).ToString();
sb.AppendLine(string.Format(__sql, new object[] { item.stockout_id, _ConvertStr(item.order_no), _ConvertStr(item.customer_no), _ConvertStr(item.customer_name), _ConvertNotStr(item.salesman_no).Trim().Equals("SYSTEM") ? "0" : _ConvertNotStr(item.salesman_no), 1, _ConvertStr(item.trade_type), _ConvertStr(item.platform_id), _ConvertStr(item.shop_no), _ConvertStr(item.shop_name), _ConvertStr(item.salesman_name), 0, _ConvertStr(deptcode), _ConvertNotStr(item.paid), _ConvertStr(item.status), item.consign_time }));
sb.AppendLine(string.Format(sql, new object[] { item.stockout_id }));
Details_List[] dl = item.details_list;
sb.AppendLine("begin");
foreach (Details_List ty in dl)
{
sb.AppendLine(string.Format(newSql, new object[] { item.stockout_id, ty.rec_id, _ConvertStr(item.warehouse_no), _ConvertStr(ty.spec_no), _ConvertNotStr(ty.goods_count), _ConvertNotStr(ty.tax_rate), (_ConvertNotStr(ty.market_price) + (Convert.ToDouble(_ConvertNotStr(ty.market_price)) * Convert.ToDouble(_ConvertNotStr(ty.tax_rate)))), _ConvertStr(ty.remark), _ConvertStr(item.warehouse_no), _ConvertNotStr(ty.sell_price), _ConvertStr(item.src_order_no), _ConvertNotStr(ty.total_amount) }));
}
sb.AppendLine("end");
_InsertIntoTempDb(sb.ToString(), item.order_no);
}
}
protected override void _DeleteDup()
{
//_operType = "删除销售出库重复数据";
////string sql = "delete a from {0}..DispatchList a,DispatchLists b,{1}..DispatchList c,DispatchLists d where a.DLID = b.DLID and b.DLID=c.DLID and c.DLID=d.DLID";
//string sql1 = "delete a from {0}..DispatchList a, {1}..DispatchList b where a.DLID = b.cdefine5 ";
//string sql2 = "delete a from {0}..DispatchLists a, {1}..DispatchLists b where a.DLID = 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);
}
}
}