using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cuidian.Common;
using cuidian.OpenApi.Api;
using cuidian.OpenApi.Model;
using cuidian.OpenApi.Utils;
using OperationLog;
using cuidian.Sql;
namespace TempDbToUfida.OpenApi
{
///
/// 从中间库将采购退货资料下载到U8数据库
///
public class DownLoadPurchasereturn : BaseDataDownLoad
{
protected override void _Init()
{
_loadSql = "select * from PU_ArrivalVouch where processflag=0";
base._Init();
}
protected override BusinessObject _ImportData(DataRow row)
{
PurchasereturnApi api = new PurchasereturnApi();
Dictionary args = new Dictionary();
string id = row["ID"].ToString();
args.Add("PU_ArrivalVouch", "select * from wtu..PU_ArrivalVouch where ID=" + id);
args.Add("PU_ArrivalVouchs", "select * from wtu..PU_ArrivalVouchs where ID=" + id);
DataSet ds = DbUtils.Fill(args, ConnectionUtils.Instance.GetConnection());
Purchasereturn v = __GetHead(ds);
v.entry = __GetDetails(ds);
PurchasereturnRoot vr = new PurchasereturnRoot();
vr.purchasereturn = v;
BusinessObject result = api.Add(JsonUtils.GetJsonString(vr));
return result;
}
protected override void _Audit(string id)
{
PurchasereturnApi api = new PurchasereturnApi();
api.Audit(id);
}
private Purchasereturn __GetHead(DataSet ds)
{
Purchasereturn v = new Purchasereturn();
DataTable dt = ds.Tables["PU_ArrivalVouch"];
v.code = dt.Rows[0]["cCode"].ToString();
v.purchasetypecode = AppSeting.Instance.GetValue("purchasetypecode");//采购类型编码默认MC
v.businesstype = dt.Rows[0]["cBusType"].ToString();
string vencode;
if (dt.Rows[0]["cVenCode"].ToString() != "")
{
//旺店通无供应商编号,把供应商名称存到编号中,现转换为编号
vencode = dt.Rows[0]["cVenCode"].ToString();
string sql =string.Format( "select * from {0}..Vendor where cVenName= '" + vencode + "'",ConnectionUtils.Instance.GetConnection().Database);
string tabl = DbUtils.ExecuteScalar(sql, ConnectionUtils.Instance.GetConnection()).ToString();
v.vendorcode = tabl.ToString();
}
v.departmentcode = "0";//部门编码默认101
v.personcode = "0";//业务员编码默认100
v.foreigncurrency = dt.Rows[0]["cexch_name"].ToString();
v.foreigncurrencyrate = AppSeting.Instance.GetValue("foreigncurrencyrate");//汇率默认1
v.memory = dt.Rows[0]["cMemo"].ToString();
v.billtype = dt.Rows[0]["bNegative"].ToString();//负发票标志
DateTime date = DateTime.Parse(dt.Rows[0]["dDate"].ToString());
v.date = date.ToString("yyyy-MM-dd");
v.cmodifydate= date.ToString("yyyy-MM-dd");
v.define5 = dt.Rows[0]["ID"].ToString();
return v;
}
private Entry[] __GetDetails(DataSet ds)
{
int cnt = ds.Tables["PU_ArrivalVouchs"].Rows.Count;
DataTable dt = ds.Tables["PU_ArrivalVouchs"];
Entry[] rtn = new Entry[cnt];
for (int i = 0; i < cnt; i++)
{
Entry e = new Entry();
e.inventorycode = dt.Rows[i]["cInvCode"].ToString().Replace(" ", "");
e.quantity = "-"+dt.Rows[i]["iQuantity"].ToString();
e.originaltaxedprice = dt.Rows[i]["iOriTaxCost"].ToString();
e.taxrate = dt.Rows[i]["iTaxRate"].ToString();
e.cbmemo = dt.Rows[i]["cbMemo"].ToString();
e.cost = dt.Rows[i]["cDefine22"].ToString();
e.define27 = dt.Rows[i]["ID"].ToString();
e.ivouchrowno = (i + 1).ToString();
rtn[i] = e;
}
return rtn;
}
protected override void _LogError(int id, string msg)
{
OperationLog.Utils.WriteErrorLog("向U8导入采购退货资料", "sysid = " + id.ToString() + " " + msg);
}
protected override void _LogSucess(int id,string newCode)
{
string sql = "update PU_ArrivalVouch set processflag = 1,processdate = getdate(),newcode='" + newCode + "'" + " where sysid = " + id.ToString();
//string sql = "update PU_ArrivalVouch set processflag = 1,processdate = getdate() where sysid = " + id.ToString();
//string sql = "update PU_ArrivalVouch set processflag = 1,processdate = getdate(),newcode=(select cCode from wtu..PU_ArrivalVouch where sysid = " + id.ToString() + ") where sysid = " + id.ToString();
OperationLog.Utils.UpdateTempRecord(sql);
}
protected override string _GetReturnCode(BusinessObject result)
{
//string ID = DbUtils.ExecuteScalar(string.Format("select ID from PU_ArrivalVouch where cCode='{0}' ", result.Id), ConnectionUtils.Instance.GetConnection("003")).ToString();
return result.Id;
}
}
}