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 delegate bool DelHander();
// public class DownLoadSaleorder : BaseDataDownLoad
// {
// protected override void _Init()
// {
// _loadSql = "select * from SO_SOMain where processflag=0";
// base._Init();
// }
// protected override void _downLoad()
// {
// List ids = new List();
// int i = 0;
// DataSet temp = null;
// int num = 0;
// //设定开关
// bool flag = false;
// //获取零售平台订单个数
// int sum = GetCount();
// //获取最后一单数量
// int counts = GetCounts();
// if (_data.Rows.Count > 0)
// {
// temp = __GetTempData(_data.Rows[0]);
// temp.Tables[0].Clear();
// temp.Tables[1].Clear();
// }
// foreach (DataRow row in _data.Rows)
// {
// BusinessObject result = null;
// DataSet dd = __GetTempData(row);
// saleorder v = new saleorder();
// DataTable dt = dd.Tables["SO_SOMain"];
// v.plat_id = dt.Rows[0]["platform_id"].ToString();//平台,用于区分是否为阿里巴巴店铺
// v.define1 = dt.Rows[0]["cDefine1"].ToString();//订单类型 1网店销售 2线下零售 3售后换货 4批发业务 5保修换新 6保修完成 7订单补发
// if ((v.plat_id.Equals("9") && v.define1.Equals("1")) || (v.define1.Equals("7")) || (!v.define1.Equals("1") && !v.define1.Equals("7")))//不合并
// {
// SaleorderApi api = new SaleorderApi();
// result = _ImportData(row);
// api.Audit(result.Id);
// string newcode = null;
// //记录日志
// _Log(result, Convert.ToInt32(row["sysid"]), out newcode);
// }
// else
// {
// //设置300单合并为一单
// if (sum - (299 * num) < 299)
// {
// flag = true;
// }
// if (flag)
// {
// NewMethod(ids, ref i, temp, ref num, row, () => i == counts - 1);
// }
// else
// {
// NewMethod(ids, ref i, temp, ref num, row, () => i == 299);
// }
// }
// }
// }
// private void NewMethod(List ids, ref int i, DataSet temp, ref int num, DataRow row, DelHander del)
// {
// DataSet ds = __GetTempData(row);
// //存300单的第一单
// if (i == 0)
// {
// temp.Tables["SO_SOMain"].ImportRow(row);
// }
// if (del())
// {
// //把最后一单加上
// ids.Add(row["sysid"].ToString());
// foreach (DataRow item in ds.Tables["SO_SODetails"].Rows)
// {
// temp.Tables["SO_SODetails"].ImportRow(item);
// }
// //要修改的300单业务ID数据
// string[] b = new string[ids.Count];
// ids.CopyTo(b);
// //String.Join(",", b);
// //result = _ImportData(temp[0]);
// SaleorderApi apiTwo = new SaleorderApi();
// saleorder vv = __GetHead(temp);
// vv.entry = __GetDetails(temp);
// SaleorderRoot vr = new SaleorderRoot();
// vr.saleorder = vv;
// BusinessObject resultTwo = apiTwo.Add(JsonUtils.GetJsonString(vr));
// //记录日志
// string newcode = null;
// _Log(resultTwo, Convert.ToInt32(temp.Tables[0].Rows[0]["sysid"]), out newcode);
// if (_logFlag)
// {
// SaleorderApi api = new SaleorderApi();
// api.Audit(newcode);
// _LogSucessTwo(______id, String.Join(",", b));
// }
// else
// {
// //跳过第一条
// for (int k = 1; k < b.Length; k++)
// {
// OperationLog.Utils.WriteErrorLog("向U8导入销售订单资料", "sysid = " + b[k] + " " + ______msg);
// }
// }
// //清空业务表数据
// temp.Tables[0].Clear();
// temp.Tables[1].Clear();
// i = 0;
// ids.Clear();
// ______id = null;
// //300单加一次
// num += 1;
// }
// else
// {
// ids.Add(row["sysid"].ToString());
// foreach (DataRow item in ds.Tables["SO_SODetails"].Rows)
// {
// temp.Tables["SO_SODetails"].ImportRow(item);
// }
// i++;
// }
// }
// private int GetCount()
// {
// return Convert.ToInt32(DbUtils.ExecuteScalar("select count(sysid) from so_somain where platform_id<>0 and platform_id<>9 ", ConnectionUtils.Instance.GetConnection("TempDB")));
// }
// //获取最后一单(小于300)
// private int GetCounts()
// {
// return Convert.ToInt32(DbUtils.ExecuteScalar("select COUNT(sysid) % 300 from SO_SOMain where platform_id<>0 and platform_id<>9", ConnectionUtils.Instance.GetConnection("TempDB")));
// }
// private DataSet __GetTempData(DataRow row)
// {
// Dictionary args = new Dictionary();
// string id = row["ID"].ToString();
// args.Add("SO_SOMain", "select * from wtu..SO_SOMain where ID=" + id);
// args.Add("SO_SODetails", "select * from wtu..SO_SODetails where ID=" + id);
// DataSet ds = DbUtils.Fill(args, ConnectionUtils.Instance.GetConnection());
// return ds;
// }
// protected override BusinessObject _ImportData(DataRow row)
// {
// SaleorderApi api = new SaleorderApi();
// Dictionary args = new Dictionary();
// string id = row["ID"].ToString();
// args.Add("SO_SOMain", "select * from wtu..SO_SOMain where ID=" + id);
// args.Add("SO_SODetails", "select * from wtu..SO_SODetails where ID=" + id);
// DataSet ds = DbUtils.Fill(args, ConnectionUtils.Instance.GetConnection());
// saleorder v = __GetHead(ds);
// v.entry = __GetDetails(ds);
// SaleorderRoot vr = new SaleorderRoot();
// vr.saleorder = v;
// BusinessObject result = api.Add(JsonUtils.GetJsonString(vr));//8724229
// return result;
// }
// private saleorder __GetHead(DataSet ds)//(v.plat_id.Equals("9")&& v.define1.Equals("1")) || (v.define1.Equals("7")) || (!v.define1.Equals("1")&&!v.define1.Equals("7"))
// {
// saleorder v = new saleorder();
// DataTable dt = ds.Tables["SO_SOMain"];
// v.plat_id = dt.Rows[0]["platform_id"].ToString();//平台,用于区分是否为阿里巴巴店铺
// v.define1 = dt.Rows[0]["cDefine1"].ToString();//订单类型 1网店销售 2线下零售 3售后换货 4批发业务 5保修换新 6保修完成 7订单补发
// if ((v.plat_id.Equals("9") && v.define1.Equals("1")) || (v.define1.Equals("7")) || (!v.define1.Equals("1") && !v.define1.Equals("7")))
// {
// //v.custcode = AppSeting.Instance.GetValue("clientOff");//线下平台客户编码KN01
// //v.define10 = dt.Rows[0]["cCusName"].ToString();
// v.custcode = dt.Rows[0]["cDefine2"].ToString();//客户编号==店铺编号custcode
// v.cusabbname = dt.Rows[0]["cDefine11"].ToString();//客户名称==店铺名称cusname
// v.personname = dt.Rows[0]["ccontactname"].ToString();//业务员名称
// v.personcode = dt.Rows[0]["cpersoncode"].ToString();//业务员编号
// }
// else//合并订单不带业务员
// {
// // v.custcode = AppSeting.Instance.GetValue("clientRetail");//零售(其他)平台客户编码KN02
// v.custcode = dt.Rows[0]["cDefine2"].ToString();//客户编号==店铺编号
// v.cusabbname = dt.Rows[0]["cDefine11"].ToString();//客户名称==店铺名称
// }
// v.deptcode = dt.Rows[0]["cDepCode"].ToString();//部门编码
// //v.code = dt.Rows[0]["cSOCode"].ToString();//订单号
// v.businesstype = dt.Rows[0]["cBusType"].ToString();//业务类型
// v.typecode = dt.Rows[0]["cSTCode"].ToString();//销售类型编码
// DateTime date = DateTime.Parse(dt.Rows[0]["dDate"].ToString());
// v.date = date.ToString("yyyy-MM-dd");
// v.define5 = dt.Rows[0]["ID"].ToString();
// //v.personname=dt.Rows[0]["ccontactname"].ToString();//业务员名称
// //v.personcode=dt.Rows[0]["cpersoncode"].ToString();//业务员编号
// //v.define6 = dt.Rows[0]["cCusName"].ToString();
// return v;
// }
// private Entry[] __GetDetails(DataSet ds)
// {
// int cnt = ds.Tables["SO_SODetails"].Rows.Count;
// DataTable dt = ds.Tables["SO_SODetails"];
// 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.quotedprice = dt.Rows[i]["iQuotedPrice"].ToString();//报价
// e.money = dt.Rows[i]["iNatMoney"].ToString();//原币无税金额
// //e.bgift = dt.Rows[i]["bgift"].ToString();//赠品
// //e.rowno = dt.Rows[i]["iRowNo"].ToString();//行号
// e.taxrate = dt.Rows[i]["iTaxRate"].ToString();//税率
// e.define27 = dt.Rows[i]["ID"].ToString();
// e.rowno = (i + 1).ToString();
// rtn[i] = e;
// }
// return rtn;
// }
// private bool _logFlag;
// private string ______id;
// private string ______msg;
// protected override void _LogError(int id, string msg)
// {
// _logFlag = false;
// ______msg = msg;
// OperationLog.Utils.WriteErrorLog("向U8导入销售订单资料", "sysid = " + id.ToString() + " " + msg);
// }
// protected override void _LogSucess(int id, string newCode)
// {
// _logFlag = true;
// ______id = newCode;
// //string sql = "update PU_ArrivalVouch set processflag = 1,processdate = getdate() where sysid = " + id.ToString();
// string sql = "update SO_SOMain set processflag = 1,processdate = getdate(),newcode='" + newCode + "'" + " where sysid = " + id.ToString();
// OperationLog.Utils.UpdateTempRecord(sql);
// }
// protected override string _GetReturnCode(BusinessObject result)
// {
// //string ID = DbUtils.ExecuteScalar(string.Format("select ID from SO_SOMain where cSOCode='{0}' ", result.Id), ConnectionUtils.Instance.GetConnection("003")).ToString();
// return result.Id;
// }
// private void _LogSucessTwo(string id, string ids)
// {
// //string sql = "update PU_ArrivalVouch set processflag = 1,processdate = getdate() where sysid = " + id.ToString();
// string sql = "update SO_SOMain set processflag = 1,processdate = getdate(),newcode='" + id + "' where sysid in (" + ids + ")";
// OperationLog.Utils.UpdateTempRecord(sql);
// }
// }
//}