using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cuidian.Sql;
using WangToTempDb;
using cuidian.OpenApi;
using cuidian.OpenApi.Api;
using cuidian.OpenApi.Model;
using cuidian.OpenApi.Utils;
using cuidian.Common;
namespace YlInit
{
public class Stock : IProcess
{
StringBuilder __sb = new StringBuilder();
public void Process()
{
__WangToTemp();
__TempDbToUfida();
}
private void __WangToTemp()
{
if (__Step0()) return;
__Step1();
__Step2();
}
///
/// 判断是否有记录
///
///
private bool __Step0()
{
string sql = "select count(1) from wtu..stock";
int cnt = (int)DbUtils.ExecuteScalar(sql, ConnectionUtils.Instance.GetConnection());
return cnt > 0;
}
///
/// 第一步,用仓库去抓库存
///
private void __Step1()
{
string sql = "select cwhcode from warehouse";
DataTable dt = DbUtils.Fill(sql, ConnectionUtils.Instance.GetConnection());
foreach (DataRow dr in dt.Rows)
{
StockUpLoad up = new StockUpLoad();
Dictionary args = new Dictionary();
args["warehouse_no"] = dr["cwhcode"].ToString();
args["start_time"] = "2019-9-1 00:00:00";
args["end_time"] = Convert.ToDateTime(DateTime.Now.AddMinutes(-10).ToString("yyyy-MM-dd HH:mm:ss")).ToString();
up.UpLoad(args);
}
}
///
/// 用存货去找遗漏的资料
///
private void __Step2()
{
string sql = "select cinvcode from inventory where cinvcode not in (select distinct cinvcode from wtu..stock)";
DataTable dt = DbUtils.Fill(sql, ConnectionUtils.Instance.GetConnection());
foreach (DataRow item in dt.Rows)
{
StockUpLoad up = new StockUpLoad();
Dictionary args = new Dictionary();
args["spec_no"] = item["cinvcode"].ToString();
up.UpLoad(args);
}
}
private void __TempDbToUfida()
{
string sql = "select distinct cwhcode from stock ORDER by cwhcode";
DataTable dt = DbUtils.Fill(sql, ConnectionUtils.Instance.GetConnection("TempDB"));
foreach (DataRow dr in dt.Rows)
{
__GetStock(dr["cwhcode"].ToString());
}
}
private void __GetStock(string cwhcode)
{
string sql = "select * from stock where cwhcode='" + cwhcode + "'";
DataTable dt = DbUtils.Fill(sql, ConnectionUtils.Instance.GetConnection("TempDB"));
List entrys = new List();
int i = 0;
List ids = new List();
DataRow tmp = null;
if (dt.Rows.Count == 0) return;
foreach (DataRow item in dt.Rows)
{
ids.Add(Convert.ToInt32(item["sysid"]));
tmp = item;
if (i == 300)
{
__Add(entrys, item,ids);
i = 0;
entrys.Clear();
ids.Clear();
}
else
{
Entry detail = __GetDetail(item);
detail.rowno = (i + 1).ToString();
entrys.Add(detail);
i++;
}
}
if (entrys.Count > 0)
{
__Add(entrys, tmp, ids);
}
if (__sb.Length > 0) throw new Exception(__sb.ToString());
}
///
/// 删除成功记录
///
private void __Delete(List ids)
{
string sql = "delete from stock where sysid in (" + ListUtils.ToString(ids, ",") + ")";
DbUtils.ExecuteNonQuery(sql, ConnectionUtils.Instance.GetConnection("TempDB"));
}
private void __Add(List entrys,DataRow dr,List ids)
{
Otherin v = __GetHead(dr);
Entry[] details = new Entry[entrys.Count];
entrys.CopyTo(details);
v.entry = details;
OtherinRoot vr = new OtherinRoot();
vr.otherin = v;
OtherinApi api = new OtherinApi();
BusinessObject result = api.Add(JsonUtils.GetJsonString(vr));
if (result.IsError == false)
{
api.Audit(result.Id);
__Delete(ids);
}
else
{
__sb.AppendLine(result.ErrMsg);
}
}
private Otherin __GetHead(DataRow item)
{
Otherin v = new Otherin();
v.date = DateTime.Now.ToString("yyyy-MM-dd");
v.warehousecode = item["cwhcode"].ToString();
v.define5 = "1";
v.define1 = "来源旺店通";
return v;
}
private Entry __GetDetail(DataRow dr)
{
Entry rtn = new Entry();
rtn.inventorycode = dr["cinvcode"].ToString();
rtn.quantity = dr["qty"].ToString();
decimal price = Convert.ToDecimal(dr["price"]);
if (price > 0)
{
rtn.price = price.ToString();
rtn.cost = Math.Round( Convert.ToDecimal(dr["qty"]) * price,2).ToString();
}
return rtn;
}
}
}