| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867 |
- using BizCom;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System.Data;
- //status可选值:
- //* TRADE_NO_CREATE_PAY(没有创建支付宝交易)
- //* WAIT_BUYER_PAY(等待买家付款)
- //* SELLER_CONSIGNED_PART(卖家部分发货)
- //* WAIT_SELLER_SEND_GOODS(等待卖家发货, 即:买家已付款)
- //* WAIT_BUYER_CONFIRM_GOODS(等待买家确认收货, 即:卖家已发货)
- //* TRADE_BUYER_SIGNED(买家已签收, 货到付款专用)
- //* TRADE_FINISHED(交易成功)
- //* TRADE_CLOSED(付款以后用户退款成功,交易自动关闭)
- //* TRADE_CLOSED_BY_TAOBAO(付款以前,卖家或买家主动关闭交易)
- //* PAY_PENDING(国际信用卡支付付款确认中)
- //* WAIT_PRE_AUTH_CONFIRM(0元购合约中)
- namespace ErpServer
- {
- /// <summary>
- /// 同步推送处理
- /// </summary>
- public class tmcHelper
- {
- public static int RemoveEmptyEntries { get; private set; }
- // case "taobao_trade_TradeCreate"://创建淘宝交易消息
- // {
- // break;
- // }
- // case "taobao_trade_TradeLogisticsAddressChanged"://卖家修改交易收货地址消息
- // case "taobao_trade_TradeMemoModified"://交易备注修改消息
- // case "taobao_trade_TradeRated"://交易评价变更消息
- // case "taobao_trade_TradeBuyerPay"://买家付完款,或万人团买家付完尾款
- // case "taobao_trade_TradeSuccess"://交易成功消息(确认收货)
- // case "taobao_trade_TradePartlyConfirmPay"://子订单打款成功消息
- // case "taobao_trade_TradePartlyRefund"://子订单退款成功消息
- // case "taobao_trade_TradeDelayConfirmPay"://延长收货时间消息
- // case "taobao_trade_TradeSellerShip"://卖家发货消息
- // case "taobao_trade_TradeClose"://关闭交易消息
- // case "taobao_trade_TradeCloseAndModifyDetailOrder"://关闭或修改子订单消息
- // case "taobao_trade_TradeModifyFee"://修改交易费用消息
- public static string GetUTF8String(byte[] buffer)
- {
- if (buffer == null)
- return null;
- if (buffer.Length <= 3)
- {
- return Encoding.UTF8.GetString(buffer);
- }
- byte[] bomBuffer = new byte[] { 0xef, 0xbb, 0xbf };
- if (buffer[0] == bomBuffer[0] && buffer[1] == bomBuffer[1] && buffer[2] == bomBuffer[2])
- {
- return new UTF8Encoding(false).GetString(buffer, 3, buffer.Length - 3);
- }
- return Encoding.UTF8.GetString(buffer);
- }
- /// <summary>
- /// 交易订单关闭
- /// </summary>
- /// <param name="content"></param>
- public static void erp_CloseTrade(string content)
- {
- Dictionary<string, object> dics = Utils.Serialization.JSON.ToDictionary(content);
- string status = dics["status"].ToString();//TRADE_CLOSED_BY_TAOBAO
- if (status == "TRADE_CLOSED_BY_TAOBAO")
- {
- StringBuilder sql = new StringBuilder();
- sql.AppendFormat("update CE_ErpTradeCell set status='{0}' where tid='{1}';", status, dics["tid"]);
- sql.AppendFormat("update CE_ErpTradeCell set OrderState={1} where tid='{0}' ;", dics["tid"], OrderState.交易关闭);
- sql.AppendFormat("update CE_ErpTradeOrder set status='{0}' where tid='{1}' and oid='{2}' ;", status, dics["tid"], dics["oid"]);
- CeErpTradeRefund.ExecuteNonQuery(sql.ToString());
- //消息通知
- }
- }
- //接收到--新订单生成消息
- public static void erp_TradeBuyerPay(string content)
- {
- Dictionary<string, object> dics = Utils.Serialization.JSON.ToDictionary(content);
- string tid = dics["tid"].ToString();
- string sellerNick = dics["seller_nick"].ToString();
- string sessionkey = taobaoHelper.ShopKeyDics[sellerNick];
- string res = taobaoHelper.TradeFullinfoGet(sessionkey, tid); //taobaoHelper.test();
- createNewOrder(res);
- }
- //创建新订单
- public static void createNewOrder(string res)
- {
- Trade_fullinfo_get_response_Obj fullObj = null;
- if (res == "") return;
- try
- {
- res = GetUTF8String(Encoding.UTF8.GetBytes(res));
- //res = res.Replace(" ", "");
- fullObj = JsonConvert.DeserializeObject<Trade_fullinfo_get_response_Obj>(res);
- }
- catch (Exception ex)
- {
- }
- if (fullObj == null)
- {
- Console.WriteLine("Trade_fullinfo_get_response_Obj isNull");
- return;
- }
- Trade tObj = fullObj.trade_fullinfo_get_response.trade;
- Orders orderList = tObj.orders;
- StringBuilder sql = new StringBuilder();
- CeErpTrade td = CeErpTrade.Get(tObj.tid);
- if (td!=null)
- {
- //已经存在了,就不要再继续创建了;
- return;
- }
-
- //原始单创建
- CeErpTrade trade = new CeErpTrade();
- trade.tid = tObj.tid;
- trade.num = tObj.num;
- trade.status = tObj.status;
- trade.type = tObj.type;
- trade.price = Convert.ToDouble(tObj.price);
- trade.discount_fee = Convert.ToDouble(tObj.discount_fee);
- trade.total_fee = Convert.ToDouble(tObj.total_fee);
- trade.created = tObj.created;
- if (tObj.pay_time.ToString() != "0001/1/1 0:00:00" && tObj.pay_time != null)
- {
- trade.pay_time = tObj.pay_time;
- }
- if (tObj.modified.ToString() != "0001/1/1 0:00:00" && tObj.modified != null)
- {
- trade.modified = tObj.modified;
- }
- if (tObj.end_time.ToString() != "0001/1/1 0:00:00" && tObj.end_time != null)
- {
- trade.end_time = tObj.end_time;
- }
- trade.buyer_nick = tObj.buyer_nick;
- trade.adjust_fee = Convert.ToDouble(tObj.adjust_fee);
- trade.seller_nick = tObj.seller_nick;
- trade.payment = Convert.ToDouble(tObj.payment);
- trade.receiver_name = tObj.receiver_name;
- trade.receiver_state = tObj.receiver_state;
- trade.receiver_city = tObj.receiver_city;
- trade.receiver_town = GetNoNullValue_string(tObj.receiver_town);
- trade.receiver_district = GetNoNullValue_string(tObj.receiver_district);
- trade.receiver_address = tObj.receiver_address;
- trade.receiver_mobile = GetNoNullValue_string(tObj.receiver_mobile);
- trade.receiver_phone = GetNoNullValue_string(tObj.receiver_phone);
- if (tObj.consign_time.ToString() != "0001/1/1 0:00:00" && tObj.consign_time != null)
- {
- trade.consign_time = tObj.consign_time;
- }
- if (tObj.delivery_time.ToString() != "0001/1/1 0:00:00" && tObj.delivery_time != null)
- {
- trade.delivery_time = tObj.delivery_time;
- }
- trade.buyer_message = GetNoNullValue_string(tObj.buyer_message);
- trade.buyer_memo = GetNoNullValue_string(tObj.buyer_memo);
- trade.seller_memo = GetNoNullValue_string(tObj.seller_memo);
- trade.Create();
- bool isXianhuo = false;//是否为现货单,现货单直接根据客户选的 选项生成Cell备注
- string xianhuoMemo = "(" + trade.tid + ")现货-" ;
- //order表创建
- foreach (Order item in orderList.order)
- {
- CeErpTradeOrder order = new CeErpTradeOrder();
- order.tid = trade.tid;
- order.oid = item.oid;
- order.buyer_nick = trade.buyer_nick;
- order.refund_status = item.refund_status;
- order.status = item.status;
- order.title = item.title;
- order.price = Convert.ToDouble(item.price);
- order.pic_path = item.pic_path;
- order.num = Convert.ToDouble(item.num.ToString());
- order.payment = Convert.ToDouble(item.payment);
- order.adjust_fee = Convert.ToDouble(item.adjust_fee);
-
- if(item.sku_id != null && item.sku_properties_name != null && item.sku_properties_name.IndexOf("现货")!=-1)
- {
- order.sku_properties_name = item.sku_properties_name;
- order.sku_id = item.sku_id;
- isXianhuo = true;
- xianhuoMemo += order.sku_properties_name;
- }
- order.Create();
- }
- if (isXianhuo)
- {
- xianhuoMemo += "-";
- xianhuoMemo += trade.seller_nick;
- }
- //cell表创建
- CeErpTradeCell entity = null;
- entity = CeErpTradeCell.GetByCtid(tObj.tid);
- if (entity == null)
- {
- try
- {
- entity = new CeErpTradeCell();
- entity.ctid = tObj.tid;
- entity.tid = tObj.tid;
- entity.payment = Convert.ToDouble(tObj.payment);
- if (isXianhuo)
- {
- entity.OrderState = 5;
- entity.seller_memo = xianhuoMemo;
- }
- entity.Create();
- }
- catch(Exception ex)
- {
- XLog.SaveLog(0, ex.Message);
- }
-
- }
- operateCustomer(tObj);
- }
- public static void operateCustomer(Trade Obj)
- {
- CeErpCustomer cus = null;
- cus = CeErpCustomer.GetByNick(Obj.buyer_nick);
-
- if (cus != null)
- {
- cus.address = Obj.receiver_state + Obj.receiver_city + Obj.receiver_address;
- cus.phone = Obj.receiver_mobile;
- TimeSpan tsday = Obj.pay_time.Subtract((DateTime)cus.lastbuy_time);
- cus.buy_day = tsday.Days ;
- cus.total_fee = cus.total_fee + Convert.ToDouble(Obj.payment);
- cus.buy_count = cus.buy_count + 1 ;
- cus.lastbuy_time = Obj.pay_time;
- cus.Update();
- }
- else
- {
- try
- {
- cus = new CeErpCustomer();
- cus.buyer_nick = Obj.buyer_nick;
- cus.address = Obj.receiver_state + Obj.receiver_city + Obj.receiver_address;
- cus.phone = Obj.receiver_mobile;
- cus.buy_day = 0;
- cus.buy_count = 1;
- cus.lastbuy_time = Obj.pay_time;
- cus.total_fee = Convert.ToDouble(Obj.payment);
- cus.Create();
- }
- catch(Exception ex)
- {
- XLog.SaveLog(0, ex.Message);
- }
-
- }
- }
- public static string GetNoNullValue_string(string value)
- {
- if (value == null)
- {
- return "";
- }
- return value;
- }
-
- public static void erp_TradeMemoModified(string content)
- {
- Dictionary<string, object> dics = Utils.Serialization.JSON.ToDictionary(content);
- if (!dics.ContainsKey("seller_memo"))
- {
- return;
- }
- string tid = dics["tid"].ToString();
- string smemo = dics["seller_memo"].ToString();//TRADE_CLOSED_BY_TAOBAO
- string buyer = dics["buyer_nick"].ToString();
-
- CeErpTrade mainEn = CeErpTrade.Get(tid);
- if (mainEn == null) return;
- createCellOrder(mainEn,tid,smemo,buyer);
- }
- public static void createCellOrder(CeErpTrade father,string tid,string smemo,string buyer,bool isJump=false)
- {
- bool isNeedCellOrder = true; //是否需要生成cell
- string issql = "select * from ce_erptradecell where tid='" + tid + "';";
- DataTable ishavedt = CeErpTradeCell.ExecuteDataset(issql).Tables[0];
- if (ishavedt.Rows.Count > 0)
- {
- if(Convert.ToInt32(ishavedt.Rows[0]["OrderState"])>0 && Convert.ToInt32(ishavedt.Rows[0]["DesignUserId"]) > 0)
- {
- isNeedCellOrder = false;
- }
- else if (ishavedt.Rows[0]["ctid"].ToString().IndexOf("C") != -1 && Convert.ToInt32(ishavedt.Rows[0]["OrderState"]) == 0 && Convert.ToInt32(ishavedt.Rows[0]["DesignUserId"]) == 0)
- {
- string sql_del = "delete from CE_ErpTradeCell where tid='" + tid + "'";
- CeErpTradeCell.ExecuteNonQuery(sql_del.ToString());
- }
-
- }
- if (isJump)
- {
- isNeedCellOrder = true;
- }
- //补差价单
- if (smemo.IndexOf("补差价") != -1)
- {
- CeErpTradeCell entity = null;
- entity = CeErpTradeCell.GetByCtid(tid);
- bool ishaveBu = true;
- if (entity == null)
- {
- entity = new CeErpTradeCell();
- entity.ctid = tid;
- entity.tid = tid;
- ishaveBu = false;
- }
- if (entity.OrderState >= 5) return;
- entity.seller_memo = smemo;
- entity.IsSample = 2;//补差价单
- smemo = smemo.Replace(":", ":");
- string[] listmemo = smemo.Split(':');
- if (listmemo.Length > 1)
- {
- string maintid = System.Text.RegularExpressions.Regex.Replace(listmemo[1], @"[^0-9]+", "");
- string sql = "select top 1 * from ce_erptradecell where tid='" + maintid + "'";
- DataTable dt_bu = CeErpTradeCell.ExecuteDataset(sql).Tables[0];
- if (dt_bu.Rows.Count > 0)
- {
- entity.ptid = dt_bu.Rows[0]["tid"].ToString();
- entity.OrderState = Convert.ToInt32(dt_bu.Rows[0]["OrderState"]);
- entity.CustomerUserId = Convert.ToInt32(dt_bu.Rows[0]["CustomerUserId"]);
- entity.DesignUserId = Convert.ToInt32(dt_bu.Rows[0]["DesignUserId"]);
- if(dt_bu.Rows[0]["WaitDesignTime"] != DBNull.Value)
- {
- entity.WaitDesignTime = Convert.ToDateTime(dt_bu.Rows[0]["WaitDesignTime"]);
- }
- if (dt_bu.Rows[0]["StartDesignTime"] != DBNull.Value)
- {
- entity.StartDesignTime = Convert.ToDateTime(dt_bu.Rows[0]["StartDesignTime"]);
- }
- if (dt_bu.Rows[0]["FinishDesignTime"] != DBNull.Value)
- {
- entity.FinishDesignTime = Convert.ToDateTime(dt_bu.Rows[0]["FinishDesignTime"]);
- }
- if (dt_bu.Rows[0]["FinishPlaceTime"] != DBNull.Value)
- {
- entity.FinishPlaceTime = Convert.ToDateTime(dt_bu.Rows[0]["FinishPlaceTime"]);
- }
- }
- }
- if (ishaveBu)
- {
- entity.Update();
- }
- else
- {
- entity.Create();
- }
- LogHelper.addLog(entity.ctid, entity.CustomerUserId, entity.seller_memo, entity.OrderState);
- isNeedCellOrder = false;
- }
- else if(smemo.IndexOf("拿样订单") != -1)
- {
- CeErpTradeCell entity = null;
- entity = CeErpTradeCell.GetByCtid(tid);
- if (entity == null)
- {
- entity = new CeErpTradeCell();
- entity.ctid = tid;
- entity.tid = tid;
- entity.seller_memo = smemo;
- entity.OrderState = 6; //下单完成
- entity.IsSample = 1;
- entity.Create();
- }
- else
- {
- entity.IsSample = 1;
- entity.seller_memo = smemo;
- entity.OrderState = 6;
- entity.Update();
- }
- LogHelper.addLog(entity.ctid, entity.CustomerUserId, entity.seller_memo, entity.OrderState);
- isNeedCellOrder = false;
- }
-
- if (isNeedCellOrder == false)
- {
- return;
- }
- if (smemo.IndexOf("*") != -1)
- {
- return;
- }
-
- string[] memoList = null;
- if (smemo.IndexOf(",") != -1)
- {
- smemo = smemo.Replace(',',',');
- }
- memoList = smemo.Split(',');
- int orderIdx = 1;
- int hebaoCount = 0;
- bool isMulity = false;
- if (memoList.Length > 1)
- {
- isMulity = true;
- foreach(string mstr in memoList)
- {
- if (mstr.IndexOf("合包") != -1)
- {
- hebaoCount += 1;
- }
- }
-
- }
- if (hebaoCount == 1)
- {
- createHebaoCell(isMulity, 0, smemo, father);
- }
- else
- {
- double hePrice = father.payment;
- string needHebaoItem = "";
- foreach (string item in memoList)
- {
- if (item.IndexOf("合包") != -1)
- {
- needHebaoItem += item;
- needHebaoItem += ",";
- }
- else
- {
- if (item.IndexOf("电子稿") != -1)
- {
- createDianziCell(isMulity,orderIdx,item,father,ref hePrice);
- }
- else if(item.IndexOf("logo") != -1 || item.IndexOf("LOGO") != -1)
- {
- createLogoCell(isMulity, orderIdx, item, father, ref hePrice);
- }
- else if (item.IndexOf("现货") != -1)
- {
- createSmallGiftCell(isMulity, orderIdx, item, father, ref hePrice);
- }
- else
- createNotHeBaoCell(isMulity, orderIdx, item, father,ref hePrice);
- orderIdx++;
- }
- }
- if (needHebaoItem.Length > 0)
- {
- createHebaoCell(isMulity, orderIdx, needHebaoItem, father,hePrice);
- orderIdx++;
- }
- if (orderIdx>2)
- {
- string sql_del = "delete from CE_ErpTradeCell where ctid='" + tid + "'";
- CeErpTradeRefund.ExecuteNonQuery(sql_del.ToString());
- }
- }
- }
- public static void createLogoCell(bool isMulty, int orderIndex, string itemMemo, CeErpTrade father,ref double hePrice)
- {
- if (itemMemo.Length <= 0) return;
- string ctid = "";
- if (isMulty && itemMemo.Length > 2)
- {
- string preStr = "C" + orderIndex + "_";
- ctid = preStr + father.tid;
- }
- else if (itemMemo.Length > 2)
- {
- ctid = father.tid;
- }
- CeErpTradeCell entity = null;
- entity = CeErpTradeCell.GetByCtid(ctid);
- bool isNull = false;
- if (entity == null)
- {
- entity = new CeErpTradeCell();
- isNull = true;
- }
- entity.ctid = ctid;
- entity.tid = father.tid;
- string[] acclist = itemMemo.Split('-');
- string count = acclist[0];
- string product = acclist[1];
- string account = acclist[3];
- DataRow cusInfo = getUserInfoByAccount(account); //客服id
- if (cusInfo != null)
- {
- entity.CustomerUserId = Convert.ToInt32(cusInfo["ID"]);
- if (entity.OrderState > 2 && entity.DesignUserId > 0)
- {
- }
- else
- entity.OrderState = Convert.ToInt32(OrderState.待抢单);
- }
- entity.ProductId = getProductIdByName("logo");
- entity.ProductCount = count;
- if (isMulty)
- {
- try
- {
- entity.payment = Convert.ToDouble(acclist[4]);
- hePrice = hePrice - Convert.ToDouble(acclist[4]);
- }
- catch (Exception ex)
- {
- XLog.SaveLog(0, "createDianziCell,payment=acclist[4]" + "|" + ex.Message);
- }
- }
- string newmemo = "(" + entity.tid + ")-";
- for (int i = 0; i < acclist.Length; i++)
- {
- if (i == 3) continue;
- newmemo += acclist[i];
- if (i < acclist.Length - 2)
- {
- newmemo += "-";
- }
- }
- string sName = getShopSNameByName(father.seller_nick);
- newmemo += ("-" + sName);
- newmemo += ("-" + father.buyer_nick);
- newmemo += ("-" + father.receiver_state);
- if (cusInfo != null)
- {
- newmemo += ("-" + Convert.ToString(cusInfo["Tb"]));
- }
- if (isMulty)
- {
- newmemo += "-[C" + orderIndex + "]";
- }
- entity.seller_memo = newmemo;
- if (isNull)
- {
- entity.Create();
- }
- else
- {
- entity.Update();
- }
- LogHelper.addLog(entity.ctid, entity.CustomerUserId, entity.seller_memo, entity.OrderState);
-
- }
- public static void createDianziCell(bool isMulity, int orderIndex, string itemMemo, CeErpTrade father, ref double hePrice)
- {
- if (itemMemo.Length <= 0) return;
- string ctid = "";
- if (isMulity && itemMemo.Length > 2)
- {
- string preStr = "C" + orderIndex + "_";
- ctid = preStr + father.tid;
- }
- else if (itemMemo.Length > 2)
- {
- ctid = father.tid;
- }
- CeErpTradeCell entity = null;
- entity = CeErpTradeCell.GetByCtid(ctid);
- bool isNull = false;
- if (entity == null)
- {
- entity = new CeErpTradeCell();
- isNull = true;
- }
- entity.ctid = ctid;
- entity.tid = father.tid;
- string[] acclist = itemMemo.Split('-');
- string count = acclist[0];
- string product = acclist[1];
- string account = acclist[3];
- DataRow cusInfo = getUserInfoByAccount(account); //客服id
- if (cusInfo != null)
- {
- entity.CustomerUserId = Convert.ToInt32(cusInfo["ID"]);
- }
- if (entity.OrderState < 3 && entity.DesignUserId == 0)
- {
- entity.OrderState = Convert.ToInt32(OrderState.待设计);
- entity.DesignUserId = 255;
- entity.WaitDesignTime = DateTime.Now;
- }
- entity.ProductId = getProductIdByName(product);
- entity.ProductCount = count;
- if (isMulity)
- {
- try
- {
- entity.payment = Convert.ToDouble(acclist[4]);
- hePrice = hePrice - Convert.ToDouble(acclist[4]);
- }
- catch (Exception ex)
- {
- XLog.SaveLog(0, "createDianziCell,payment=acclist[4]" + "|" + ex.Message);
- }
- }
- string newmemo = "(" + entity.tid + ")-";
- for (int i = 0; i < acclist.Length; i++)
- {
- if (i == 3) continue;
- newmemo += acclist[i];
- if (i < acclist.Length - 2)
- {
- newmemo += "-";
- }
- }
- string sName = getShopSNameByName(father.seller_nick);//获取店铺简称
- newmemo += ("-" + sName);
- newmemo += ("-" + father.buyer_nick);
- newmemo += ("-" + father.receiver_state);
- if (cusInfo != null)
- {
- newmemo += ("-" + Convert.ToString(cusInfo["Tb"]));
- }
- if (isMulity)
- {
- newmemo += "-[C" + orderIndex + "]";
- }
- entity.seller_memo = newmemo;
- if (isNull)
- {
- entity.Create();
- }
- else
- {
- entity.Update();
- }
- LogHelper.addLog(entity.ctid, entity.CustomerUserId, entity.seller_memo, entity.OrderState);
- }
- public static void createSmallGiftCell(bool isMulty, int orderIndex, string itemMemo, CeErpTrade father,ref double hePrice)
- {
- if (itemMemo.Length <= 0) return;
- string ctid = "";
- if (isMulty && itemMemo.Length > 2)
- {
- string preStr = "C" + orderIndex + "_";
- ctid = preStr + father.tid;
- }
- else if (itemMemo.Length > 2)
- {
- ctid = father.tid;
- }
- CeErpTradeCell entity = null;
- entity = CeErpTradeCell.GetByCtid(ctid);
- bool isNull = false;
- if (entity == null)
- {
- entity = new CeErpTradeCell();
- isNull = true;
- }
- entity.ctid = ctid;
- entity.tid = father.tid;
- string[] acclist = itemMemo.Split('-');
- string product = acclist[0];
- string account = acclist[1];
- DataRow cusInfo = getUserInfoByAccount(account); //客服id
- if (cusInfo != null)
- {
- entity.CustomerUserId = Convert.ToInt32(cusInfo["ID"]);
- }
- entity.OrderState = Convert.ToInt32(OrderState.下单完成);
- if (isMulty)
- {
- try
- {
- entity.payment = Convert.ToDouble(acclist[2]);
- hePrice = hePrice - Convert.ToDouble(acclist[2]);
- }
- catch (Exception ex)
- {
- XLog.SaveLog(0, "createDianziCell,payment=acclist[4]" + "|" + ex.Message);
- }
- }
- string newmemo = "(" + entity.tid + ")-";
- for (int i = 0; i < acclist.Length; i++)
- {
- if (i == 1) continue;
- newmemo += acclist[i];
- if (i < acclist.Length - 1)
- {
- newmemo += "-";
- }
- }
- string sName = getShopSNameByName(father.seller_nick);
- newmemo += ("-" + sName);
- newmemo += ("-" + father.buyer_nick);
- newmemo += ("-" + father.receiver_state);
- if (cusInfo != null)
- {
- newmemo += ("-" + Convert.ToString(cusInfo["Tb"]));
- }
- if (isMulty)
- {
- newmemo += "-[C" + orderIndex + "]";
- }
- entity.seller_memo = newmemo;
- if (isNull)
- {
- entity.Create();
- }
- else
- {
- entity.Update();
- }
- LogHelper.addLog(entity.ctid, entity.CustomerUserId, entity.seller_memo, entity.OrderState);
- }
-
- public static void createHebaoCell(bool isMulity, int orderIndex, string itemMemo, CeErpTrade father,double hePrice=0)
- {
- if (itemMemo.Length <= 0) return;
- string[] hebaoMemoList = itemMemo.Split(',');
- string memo_index1 = hebaoMemoList[0];
- string ctid = "";
- if (isMulity && orderIndex>1 && itemMemo.Length > 2)
- {
- string preStr = "C" + orderIndex + "_";
- ctid = preStr + father.tid;
- }
- else if (itemMemo.Length > 2)
- {
- ctid = father.tid;
- }
- CeErpTradeCell entity = CeErpTradeCell.GetByCtid(ctid);
- bool ishave = true;
- if (entity == null)
- {
- entity = new CeErpTradeCell();
- ishave = false;
- }
- entity.ctid = ctid;
- entity.tid = father.tid;
- string[] acclist = memo_index1.Split('-');
- if (acclist.Length < 6)
- {
- return;
- }
- string size = acclist[0];
- string count = acclist[1];
- bool isBuGanJiao = false;
- string product = "";
- string mt = "", ct = "";
- string account = "";
- //不干胶材质和产品名对换
- memo_index1 = memo_index1.Replace("PVC", "pvc");
- if ((memo_index1.IndexOf("不干胶") != -1 || memo_index1.IndexOf("PVC卡片") != -1 || memo_index1.IndexOf("pvc卡片") != -1 || memo_index1.IndexOf("海报") != -1) && memo_index1.IndexOf("现货")==-1)
- {
- isBuGanJiao = true;
- product = acclist[3];
- mt = acclist[2];
- }
- else
- {
- product = acclist[2];
- mt = acclist[3];
- }
-
- account = acclist[5];
- ct = acclist[4];
- entity.ProductId = getProductIdByName(product);
- //找不到对应产品id的不生成
- if (entity.ProductId == 0)
- {
- return;
- }
- if (isMulity && hePrice != 0)
- {
- entity.payment = hePrice;
- }
- else
- {
- entity.payment = father.payment;
- }
- DataRow cusInfo = getUserInfoByAccount(account); //客服id
- if (cusInfo != null)
- {
- entity.CustomerUserId = Convert.ToInt32(cusInfo["ID"]);
- if (entity.OrderState > 2 && entity.DesignUserId > 0)
- {
- }
- else
- {
- entity.OrderState = 2;
- if (entity.DesignUserId > 0)
- {
- entity.OrderState = 3;
- }
- }
- }
- if (itemMemo.IndexOf("老客户") != -1 || itemMemo.IndexOf("老顾客") != -1)
- {
- if (itemMemo.IndexOf("改稿") != -1)
- {
- int designId = getOldCusByBuyer(father.buyer_nick);
- entity.DesignUserId = designId;
- if (designId != 0)
- {
- if (entity.OrderState > 2 && entity.DesignUserId > 0)
- {
- }
- else
- entity.OrderState = Convert.ToInt32(OrderState.待设计);
- entity.WaitDesignTime = DateTime.Now;
- LogHelper.addLog(entity.ctid, 0, "老客户改稿", entity.OrderState);
- }
-
- }
- else
- {
- entity.OrderState = 3; // Convert.ToInt32(OrderState.待设计);
- entity.DesignUserId = Convert.ToInt32(webConfig.PlaceDesigner_id); //下单不林春鸿
- entity.WaitDesignTime = DateTime.Now;
- LogHelper.addLog(entity.ctid, 0, "老客户单指派到下单部", entity.OrderState);
- }
- }
- entity.Material = mt;
- entity.ProductSize = size;
- entity.ProductCount = count;
- entity.Craft = ct;
- string newmemo = "(" + father.tid + ")-";
- for(int idx = 0; idx < hebaoMemoList.Length; idx++)
- {
-
- string smallitem = hebaoMemoList[idx];
- if (idx == 0 && smallitem.IndexOf("海报") != -1)
- {
- newmemo = "";
- }
- smallitem = smallitem.Replace("-合包", "");
- smallitem = smallitem.Replace("-(合包)", "");
- string[] smallList = smallitem.Split('-');
- if (smallitem.IndexOf("海报") != -1)
- {
- newmemo += smallList[2];
- newmemo += smallList[0];
- newmemo += "-";
- newmemo += smallList[1];
- newmemo += "(";
- newmemo += father.tid;
- newmemo += ")";
- newmemo += "-";
- newmemo += smallList[4];
- }
- else
- {
- for (int i = 0; i < smallList.Length; i++)
- {
- if (isMulity && i == 5)
- {
- continue;
- }
- newmemo += smallList[i];
- if (i < smallList.Length - 2 && i != 2 && i != 3)
- {
- newmemo += "-";
- }
- }
- }
-
- if (hebaoMemoList.Length > idx + 1)
- {
- newmemo += ",";
- }
- }
- //string nohebaomemo = itemMemo.Replace("-合包","");
- //string newmemo = "(" + entity.tid + ")-" + nohebaomemo;
- string sName = getShopSNameByName(father.seller_nick);//获取店铺简称
- newmemo += ("-" + sName);
- newmemo += ("-" + father.buyer_nick);
- newmemo += ("-" + father.receiver_state);
- if (cusInfo != null)
- {
- newmemo += ("-" + Convert.ToString(cusInfo["Tb"]));
- }
- if (orderIndex > 1 && isMulity)
- {
- newmemo += "-[C" + orderIndex + "]";
- }
- newmemo += "-合包";
- entity.seller_memo = newmemo;
- if (ishave)
- {
- entity.Update();
- }
- else
- entity.Create();
- LogHelper.addLog(entity.ctid, entity.CustomerUserId, entity.seller_memo, entity.OrderState);
- }
- public static void createNotHeBaoCell(bool isMulity, int orderIndex,string itemMemo,CeErpTrade father,ref double hePrice)
- {
- if (itemMemo.Length <= 0) return;
- string ctid = "";
- if (isMulity && itemMemo.Length > 2) //item.length>2 避免备注加了分隔符,,且后面的是空的或者回车或者空格
- {
- string preStr = "C" + orderIndex + "_";
- ctid = preStr + father.tid;
- }
- else if (itemMemo.Length > 2)
- {
- ctid = father.tid;
- }
- CeErpTradeCell entity = CeErpTradeCell.GetByCtid(ctid);
- bool ishave = true;
- if (entity == null)
- {
- entity = new CeErpTradeCell();
- ishave = false;
- }
- entity.ctid = ctid;
- entity.tid = father.tid;
- string[] acclist = itemMemo.Split('-');
- if (isMulity && acclist.Length < 7)
- {
- return;
- }
- if (acclist.Length < 6)
- {
- return;
- }
- string size = acclist[0];
- string count = acclist[1];
- bool isBuGanJiao = false;
- string product = "";
- string mt = "", ct = "";
- string account = "";
- //不干胶材质和产品名对换
- itemMemo = itemMemo.Replace("PVC", "pvc");
- if ((itemMemo.IndexOf("不干胶") != -1 || itemMemo.IndexOf("PVC卡片") != -1 || itemMemo.IndexOf("pvc卡片") != -1 || itemMemo.IndexOf("海报") != -1) && itemMemo.IndexOf("现货") == -1)
- {
- isBuGanJiao = true;
- product = acclist[3];
- mt = acclist[2];
- }
- else
- {
- product = acclist[2];
- mt = acclist[3];
- }
- account = acclist[5];
- ct = acclist[4];
- //如果是拆分单的话要多一个价格的拆分
- if (isMulity)
- {
- try
- {
- string strPrice = acclist[6];
- if (strPrice.IndexOf("元") != -1)
- {
- strPrice = strPrice.Replace("元", "");
- }
- entity.payment = Convert.ToDouble(strPrice);
- hePrice = hePrice - Convert.ToDouble(strPrice); //总价格-拆分后其他的价格=剩下需要拿去合包的价格
- }
- catch (Exception ex)
- {
- //XLog.SaveLog(0, "createNotHeBaoCell,payment=acclist[6]" + "|" + ex.Message);
- }
- }
- else
- {
- entity.payment = father.payment;
- }
-
- entity.ProductId = getProductIdByName(product);
- //找不到对应产品id的不生成
- if (entity.ProductId == 0)
- {
- return;
- }
- DataRow cusInfo = getUserInfoByAccount(account); //客服id
- if (cusInfo != null)
- {
- entity.CustomerUserId = Convert.ToInt32(cusInfo["ID"]);
- if(entity.OrderState>2 && entity.DesignUserId > 0)
- {
- //到了设计那边,就更新memo,状态不设置
- }
- else
- {
- entity.OrderState = 2;
- if (entity.DesignUserId > 0) //推送过来更新的时候,如果已经指派给设计师了就待设计
- {
- entity.OrderState = 3;
- }
- }
-
- }
- if (itemMemo.IndexOf("老客户") != -1 || itemMemo.IndexOf("老顾客") != -1)
- {
- if (itemMemo.IndexOf("改稿") != -1)
- {
- int designId = getOldCusByBuyer(father.buyer_nick);
- entity.DesignUserId = designId;
- if (designId != 0)
- {
- if (entity.OrderState > 2 && entity.DesignUserId > 0)
- {
- //到了设计那边,就更新memo,状态不设置
- }
- else
- entity.OrderState = Convert.ToInt32(OrderState.待设计);
- entity.WaitDesignTime = DateTime.Now;
- LogHelper.addLog(entity.ctid, 0, "老客户改稿", entity.OrderState);
- }
- }
- else
- {
- entity.OrderState = 3; // Convert.ToInt32(OrderState.待设计);
- entity.DesignUserId = Convert.ToInt32(webConfig.PlaceDesigner_id); //下单不林春鸿;//下单不林春鸿
- entity.WaitDesignTime = DateTime.Now;
- LogHelper.addLog(entity.ctid, 0, "老客户单指派到下单部", entity.OrderState);
- }
- }
- entity.Material = mt;
- entity.ProductSize = size;
- entity.ProductCount = count;
- entity.Craft = ct;
- string newmemo = "(" + father.tid + ")-";
- if (itemMemo.IndexOf("海报") != -1)
- {
- newmemo = acclist[2];
- newmemo += acclist[0];
- newmemo += "-";
- newmemo += acclist[1];
- newmemo += "(";
- newmemo += father.tid;
- newmemo += ")";
- newmemo += "-";
- newmemo += acclist[4];
- }
- else
- {
- for (int i = 0; i < acclist.Length; i++)
- {
- if (isMulity && i == 6)
- {
- continue;
- }
- else if (i == 5)
- {
- continue;
- }
- newmemo += acclist[i];
- if (i < acclist.Length - 2 && i != 2 && i != 3)
- {
- newmemo += "-";
- }
- }
- }
-
- string sName = getShopSNameByName(father.seller_nick);//获取店铺简称
- newmemo += ("-" + sName);
- newmemo += ("-" + father.buyer_nick);
- newmemo += ("-" + father.receiver_state);
- if (cusInfo != null)
- {
- newmemo += ("-" + Convert.ToString(cusInfo["Tb"]));
- }
- if (isMulity)
- {
- newmemo += "-[C" + orderIndex + "]";
- }
- entity.seller_memo = newmemo;
- if (ishave)
- {
- entity.Update();
- }
- else
- entity.Create();
- LogHelper.addLog(entity.ctid, entity.CustomerUserId, "推送生成:" + entity.seller_memo, entity.OrderState);
- }
- public static int getOldCusByBuyer(string buyer)
- {
- string sql = "select top 5 * from view_erptradecell where buyer_nick='" + buyer + "';";
- DataTable dt = CeErpTradeRefund.ExecuteDataset(sql).Tables[0];
- foreach (DataRow dr in dt.Rows)
- {
- if (Convert.ToInt32(dr["DesignUserId"]) != 0 && Convert.ToInt32(dr["DesignUserId"])!= Convert.ToInt32(webConfig.PlaceDesigner_id)) //188林春鸿下单部设计
- {
- return Convert.ToInt32(dr["DesignUserId"]);
- }
- }
- return 0;
- }
- public static int getProductIdByName(string proName)
- {
- string dname = proName;
- if(dname == "暧心贴")
- {
- dname = "暖心贴";
- }
- string sql = "select top 1 * from ce_erpproduct where PType='" + dname + "';";
- DataTable dt = CeErpTradeRefund.ExecuteDataset(sql).Tables[0];
- if (dt.Rows.Count > 0)
- {
- return Convert.ToInt32(dt.Rows[0]["ID"]);
- }
- return 0;
- }
- public static string getShopSNameByName(string seller_nick)
- {
- string sql = "select * from CE_ErpShop where ShopName='" + seller_nick + "';";
- DataTable dt = CeErpTradeRefund.ExecuteDataset(sql).Tables[0];
- if (dt.Rows.Count > 0)
- {
- return Convert.ToString(dt.Rows[0]["SName"]);
- }
- return "";
- }
- //public static string getShopRealName(string seller_nick)
- //{
- // string sql = "select * from CE_ErpShop where ShopName='" + seller_nick + "';";
- // DataTable dt = DbHelper.DbConn.ExecuteDataset(sql).Tables[0];
- // if (dt.Rows.Count > 0)
- // {
- // if(dt.Rows[0]["ShopName"].ToString()!= dt.Rows[0]["Summary"].ToString())
- // return Convert.ToString(dt.Rows[0]["ShopName"]);
- // }
- // return seller_nick;
- //}
- //补差价单 关联
- public static DataRow getRelationOrder(string buyer,string sampTid)
- {
- string sql = "select top 3 * from view_erptradecell where buyer_nick='" + buyer+"' and seller_memo not like '%补差价%' and tid<>'"+sampTid+"' order by id desc";
- DataTable dt = CeErpTradeRefund.ExecuteDataset(sql).Tables[0];
- if (dt.Rows.Count > 0)
- {
- DataRow need = dt.Rows[0];
- if (dt.Rows.Count == 1)
- {
- return need;
- }
- int idx = 0;
- foreach(DataRow dr in dt.Rows)
- {
- if (idx != 0)
- {
- int day = Convert.ToDateTime(need["pay_time"]).Day;
- int nday = Convert.ToDateTime(dr["pay_time"]).Day;
- if (day==nday)
- {
- need = dr;
- }
- }
- idx++;
- }
- return need;
- }
- return null;
- }
- public static DataRow getUserInfoByAccount(string account)
- {
- string sql = "select * from view_erpuser where Account='"+account+"'";
- DataTable dt = CeErpTradeCell.ExecuteDataset(sql).Tables[0];
- if (dt != null && dt.Rows.Count > 0)
- {
- return dt.Rows[0];
- }
- return null;
- }
- public static void erp_TradeSuccess(string content)
- {
- Dictionary<string, object> dics = Utils.Serialization.JSON.ToDictionary(content);
- string status = dics["status"].ToString();//TRADE_CLOSED_BY_TAOBAO
- //if (status == "TRADE_FINISHED")
- //{
- StringBuilder sql = new StringBuilder();
- sql.AppendFormat("update CE_ErpTrade set status='{0}' where tid='{1}';", "TRADE_FINISHED", dics["tid"]);
- sql.AppendFormat("update view_ErpTradeCell set OrderState={1} where tid='{0}';", dics["tid"], (int)OrderState.交易完成);
- sql.AppendFormat("update CE_ErpTradeOrder set status='{0}' where tid='{1}' and oid='{2}' ;", "TRADE_FINISHED", dics["tid"], dics["oid"]);
- CeErpTradeCell.ExecuteNonQuery(sql.ToString());
- //}
- }
- public static void erp_TradeSellerShip(string content)
- {
- Dictionary<string, object> dics = Utils.Serialization.JSON.ToDictionary(content);
- string status = dics["status"].ToString();//TRADE_CLOSED_BY_TAOBAO
- //if (status == "WAIT_BUYER_CONFIRM_GOODS")
- //{
- StringBuilder sql = new StringBuilder();
- //sql.AppendFormat("update view_ErpTradeCell set OrderState={2} where tid='{0}' and IsPreDelivery=0 ;", dics["tid"], (int)OrderState.已发货);//发货接口已经改变OrderState
- sql.AppendFormat("update CE_ErpTrade set status='{0}' where tid='{1}' ;", "WAIT_BUYER_CONFIRM_GOODS", dics["tid"]);
- sql.AppendFormat("update CE_ErpTradeOrder set status='{0}' where tid='{1}' ;", "WAIT_BUYER_CONFIRM_GOODS", dics["tid"]);
- CeErpTradeCell.ExecuteNonQuery(sql.ToString());
- //}
- }
- public static void erp_TradePartlyRefund(string content)
- {
- Dictionary<string, object> dics = Utils.Serialization.JSON.ToDictionary(content);
- string status = dics["status"].ToString();//TRADE_CLOSED_BY_TAOBAO
- //if (status == "TRADE_CLOSED")
- //{
- StringBuilder sql = new StringBuilder();
- sql.AppendFormat("update CE_ErpTrade set status='{0}'where tid='{1}' ;", "TRADE_CLOSED", dics["tid"]);
- sql.AppendFormat("update CE_ErpTradeOrder set status='{0}' where tid='{1}' and oid='{2}';", "TRADE_CLOSED", dics["tid"], dics["oid"]);
- sql.AppendFormat("update CE_ErpTradeCell set OrderState='{0}',IsRefund={1} where tid='{2}';", (int)OrderState.交易关闭,1, dics["tid"]);
- CeErpTradeCell.ExecuteNonQuery(sql.ToString());
- LogHelper.addLog(dics["tid"].ToString(),0,"客户申请退款,已确认", (int)OrderState.交易关闭) ;
- //}
- }
- public static void Api_TmcTradeMsg(string res)
- {
- Api_tmc_trade_info_Obj fullObj = null;
- if (res == "") return;
- try
- {
- res = GetUTF8String(Encoding.UTF8.GetBytes(res));
- //res = res.Replace(" ", "");
- fullObj = JsonConvert.DeserializeObject<Api_tmc_trade_info_Obj>(res);
- }
- catch (Exception ex)
- {
- XLog.SaveLog(0, res + "|1|"+ex.Message);
- }
- if (fullObj == null)
- {
- Console.WriteLine("Api_tmc_trade_info_Obj isNull");
- return;
- }
- Content tObj = fullObj.content;
- List<Api_tmc_trade_info_Obj.LinesItem> orderList = tObj.lines;
- if(tObj.status== "DELETED" || tObj.status == "CANCEL" || tObj.status == "UNKNOWN" || tObj.status == "UNPAID")
- {
- return;
- }
- bool isCreate = false;
- CeErpTrade trade = null;
- trade = CeErpTrade.Get(tObj.refOid);
- if (trade == null)
- {
- isCreate = true;
- trade = new CeErpTrade();
- trade.tid = tObj.refOid;
- trade.type = tObj.refType;
- trade.price = Convert.ToDouble(tObj.totalPrice);
- trade.discount_fee = Convert.ToDouble(tObj.discountFee);
- trade.total_fee = Convert.ToDouble(tObj.totalFee);
- trade.created = StampToDateTime(tObj.orderTime);
- if (tObj.payTime != null && tObj.payTime.Length > 0)
- {
- trade.pay_time = StampToDateTime(tObj.payTime);
- }
- trade.buyer_nick = tObj.openBuyerNick;
- //trade.adjust_fee = Convert.ToDouble(tObj.adjust_fee);
- trade.seller_nick = tObj.openSellerNick;
- trade.payment = Convert.ToDouble(tObj.payment);
- //trade.receiver_name = tObj.receiver_name;
- trade.receiver_state = tObj.receiverState;
- trade.receiver_city = tObj.receiverCity;
- trade.receiver_town = GetNoNullValue_string(tObj.receiverTown);
- trade.receiver_district = GetNoNullValue_string(tObj.receiverDistrict);
- }
- if (tObj.posCode != null)trade.posCode = tObj.posCode;
- trade.status = tObj.status;
- //if (tObj.refundStatus == "PART_REFUNDED" || tObj.refundStatus == "ALL_REFUNDED")
- //{
- //}
- trade.seller_flag = tObj.flag;
-
- if (tObj.modifyTime != null && tObj.modifyTime.Length>0)
- {
- trade.modified = StampToDateTime(tObj.modifyTime);
- }
- if ( tObj.finishTime!=null && tObj.finishTime.Length>0)
- {
- trade.end_time = StampToDateTime(tObj.finishTime);
- }
- if (tObj.shippingTime != null && tObj.shippingTime.Length>0)
- {
- trade.delivery_time = StampToDateTime(tObj.shippingTime);
- }
- //trade.buyer_message = GetNoNullValue_string(tObj.buyerMemo);
- trade.buyer_memo = GetNoNullValue_string(tObj.buyerMemo);
- bool isSellerMemoChange = false;
- if (trade.seller_memo != tObj.sellerMemo)
- {
- isSellerMemoChange = true;
- }
- trade.seller_memo = GetNoNullValue_string(tObj.sellerMemo);
- if (isCreate)
- {
- trade.Create();
- }
- else
- {
- trade.Update();
- }
- if (tObj.status == "CLOSE")
- {
- StringBuilder sql = new StringBuilder();
- sql.AppendFormat("update CE_ErpTradeCell set OrderState='{0}' where tid='{1}' and ctid not like '%S_%';", (int)OrderState.交易关闭, tObj.refOid);
- CeErpTradeRefund.ExecuteNonQuery(sql.ToString());
- LogHelper.addLog(tObj.refOid, 0, "交易关闭", (int)OrderState.交易关闭);
- return;
- }
- else if (tObj.status == "COMPLETE")
- {
- StringBuilder sql = new StringBuilder();
- sql.AppendFormat("update ce_ErpTradeCell set OrderState={1},IsUrgency=0 where IsPreDelivery=0 and tid='{0}' and ctid not like '%C_%' and ctid not like '%S_%';", tObj.refOid, (int)OrderState.交易完成);
- sql.AppendFormat("update ce_ErpTradeCell set OrderState={1},IsUrgency=0 where IsPreDelivery=0 and tid='{0}' and ctid like '%C_%' and OrderState>5 and ctid not like '%S_%';", tObj.refOid, (int)OrderState.交易完成);
- CeErpTradeRefund.ExecuteNonQuery(sql.ToString());
- return;
- }
- else if (tObj.status == "SHIPPED")//已发货
- {
- StringBuilder sql = new StringBuilder();
- sql.AppendFormat("update ce_ErpTradeCell set OrderState={1} where IsPreDelivery=0 and tid='{0}' and ctid not like '%C_%' and ctid not like '%S_%';", tObj.refOid, (int)OrderState.已发货);
- sql.AppendFormat("update ce_ErpTradeCell set OrderState={1} where IsPreDelivery=0 and tid='{0}' and ctid like '%C_%' and OrderState>5 and ctid not like '%S_%';", tObj.refOid, (int)OrderState.已发货);
- CeErpTradeRefund.ExecuteNonQuery(sql.ToString());
- return;
- }
- bool isXianhuo = false;
- string xianhuoMemo = "(" + trade.tid + ")现货-";
- try
- {
- foreach (Api_tmc_trade_info_Obj.LinesItem item in orderList)
- {
- CeErpTradeOrder order = CeErpTradeOrder.GetByOid(item.refOlId);
- bool isCreateOrder = false;
- if (order == null)
- {
- order = new CeErpTradeOrder();
- isCreateOrder = true;
- }
- order.tid = trade.tid;
- order.oid = item.refOlId;
- order.buyer_nick = trade.buyer_nick;
-
- if (item.refundStatus == "PART_REFUNDED" || item.refundStatus == "REFUNDED" || item.refundStatus == "WAIT_BUYER_RETURN_GOODS" || item.refundStatus == "REJECT_REFUNDED")
- {
- CeErpTradeRefund refund = CeErpTradeRefund.GetByTid(tObj.refOid);
- if (refund == null)
- {
- refund = new CeErpTradeRefund();
- refund.refund_id = tObj.refOid;
- refund.tid = tObj.refOid;
- //refund.oid = item.oid;
- refund.created = StampToDateTime(tObj.orderTime);
- if (tObj.modifyTime != null && tObj.modifyTime.Length > 0)
- {
- refund.modified = StampToDateTime(tObj.modifyTime);
- }
- refund.status = tObj.refundStatus;
- refund.order_status = tObj.status;
- //refund.good_status = item.good_status;
- refund.payment = tObj.payment;
- refund.refund_fee = tObj.payment;
- //refund.refund_phase = item.refund_phase;
- //refund.reason = item.reason;
- //refund.describe = item.desc;
- refund.buyer_nick = tObj.openBuyerNick;
- refund.seller_nick = tObj.openSellerNick;
- refund.total_fee = tObj.payment;
- //refund.has_good_return = item.has_good_return;
- refund.RefundState = 1;
- refund.Create();
- StringBuilder sql = new StringBuilder();
- sql.AppendFormat("update CE_ErpTradeCell set OrderState='{0}',IsRefund=1 where tid='{1}' and ctid not like '%S_%';", (int)OrderState.交易关闭, tObj.refOid);
- CeErpTradeRefund.ExecuteNonQuery(sql.ToString());
- LogHelper.addLog(tObj.refOid, 0, "已退款,交易关闭", (int)OrderState.交易关闭);
- }
- }
- else if(item.refundStatus == "REFUNDING")
- {
- StringBuilder sql = new StringBuilder();
- sql.AppendFormat("update CE_ErpTradeCell set IsRefund=2 where tid='{0}' and ctid not like '%S_%';", tObj.refOid);
- CeErpTradeRefund.ExecuteNonQuery(sql.ToString());
- LogHelper.addLog(tObj.refOid, 0, "申请退款中");
- }
- else if(item.refundStatus == "NO_REFUND" && order.refund_status!= "NO_REFUND" && isCreateOrder==false)
- {
- StringBuilder sql = new StringBuilder();
- sql.AppendFormat("update CE_ErpTradeCell set IsRefund=0 where tid='{0}' and ctid not like '%S_%';", tObj.refOid);
- CeErpTradeRefund.ExecuteNonQuery(sql.ToString());
- LogHelper.addLog(tObj.refOid, 0, "关闭申请退款");
- }
- order.refund_status = item.refundStatus;
- order.status = trade.status;
- order.title = item.title;
- order.price = Convert.ToDouble(item.price);
- if (item.picPath != null)
- order.pic_path = item.picPath;
- order.num = Convert.ToDouble(item.num.ToString());
- order.payment = Convert.ToDouble(item.price);
- //order.adjust_fee = Convert.ToDouble(item.price);
- if (item.refSkuId != null && item.standards != null && item.standards.IndexOf("现货") != -1 && item.standards.IndexOf("定制") == -1)
- {
- order.sku_properties_name = item.standards;
- order.sku_id = item.refSkuId;
- isXianhuo = true;
- xianhuoMemo += order.sku_properties_name;
- }
- if (isCreateOrder)
- {
- order.Create();
- }
- else
- {
- order.Update();
- }
- }
- }
- catch(Exception ex)
- {
- XLog.SaveLog(0, res + "|order表" + ex.Message);
- }
- if (isXianhuo)
- {
- xianhuoMemo += "-";
- xianhuoMemo += trade.seller_nick;
- }
- if (isCreate == false) {
- if (isSellerMemoChange)
- {
- try
- {
- createCellOrder(trade, tObj.refOid, tObj.sellerMemo, tObj.openBuyerNick);
- }
- catch (Exception ex)
- {
- XLog.SaveLog(0, res + "|2|" + ex.Message);
- }
- }
- return;
- }
- //cell表创建
- CeErpTradeCell entity = null;
- entity = CeErpTradeCell.GetByCtid(tObj.refOid);
- if (entity == null)
- {
- try
- {
- entity = new CeErpTradeCell();
- entity.ctid = tObj.refOid;
- entity.tid = tObj.refOid;
- entity.seller_memo = "";
- entity.payment = Convert.ToDouble(tObj.payment);
- entity.AfterSalePayment = Convert.ToDouble(tObj.payment);
- if (isXianhuo)
- {
- entity.OrderState = 6; //下单完成
- entity.seller_memo = xianhuoMemo;
- entity.SupplierId = 2; //领淘本部
- }
- entity.Create();
- }
- catch (Exception ex)
- {
- XLog.SaveLog(0, res + "|cell创建" + ex.Message);
- }
- }
- if (tObj.sellerMemo.Length > 0)
- {
- try
- {
- createCellOrder(trade, tObj.refOid, tObj.sellerMemo, tObj.openBuyerNick);
- }
- catch (Exception ex)
- {
- XLog.SaveLog(0, res + "|3|" + ex.Message);
- }
-
- }
- //生成档案客户信息
- insertCustomerInfo(tObj);
- }
- public static DateTime StampToDateTime(string timeStamp)
- {
- DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
- long lTime = long.Parse(timeStamp + "0000");
- TimeSpan toNow = new TimeSpan(lTime);
- return dateTimeStart.Add(toNow);
- }
- public static void Api_Tmc_CreateNewOrder(string res)
- {
- Api_trade_fullinfo_get_response_Obj fullObj = null;
- if (res == "") return;
- try
- {
- res = GetUTF8String(Encoding.UTF8.GetBytes(res));
- //res = res.Replace(" ", "");
- fullObj = JsonConvert.DeserializeObject<Api_trade_fullinfo_get_response_Obj>(res);
- }
- catch (Exception ex)
- {
- XLog.SaveLog(0, ex.Message);
- }
- if (fullObj == null)
- {
- Console.WriteLine("Api_trade_fullinfo_get_response_Obj isNull");
- return;
- }
- ContentItem tObj = null;
- if (fullObj.response.data.content.Count > 0) {
- tObj = fullObj.response.data.content[0];
- }
- if (tObj == null)
- {
- return;
- }
- if (tObj.status == "DELETED" || tObj.status == "CANCEL" || tObj.status == "UNKNOWN" || tObj.status == "UNPAID")
- {
- return;
- }
- List<Api_trade_fullinfo_get_response_Obj.LinesItem> orderList = tObj.lines;
- StringBuilder sql = new StringBuilder();
- CeErpTrade td = CeErpTrade.Get(tObj.refOid);
- if (td != null)
- {
- return;
- }
- CeErpTrade trade = new CeErpTrade();
- trade.tid = tObj.refOid;
- if (tObj.posCode != null)
- trade.posCode = tObj.posCode;
- //trade.num = tObj.num;
- trade.status = tObj.status;
- trade.seller_flag = tObj.flag;
- trade.type = tObj.refType;
- trade.price = Convert.ToDouble(tObj.totalPrice);
- trade.discount_fee = Convert.ToDouble(tObj.discountFee);
- trade.total_fee = Convert.ToDouble(tObj.totalFee);
- trade.created = StampToDateTime(tObj.orderTime);
- if (tObj.payTime!=null)
- {
- trade.pay_time = StampToDateTime(tObj.payTime);
- }
- if ( tObj.modifyTime != null)
- {
- trade.modified = StampToDateTime(tObj.modifyTime);
- }
- if (tObj.finishTime != null)
- {
- trade.end_time = StampToDateTime(tObj.finishTime);
- }
- trade.buyer_nick = tObj.openBuyerNick;
- //trade.adjust_fee = Convert.ToDouble(tObj.adjust_fee);
- trade.seller_nick = tObj.openSellerNick;
- trade.payment = Convert.ToDouble(tObj.payment);
- //trade.receiver_name = tObj.receiver_name;
- trade.receiver_state = tObj.receiverState;
- trade.receiver_city = tObj.receiverCity;
- trade.receiver_town = GetNoNullValue_string(tObj.receiverTown);
- trade.receiver_district = GetNoNullValue_string(tObj.receiverDistrict);
- //trade.receiver_address = tObj.receiver_address;
- //trade.receiver_mobile = GetNoNullValue_string(tObj.receiver_mobile);
- //trade.receiver_phone = GetNoNullValue_string(tObj.receiver_phone);
- //if (tObj.consign_time.ToString() != "0001/1/1 0:00:00" && tObj.consign_time != null)
- //{
- // trade.consign_time = tObj.consign_time;
- //}
- if ( tObj.shippingTime != null)
- {
- trade.delivery_time = StampToDateTime(tObj.shippingTime);
- }
- //trade.buyer_message = GetNoNullValue_string(tObj.buyerMemo);
- trade.buyer_memo = GetNoNullValue_string(tObj.buyerMemo);
- trade.seller_memo = GetNoNullValue_string(tObj.sellerMemo);
- trade.Create();
- bool isXianhuo = false;
- string xianhuoMemo = "(" + trade.tid + ")现货-";
- foreach (Api_trade_fullinfo_get_response_Obj.LinesItem item in orderList)
- {
- CeErpTradeOrder order = new CeErpTradeOrder();
- order.tid = trade.tid;
- order.oid = item.refOlId;
- order.buyer_nick = trade.buyer_nick;
- order.refund_status = item.refundStatus;
- order.status = trade.status;
- order.title = item.title;
- order.price = Convert.ToDouble(item.price);
- if(item.picPath!=null)
- order.pic_path = item.picPath;
- order.num = Convert.ToDouble(item.num.ToString());
- order.payment = Convert.ToDouble(item.price);
- //order.adjust_fee = Convert.ToDouble(item.price);
- if (item.refSkuId != null && item.standards != null && item.standards.IndexOf("现货") != -1 && item.standards.IndexOf("定制")==-1)
- {
- order.sku_properties_name = item.standards;
- order.sku_id = item.refSkuId;
- isXianhuo = true;
- xianhuoMemo += order.sku_properties_name;
- }
- order.Create();
- }
- if (isXianhuo)
- {
- xianhuoMemo += "-";
- xianhuoMemo += trade.seller_nick;
- }
- CeErpTradeCell entity = null;
- entity = CeErpTradeCell.GetByCtid(tObj.refOid);
- if (entity == null)
- {
- try
- {
- entity = new CeErpTradeCell();
- entity.ctid = tObj.refOid;
- entity.tid = tObj.refOid;
- entity.payment = Convert.ToDouble(tObj.payment);
- if (isXianhuo)
- {
- entity.OrderState = 6;//现货直接下单完成
- entity.seller_memo = xianhuoMemo;
- entity.SupplierId = 2; //领淘本部
- }
- entity.Create();
- }
- catch (Exception ex)
- {
- XLog.SaveLog(0, ex.Message);
- }
- }
- if (tObj.sellerMemo.Length > 0)
- {
- try
- {
- createCellOrder(trade, tObj.refOid, tObj.sellerMemo, tObj.openBuyerNick);
- }
- catch (Exception ex)
- {
- XLog.SaveLog(0, res + "|" + ex.Message);
- }
- }
- ////生成档案客户信息
- //insertCustomerInfo(tObj);
- }
- public static void insertCustomerInfo(Content Obj)
- {
- CeErpCustomer cus = null;
- cus = CeErpCustomer.GetByNick(Obj.openBuyerNick);
- if (cus != null)
- {
- //cus.address = Obj.receiver_state + Obj.receiver_city + Obj.receiver_address;
- //cus.phone = Obj.receiver_mobile;
- TimeSpan tsday = StampToDateTime(Obj.payTime).Subtract((DateTime)cus.lastbuy_time);
- cus.buy_day = tsday.Days;
- cus.total_fee = cus.total_fee + Convert.ToDouble(Obj.payment);
- cus.buy_count = cus.buy_count + 1;
- cus.lastbuy_time = StampToDateTime(Obj.payTime);
- cus.Update();
- }
- else
- {
- try
- {
- cus = new CeErpCustomer();
- cus.buyer_nick = Obj.openBuyerNick;
- //cus.address = Obj.receiver_state + Obj.receiver_city + Obj.receiver_address;
- //cus.phone = Obj.receiver_mobile;
- cus.buy_day = 0;
- cus.buy_count = 1;
- cus.lastbuy_time = StampToDateTime(Obj.payTime);
- cus.total_fee = Convert.ToDouble(Obj.payment);
- cus.Create();
- }
- catch (Exception ex)
- {
- XLog.SaveLog(0, ex.Message);
- }
- }
- }
- //接收到--新订单生成消息
- public static void API_Tmc_TradeBuyerPay(string content)
- {
- Api_tmc_trade_info_Obj fullObj = null;
- if (content == "") return;
- try
- {
- content = GetUTF8String(Encoding.UTF8.GetBytes(content));
- //res = res.Replace(" ", "");
- fullObj = JsonConvert.DeserializeObject<Api_tmc_trade_info_Obj>(content);
- }
- catch (Exception ex)
- {
- }
- if (fullObj == null)
- {
- Console.WriteLine("Api_tmc_trade_info_Obj isNull");
- return;
- }
- Content tObj = fullObj.content;
- string res = apiHelper.API_TradeFullinfoGet(tObj.refOid); //taobaoHelper.test();
- Api_Tmc_CreateNewOrder(res);
- }
- //推送接收到卖家备注修改
- public static void API_Tmc_TradeMemoModified(string content)
- {
- Api_tmc_trade_info_Obj fullObj = null;
- if (content == "") return;
- try
- {
- content = GetUTF8String(Encoding.UTF8.GetBytes(content));
- //res = res.Replace(" ", "");
- fullObj = JsonConvert.DeserializeObject<Api_tmc_trade_info_Obj>(content);
- }
- catch (Exception ex)
- {
- }
- if (fullObj == null)
- {
- Console.WriteLine("Api_tmc_trade_info_Obj isNull");
- return;
- }
- Content tObj = fullObj.content;
- //原始表--没有这个单,不能拆分
- CeErpTrade mainEn = CeErpTrade.Get(tObj.refOid);
- if (mainEn == null) return;
- createCellOrder(mainEn, tObj.refOid, tObj.sellerMemo, tObj.sellerMemo);
- }
- }
- }
|