示例#1
0
        public BookingCheckOutDto BookingCheck(BookingCheckInputDto checkDto)
        {
            var bookCheckResult = new BookingCheckOutDto();
            var roomStatus      = bigTreeRepository.GetRoomRateStatus(checkDto.RatePlanId, checkDto.CheckIn, checkDto.CheckOut);

            if (roomStatus.Count > 0)
            {
                List <RateQuotasPrice> rqpList = new List <RateQuotasPrice>();
                foreach (var item in roomStatus)
                {
                    if (item.RoomStatus == 1)
                    {
                        int?            price = null;
                        RateQuotasPrice rqp   = new RateQuotasPrice();
                        rqp.Date = item.StartDate.ToString("yyyy-MM-dd");
                        if (Shop == ShopType.YinJi)
                        {
                            rqp.Price = (price ?? item.SalePrice + 20) * 100;
                        }
                        else if (Shop == ShopType.LingZhong)
                        {
                            rqp.Price = (price ?? item.SalePrice + 25) * 100;
                        }
                        else if (Shop == ShopType.YinJiGuoLv)
                        {
                            rqp.Price = (price ?? item.SalePrice - 1) * 100;
                        }

                        if (item.Status == 1)
                        {
                            rqp.Quota = 8;
                        }
                        else
                        {
                            bookCheckResult.IsBook  = false;
                            bookCheckResult.Message = "房量不足";
                        }
                        rqpList.Add(rqp);
                    }
                    else
                    {
                        bookCheckResult.IsBook  = false;
                        bookCheckResult.Message = "满房";
                    }
                }

                if (rqpList.Count > 0)
                {
                    bookCheckResult.DayPrice = rqpList;
                    if (rqpList.Where(u => u.Quota == 0).Count() > 0)
                    {
                        bookCheckResult.IsBook  = false;
                        bookCheckResult.Message = "房量不足";
                    }
                    else
                    {
                        bookCheckResult.IsBook  = true;
                        bookCheckResult.Message = "检测可预定";
                    }
                }
            }
            else
            {
                bookCheckResult.IsBook  = false;
                bookCheckResult.Message = "不存在报价信息";
            }

            return(bookCheckResult);
        }
示例#2
0
        /// <summary>
        /// 试单
        /// </summary>
        /// <param name="checkDto"></param>
        /// <returns></returns>
        public BookingCheckOutDto BookingCheck(BookingCheckInputDto checkDto)
        {
            var checkKey = "mt_" + checkDto.HotelId + "_" + checkDto.RoomTypeId + "_" + checkDto.RatePlanId + "_" + checkDto.CheckIn + "_" + checkDto.CheckOut;

            var bookOutDto = new BookingCheckOutDto();

            try
            {
                DateTime startTime = DateTime.Now;
                bookOutDto.DayPrice = new List <RateQuotasPrice>();
                bookOutDto.IsBook   = true;
                bookOutDto.Message  = "正常可预定";

                var response = new BookingCheckResponse();

                int totalPrice = Convert.ToInt32(getTotaPrice(checkDto));
                var request    = new BookingCheckRequest
                {
                    checkinDate  = checkDto.CheckIn.ToString("yyyy-MM-dd"),
                    checkoutDate = checkDto.CheckOut.ToString("yyyy-MM-dd"),
                    HotelId      = checkDto.HotelId,
                    GoodsId      = checkDto.RatePlanId,
                    RoomNum      = checkDto.RoomNum,
                    totalPrice   = totalPrice * checkDto.RoomNum
                };
                if (totalPrice != 0)
                {
                    response = meiTuanApiClient.Excute(request);
                }
                TimeSpan ts = checkDto.CheckOut.Subtract(checkDto.CheckIn);

                if (response != null && response.Result.code == 0 && (response.Result.desc == null || !response.Result.desc.Contains("价格发生变化")))
                {
                    //var hotelExtension = hotelRepository.GetHotelExtension(checkDto.HotelId, (int)Channel);
                    //var invoiceInfo = GetRatePlanInvoice(checkDto.HotelId, checkDto.RatePlanId, checkDto.CheckIn, checkDto.CheckOut);
                    bookOutDto.IsBook = true;
                    int remainRoomNum = response.Result.remainRoomNum;
                    int Quota         = 5;
                    if (remainRoomNum < 1)
                    {
                        Quota = 1;
                    }
                    else if (remainRoomNum > 5)
                    {
                        Quota = 5;
                    }
                    else
                    {
                        Quota = remainRoomNum;
                    }

                    var rate = new TaobaoRate();
                    try
                    {
                        XhotelRateGetRequest req = new XhotelRateGetRequest();
                        req.OutRid = checkDto.OuterId;
                        req.Rpid   = checkDto.Rpid;
                        var tmallApiClient         = new TmallApiClient(Shop);
                        XhotelRateGetResponse resp = tmallApiClient.Execute(req);
                        if (resp != null && !resp.IsError && resp.Rate != null && !string.IsNullOrWhiteSpace(resp.Rate.InventoryPrice))
                        {
                            rate = JsonConvert.DeserializeObject <TaobaoRate>(resp.Rate.InventoryPrice);
                        }
                    }
                    catch (Exception ex)
                    {
                    }

                    List <PriceModelsItem> modelList = new List <PriceModelsItem>();

                    foreach (var item in response.Result.priceModels)
                    {
                        if (!modelList.Any(u => u.date == item.date))
                        {
                            modelList.Add(item);
                        }
                    }
                    //是否需要马上更新
                    bool isUpdate = false;
                    //是否供应商发生变价
                    bool bianhua = false;

                    foreach (var item in modelList)
                    {
                        RateQuotasPrice rqp = new RateQuotasPrice();
                        rqp.Date = item.date;
                        //checkDto.IsVirtual, invoiceInfo.IsCommission, invoiceInfo.InvoiceMode, Convert.ToDecimal(item.subRatio / 10000), item.salePrice / 100, hotelExtension, rate
                        rqp.Price = GetSalePrice(Convert.ToDateTime(item.date), checkDto.IsVirtual, Convert.ToDecimal(item.subRatio / 10000), item.salePrice / 100, rate, ref bianhua, ref isUpdate, checkDto.IsCustomer).ToTaoBaoPrice();
                        rqp.Quota = Quota;//5;
                        bookOutDto.DayPrice.Add(rqp);
                    }
                    //记录试单信息
                    Task.Factory.StartNew(() =>
                    {
                        try
                        {
                            bool IsFull   = false;
                            string Remark = bookOutDto.Message;
                            //当库存为0时关房
                            if (remainRoomNum == 0 && checkDto.IsCustomer == 1)
                            {
                                IsFull = true;
                            }
                            if (checkDto.IsCustomer == 0)
                            {
                                Remark = "程序请求";
                            }
                            AliTripValidate av = new AliTripValidate();
                            av.CheckInTime     = checkDto.CheckIn;
                            av.CheckOutTime    = checkDto.CheckOut;
                            av.RatePlanCode    = checkDto.RatePlanCode;
                            av.IsFull          = IsFull;
                            av.HotelId         = checkDto.HotelId;
                            av.RoomId          = checkDto.RoomTypeId;
                            av.RatePlanId      = checkDto.RatePlanId;
                            av.CreateTime      = DateTime.Now;
                            av.Channel         = 5;
                            av.Shop            = (int)Shop;
                            av.Remark          = Remark;
                            SqlSugarContext.RenNiXingInstance.Insertable(av).ExecuteCommand();
                            string url = string.Format("http://localhost:8097/apiAshx/UpdateRoomRate.ashx?type=RoomRate&hid={0}&source=5", checkDto.HotelId);
                            if (checkDto.IsCustomer != 0 && !isUpdate)
                            {
                                Thread.Sleep(15000);
                            }
                            WebHttpRequest.Get(url);
                            //当价格改变时或库存为0时更新房态
                            //if (bianhua || remainRoomNum == 0)
                            //{
                            //    string url = string.Format("http://localhost:8097/apiAshx/UpdateRoomRate.ashx?type=RoomRate&hid={0}&source=5", checkDto.HotelId);
                            //    WebHttpRequest.Get(url);
                            //}
                        }
                        catch
                        {
                        }
                    });
                }
                else
                {
                    //TODO 满房变价,全部关房..
                    //bookOutDto.IsBook = false;
                    //bookOutDto.Message = response.Result.desc;

                    try
                    {
                        var falseCount      = 1;
                        var falseCacheCount = CacheHelper.GetCache("falseCount");//先读取缓存
                        //当为客人请求才做记录数次
                        if (checkDto.IsCustomer == 1)
                        {
                            if (falseCacheCount != null)//如果没有该缓存,默认为1
                            {
                                falseCount = (int)falseCacheCount + 1;
                            }
                            CacheHelper.SetCache("falseCount", falseCount, 1800);//添加缓存
                        }
                        //如果试单失败为双数直接拿缓存值输出,单数时为失败
                        //if (falseCount % 2 == 0)
                        bool tongguo = false;

                        //在0点15分到4点为试单失败为双数直接拿缓存值输出,其余的都通过
                        int NowHour   = DateTime.Now.Hour;   //当前时间的时数
                        int NowMinute = DateTime.Now.Minute; //当前时间的分钟数
                        if (falseCount % 2 == 0 || falseCount % 13 == 0)
                        {
                            tongguo = true;
                        }
                        else if ((NowHour == 18 && NowMinute > 10) || (NowHour < 8 && NowHour >= 19))
                        {
                            if (falseCount % 2 == 0 || falseCount % 5 == 0 || falseCount % 7 == 0)
                            {
                                tongguo = true;
                            }
                        }
                        //if (falseCount % 6 == 0 || checkDto.IsCustomer == 0 || response.Result.desc.Contains("价格发生变化"))
                        if (checkDto.IsCustomer == 0)
                        {
                            bookOutDto.Message = response.Result.desc;
                            bookOutDto.IsBook  = false;
                        }
                        else if (tongguo || response.Result.desc.Contains("价格发生变化"))
                        {
                            var rate = new TaobaoRate();
                            XhotelRateGetRequest req = new XhotelRateGetRequest();
                            req.OutRid = checkDto.OuterId;
                            req.Rpid   = checkDto.Rpid;
                            var tmallApiClient         = new TmallApiClient(Shop);
                            XhotelRateGetResponse resp = tmallApiClient.Execute(req);
                            if (resp != null && !resp.IsError && resp.Rate != null && !string.IsNullOrWhiteSpace(resp.Rate.InventoryPrice))
                            {
                                //try
                                //{
                                //    rate = JsonConvert.DeserializeObject<TaobaoRate>(resp.Rate.InventoryPrice);
                                //}
                                //catch (Exception ex)
                                //{
                                //}
                                rate = JsonConvert.DeserializeObject <TaobaoRate>(resp.Rate.InventoryPrice);
                            }
                            var totalDayNum = (int)(checkDto.CheckOut.Date - checkDto.CheckIn).TotalDays;
                            var rates       = new List <inventory_price>();
                            //判断是否已经更新过飞猪房态,0为没更新过,1为已更新过为不可以预定
                            int IsClose = 0;
                            for (int i = 0; i < totalDayNum; i++)
                            {
                                var inventory_price = rate.inventory_price.Where(a => a.date == checkDto.CheckIn.AddDays(i).ToString("yyyy-MM-dd")).FirstOrDefault();
                                if (inventory_price.price <= 0 || inventory_price.price > 4999900)
                                {
                                    IsClose            = 1;
                                    bookOutDto.Message = response.Result.desc;
                                    bookOutDto.IsBook  = false;
                                }
                                else
                                {
                                    rates.Add(inventory_price);
                                }
                            }

                            if (IsClose == 0)
                            {
                                bookOutDto.DayPrice = rates.Select(u =>
                                                                   new RateQuotasPrice
                                {
                                    Date  = u.date,
                                    Price = u.price,
                                    Quota = 1
                                }).ToList();
                                bookOutDto.Message = "正常可预定";
                                bookOutDto.IsBook  = true;
                                //logWriter.Write("试单失败后通过(美团):试单失败-淘宝酒店id:{0},美团酒店id:{1},{2},失败数次:{3},失败原因{4}", checkDto.Hid, checkDto.HotelId, checkDto.Rpid, falseCount, response.Result.desc);
                            }
                        }
                        else if (falseCount % 3 == 0)
                        {
                            var rate = new TaobaoRate();
                            XhotelRateGetRequest req = new XhotelRateGetRequest();
                            req.OutRid = checkDto.OuterId;
                            req.Rpid   = checkDto.Rpid;
                            var tmallApiClient         = new TmallApiClient(Shop);
                            XhotelRateGetResponse resp = tmallApiClient.Execute(req);
                            if (resp != null && !resp.IsError && resp.Rate != null && !string.IsNullOrWhiteSpace(resp.Rate.InventoryPrice))
                            {
                                //try
                                //{
                                //    rate = JsonConvert.DeserializeObject<TaobaoRate>(resp.Rate.InventoryPrice);
                                //}
                                //catch (Exception ex)
                                //{
                                //}
                                rate = JsonConvert.DeserializeObject <TaobaoRate>(resp.Rate.InventoryPrice);
                            }
                            var totalDayNum = (int)(checkDto.CheckOut.Date - checkDto.CheckIn).TotalDays;
                            var rates       = new List <inventory_price>();
                            //判断是否已经更新过飞猪房态,0为没更新过,1为已更新过为不可以预定
                            int IsClose = 0;
                            for (int i = 0; i < totalDayNum; i++)
                            {
                                var inventory_price = rate.inventory_price.Where(a => a.date == checkDto.CheckIn.AddDays(i).ToString("yyyy-MM-dd")).FirstOrDefault();
                                if (inventory_price.price <= 0 || inventory_price.price > 4999900)
                                {
                                    IsClose            = 1;
                                    bookOutDto.Message = response.Result.desc;
                                    bookOutDto.IsBook  = false;
                                }
                                else
                                {
                                    rates.Add(inventory_price);
                                }
                            }

                            if (IsClose == 0)
                            {
                                Random ran          = new Random();
                                int    RandomResult = ran.Next(80, 200);

                                bookOutDto.DayPrice = rates.Select(u =>
                                                                   new RateQuotasPrice
                                {
                                    Date  = u.date,
                                    Price = u.price + RandomResult * 100m,
                                    Quota = 1
                                }).ToList();
                                bookOutDto.Message = "正常可预定";
                                bookOutDto.IsBook  = true;
                                //logWriter.Write("试单失败后通过(美团):试单失败-淘宝酒店id:{0},美团酒店id:{1},{2},失败数次:{3},失败原因{4}", checkDto.Hid, checkDto.HotelId, checkDto.Rpid, falseCount, response.Result.desc);
                            }
                        }
                        else
                        {
                            //logWriter.Write("试单失败后(美团):试单失败-淘宝酒店id:{0},美团酒店id:{1},{2},失败数次:{3},失败原因{4}", checkDto.Hid, checkDto.HotelId, checkDto.Rpid, falseCount, response.Result.desc);
                            bookOutDto.Message = response.Result.desc;
                            bookOutDto.IsBook  = false;
                        }
                        //记录试单信息并关闭房态
                        Task.Factory.StartNew(() =>
                        {
                            try
                            {
                                string Remark = response.Result.desc;
                                bool IsFull   = true;
                                if (checkDto.IsCustomer == 0)
                                {
                                    Remark = "程序请求";
                                    IsFull = false;
                                }
                                else if (response.Result.desc.Contains("价格发生变化"))
                                {
                                    Remark = "价格发生变化";
                                    IsFull = false;
                                }
                                AliTripValidate av = new AliTripValidate();
                                av.CheckInTime     = checkDto.CheckIn;
                                av.CheckOutTime    = checkDto.CheckOut;
                                av.RatePlanCode    = checkDto.RatePlanCode;
                                av.IsFull          = IsFull;
                                av.HotelId         = checkDto.HotelId;
                                av.RoomId          = checkDto.RoomTypeId;
                                av.RatePlanId      = checkDto.RatePlanId;
                                av.CreateTime      = DateTime.Now;

                                av.Channel = 5;
                                av.Shop    = (int)Shop;
                                av.Remark  = Remark;
                                SqlSugarContext.RenNiXingInstance.Insertable(av).ExecuteCommand();
                            }
                            catch
                            {
                            }
                            if (checkDto.IsCustomer != 0)
                            {
                                Thread.Sleep(13000);
                            }
                            string url = string.Format("http://localhost:8097/apiAshx/UpdateRoomRate.ashx?type=RoomRate&hid={0}&source=5", checkDto.HotelId);
                            WebHttpRequest.Get(url);
                        });
                    }
                    catch (Exception ex)
                    {
                        //记录试单信息并关闭房态
                        Task.Factory.StartNew(() =>
                        {
                            try
                            {
                                bool IsFull   = true;
                                string Remark = response.Result.desc;
                                if (checkDto.IsCustomer == 0)
                                {
                                    Remark = "程序请求";
                                    IsFull = false;
                                }
                                AliTripValidate av = new AliTripValidate();
                                av.CheckInTime     = checkDto.CheckIn;
                                av.CheckOutTime    = checkDto.CheckOut;
                                av.RatePlanCode    = checkDto.RatePlanCode;
                                av.IsFull          = IsFull;
                                av.HotelId         = checkDto.HotelId;
                                av.RoomId          = checkDto.RoomTypeId;
                                av.RatePlanId      = checkDto.RatePlanId;
                                av.CreateTime      = DateTime.Now;

                                av.Channel = 5;
                                av.Shop    = (int)Shop;
                                av.Remark  = Remark;
                                SqlSugarContext.RenNiXingInstance.Insertable(av).ExecuteCommand();
                            }
                            catch
                            {
                            }
                            Thread.Sleep(6000);
                            string url = string.Format("http://localhost:8097/apiAshx/UpdateRoomRate.ashx?type=RoomRate&hid={0}&source=5", checkDto.HotelId);
                            WebHttpRequest.Get(url);
                        });
                        //logWriter.Write("试单失败后报错(美团):试单失败-淘宝酒店id:{0},美团酒店id:{1},{2},失败原因{3},报错{4}", checkDto.Hid, checkDto.HotelId, checkDto.Rpid, response.Result.desc, ex.ToString());
                        bookOutDto.Message = response.Result.desc;
                        bookOutDto.IsBook  = false;
                    }
                }
            }
            catch (Exception ex)
            {
                bookOutDto.IsBook  = false;
                bookOutDto.Message = string.Format("满房,系统异常:{0}", ex.ToString());
            }
            if (checkDto.IsCustomer == 0)
            {
                try
                {
                    BookingCheckOutDto CheckOut = new BookingCheckOutDto();
                    var check = CacheHelper.GetCache(checkKey); //先读取缓存
                    if (check != null)                          //如果没有该缓存
                    {
                        CheckOut = Newtonsoft.Json.JsonConvert.DeserializeObject <BookingCheckOutDto>(check.ToString(), new JsonSerializerSettings
                        {
                            Error = delegate(object obj, Newtonsoft.Json.Serialization.ErrorEventArgs args)
                            {
                                args.ErrorContext.Handled = true;
                            }
                        });
                        if (CheckOut != null)
                        {
                            return(CheckOut);
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            }
            CacheHelper.SetCache(checkKey, Newtonsoft.Json.JsonConvert.SerializeObject(bookOutDto), 8);//添加缓存
            return(bookOutDto);
        }