yumeng %!s(int64=5) %!d(string=hai) anos
pai
achega
a6b581ef0c

+ 7 - 3
jeecg-boot-module-system/src/main/resources/application-dev.yml

@@ -96,12 +96,13 @@ spring:
         poolPreparedStatements: true
         maxPoolPreparedStatementPerConnectionSize: 20
         # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
-        filters: stat,wall,slf4j
+        filters: stat,slf4j
         # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
-        connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
+
+
       datasource:
         master:
-          url: jdbc:mysql://39.106.184.70:3306/jeecg-boot?characterEncoding=UTF-8&useUnicode=true&useSSL=false
+          url: jdbc:mysql://39.106.184.70:3306/jeecg-boot?allowMultiQueries=true&characterEncoding=UTF-8&useUnicode=true&useSSL=false
           username: root
           password: hcst2019
           driver-class-name: com.mysql.jdbc.Driver
@@ -149,6 +150,7 @@ jeecg:
     image-upload: D://upFiles//image//
     chrome-driver: D://chromedriver.exe
     csv-upload: D://upFiles//csv//
+    reportDownload: D://report//
 kuaishou:
   group-control:
     path: http://39.106.184.70
@@ -167,3 +169,5 @@ oss:
     replace-old-value: oss-cn-beijing
     download: D:\mnt\file\video
 
+
+

+ 14 - 34
jeecg-boot-module-system/src/test/java/org/jeecg/SampleTest.java

@@ -1,54 +1,34 @@
 package org.jeecg;
 
-import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouReportDailyAccountMapper;
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouReportTaskService;
 import lombok.extern.slf4j.Slf4j;
-import org.junit.After;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
-import java.io.*;
-import java.net.HttpURLConnection;
-import java.net.URL;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
 
 @RunWith(SpringRunner.class)
 @SpringBootTest
 @Slf4j
 public class SampleTest {
-    @Value("${jeecg.path.chrome-driver}")
-    private String chromeDriver;
+
 
     @Autowired
-    private KuaishouReportDailyAccountMapper reportDailyAccountMapper;
+    private IKuaiShouReportTaskService reportTaskService;
+
     @Test
     public void kuaisShouReport() {
 
 
         try {
-          /*  String url = "https://ad.e.kuaishou.com/rest/openapi/v1/async_task/create";
-            Map<String, String> headers = new HashMap<>();
-            headers.put("Content-Type", " application/json");
-            headers.put("Access-Token", "eb9a0d18072eabc339ee26ad010ecd6b");
-            JSONObject param = new JSONObject();
-            param.put("advertiser_id", 142402);
-            param.put("task_name", "142402-test");
 
-            JSONObject taskParams = new JSONObject();
-            taskParams.put("start_date", "2020-01-01");
-            taskParams.put("end_date", "2020-03-05");
-            taskParams.put("view_type", 1);
-            param.put("task_params",taskParams);
 
-
-
-            String result = HttpUtils.kuaiShouhttpPostRequest(url, param.toJSONString(), headers);
-            JSONObject resultJson = JSONObject.parseObject(result);
-            System.err.println(resultJson);*/
-
-            // 1229917  142402
+            reportTaskService.getTaskList();
 
 
 
@@ -84,7 +64,7 @@ public class SampleTest {
             conn.setConnectTimeout(10 * 1000);
             //防止屏蔽程序抓取而返回403错误
             conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
-            conn.setRequestProperty("Access-Token", "0b5a280170a155f50636b2efd95def15");
+            conn.setRequestProperty("Access-Token", "6a1fbe20e49fe519ec8120aac5c9e55a");
             //得到输入流
             InputStream inputStream = conn.getInputStream();
             //获取自己数组
@@ -104,12 +84,10 @@ public class SampleTest {
             }
             if (inputStream != null) {
                 inputStream.close();
-            }
-*/
-            String localPath = "D:\\file\\123.csv";
-
-            reportDailyAccountMapper.loadAccountDailyReport(142402L,localPath);
+            }*/
+         /*   String localPath = "D:\\file\\123.csv";
 
+            reportDailyAccountMapper.loadAccountDailyReport(142402L,localPath);*/
 
 
         } catch (Exception e) {
@@ -130,6 +108,8 @@ public class SampleTest {
         bos.close();
         return bos.toByteArray();
     }
+
+
 }
 
 

+ 16 - 0
module-common/src/main/java/cn/com/ctop/common/module/utils/KuaishouInterfaceConstant.java

@@ -168,6 +168,22 @@ public class KuaishouInterfaceConstant {
      */
     public static final String IMAGE_LIST = "/rest/openapi/v1/file/ad/image/list";
 
+
+    /**
+     * 历史数据查询任务
+     */
+    public static final String ASYNC_TASK_CREAT = "/rest/openapi/v1/async_task/create";
+    /**
+     * 获取任务列表
+     */
+    public static final String ASYNC_TASK_LIST = "/rest/openapi/v1/async_task/list";
+
+    /**
+     * 下载数据
+     */
+    public static final String ASYNC_TASK_DOWNLOAD = "/rest/openapi/v1/async_task/download";
+
+
     /**
      * 登录
      */

+ 248 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/controller/KuaiShouReportTaskController.java

@@ -0,0 +1,248 @@
+package cn.com.ctop.kuaishou.modules.batch.controller;
+
+import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouReportTask;
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouReportTaskService;
+import com.alibaba.fastjson.JSON;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.jeecg.common.api.vo.Result;
+import org.jeecg.common.aspect.annotation.AutoLog;
+import org.jeecg.common.system.query.QueryGenerator;
+import org.jeecg.common.util.oConvertUtils;
+import org.jeecgframework.poi.excel.ExcelImportUtil;
+import org.jeecgframework.poi.excel.def.NormalExcelConstants;
+import org.jeecgframework.poi.excel.entity.ExportParams;
+import org.jeecgframework.poi.excel.entity.ImportParams;
+import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.multipart.MultipartHttpServletRequest;
+import org.springframework.web.servlet.ModelAndView;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 快手历史数据任务记录
+ *
+ * @author jeecg-boot
+ * @version V1.0
+ * @date 2020-03-09
+ */
+@Slf4j
+@Api(tags = "快手历史数据任务记录")
+@RestController
+@RequestMapping("/ctop/kuaiShouReportTask")
+public class KuaiShouReportTaskController {
+    @Autowired
+    private IKuaiShouReportTaskService kuaiShouReportTaskService;
+
+    /**
+     * 分页列表查询
+     *
+     * @param kuaiShouReportTask
+     * @param pageNo
+     * @param pageSize
+     * @param req
+     * @return
+     */
+    @AutoLog(value = "快手历史数据任务记录-分页列表查询")
+    @ApiOperation(value = "快手历史数据任务记录-分页列表查询", notes = "快手历史数据任务记录-分页列表查询")
+    @GetMapping(value = "/list")
+    public Result<IPage<KuaiShouReportTask>> queryPageList(KuaiShouReportTask kuaiShouReportTask,
+                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
+                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
+                                                           HttpServletRequest req) {
+        Result<IPage<KuaiShouReportTask>> result = new Result<IPage<KuaiShouReportTask>>();
+        QueryWrapper<KuaiShouReportTask> queryWrapper = QueryGenerator.initQueryWrapper(kuaiShouReportTask, req.getParameterMap());
+        Page<KuaiShouReportTask> page = new Page<KuaiShouReportTask>(pageNo, pageSize);
+        IPage<KuaiShouReportTask> pageList = kuaiShouReportTaskService.page(page, queryWrapper);
+        result.setSuccess(true);
+        result.setResult(pageList);
+        return result;
+    }
+
+    /**
+     * 添加
+     *
+     * @param kuaiShouReportTask
+     * @return
+     */
+    @AutoLog(value = "快手历史数据任务记录-添加")
+    @ApiOperation(value = "快手历史数据任务记录-添加", notes = "快手历史数据任务记录-添加")
+    @PostMapping(value = "/add")
+    public Result<KuaiShouReportTask> add(@RequestBody KuaiShouReportTask kuaiShouReportTask) {
+        Result<KuaiShouReportTask> result = new Result<KuaiShouReportTask>();
+        try {
+            kuaiShouReportTaskService.save(kuaiShouReportTask);
+            result.success("添加成功!");
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+            result.error500("操作失败");
+        }
+        return result;
+    }
+
+    /**
+     * 编辑
+     *
+     * @param kuaiShouReportTask
+     * @return
+     */
+    @AutoLog(value = "快手历史数据任务记录-编辑")
+    @ApiOperation(value = "快手历史数据任务记录-编辑", notes = "快手历史数据任务记录-编辑")
+    @PutMapping(value = "/edit")
+    public Result<KuaiShouReportTask> edit(@RequestBody KuaiShouReportTask kuaiShouReportTask) {
+        Result<KuaiShouReportTask> result = new Result<KuaiShouReportTask>();
+        KuaiShouReportTask kuaiShouReportTaskEntity = kuaiShouReportTaskService.getById(kuaiShouReportTask.getId());
+        if (kuaiShouReportTaskEntity == null) {
+            result.error500("未找到对应实体");
+        } else {
+            boolean ok = kuaiShouReportTaskService.updateById(kuaiShouReportTask);
+            if (ok) {
+                result.success("修改成功!");
+            }
+        }
+
+        return result;
+    }
+
+    /**
+     * 通过id删除
+     *
+     * @param id
+     * @return
+     */
+    @AutoLog(value = "快手历史数据任务记录-通过id删除")
+    @ApiOperation(value = "快手历史数据任务记录-通过id删除", notes = "快手历史数据任务记录-通过id删除")
+    @DeleteMapping(value = "/delete")
+    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
+        try {
+            kuaiShouReportTaskService.removeById(id);
+        } catch (Exception e) {
+            log.error("删除失败", e.getMessage());
+            return Result.error("删除失败!");
+        }
+        return Result.ok("删除成功!");
+    }
+
+    /**
+     * 批量删除
+     *
+     * @param ids
+     * @return
+     */
+    @AutoLog(value = "快手历史数据任务记录-批量删除")
+    @ApiOperation(value = "快手历史数据任务记录-批量删除", notes = "快手历史数据任务记录-批量删除")
+    @DeleteMapping(value = "/deleteBatch")
+    public Result<KuaiShouReportTask> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
+        Result<KuaiShouReportTask> result = new Result<KuaiShouReportTask>();
+        if (ids == null || "".equals(ids.trim())) {
+            result.error500("参数不识别!");
+        } else {
+            this.kuaiShouReportTaskService.removeByIds(Arrays.asList(ids.split(",")));
+            result.success("删除成功!");
+        }
+        return result;
+    }
+
+    /**
+     * 通过id查询
+     *
+     * @param id
+     * @return
+     */
+    @AutoLog(value = "快手历史数据任务记录-通过id查询")
+    @ApiOperation(value = "快手历史数据任务记录-通过id查询", notes = "快手历史数据任务记录-通过id查询")
+    @GetMapping(value = "/queryById")
+    public Result<KuaiShouReportTask> queryById(@RequestParam(name = "id", required = true) String id) {
+        Result<KuaiShouReportTask> result = new Result<KuaiShouReportTask>();
+        KuaiShouReportTask kuaiShouReportTask = kuaiShouReportTaskService.getById(id);
+        if (kuaiShouReportTask == null) {
+            result.error500("未找到对应实体");
+        } else {
+            result.setResult(kuaiShouReportTask);
+            result.setSuccess(true);
+        }
+        return result;
+    }
+
+    /**
+     * 导出excel
+     *
+     * @param request
+     * @param response
+     */
+    @RequestMapping(value = "/exportXls")
+    public ModelAndView exportXls(HttpServletRequest request, HttpServletResponse response) {
+        // Step.1 组装查询条件
+        QueryWrapper<KuaiShouReportTask> queryWrapper = null;
+        try {
+            String paramsStr = request.getParameter("paramsStr");
+            if (oConvertUtils.isNotEmpty(paramsStr)) {
+                String deString = URLDecoder.decode(paramsStr, "UTF-8");
+                KuaiShouReportTask kuaiShouReportTask = JSON.parseObject(deString, KuaiShouReportTask.class);
+                queryWrapper = QueryGenerator.initQueryWrapper(kuaiShouReportTask, request.getParameterMap());
+            }
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        }
+
+        //Step.2 AutoPoi 导出Excel
+        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
+        List<KuaiShouReportTask> pageList = kuaiShouReportTaskService.list(queryWrapper);
+        //导出文件名称
+        mv.addObject(NormalExcelConstants.FILE_NAME, "快手历史数据任务记录列表");
+        mv.addObject(NormalExcelConstants.CLASS, KuaiShouReportTask.class);
+        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("快手历史数据任务记录列表数据", "导出人:Jeecg", "导出信息"));
+        mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
+        return mv;
+    }
+
+    /**
+     * 通过excel导入数据
+     *
+     * @param request
+     * @param response
+     * @return
+     */
+    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
+    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
+        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
+        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
+        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
+            MultipartFile file = entity.getValue();
+            ImportParams params = new ImportParams();
+            params.setTitleRows(2);
+            params.setHeadRows(1);
+            params.setNeedSave(true);
+            try {
+                List<KuaiShouReportTask> listKuaiShouReportTasks = ExcelImportUtil.importExcel(file.getInputStream(), KuaiShouReportTask.class, params);
+                kuaiShouReportTaskService.saveBatch(listKuaiShouReportTasks);
+                return Result.ok("文件导入成功!数据行数:" + listKuaiShouReportTasks.size());
+            } catch (Exception e) {
+                log.error(e.getMessage(), e);
+                return Result.error("文件导入失败:" + e.getMessage());
+            } finally {
+                try {
+                    file.getInputStream().close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        return Result.ok("文件导入失败!");
+    }
+
+}

+ 95 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/entity/KuaiShouReportTask.java

@@ -0,0 +1,95 @@
+package cn.com.ctop.kuaishou.modules.batch.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+import org.jeecgframework.poi.excel.annotation.Excel;
+import org.springframework.format.annotation.DateTimeFormat;
+
+import java.util.Date;
+
+/**
+ * 快手历史数据任务记录
+ *
+ * @author jeecg-boot
+ * @version V1.0
+ * @date 2020-03-09
+ */
+@Data
+@TableName("ctop_kuaishou_report_task")
+@EqualsAndHashCode(callSuper = false)
+@Accessors(chain = true)
+@ApiModel(value = "ctop_kuaishou_report_task对象", description = "快手历史数据任务记录")
+public class KuaiShouReportTask {
+
+    /**
+     * 主键ID
+     */
+    @TableId(type = IdType.AUTO)
+    @ApiModelProperty(value = "主键ID")
+    private Long id;
+    /**
+     * 广告账户ID(相当于头条返回的access_token数据中的advertiser_id)
+     */
+    @Excel(name = "广告账户ID(相当于头条返回的access_token数据中的advertiser_id)", width = 15)
+    @ApiModelProperty(value = "广告账户ID(相当于头条返回的access_token数据中的advertiser_id)")
+    private Long accountId;
+    /**
+     * 任务id
+     */
+    @Excel(name = "任务id", width = 15)
+    @ApiModelProperty(value = "任务id")
+    private String taskId;
+    /**
+     * 任务名称
+     */
+    @Excel(name = "任务名称", width = 15)
+    @ApiModelProperty(value = "任务名称")
+    private String taskName;
+    /**
+     * 0:新建,1:处理中,2:处理成功,3:处理失败,4:数据已下载
+     */
+    @Excel(name = "0:新建,1:处理中,2:处理成功,3:处理失败,4:数据已下载", width = 15)
+    @ApiModelProperty(value = "0:新建,1:处理中,2:处理成功,3:处理失败,4:数据已下载")
+    private Integer taskStatus;
+    /**
+     * 1:账户维度查询,2:  广告计划维度查询,3:广告组维度查询,4:广告创意维度查询
+     */
+    @Excel(name = "1:账户维度查询,2:  广告计划维度查询,3:广告组维度查询,4:广告创意维度查询", width = 15)
+    @ApiModelProperty(value = "1:账户维度查询,2:  广告计划维度查询,3:广告组维度查询,4:广告创意维度查询")
+    private Integer viewType;
+    /**
+     * 任务查询数据起始时间
+     */
+    @Excel(name = "任务查询数据起始时间", width = 15)
+    @ApiModelProperty(value = "任务查询数据起始时间")
+    private String startDate;
+    /**
+     * 任务查询数据结束时间
+     */
+    @Excel(name = "任务查询数据结束时间", width = 15)
+    @ApiModelProperty(value = "任务查询数据结束时间")
+    private String endDate;
+    /**
+     * 创建时间
+     */
+    @Excel(name = "创建时间", width = 20, format = "yyyy-MM-dd HH:mm:ss")
+    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
+    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    @ApiModelProperty(value = "创建时间")
+    private Date createTime;
+    /**
+     * 修改时间
+     */
+    @Excel(name = "修改时间", width = 20, format = "yyyy-MM-dd HH:mm:ss")
+    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
+    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    @ApiModelProperty(value = "修改时间")
+    private Date updateTime;
+}

+ 15 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/mapper/KuaiShouReportTaskMapper.java

@@ -0,0 +1,15 @@
+package cn.com.ctop.kuaishou.modules.batch.mapper;
+
+import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouReportTask;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+ * 快手历史数据任务记录
+ *
+ * @author: jeecg-boot
+ * @date: 2020-03-09
+ * @cersion: V1.0
+ */
+public interface KuaiShouReportTaskMapper extends BaseMapper<KuaiShouReportTask> {
+
+}

+ 5 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/mapper/xml/KuaiShouReportTaskMapper.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouReportTaskMapper">
+
+</mapper>

+ 18 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/service/IKuaiShouReportTaskService.java

@@ -0,0 +1,18 @@
+package cn.com.ctop.kuaishou.modules.batch.service;
+
+import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouReportTask;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+ * 快手历史数据任务记录
+ *
+ * @author jeecg-boot
+ * @version V1.0
+ * @date 2020-03-09
+ */
+public interface IKuaiShouReportTaskService extends IService<KuaiShouReportTask> {
+    void createTask(Long accountId, String token);
+
+    void getTaskList();
+
+}

+ 193 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/service/impl/KuaiShouReportTaskServiceImpl.java

@@ -0,0 +1,193 @@
+package cn.com.ctop.kuaishou.modules.batch.service.impl;
+
+import cn.com.ctop.common.module.entity.CtopOauthToken;
+import cn.com.ctop.common.module.service.ICtopOauthTokenService;
+import cn.com.ctop.common.module.utils.Check;
+import cn.com.ctop.common.module.utils.HttpUtils;
+import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
+import cn.com.ctop.common.module.utils.PropertiesUtils;
+import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouReportTask;
+import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouReportTaskMapper;
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouReportTaskService;
+import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouReportDailyAccountMapper;
+import cn.com.ctop.kuaishou.modules.utils.DownloadUtils;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import lombok.extern.slf4j.Slf4j;
+import org.jeecg.common.util.DateUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+/**
+ * 快手历史数据任务记录
+ *
+ * @author jeecg-boot
+ * @version V1.0
+ * @date 2020-03-09
+ */
+@Slf4j
+@Service
+public class KuaiShouReportTaskServiceImpl extends ServiceImpl<KuaiShouReportTaskMapper, KuaiShouReportTask> implements IKuaiShouReportTaskService {
+    @Value("${jeecg.path.reportDownload}")
+    private static String downloadPath;
+    static List<Integer> typeList;
+    static ExecutorService executorService = Executors.newFixedThreadPool(3);
+
+    @Autowired
+    private ICtopOauthTokenService ctopOauthTokenService;
+    @Autowired
+    private KuaishouReportDailyAccountMapper reportDailyAccountMapper;
+
+    @Override
+    public void createTask(Long accountId, String token) {
+        if (Check.isNull(accountId) || Check.isNull(token)) {
+            log.error("token或accountId为空");
+            return;
+        }
+        try {
+            String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.ASYNC_TASK_CREAT;
+            Map<String, String> headers = new HashMap<>();
+            headers.put("Content-Type", "application/json");
+            headers.put("Access-Token", token);
+            JSONObject param = new JSONObject();
+            param.put("advertiser_id", accountId);
+            String endDate = DateUtils.getDate("yyyy-MM-dd");
+            String startDate = DateUtils.addMonth(endDate, -6);
+            JSONObject taskParams = new JSONObject();
+            taskParams.put("start_date", startDate);
+            taskParams.put("end_date", endDate);
+
+            if (Check.isNull(typeList)) {
+                getTypeList();
+            }
+            for (int i = 0; i < typeList.size(); i++) {
+                Integer type = typeList.get(i);
+                String taskName = accountId + "_" + type;
+                param.put("task_name", taskName);
+                taskParams.put("view_type", type);
+                param.put("task_params", taskParams);
+                String result = HttpUtils.kuaiShouhttpPostRequest(url, param.toJSONString(), headers);
+                JSONObject resultJson = JSONObject.parseObject(result);
+                if (!Check.isNull(resultJson)) {
+                    Integer code = resultJson.getInteger("code");
+                    if (code == 0) {
+                        JSONObject dataJson = resultJson.getJSONObject("data");
+                        KuaiShouReportTask reportTask = new KuaiShouReportTask();
+                        reportTask.setAccountId(accountId);
+                        reportTask.setStartDate(startDate);
+                        reportTask.setEndDate(endDate);
+                        reportTask.setTaskId(dataJson.getString("task_id"));
+                        reportTask.setViewType(type);
+                        reportTask.setTaskStatus(0);
+                        reportTask.setTaskName(taskName);
+                        this.save(reportTask);
+                    } else {
+                        log.error("快手历史报表请求失败,accountId:{},返回数据:{}", accountId, resultJson);
+                    }
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+
+    /**
+     * 获取任务列表
+     */
+    @Override
+    public void getTaskList() {
+        QueryWrapper<KuaiShouReportTask> taskQueryWrapper = new QueryWrapper<>();
+        taskQueryWrapper.eq("task_status", 0);
+        taskQueryWrapper.orderByDesc("create_time");
+        List<KuaiShouReportTask> taskList = this.list(taskQueryWrapper);
+        if (!Check.isNull(taskList)) {
+            for (KuaiShouReportTask task : taskList) {
+                CtopOauthToken token = ctopOauthTokenService.getTokenByAccountId(task.getAccountId());
+                if (!Check.isNull(token)) {
+                    String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.ASYNC_TASK_LIST;
+                    Map<String, String> headers = new HashMap<>();
+                    headers.put("Content-Type", "application/json");
+                    headers.put("Access-Token", token.getAccessToken());
+                    JSONObject param = new JSONObject();
+                    param.put("advertiser_id", task.getAccountId());
+                    JSONArray taskIds = new JSONArray();
+                    taskIds.add(task.getTaskId());
+                    param.put("task_ids", taskIds);
+                    String result = HttpUtils.kuaiShouhttpPostRequest(url, param.toJSONString(), headers);
+                    JSONObject resultJson = JSONObject.parseObject(result);
+                    System.err.println(resultJson);
+                    if (!Check.isNull(resultJson)) {
+                        Integer code = resultJson.getInteger("code");
+                        if (code == 0) {
+                            JSONObject dataJson = resultJson.getJSONObject("data");
+                            if (!Check.isNull(dataJson)) {
+                                JSONArray details = dataJson.getJSONArray("details");
+                                if (!Check.isNull(details)) {
+                                    for (int i = 0; i < details.size(); i++) {
+                                        JSONObject detailJson = details.getJSONObject(i);
+                                        if (!Check.isNull(detailJson)) {
+                                            Integer taskStatus = detailJson.getInteger("task_status");
+                                            if (taskStatus == 2) {
+                                                loadFile(token.getAccessToken(), task);
+                                            } else if (taskStatus == 3) {
+                                                task.setTaskStatus(3);
+                                                this.updateById(task);
+                                            }
+                                        }
+                                    }
+                                }
+                            }
+                        } else {
+                            log.error("快手获取任务列表返回错误,accountId:{},返回结果:{}", task.getAccountId(), resultJson);
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+
+    private void loadFile(String token, KuaiShouReportTask task) {
+        try {
+            String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.ASYNC_TASK_DOWNLOAD;
+            String fileName = task.getAccountId() + "_" + task.getViewType() + ".csv";
+            Map<String, Object> requestMap = new HashMap<>();
+            requestMap.put("advertiser_id", task.getAccountId());
+            requestMap.put("task_id", task.getTaskId());
+            String localPath = DownloadUtils.downloadByUrl(requestMap, "D://report//", url, token, fileName);
+            if (!Check.isNull(localPath)) {
+                if (task.getViewType() == 1) {
+                    reportDailyAccountMapper.loadAccountDailyReport(task.getAccountId(), localPath);
+                }
+
+            }
+            task.setTaskStatus(4);
+            this.updateById(task);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+    }
+
+
+    private void getTypeList() {
+        typeList = new ArrayList<>();
+        typeList.add(1);
+        typeList.add(2);
+        typeList.add(3);
+        typeList.add(4);
+
+    }
+
+}

+ 10 - 15
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/report/mapper/xml/KuaishouReportDailyAccountMapper.xml

@@ -1,22 +1,18 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
 <mapper namespace="cn.com.ctop.kuaishou.modules.report.mapper.KuaishouReportDailyAccountMapper">
-
-
     <insert id="loadAccountDailyReport">
-		LOAD DATA local INFILE 'D://file//123.csv'
+		LOAD DATA local INFILE  #{localPath}
 		INTO TABLE ctop_kuaishou_report_daily_account_new CHARACTER SET utf8
-		FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
+		FIELDS TERMINATED BY ',' enclosed by '"' LINES TERMINATED BY '\n'
 		IGNORE 1 LINES
-		(
-		ad_scene,
+		(@ad_scene,
         charge,
         photo_show,
         aclick,
         bclick,
-        share,
-        comment,
+        photo_share,
+        photo_comment,
         photo_like,
         follow,
         cancel_follow,
@@ -28,7 +24,7 @@
         activation,
         submit,
         stat_date,
-        stat_hour,
+        @stat_hour,
         photo_click,
         photo_click_ratio,
         action_ratio,
@@ -40,7 +36,8 @@
         event_pay_first_day_roi,
         event_pay,
         event_pay_purchase_amount,
-        event_pay_roi,event_register,
+        event_pay_roi,
+        event_register,
         event_register_cost,
         event_register_ratio,
         event_jin_jian_app,
@@ -63,9 +60,7 @@
         event_next_day_stay_ratio,
         event_next_day_stay,
         play_3s_ratio
-         )
-          set account_id = 131332
+        )
+          set account_id = #{accountId}
     </insert>
-
-
 </mapper>

+ 87 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/utils/DownloadUtils.java

@@ -0,0 +1,87 @@
+package cn.com.ctop.kuaishou.modules.utils;
+
+import cn.com.ctop.common.module.utils.Check;
+
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.util.Map;
+
+public class DownloadUtils {
+
+
+    public static String downloadByUrl(Map<String, Object> requestMap, String downloadPath, String urlStr, String token, String fileName) {
+
+        try {
+            StringBuilder postBody = null;
+            if (!Check.isNull(requestMap)) {
+                postBody = new StringBuilder();
+                for (Map.Entry<String, Object> entry : requestMap.entrySet()) {
+                    if (entry.getValue() == null) {
+                        continue;
+                    }
+                    postBody.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue().toString(),
+                            "utf-8")).append("&");
+                }
+                if (!requestMap.isEmpty()) {
+                    postBody.deleteCharAt(postBody.length() - 1);
+                }
+            }
+
+            URL url = new URL(urlStr + "?" + postBody);
+            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+            //设置超时间为3秒
+            conn.setConnectTimeout(10 * 1000);
+            //防止屏蔽程序抓取而返回403错误
+            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
+            conn.setRequestProperty("Access-Token", token);
+            //得到输入流
+            InputStream inputStream = null;
+
+            inputStream = conn.getInputStream();
+
+            //获取自己数组
+            byte[] getData = readInputStream(inputStream);
+
+            //文件保存位置
+            System.err.println(downloadPath);
+            File saveDir = new File(downloadPath);
+            if (!saveDir.exists()) {
+                saveDir.mkdirs();
+            }
+            String filePath = downloadPath + fileName;
+
+            File file = new File(saveDir + File.separator + fileName);
+            FileOutputStream fos = new FileOutputStream(file);
+            fos.write(getData);
+            if (fos != null) {
+                fos.close();
+            }
+            if (inputStream != null) {
+                inputStream.close();
+            }
+
+            return filePath;
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+
+        return null;
+
+    }
+
+    public static byte[] readInputStream(InputStream inputStream) throws IOException {
+        byte[] buffer = new byte[1024];
+        int len = 0;
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        while ((len = inputStream.read(buffer)) != -1) {
+            bos.write(buffer, 0, len);
+        }
+        bos.close();
+        return bos.toByteArray();
+    }
+
+
+}