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 DownLoadPurchasein : BaseDataDownLoad
{
protected override void _Init()
{
_loadSql = "select * from RdRecord01 where processflag=0";
base._Init();
}
protected override BusinessObject _ImportData(DataRow row)
{
PurchaseinApi api = new PurchaseinApi();
Dictionary args = new Dictionary();
string id = row["ID"].ToString();
args.Add("RdRecord01", "select * from wtu..RdRecord01 where ID=" + id);
args.Add("RdRecords01", "select * from wtu..RdRecords01 where ID=" + id);
DataSet ds = DbUtils.Fill(args, ConnectionUtils.Instance.GetConnection());
Purchasein v = __GetHead(ds);
v.entry = __GetDetails(ds);
PurchaseinRoot vr = new PurchaseinRoot();
vr.purchasein = v;
BusinessObject result = api.Add(JsonUtils.GetJsonString(vr));
return result;
}
protected override void _Audit(string id)
{
PurchaseinApi api = new PurchaseinApi();
api.Audit(id);
}
private Purchasein __GetHead(DataSet ds)
{
Purchasein v = new Purchasein();
DataTable dt = ds.Tables["RdRecord01"];
v.code = dt.Rows[0]["cCode"].ToString();
v.departmentcode= dt.Rows[0]["cDepCode"].ToString();//部门编码
v.warehousecode = dt.Rows[0]["cWhCode"].ToString();//仓库编码
v.vendorcode = dt.Rows[0]["cVenCode"].ToString();//供货单位编码
//v.vendorabbname = "";//供货单位名称
//v.receivecode = AppSeting.Instance.GetValue("receivecode");//收发类型默认S
v.receivecode = AppSeting.Instance.GetValue("receivecode");//收发类型默认MC
v.receivename = "普通采购";//收发类型默认普通采购
v.memory = dt.Rows[0]["cMemo"].ToString();
DateTime date = DateTime.Parse(dt.Rows[0]["dDate"].ToString());
v.date = date.ToString("yyyy-MM-dd");
v.define12 = dt.Rows[0]["cDefine12"].ToString();//采购单号
//v.maker = dt.Rows[0]["cMaker"].ToString();//制单人
//define5 用友U8API中 采购入库 的 单据头自定义项5
v.define5 = dt.Rows[0]["ID"].ToString();
v.define1 = "来源旺店通";
return v;
}
private Entry[] __GetDetails(DataSet ds)
{
int cnt = ds.Tables["RdRecords01"].Rows.Count;
DataTable dt = ds.Tables["RdRecords01"];
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.price = dt.Rows[i]["iUnitCost"].ToString();//本币单价
e.cost =( Convert.ToDouble(e.quantity) * Convert.ToDouble(e.price)).ToString(); //dt.Rows[i]["cDefine22"].ToString();//本币金额
//define27 用友 U8 API中 采购入库
e.define27 = dt.Rows[i]["ID"].ToString();
e.define26 = dt.Rows[i]["cDefine26"].ToString();
e.rowno = (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 RdRecord01 set processflag = 1,processdate = getdate(),newcode='" + newCode + "'" + " where sysid = " + id.ToString();
sql +=string.Format(" update {0}..rdrecords01 set iOriMoney=iPrice,iOriCost=iUnitCost where id in(select id from {0}..rdrecord01 where ccode='{1}')",ConnectionUtils.Instance.GetConnection().Database,newCode);
//string sql = "update RdRecord01 set processflag = 1,processdate = getdate(),newcode=(select cCode from wtu..RdRecord01 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 RdRecord01 where cCode='{0}' ", result.Id), ConnectionUtils.Instance.GetConnection("003")).ToString();
return result.Id;
}
}
}