예제 #1
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);

            if (product == null)
            {
                return(false);
            }

            string reqURL = DailyReportAddress();

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

            paramList.Add(new KeyValuePair <string, string>("advertiser_id", account.ThirdpartyId));
            paramList.Add(new KeyValuePair <string, string>("start_date", startDate.DateString()));
            paramList.Add(new KeyValuePair <string, string>("end_date", endDate.DateString()));
            paramList.Add(new KeyValuePair <string, string>("time_granularity", "STAT_TIME_GRANULARITY_DAILY"));
            paramList.Add(new KeyValuePair <string, string>("page_size", "1000"));

            var httpClient = new HttpClient();

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

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

                var result = JsonConvert.DeserializeObject <ToutiaoAdvertResponse <ToutiaoDailyReportListResponse> >(jsonResultString);
                var datas  = result.Data.List.OrderByDescending(l => l.StatDatetime).ToList();

                foreach (var item in datas)
                {
                    if (item.AdvertiserId.IsNullOrEmpty())
                    {
                        continue;
                    }

                    if (item.Cost == decimal.Zero)
                    {
                        continue;
                    }

                    var dataImport = new AdvertStatisticImport()
                    {
                        AdvertAccountId = account.Id,
                        ProductId       = account.ProductId,
                        ProductName     = product.Name,
                        StatisticOnUtc  = item.StatDatetime.GetStartTimeOfDate(),

                        ClickNum      = item.Click,
                        DisplayNum    = item.Show,
                        ClickPrice    = item.Click > 0 ? Math.Round(item.Cost / item.Click, 4) : 0,
                        ThDisplayCost = item.Show > 0 ? Math.Round(item.Cost / item.Show * 1000, 4) : 0,
                        TotalCost     = item.Cost,
                    };

                    await _advertDailyStatisticManager.InsertOrUpdateAdvertStatisticAsync(dataImport);
                }
            }
            catch (Exception)
            {
                return(false);
            }
            finally
            {
                httpClient.Dispose();
            }

            return(true);
        }
예제 #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);
        }