|
@@ -0,0 +1,185 @@
|
|
|
+package cn.com.ctop.kuaishou.modules.report.service.impl;
|
|
|
+
|
|
|
+import cn.com.ctop.common.module.utils.HttpUtils2;
|
|
|
+import cn.com.ctop.kuaishou.modules.report.entity.KuaishouReportDailyAgent;
|
|
|
+import cn.com.ctop.kuaishou.modules.report.entity.KuaishouReportDailyAgentSum;
|
|
|
+import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouReportDailyAgentMapper;
|
|
|
+import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouReportDailyAgentSumMapper;
|
|
|
+import cn.com.ctop.kuaishou.modules.report.service.IKuaishouReportDailyAgentService;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import org.apache.http.client.CookieStore;
|
|
|
+import org.apache.http.impl.client.BasicCookieStore;
|
|
|
+import org.apache.http.impl.cookie.BasicClientCookie;
|
|
|
+import org.jeecg.common.util.DateUtils;
|
|
|
+import org.openqa.selenium.By;
|
|
|
+import org.openqa.selenium.Cookie;
|
|
|
+import org.openqa.selenium.WebDriver;
|
|
|
+import org.openqa.selenium.WebElement;
|
|
|
+import org.openqa.selenium.chrome.ChromeDriver;
|
|
|
+import org.openqa.selenium.chrome.ChromeOptions;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 代理商日报
|
|
|
+ * @author jeecg-boot
|
|
|
+ * @date 2019-12-13
|
|
|
+ * @version V1.0
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class KuaishouReportDailyAgentServiceImpl extends ServiceImpl<KuaishouReportDailyAgentMapper, KuaishouReportDailyAgent> implements IKuaishouReportDailyAgentService {
|
|
|
+ @Autowired
|
|
|
+ private KuaishouReportDailyAgentMapper kuaishouReportDailyAgentMapper;
|
|
|
+ @Autowired
|
|
|
+ private KuaishouReportDailyAgentSumMapper kuaishouReportDailyAgentSumMapper;
|
|
|
+ @Value("${jeecg.path.chrome-driver}")
|
|
|
+ private String chromeDriver;
|
|
|
+ @Override
|
|
|
+ public void replaceBatch(List<KuaishouReportDailyAgent> list) {
|
|
|
+ kuaishouReportDailyAgentMapper.replaceBatch(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void getReport(String startDate,String endDate){
|
|
|
+ System.getProperties().setProperty("webdriver.chrome.driver", "D:/chromedriver.exe");
|
|
|
+ String url = "https://agent.e.kuaishou.com";
|
|
|
+ ChromeOptions chromeOptions = new ChromeOptions();
|
|
|
+ chromeOptions.addArguments("--headless");
|
|
|
+ chromeOptions.addArguments("--incognito");
|
|
|
+ chromeOptions.addArguments("--disable-gpu");
|
|
|
+ chromeOptions.addArguments("--window-size=1920,1080");
|
|
|
+ chromeOptions.addArguments("--user-agent=" + HttpUtils2.USER_AGENT);
|
|
|
+ chromeOptions.setAcceptInsecureCerts(true);
|
|
|
+ WebDriver webDriver = new ChromeDriver(chromeOptions);
|
|
|
+ try {
|
|
|
+ Thread.sleep(3000L);
|
|
|
+ HttpUtils2.cookieStore = new BasicCookieStore();
|
|
|
+ webDriver.manage().deleteAllCookies();
|
|
|
+ //获取登录页面
|
|
|
+ webDriver.get(url);
|
|
|
+ Thread.sleep(3000L);
|
|
|
+ WebElement accountElement = webDriver.findElement(By.xpath("//div[@class='phone ']/input[@type='text']"));
|
|
|
+ accountElement.sendKeys("17718376864");
|
|
|
+ Thread.sleep(3000L);
|
|
|
+ WebElement passwordElement = webDriver.findElement(By.xpath("//div[@class='password ']/input[@type='password']"));
|
|
|
+ passwordElement.sendKeys("hcstbaoliang567");
|
|
|
+ WebElement loginElement = webDriver.findElement(By.xpath("//div[@class='foot']"));
|
|
|
+ Thread.sleep(3000L);
|
|
|
+ //点击登录
|
|
|
+ loginElement.click();
|
|
|
+ Thread.sleep(3000L);
|
|
|
+ Map<String,String> headerMap = new HashMap<String,String>();
|
|
|
+ headerMap.put("Accept","application/json");
|
|
|
+ headerMap.put("Accept-Encoding","gzip, deflate, br");
|
|
|
+ headerMap.put("Accept-Language","zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
|
|
|
+ headerMap.put("Connection","keep-alive");
|
|
|
+ headerMap.put("Host","agent.e.kuaishou.com");
|
|
|
+ headerMap.put("Referer","https://agent.e.kuaishou.com/");
|
|
|
+ headerMap.put("Content-Type","application/json;charset=utf-8");
|
|
|
+ headerMap.put("User-Agent",HttpUtils2.USER_AGENT);
|
|
|
+ Set<Cookie> cookies = webDriver.manage().getCookies();
|
|
|
+ Iterator iterator = cookies.iterator();
|
|
|
+ CookieStore cookieStore = new BasicCookieStore();
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ String cookieStr = iterator.next().toString();
|
|
|
+ String[] cookieArray = cookieStr.split(";");
|
|
|
+ Map<String, String> cookieMap = new HashMap<String, String>();
|
|
|
+ for (String cookie : cookieArray) {
|
|
|
+ String[] kv = cookie.split("=");
|
|
|
+ if (kv.length > 1) {
|
|
|
+ cookieMap.put(kv[0].trim(), kv[1].trim());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ BasicClientCookie clientCookie = new BasicClientCookie(cookieArray[0].split("=")[0],
|
|
|
+ cookieArray[0].split("=")[1]);
|
|
|
+ clientCookie.setDomain("agent.e.kuaishou.com");
|
|
|
+ clientCookie.setPath(cookieMap.get("path"));
|
|
|
+ HttpUtils2.cookieStore.addCookie(clientCookie);
|
|
|
+ }
|
|
|
+ String currentDate = startDate;
|
|
|
+
|
|
|
+ while(DateUtils.compareDate(currentDate,endDate)<=0){
|
|
|
+ String listUrl = "https://agent.e.kuaishou.com/rest/dsp/agent/account/data/list";
|
|
|
+ String postData = "{\"agentId\":403756224,\"date\":\""+currentDate+"\",\"sortKey\":\"totalCharge\",\"isAscending\":false,\"pageInfo\":{\"totalCount\":1616,\"currentPage\":1,\"pageSize\":999999}}";
|
|
|
+ String result = HttpUtils2.httpPostRequestTest(listUrl,postData,headerMap);
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ JsonNode resultNode = mapper.readTree(result);
|
|
|
+ Integer code = resultNode.get("result").intValue();
|
|
|
+ if(code == 1){
|
|
|
+ Iterator<JsonNode> iterator1 = resultNode.get("data").elements();
|
|
|
+ int i = 0;
|
|
|
+ List<KuaishouReportDailyAgent> reportList = new ArrayList<>();
|
|
|
+ while (iterator1.hasNext()){
|
|
|
+ JsonNode dataNode = iterator1.next();
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ if(i == 0){
|
|
|
+ KuaishouReportDailyAgentSum kuaishouReportDailyAgentSum = new KuaishouReportDailyAgentSum();
|
|
|
+ kuaishouReportDailyAgentSum.setBalance(dataNode.get("totalBalanceDouble").decimalValue());
|
|
|
+ kuaishouReportDailyAgentSum.setConvertClickRate(dataNode.get("actionbarClickRatio").decimalValue());
|
|
|
+ kuaishouReportDailyAgentSum.setConvertCount(dataNode.get("actionbarClick").asInt());
|
|
|
+ kuaishouReportDailyAgentSum.setCost(dataNode.get("totalChargedInYuan").decimalValue());
|
|
|
+ kuaishouReportDailyAgentSum.setCostCampaignCount(dataNode.get("chargedCampaignCount").asInt());
|
|
|
+ kuaishouReportDailyAgentSum.setDate(sdf.parse(currentDate));
|
|
|
+ kuaishouReportDailyAgentSum.setFengmianClickCount(dataNode.get("adPhotoClick").asInt());
|
|
|
+ kuaishouReportDailyAgentSum.setFengmianClickRate(dataNode.get("clickRatio").decimalValue());
|
|
|
+ kuaishouReportDailyAgentSum.setFengmianShowCount(dataNode.get("impression").asInt());
|
|
|
+ kuaishouReportDailyAgentSum.setFandianCost(dataNode.get("rebateRealChargedInYuan").decimalValue());
|
|
|
+ kuaishouReportDailyAgentSum.setJiliCost(dataNode.get("directRebateRealChargedInYuan").decimalValue());
|
|
|
+ kuaishouReportDailyAgentSum.setKuangfanCost(dataNode.get("contractRebateRealChargedInYuan").decimalValue());
|
|
|
+ kuaishouReportDailyAgentSum.setSucaiShowCount(dataNode.get("click").asInt());
|
|
|
+ kuaishouReportDailyAgentSum.setXianjinCost(dataNode.get("realChargedInYuan").decimalValue());
|
|
|
+ kuaishouReportDailyAgentSum.setXinyongCost(dataNode.get("creditRealChargedInYuan").decimalValue());
|
|
|
+ QueryWrapper<KuaishouReportDailyAgentSum> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("date",currentDate);
|
|
|
+ kuaishouReportDailyAgentSumMapper.delete(queryWrapper);
|
|
|
+ kuaishouReportDailyAgentSumMapper.insert(kuaishouReportDailyAgentSum);
|
|
|
+ }else {
|
|
|
+ KuaishouReportDailyAgent kuaishouReportDailyAgent = new KuaishouReportDailyAgent();
|
|
|
+ kuaishouReportDailyAgent.setAccountId(dataNode.get("accountId").asInt());
|
|
|
+ kuaishouReportDailyAgent.setAdvertiserCreateTime(new Date(dataNode.get("createTime").asLong()));
|
|
|
+ kuaishouReportDailyAgent.setAdvertiserName(dataNode.get("accountName").asText());
|
|
|
+ kuaishouReportDailyAgent.setBalance(dataNode.get("totalBalanceDouble").decimalValue());
|
|
|
+ kuaishouReportDailyAgent.setConvertClickRate(dataNode.get("actionbarClickRatio").decimalValue());
|
|
|
+ kuaishouReportDailyAgent.setConvertCount(dataNode.get("actionbarClick").asInt());
|
|
|
+ kuaishouReportDailyAgent.setCost(dataNode.get("totalChargedInYuan").decimalValue());
|
|
|
+ kuaishouReportDailyAgent.setCostCampaignCount(dataNode.get("chargedCampaignCount").asInt());
|
|
|
+ kuaishouReportDailyAgent.setDate(sdf.parse(dataNode.get("dateTime").asText()));
|
|
|
+ kuaishouReportDailyAgent.setFengmianClickCount(dataNode.get("adPhotoClick").asInt());
|
|
|
+ kuaishouReportDailyAgent.setFengmianClickRate(dataNode.get("clickRatio").decimalValue());
|
|
|
+ kuaishouReportDailyAgent.setFengmianShowCount(dataNode.get("impression").asInt());
|
|
|
+ kuaishouReportDailyAgent.setFandianCost(dataNode.get("rebateRealChargedInYuan").decimalValue());
|
|
|
+ kuaishouReportDailyAgent.setJiliCost(dataNode.get("directRebateRealChargedInYuan").decimalValue());
|
|
|
+ kuaishouReportDailyAgent.setKid(dataNode.get("userId").asInt());
|
|
|
+ kuaishouReportDailyAgent.setKuangfanCost(dataNode.get("contractRebateRealChargedInYuan").decimalValue());
|
|
|
+ kuaishouReportDailyAgent.setSucaiShowCount(dataNode.get("click").asInt());
|
|
|
+ kuaishouReportDailyAgent.setXianjinCost(dataNode.get("realChargedInYuan").decimalValue());
|
|
|
+ kuaishouReportDailyAgent.setXinyongCost(dataNode.get("creditRealChargedInYuan").decimalValue());
|
|
|
+ reportList.add(kuaishouReportDailyAgent);
|
|
|
+ }
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ if(reportList != null && reportList.size() > 0){
|
|
|
+ replaceBatch(reportList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.out.println("代理商日报"+currentDate+"入库完成");
|
|
|
+ currentDate = DateUtils.addDay(currentDate,1);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ webDriver.manage().deleteAllCookies();
|
|
|
+// webDriver.close();
|
|
|
+ webDriver.quit();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|