Exemplo n.º 1
0
        /// <summary>
        /// 查询订单
        /// </summary>
        /// <param name="account"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task <TenantAdvertRequestResult <TenantOrder> > QueryOrderAsync(AdvertAccount account, DateTime startDate, DateTime endDate, int page, int pageSize = 100)
        {
            var dataParam = new DateRangePara()
            {
                StartDate = startDate.DateString(),
                EndDate   = endDate.DateString(),
            };


            string reqURL = OrderQueryAddress() + GetRequestPara(account);

            List <KeyValuePair <String, String> > paramList = new List <KeyValuePair <String, String> >();

            paramList.Add(new KeyValuePair <string, string>("account_id", account.ThirdpartyId));
            paramList.Add(new KeyValuePair <string, string>("date_range", JsonConvert.SerializeObject(dataParam)));
            paramList.Add(new KeyValuePair <string, string>("page", page.ToString()));
            paramList.Add(new KeyValuePair <string, string>("page_size", pageSize.ToString()));

            var httpClient = new HttpClient();

            try
            {
                var response = await httpClient.PostAsync(new Uri(reqURL), new FormUrlEncodedContent(paramList));

                var jsonResultString = (await response.Content.ReadAsStringAsync()).HtmlDecode();

                var result = JsonConvert.DeserializeObject <TenantAdvertRequestResult <TenantOrder> >(jsonResultString);

                return(result);
            }
            catch (Exception)
            {
                return(null);
            }
            finally
            {
                httpClient.Dispose();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 查询每日消耗
        /// </summary>
        /// <param name="account"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public async Task <bool> QueryDailyReportsAsync(AdvertAccount account,
                                                        DateTime startDate,
                                                        DateTime endDate)
        {
            if (!account.IsAuth())
            {
                return(false);
            }

            if (account.IsAuthExpires())
            {
                await RefreshAccessTokenAsync(account);
            }

            var product = await _productManager.GetByIdAsync(account.ProductId);

            var reqURL = DailyRepoersAddress() + GetRequestPara(account);

            var dataParam = new DateRangePara()
            {
                StartDate = startDate.DateString(),
                EndDate   = endDate.DateString(),
            };

            bool readEnd   = false;
            int  pageIndex = 1;
            int  pageSize  = 1000;

            var dateRange = JsonConvert.SerializeObject(dataParam).UrlDecode();

            var httpClient = new HttpClient();

            try
            {
                while (!readEnd)
                {
                    List <KeyValuePair <String, String> > paramList = new List <KeyValuePair <String, String> >();
                    paramList.Add(new KeyValuePair <string, string>("account_id", account.ThirdpartyId));
                    paramList.Add(new KeyValuePair <string, string>("level", "ADGROUP"));
                    paramList.Add(new KeyValuePair <string, string>("date_range", dateRange));
                    paramList.Add(new KeyValuePair <string, string>("group_by", "[\"date\"]"));
                    paramList.Add(new KeyValuePair <string, string>("page", pageIndex.ToString()));
                    paramList.Add(new KeyValuePair <string, string>("pageSize", pageSize.ToString()));

                    var response = await httpClient.PostAsync(new Uri(reqURL), new FormUrlEncodedContent(paramList));

                    var jsonResultString = await response.Content.ReadAsStringAsync();

                    var result = JsonConvert.DeserializeObject <TenantAdvertRequestResult <TenantDailyAdvert> >(jsonResultString);

                    foreach (var dailyStatistic in result.Data.Items)
                    {
                        //统计日期
                        var dataOnUtc = dailyStatistic.Date.LocalTimeConverUtcTime(_dateTimeHelper);

                        var dto = new AdvertStatisticImport()
                        {
                            AdvertAccountId = account.Id,
                            ProductId       = account.ProductId,
                            ProductName     = product.Name,
                            StatisticOnUtc  = dailyStatistic.Date,

                            ClickNum      = dailyStatistic.Click,
                            DisplayNum    = dailyStatistic.Impression,
                            ClickPrice    = Math.Round((dailyStatistic.Cost / 100) / dailyStatistic.Click, 4),
                            ThDisplayCost = Math.Round((dailyStatistic.Cost / 100) / dailyStatistic.Impression * 1000, 4),
                            TotalCost     = dailyStatistic.Cost / 100,
                        };

                        await _advertDailyStatisticManager.InsertOrUpdateAdvertStatisticAsync(dto);
                    }

                    readEnd = (pageIndex) * pageSize > result.Data.PageInfo.TotalNumber;
                }
            }
            catch (Exception)
            {
                return(false);
            }
            finally
            {
                httpClient.Dispose();
            }
            return(true);
        }