/// <summary>
        /// 处理订单完成时的相应逻辑
        /// </summary>
        public void Start()
        {
            //获取店铺的基础数据
            ShopData data = new ShopData();
            ShopInfo shop = data.ShopInfoGetByNick(TradeInfo.Nick);

            //Console.Write(shop.Session + "!!!!!!!!!!!!\r\n");
            if (shop.Version != "2" && shop.Version != "3")
            {
                return;
            }

            //通过TOP接口查询该订单的详细数据并记录到数据库中
            TopApiHaoping api       = new TopApiHaoping(shop.Session);
            TradeData     tradeData = new TradeData();
            Trade         trade     = tradeData.GetTradeDetailShippingInfo(TradeInfo);

            trade = api.GetTradeByTid(trade);

            //记录该订单对应的评价记录
            TradeRate tradeRate = api.GetTradeRate(trade);

            tradeRate.ItemId = trade.NumIid;
            tradeRate.Nick   = TradeInfo.Nick;

            //只有双方都评价了才会有插入数据库和赠送的操作
            if (tradeRate.Content != "")
            {
                //判断该订单是否已经有过评价记录
                TradeRateData dbTradeRate = new TradeRateData();
                if (!dbTradeRate.CheckTradeRateExits(tradeRate))
                {
                    //没有记录过写入数据库
                    dbTradeRate.InsertTradeInfo(tradeRate);
                }

                //判断该订单是否存在
                TradeData dbTrade = new TradeData();
                if (!dbTrade.CheckTradeExits(trade))
                {
                    //更新该订单的评价时间
                    dbTrade.InsertTradeInfo(trade);
                }

                //更新该订单的评价时间
                dbTrade.UpdateTradeRateById(trade, tradeRate);
            }
            else
            {
                //否则中断
                return;
            }

            //判断如果是分销的订单,则不处理
            if (trade.OrderType.ToLower() == "fenxiao")
            {
                return;
            }

            try
            {
                //更新订单的优惠券使用情况
                TopApiHaoping apiCoupon = new TopApiHaoping(shop.Session);
                string        result    = apiCoupon.GetCouponTradeTotalByNick(trade);

                MatchCollection match = new Regex(@"<promotion_details list=""true""><promotion_detail><discount_fee>([^\<]*)</discount_fee><id>[0-9]*</id><promotion_desc>[^\<]*</promotion_desc><promotion_id>shopbonus-[0-9]*_[0-9]*-([0-9]*)</promotion_id><promotion_name>店铺优惠券</promotion_name></promotion_detail>", RegexOptions.IgnoreCase).Matches(result);

                if (match.Count != 0)
                {
                    string price    = match[0].Groups[1].ToString();
                    string couponid = match[0].Groups[2].ToString();

                    if (couponid.Length != 0)
                    {
                        TradeData dataTradeCoupon = new TradeData();
                        dataTradeCoupon.UpdateTradeCouponInfo(trade, price, couponid);
                    }
                }
            }
            catch { }

            //判断是否开启了客服审核,如果开启了则自动记录并中断
            if (shop.IsKefu == "1")
            {
                TradeRateData dataKefu   = new TradeRateData();
                string        resultKefu = "手动审核订单!";
                dataKefu.UpdateTradeRateResult(tradeRate, resultKefu);

                //更新该订单的评价为待审核状态
                TradeData dbTrade = new TradeData();
                if (!dbTrade.CheckTradeRateCheckExits(trade))
                {
                    dbTrade.UpdateTradeKefuById(trade, tradeRate);
                }

                try
                {
                    //记录会员信息数据
                    GetUserData getUser = new GetUserData();
                    getUser.Get(trade);
                }
                catch { }

                return;
            }

            Console.WriteLine(trade.ShippingType + "!!");

            //获取订单的具体物流状态,如果订单物流状态为空则获取物流信息
            if (trade.ShippingType != "system" && trade.ShippingType != "self")
            {
                //获取该订单的物流状态
                TopApiHaoping apiHaoping = new TopApiHaoping(shop.Session);
                string        status     = apiHaoping.GetShippingStatusByTid(trade);
                TradeData     dbTrade    = new TradeData();

                Console.Write(status + "\r\n");
                //如果该物流信息不存在
                if (status.IndexOf("不存在") != -1)
                {
                    //如果该物流公司不支持查询则更新为self
                    dbTrade.UpdateTradeShippingStatusSelf(trade, status);
                }

                //获取该订单的物流相关信息
                trade = api.GetOrderShippingInfo(trade);
                if (!dbTrade.IsTaobaoCompany(trade))
                {
                    //如果不是淘宝合作物流则直接更新
                    dbTrade.UpdateTradeShippingStatusSelf(trade, status);
                    trade.ShippingType = "self";
                }
                else
                {
                    //根据服务器的物流状态进行判断,如果物流状态是已签收
                    if (status == "ACCEPTED_BY_RECEIVER" || status == "ACCEPTING" || status == "ACCEPTED")
                    {
                        string result = api.GetShippingStatusDetailByTid(trade);
                        Console.Write("【" + result + "】\r\n");
                        //如果是虚拟物品
                        if (result.IndexOf("该订单未指定运单号") != -1)
                        {
                            //如果该物流公司不支持查询则更新为self
                            dbTrade.UpdateTradeShippingStatusSelf(trade, status);
                        }

                        //如果订单不是服务器错误
                        if (result.IndexOf("company-not-support") != -1)
                        {
                            //如果该物流公司不支持查询则更新为self
                            dbTrade.UpdateTradeShippingStatusSelf(trade, status);
                        }

                        //再根据订单的详细物流信息判断签收的状态
                        if (result.IndexOf("签收人") != -1 || result.IndexOf(" 妥投") != -1 || result.IndexOf(" 签收") != -1 || result.IndexOf("正常签收") != -1 || result.IndexOf(" 已签收") != -1)
                        {
                            //如果物流已经签收了则更新对应订单状态
                            trade.DeliveryEnd  = utils.GetShippingEndTime(result);;
                            trade.DeliveryMsg  = result;
                            trade.ShippingType = "system";

                            //如果物流到货时间还是为空
                            if (trade.DeliveryEnd == "")
                            {
                                LogData dbLog = new LogData();
                                dbLog.InsertErrorLog(trade.Nick, "deliveryDateNullOrder", "", result, "");
                            }

                            dbTrade.UpdateTradeShippingStatusSystem(trade, status);
                        }
                    }
                }
            }

            //处理优惠券赠送及短信-上LOCK锁定
            lock (TradeSuccess.padlockRate)
            {
                //淘宝优惠券赠送
                bool isSendCoupon = true;
                //判断是否符合赠送条件
                if (CheckCouponSend(shop, tradeRate, trade))
                {
                    //如果符合赠送条件调用赠送接口
                    CouponData dbCoupon = new CouponData();
                    Coupon     coupon   = dbCoupon.GetCouponInfoById(shop);

                    //判定该优惠券是否过期或删除
                    if (!dbCoupon.CheckCouponCanUsed(shop))
                    {
                        //优惠券过期,自动帮客户延长优惠券期限
                        //考虑到淘宝即将开启短授权,该功能改成消息通知,暂不制作
                        isSendCoupon = false;
                        //return;
                    }
                    else
                    {
                        //解决多线程冲突问题先插入优惠券赠送记录,如果赠送失败再删除记录
                        string couponId = string.Empty;
                        if (dbCoupon.InsertCouponSendRecord(trade, shop, couponId))
                        {
                            string taobaoResult = string.Empty;
                            couponId = api.SendCoupon(trade.BuyNick, coupon.TaobaoCouponId, ref taobaoResult);

                            //获取的赠送接口的返回结果
                            if (couponId != "")
                            {
                                //如果成功赠送则记录
                                dbCoupon.UpdateCouponSendRecord(trade, shop, couponId);
                            }
                            else
                            {
                                //如果没有赠送成功则删除刚才的临时记录
                                dbCoupon.DeleteCouponSendRecord(trade, shop, couponId);
                                Console.WriteLine(couponId);
                                try
                                {
                                    //记录淘宝自身错误
                                    string        err      = new Regex(@"<reason>([^<]*)</reason>", RegexOptions.IgnoreCase).Match(taobaoResult).Groups[1].ToString();
                                    TradeRateData dataRate = new TradeRateData();
                                    if (err.Length == 0)
                                    {
                                        taobaoResult += "淘宝系统错误,不赠送优惠券,错误代码是【" + taobaoResult + "】!";
                                    }
                                    else
                                    {
                                        taobaoResult += "淘宝系统错误,不赠送优惠券,错误代码是【" + err + "】!";
                                    }
                                    dataRate.UpdateTradeRateResult(tradeRate, taobaoResult);
                                }
                                catch { }
                                //有可能是客户订购的优惠券服务已经到期记录错误信息并中断
                                //return;
                                isSendCoupon = false;
                            }
                        }
                        else
                        {
                            isSendCoupon = false;
                        }
                    }
                }
                else
                {
                    isSendCoupon = false;
                }

                //支付宝现金券赠送
                bool isSendAlipay = true;
                //判断是否符合赠送条件
                if (CheckAlipaySend(shop, tradeRate, trade))
                {
                    //获取一条需要赠送的支付宝红包数据
                    CouponData   cou    = new CouponData();
                    AlipayDetail detail = cou.GetAlipayDetailInfoById(shop);

                    //如果符合赠送条件调用短信接口直接将红包发到客户手机上
                    string shopName = shop.MsgShopName;
                    if (shop.MsgShopName.Length == 0)
                    {
                        shopName = shop.Nick;
                    }
                    string msgAlipay = "亲," + shopName + "赠送您支付宝红包,卡号" + detail.Card + "密码" + detail.Pass + ",您可以到支付宝绑定使用。";
                    Console.Write(msgAlipay + "\r\n");
                    string msgResultAlipay = Message.Send(trade.Mobile, msgAlipay);

                    Console.Write(msgResultAlipay + "\r\n");
                    //更新支付宝红包使用状态
                    cou.InsertAlipaySendRecord(trade, shop, detail);

                    //记录短信发送记录
                    ShopData dbAlipay = new ShopData();
                    if (msgResultAlipay != "0")
                    {
                        dbAlipay.InsertShopMsgLog(shop, trade, msgAlipay, msgResultAlipay, "alipay");
                    }
                    else
                    {
                        isSendAlipay = false;
                        dbAlipay.InsertShopErrMsgLog(shop, trade, msgAlipay, msgResultAlipay, "alipay");
                    }
                    shop.MsgCount = (int.Parse(shop.MsgCount) - 1).ToString();
                }
                else
                {
                    isSendAlipay = false;
                }

                //包邮卡赠送
                bool isSendFreeCard = true;
                try
                {
                    //判断是否符合赠送条件
                    if (CheckFreeCardSend(shop, tradeRate, trade))
                    {
                        //赠送包邮卡
                        FreeCardData freeData = new FreeCardData();
                        freeData.SendFreeCard(shop, trade);

                        //判断该用户是否开启了包邮卡短信
                        if (shop.MsgIsFreecard == "1" && int.Parse(shop.MsgCount) > 0)
                        {
                            ShopData db   = new ShopData();
                            FreeCard free = freeData.GetFreeCardById(shop.FreeCardId);
                            //发送短信
                            string msg = Message.GetMsg(shop.MsgFreecardContent, shop.MsgShopName, TradeInfo.BuyNick, shop.IsCoupon, free.Name);

                            //手机号码为空不发
                            if (trade.Mobile.Length == 0)
                            {
                                return;
                            }

                            if (!db.IsSendMsgToday(trade, "freecard"))
                            {
                                string msgResult = Message.Send(trade.Mobile, msg);

                                //记录
                                if (msgResult != "0")
                                {
                                    db.InsertShopMsgLog(shop, trade, msg, msgResult, "freecard");
                                }
                                else
                                {
                                    db.InsertShopErrMsgLog(shop, trade, msg, msgResult, "freecard");
                                }
                                shop.MsgCount = (int.Parse(shop.MsgCount) - 1).ToString();
                            }
                        }
                    }
                    else
                    {
                        isSendFreeCard = false;
                    }
                }
                catch (Exception e) {
                    Console.WriteLine(e.Message + e.StackTrace + e.Source);
                }



                try
                {
                    //记录会员信息数据
                    GetUserData getUser = new GetUserData();
                    getUser.Get(trade);
                }
                catch { }

                //Console.WriteLine(isSendCoupon + "!!");
                //Console.WriteLine(isSendAlipay + "!!");
                //Console.WriteLine(trade.Mobile + "!!");

                //如果优惠券和支付宝现金都没有赠送成功,则直接中断方法不发短信
                //2012.9.17改为只有赠送成功优惠券才发送此短信
                if (!isSendCoupon)
                {
                    return;
                }

                //判断该用户是否开启了发货短信
                if (shop.MsgIsCoupon == "1" && int.Parse(shop.MsgCount) > 0)
                {
                    ShopData db = new ShopData();
                    //发送短信
                    string msg = Message.GetMsg(shop.MsgCouponContent, shop.MsgShopName, TradeInfo.BuyNick, shop.IsCoupon);
                    //string msg = Message.GetMsg(shop, trade, shop.MsgCouponContent);

                    //手机号码为空不发
                    if (trade.Mobile.Length == 0)
                    {
                        return;
                    }

                    if (!db.IsSendMsgToday(trade, "gift"))
                    {
                        ////先插入数据库 解决多优惠券赠送短信多发问题
                        //db.InsertShopMsgLog(shop, trade, msg, "888888", "gift");

                        string msgResult = Message.Send(trade.Mobile, msg);

                        //记录
                        if (msgResult != "0")
                        {
                            db.InsertShopMsgLog(shop, trade, msg, msgResult, "gift");
                        }
                        else
                        {
                            db.InsertShopErrMsgLog(shop, trade, msg, msgResult, "gift");
                        }
                        shop.MsgCount = (int.Parse(shop.MsgCount) - 1).ToString();
                    }
                }
            }
        }
Exemple #2
0
        public void Start(string index)
        {
            //获取目前正在使用的卖家(需要改成全部卖家,否则物流状态无法获取)
            ShopData        dbShop = new ShopData();
            List <ShopInfo> list   = dbShop.GetShopInfoListShippingAlert(index);

            //循环判定这些卖家的订单是否物流到货
            for (int i = 0; i < list.Count; i++)
            {
                try
                {
                    ShopInfo shop = list[i];
                    //获取那些状态是发货中且没发过短信的订单给予提示
                    TradeData    dbTrade   = new TradeData();
                    List <Trade> listTrade = dbTrade.GetShippingTrade(shop);

                    Console.Write(listTrade.Count.ToString() + "\r\n");
                    for (int j = 0; j < listTrade.Count; j++)
                    {
                        //如果是晚上10点到早上9点这个时间端内,不发送物流提醒短信
                        if (DateTime.Now.Hour > 8 && DateTime.Now.Hour < 22)
                        {
                            Trade trade = listTrade[j];

                            //判断如果是分销的订单,则不处理
                            if (trade.OrderType.ToLower() == "fenxiao")
                            {
                                continue;
                            }

                            //获取该订单的物流状态
                            TopApiHaoping api    = new TopApiHaoping(shop.Session);
                            string        status = api.GetShippingStatusByTid(listTrade[j]);

                            Console.Write(status + "\r\n");
                            //如果该物流信息不存在
                            if (status.IndexOf("不存在") != -1)
                            {
                                //如果该物流公司不支持查询则更新为self
                                dbTrade.UpdateTradeShippingStatusSelf(trade, status);
                                continue;
                            }

                            trade = api.GetOrderShippingInfo(trade);

                            if (!dbTrade.IsTaobaoCompany(trade))
                            {
                                //如果不是淘宝合作物流则直接更新
                                dbTrade.UpdateTradeShippingStatusSelf(trade, status);
                            }
                            else
                            {
                                //根据服务器的物流状态进行判断,如果物流状态是已签收
                                if (status == "ACCEPTED_BY_RECEIVER" || status == "ACCEPTING" || status == "ACCEPTED")
                                {
                                    string result = api.GetShippingStatusDetailByTid(trade);
                                    Console.Write("【" + result + "】\r\n");
                                    //如果订单不是服务器错误
                                    if (result.IndexOf("company-not-support") != -1)
                                    {
                                        //如果该物流公司不支持查询则更新为self
                                        dbTrade.UpdateTradeShippingStatusSelf(trade, status);
                                        continue;
                                    }

                                    //再根据订单的详细物流信息判断签收的状态
                                    if (result.IndexOf("签收人") != -1 || result.IndexOf(" 签收") != -1 || result.IndexOf(" 已签收") != -1 || result.IndexOf(" 妥投") != -1 || result.IndexOf("正常签收") != -1)
                                    {
                                        //如果物流已经签收了则更新对应订单状态
                                        trade.DeliveryEnd = utils.GetShippingEndTime(result);;
                                        trade.DeliveryMsg = result;

                                        //如果物流到货时间还是为空
                                        if (trade.DeliveryEnd == "")
                                        {
                                            LogData dbLog = new LogData();
                                            dbLog.InsertErrorLog(trade.Nick, "deliveryDateNull", "", result, "");
                                            continue;
                                        }

                                        //更新物流到货时间
                                        dbTrade.UpdateTradeShippingStatusSystem(trade, status);

                                        //发送短信-上LOCK锁定
                                        lock (ShippingSuccess.padlock1)
                                        {
                                            //判断同类型的短信该客户今天是否只收到一条
                                            ShopData db = new ShopData();
                                            if (!db.IsSendMsgOrder(trade, "shipping") && !db.IsSendMsgNear(trade, "shipping"))
                                            {
                                                //判断该用户是否开启了发货短信
                                                if (shop.MsgIsShipping == "1" && int.Parse(shop.MsgCount) > 0)
                                                {
                                                    //发送短信
                                                    //string msg = Message.GetMsg(shop.MsgShippingContent, shop.MsgShopName, trade.BuyNick, shop.IsCoupon);
                                                    string msg       = Message.GetMsg(shop, trade, shop.MsgShippingContent);
                                                    string msgResult = Message.Send(trade.Mobile, msg);

                                                    //记录
                                                    if (msgResult != "0")
                                                    {
                                                        db.InsertShopMsgLog(shop, trade, msg, msgResult, "shipping");
                                                    }
                                                    else
                                                    {
                                                        db.InsertShopErrMsgLog(shop, trade, msg, msgResult, "shipping");
                                                    }

                                                    shop.MsgCount = (int.Parse(shop.MsgCount) - 1).ToString();
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            //return;
                        }
                    }
                }
                catch { }
            }
        }