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 DelHanderTwo(); public class DownLoadSellReturn : BaseDataDownLoad { protected override void _Init() { //_loadSql = "select * from DispatchList where processflag=0 and bReturnFlag=1 "; //base._Init(); } private int ___bReturnFlag; protected override void _downLoad() { _loadSql = "select * from DispatchList where processflag=0 and bReturnFlag=1 "; _data = DbUtils.Fill(_loadSql, ConnectionUtils.Instance.GetConnection("TempDB")); 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(); //} #region 不合并订单 foreach (DataRow row in _data.Rows) { BusinessObject result = null; DataSet dd = __GetTempData(row); Consignment v = new Consignment(); DataTable dt = dd.Tables["DispatchList"]; v.define3 = dt.Rows[0]["cDefine3"].ToString();//平台,用于区分是否为阿里巴巴店铺 v.define1 = dt.Rows[0]["cDefine1"].ToString();//订单类型 1网店销售 2线下零售 3售后换货 4批发业务 5保修换新 6保修完成 7订单补发 string str = dt.Rows[0]["bReturnFlag"].ToString(); v.bReturnFlag = Convert.ToBoolean(dt.Rows[0]["bReturnFlag"]) ? "1" : "0";//退货标识 ___bReturnFlag = Convert.ToInt32(v.bReturnFlag); #region if (!v.define3.Equals("9") && v.define1.Equals("1") && v.bReturnFlag.Equals("0")) //(((v.define3.Equals("9") && v.define1.Equals("1")) || (v.define1.Equals("7")) || (!v.define1.Equals("1") && !v.define1.Equals("7")))||v.bReturnFlag.Equals("0")||v.bReturnFlag.Equals("1"))//不合并 { #region 注释 //if (sum - (99 * num) < 99) //{ // 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 == 99); //} // i = Convert.ToInt32(DbUtils.ExecuteScalar(string.Format("select count(1) from DispatchList where cDefine3<>'9' and cDefine1<>'1' and cDefine2='{0}' ", dt.Rows[0]["cDefine2"].ToString()), ConnectionUtils.Instance.GetConnection("TempDB"))); #endregion } else { BasicApi api = null; if (___bReturnFlag == 0) { api = new ConsignmentApi(); } else { api = new ReturnorderApi(); } result = _ImportData(row); api.Audit(result.Id); string newcode = null; //记录日志 _Log(result, Convert.ToInt32(row["sysid"]), out newcode); } #endregion } #endregion //Merge(); } #region 注释 /// /// 合并单专用 /// /// private void NewMethod(List ids, ref int i, DataSet temp, ref int num, DataRow row, DelHanderTwo del) { DataSet ds = __GetTempData(row); //存300单的第一单 if (i == 0) { temp.Tables["DispatchList"].ImportRow(row); } if (del())//合并无退货单 { //把最后一单加上 ids.Add(row["sysid"].ToString()); foreach (DataRow item in ds.Tables["DispatchLists"].Rows) { temp.Tables["DispatchLists"].ImportRow(item); } //要修改的300单业务ID数据 string[] b = new string[ids.Count]; ids.CopyTo(b); //String.Join(",", b); //result = _ImportData(temp[0]); ConsignmentApi apiTwo = new ConsignmentApi(); Consignment vv = __GetHead(temp); vv.entry = __GetDetails(temp); ReturnorderRoot vr = new ReturnorderRoot(); vr.consignment = 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) { ConsignmentApi api = new ConsignmentApi(); 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["DispatchLists"].Rows) { temp.Tables["DispatchLists"].ImportRow(item); } i++; } } #endregion //合并单专用方法 public void Merge() { DataTable dtMg = DbUtils.Fill("select distinct cDefine2 from DispatchList where cDefine3<>'9' and cDefine1='1' and bReturnFlag='0' and processflag='0' ", ConnectionUtils.Instance.GetConnection("TempDB")); if (dtMg.Rows.Count <= 0) { return; } foreach (DataRow mgRow in dtMg.Rows) { List ids = new List(); int i = 0; DataSet temp = null; int num = 0; //设定开关 bool flag = false; DataTable mgTb = null; mgTb = __GetTempDataMerge(mgRow[0].ToString()).Tables["DispatchList"]; if (dtMg.Rows.Count > 0) { temp = __GetTempDataMerge(mgRow[0].ToString()); temp.Tables[0].Clear(); temp.Tables[1].Clear(); } //获取零售平台订单个数 int sum = GetCount(mgRow[0].ToString()); //获取最后一单数量 int counts = 0; //GetCounts(mgRow[0].ToString()); if (sum % 100 == 0 && sum != 0) { counts = 100; } else { counts = sum % 100; } foreach (DataRow mgSingle in mgTb.Rows) { if (sum - (99 * num) < 99) { flag = true; } if (flag) { NewMethod(ids, ref i, temp, ref num, mgSingle, () => i == counts - 1); } else { NewMethod(ids, ref i, temp, ref num, mgSingle, () => i == 99); } } } } //合并单专用 private DataSet __GetTempDataMerge(string cDefine2) { Dictionary args = new Dictionary(); args.Add("DispatchList", string.Format("select * from DispatchList where cDefine2 = '{0}' and cDefine3 <> '9' and cDefine1 = '1' and bReturnFlag = '0' and processflag = '0'", cDefine2)); args.Add("DispatchLists", string.Format(@"select *from DispatchLists where dlid in(select dlid from DispatchList where cDefine2='{0}' and cDefine3<>'9' and cDefine1='1' and bReturnFlag='0' and processflag='0') ", cDefine2)); DataSet ds = DbUtils.Fill(args, ConnectionUtils.Instance.GetConnection("TempDB")); args.Remove("DispatchList"); args.Remove("DispatchLists"); return ds; } private int GetCount(string cDefine2) { return Convert.ToInt32(DbUtils.ExecuteScalar(string.Format("select count(1) from DispatchList where cDefine2='{0}' and cDefine3<>'9' and cDefine1='1' and bReturnFlag='0' and processflag='0' ", cDefine2), ConnectionUtils.Instance.GetConnection("TempDB"))); } //获取最后一单(小于300) private int GetCounts(string lastCDefine2) { return Convert.ToInt32(DbUtils.ExecuteScalar(string.Format("select count(1)%100 from DispatchList where cDefine2='{0}' and cDefine3<>'9' and cDefine1='1' and bReturnFlag='0' and processflag='0' ", lastCDefine2), ConnectionUtils.Instance.GetConnection("TempDB"))); } private DataSet __GetTempData(DataRow row) { Dictionary args = new Dictionary(); string id = row["DLID"].ToString(); args.Add("DispatchList", "select * from wtu..DispatchList where DLID=" + id); args.Add("DispatchLists", "select * from wtu..DispatchLists where DLID=" + id); DataSet ds = DbUtils.Fill(args, ConnectionUtils.Instance.GetConnection()); return ds; } /// /// 不合并单据 /// /// /// protected override BusinessObject _ImportData(DataRow row) { BasicApi api = null; if (___bReturnFlag == 0) { api = new ConsignmentApi(); } else { api = new ReturnorderApi(); } Dictionary args = new Dictionary(); string id = row["DLID"].ToString(); args.Add("DispatchList", "select * from wtu..DispatchList where DLID=" + id); args.Add("DispatchLists", "select * from wtu..DispatchLists where DLID=" + id); DataSet ds = DbUtils.Fill(args, ConnectionUtils.Instance.GetConnection()); BusinessObject result = null; if (___bReturnFlag == 0) { Consignment v = __GetHead(ds); v.entry = __GetDetails(ds); ReturnorderRoot vr = new ReturnorderRoot(); vr.consignment = v; result = api.Add(JsonUtils.GetJsonString(vr)); } else { Returnorder v = __GetHeadTwo(ds); v.entry = __GetDetails(ds); ReturnorderRoot vr = new ReturnorderRoot(); vr.returnorder = v; result = api.Add(JsonUtils.GetJsonString(vr)); } return result; } /// /// 发货 /// /// /// private Consignment __GetHead(DataSet ds)//(v.plat_id.Equals("9")&& v.define1.Equals("1")) || (v.define1.Equals("7")) || (!v.define1.Equals("1")&&!v.define1.Equals("7")) { Consignment v = new Consignment(); DataTable dt = ds.Tables["DispatchList"]; v.code = dt.Rows[0]["cDLCode"].ToString(); v.define3 = dt.Rows[0]["cDefine3"].ToString();//平台,用于区分是否为阿里巴巴店铺 v.define1 = dt.Rows[0]["cDefine1"].ToString();//订单类型 1网店销售 2线下零售 3售后换货 4批发业务 5保修换新 6保修完成 7订单补发 #region 备注平台和销售类型 v.remark = v.define3.Equals("9") ? "阿里巴巴" : "非阿里巴巴" + ","; if (v.define1.Equals("1")) { v.remark += "网店销售"; } else if (v.define1.Equals("2")) { v.remark += "线下零售"; } else if (v.define1.Equals("3")) { v.remark += "售后换货"; } else if (v.define1.Equals("4")) { v.remark += "批发业务"; } else if (v.define1.Equals("5")) { v.remark += "保修换新"; } else if (v.define1.Equals("6")) { v.remark += "保修完成"; } else if (v.define1.Equals("7")) { v.remark += "订单补发"; } #endregion v.bReturnFlag = Convert.ToBoolean(dt.Rows[0]["bReturnFlag"]) ? "1" : "0"; if (!v.define3.Equals("9") && v.define1.Equals("1") && v.bReturnFlag.Equals("0"))//合并订单不带业务员 //((v.define3.Equals("9") && v.define1.Equals("1")) || (v.define1.Equals("7")) || (!v.define1.Equals("1") && !v.define1.Equals("7"))) { // v.custcode = AppSeting.Instance.GetValue("clientRetail");//零售(其他)平台客户编码KN02 v.custcode = dt.Rows[0]["cDefine2"].ToString();//客户编号==店铺编号 v.cusname = dt.Rows[0]["cDefine11"].ToString();//客户名称==店铺名称 v.cusabbname = dt.Rows[0]["cDefine11"].ToString();//客户名称==店铺名称 } else { //if(!v.define1.Equals("1") && !v.define1.Equals("7") && !v.bReturnFlag.Equals("1"))//手工建单 //{ // v.custcode = dt.Rows[0]["cCusCode"].ToString();//客户编号==店铺编号 // v.cusname = dt.Rows[0]["cCusName"].ToString();//客户名称==店铺名称 // v.cusabbname = dt.Rows[0]["cCusName"].ToString();//客户名称==店铺名称 //} //else //{ v.custcode = dt.Rows[0]["cDefine2"].ToString();//客户编号==店铺编号 v.cusname = dt.Rows[0]["cDefine11"].ToString();//客户名称==店铺名称 v.cusabbname = dt.Rows[0]["cDefine11"].ToString();//客户名称==店铺名称 //} //v.custcode = AppSeting.Instance.GetValue("clientOff");//线下平台客户编码KN01 //v.define10 = dt.Rows[0]["cCusName"].ToString(); v.define10 = dt.Rows[0]["cCusName"].ToString(); v.define11 = dt.Rows[0]["cCusCode"].ToString(); if (v.bReturnFlag.Equals("0")) { //v.personname = dt.Rows[0]["cDefine12"].ToString();//业务员名称 //v.define12 = dt.Rows[0]["cDefine12"].ToString();//业务员名称 v.personcode = dt.Rows[0]["cPersonCode"].ToString();//业务员编号 } else { v.personcode = DbUtils.ExecuteScalar(string.Format(@" select cpersoncode from {0}..person where cPersonName='{1}' ", ConnectionUtils.Instance.GetConnection().Database, dt.Rows[0]["cDefine12"].ToString()), ConnectionUtils.Instance.GetConnection("TempDB")).ToString(); } //v.bReturnFlag.Equals("0"); //v.deptcode = DbUtils.ExecuteScalar(string.Format(@" select cDepCode from {0}..person where cpersoncode='{1}' ", // ConnectionUtils.Instance.GetConnection().Database, "salesman_no"), // ConnectionUtils.Instance.GetConnection("TempDB")).ToString(); //------------------------------------------ } //v.deptcode = dt.Rows[0]["cDepCode"].ToString();//部门编码 //v.code = dt.Rows[0]["cSOCode"].ToString();//订单号 //v.operation_type = dt.Rows[0]["cBusType"].ToString();//业务类型//dt.Rows[0]["cSTCode"].ToString();//销售类型编码 //string bReturnFlag = dt.Rows[0]["bReturnFlag"].ToString(); //if (bReturnFlag.Equals("0")) //{ v.operation_type = "普通销售"; v.saletype = "MX"; //} //else //{ // v.saletypename = "退货入库"; //} //DateTime date = DateTime.Parse(dt.Rows[0]["dDate"].ToString()); //v.date = date.ToString("yyyy-MM-dd"); v.define5 = dt.Rows[0]["DLID"].ToString(); v.deptcode = dt.Rows[0]["cDepCode"].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 Returnorder __GetHeadTwo(DataSet ds)//(v.plat_id.Equals("9")&& v.define1.Equals("1")) || (v.define1.Equals("7")) || (!v.define1.Equals("1")&&!v.define1.Equals("7")) { Returnorder v = new Returnorder(); DataTable dt = ds.Tables["DispatchList"]; v.define3 = dt.Rows[0]["cDefine3"].ToString();//平台,用于区分是否为阿里巴巴店铺 v.define1 = dt.Rows[0]["cDefine1"].ToString();//订单类型 1网店销售 2线下零售 3售后换货 4批发业务 5保修换新 6保修完成 7订单补发 v.code = dt.Rows[0]["cDLCode"].ToString(); #region 备注平台和销售类型 v.remark = v.define3.Equals("9") ? "阿里巴巴" : "非阿里巴巴" + ","; if (v.define1.Equals("1")) { v.remark += "网店销售"; } else if (v.define1.Equals("2")) { v.remark += "线下零售"; } else if (v.define1.Equals("3")) { v.remark += "售后换货"; } else if (v.define1.Equals("4")) { v.remark += "批发业务"; } else if (v.define1.Equals("5")) { v.remark += "保修换新"; } else if (v.define1.Equals("6")) { v.remark += "保修完成"; } else if (v.define1.Equals("7")) { v.remark += "订单补发"; } #endregion v.bReturnFlag = Convert.ToBoolean(dt.Rows[0]["bReturnFlag"]) ? "1" : "0"; if (!v.define3.Equals("9") && v.define1.Equals("1") && v.bReturnFlag.Equals("0"))//合并订单不带业务员 //((v.define3.Equals("9") && v.define1.Equals("1")) || (v.define1.Equals("7")) || (!v.define1.Equals("1") && !v.define1.Equals("7"))) { // v.custcode = AppSeting.Instance.GetValue("clientRetail");//零售(其他)平台客户编码KN02 v.custcode = dt.Rows[0]["cDefine2"].ToString();//客户编号==店铺编号 v.cusname = dt.Rows[0]["cDefine11"].ToString();//客户名称==店铺名称 v.cusabbname = dt.Rows[0]["cDefine11"].ToString();//客户名称==店铺名称 } else { //v.custcode = AppSeting.Instance.GetValue("clientOff");//线下平台客户编码KN01 //v.define10 = dt.Rows[0]["cCusName"].ToString(); v.custcode = dt.Rows[0]["cDefine2"].ToString();//客户编号==店铺编号custcode v.cusname = dt.Rows[0]["cDefine11"].ToString();//客户名称==店铺名称 v.cusabbname = dt.Rows[0]["cDefine11"].ToString();//客户名称==店铺名称cusname v.define10 = dt.Rows[0]["cCusName"].ToString(); v.define11 = dt.Rows[0]["cCusCode"].ToString(); if (v.bReturnFlag.Equals("0")) { // v.personname = dt.Rows[0]["cDefine12"].ToString();//业务员名称 //v.define12 = dt.Rows[0]["cDefine12"].ToString();//业务员名称 v.personcode = dt.Rows[0]["cPersonCode"].ToString();//业务员编号 } else { // v.personcode = DbUtils.ExecuteScalar(string.Format(@" select cpersoncode from {0}..person where cPersonName='{1}' ", //ConnectionUtils.Instance.GetConnection().Database, dt.Rows[0]["cDefine12"].ToString()), //ConnectionUtils.Instance.GetConnection("TempDB")).ToString(); } v.state = "Approved"; v.deptcode = dt.Rows[0]["cDepCode"].ToString(); //v.bReturnFlag.Equals("0"); //v.deptcode = DbUtils.ExecuteScalar(string.Format(@" select cDepCode from {0}..person where cpersoncode='{1}' ", // ConnectionUtils.Instance.GetConnection().Database, "salesman_no"), // ConnectionUtils.Instance.GetConnection("TempDB")).ToString(); //------------------------------------------ } //v.deptcode = dt.Rows[0]["cDepCode"].ToString();//部门编码 //v.code = dt.Rows[0]["cSOCode"].ToString();//订单号 //v.operation_type = dt.Rows[0]["cBusType"].ToString();//业务类型//dt.Rows[0]["cSTCode"].ToString();//销售类型编码 string bReturnFlag = dt.Rows[0]["bReturnFlag"].ToString(); //if (bReturnFlag.Equals("0")) //{ v.operation_type = "普通销售"; v.saletype = AppSeting.Instance.GetValue("cSTCode"); //} //else //{ // v.saletypename = "退货入库"; //} //DateTime date = DateTime.Parse(dt.Rows[0]["dDate"].ToString()); //v.date = date.ToString("yyyy-MM-dd"); v.define5 = dt.Rows[0]["DLID"].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["DispatchLists"].Rows.Count; DataTable dt = ds.Tables["DispatchLists"]; Entry[] rtn = new Entry[cnt]; for (int i = 0; i < cnt; i++) { Entry e = new Entry(); e.inventory_code = dt.Rows[i]["cInvCode"].ToString().Replace(" ", "");//存货编码 if (___bReturnFlag == 0) { e.quantity = dt.Rows[i]["iQuantity"].ToString();//数量 //e.price = dt.Rows[i]["iQuotedPrice"].ToString();//单价 e.taxprice = dt.Rows[i]["iQuotedPrice"].ToString();//含税单价 } else { e.quantity = (Convert.ToInt32(dt.Rows[i]["iQuantity"]) * (-1)).ToString(); //e.price = dt.Rows[i]["iQuotedPrice"].ToString(); e.taxprice = dt.Rows[i]["iQuotedPrice"].ToString();//含税单价 } e.define29 = dt.Rows[i]["cDefine29"].ToString(); e.define28 = dt.Rows[i]["cDefine28"].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 = AppSeting.Instance.GetValue("taxrate"); //dt.Rows[i]["iTaxRate"].ToString();//税率 e.warehouse_code = dt.Rows[i]["cCusInvCode"].ToString(); e.define27 = dt.Rows[i]["DLID"].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 DispatchList 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 DispatchList 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 DispatchList set processflag = 1,processdate = getdate(),newcode='" + id + "' where sysid in (" + ids + ")"; OperationLog.Utils.UpdateTempRecord(sql); } } }