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 DownLoadOtherin : BaseDataDownLoad
{
protected override void _Init()
{
_loadSql = "select * from RdRecord08 where processflag=0 and direction=1";
base._Init();
}
protected override BusinessObject _ImportData(DataRow row)
{
OtherinApi api = new OtherinApi();
Dictionary args = new Dictionary();
string id = row["ID"].ToString();
args.Add("RdRecord08", "select * from wtu..RdRecord08 where ID=" + id);
args.Add("RdRecords08", "select * from wtu..RdRecords08 where ID=" + id);
DataSet ds = DbUtils.Fill(args, ConnectionUtils.Instance.GetConnection());
Otherin v = __GetHead(ds);
v.entry = __GetDetails(ds);
OtherinRoot vr = new OtherinRoot();
vr.otherin = v;
BusinessObject result = api.Add(JsonUtils.GetJsonString(vr));
return result;
}
protected override void _Audit(string id)
{
OtherinApi api = new OtherinApi();
api.Audit(id);
}
private Otherin __GetHead(DataSet ds)
{
Otherin v = new Otherin();
DataTable dt = ds.Tables["RdRecord08"];
v.code = dt.Rows[0]["cCode"].ToString();
DateTime date = DateTime.Parse(dt.Rows[0]["dDate"].ToString());
v.date = date.ToString("yyyy-MM-dd");
v.maker = dt.Rows[0]["cMaker"].ToString();
v.warehousecode = dt.Rows[0]["cWhCode"].ToString();
v.memory = dt.Rows[0]["cMemo"].ToString();
v.define5 = dt.Rows[0]["ID"].ToString();
v.define1 = "来源旺店通";
string cDefine15 = dt.Rows[0]["cDefine15"].ToString();
if (cDefine15.Equals("2"))
{
v.receivecode = AppSeting.Instance.GetValue("receivecodeIn2");//收发类型默认S
}
else if (cDefine15.Equals("4"))
{
v.receivecode = AppSeting.Instance.GetValue("receivecodeIn4");//收发类型默认S
}
else if (cDefine15.Equals("6"))
{
v.receivecode = AppSeting.Instance.GetValue("receivecodeIn6");//收发类型默认S
}
else if (cDefine15.Equals("7"))
{
v.receivecode = AppSeting.Instance.GetValue("receivecodeIn7");//收发类型默认S
}
else if (cDefine15.Equals("3"))
{
v.receivecode = "S8";
}
return v;
}
private Entry[] __GetDetails(DataSet ds)
{
int cnt = ds.Tables["RdRecords08"].Rows.Count;
DataTable dt = ds.Tables["RdRecords08"];
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.quantity = string.IsNullOrEmpty(e.quantity) ? "0" : e.quantity;
e.price = dt.Rows[i]["iUnitCost"].ToString();
e.price = string.IsNullOrEmpty(e.price) ? "0" : e.price;
e.cost = dt.Rows[i]["iPUnitCost"].ToString();
e.define27 = dt.Rows[i]["ID"].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 RdRecord08 set processflag = 1,processdate = getdate(),newcode='" + newCode + "'" + " where sysid = " + id.ToString();
//string sql = "update RdRecord08 set processflag = 1,processdate = getdate(),newcode=(select cCode from wtu..RdRecord08 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 RdRecord08 where cCode='{0}' ", result.Id), ConnectionUtils.Instance.GetConnection("003")).ToString();
//return ID;
return result.Id;
}
}
}