|
@@ -0,0 +1,174 @@
|
|
|
|
+package cn.com.ctop.toutiao.modules.report.service.impl;
|
|
|
|
+
|
|
|
|
+import cn.com.ctop.common.module.entity.CtopOauthToken;
|
|
|
|
+import cn.com.ctop.common.module.entity.MaterialParameter;
|
|
|
|
+import cn.com.ctop.common.module.utils.HttpUtils;
|
|
|
|
+import cn.com.ctop.toutiao.modules.material.entity.ByteDanceAdvertisePlan;
|
|
|
|
+import cn.com.ctop.toutiao.modules.material.entity.ByteDanceCampaign;
|
|
|
|
+import cn.com.ctop.toutiao.modules.material.entity.ByteDanceCreative;
|
|
|
|
+import cn.com.ctop.toutiao.modules.material.entity.ByteDanceOperationRecord;
|
|
|
|
+import cn.com.ctop.toutiao.modules.material.service.IByteDanceAdvertisePlanService;
|
|
|
|
+import cn.com.ctop.toutiao.modules.material.service.IByteDanceCampaignService;
|
|
|
|
+import cn.com.ctop.toutiao.modules.material.service.IByteDanceCreativeService;
|
|
|
|
+import cn.com.ctop.toutiao.modules.report.entity.BytedancePlanDailyReport;
|
|
|
|
+import cn.com.ctop.toutiao.modules.report.mapper.BytedanceOperationRecordMapper;
|
|
|
|
+import cn.com.ctop.toutiao.modules.report.service.IBytedanceInterfaceService;
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.jeecg.common.util.DateUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.context.annotation.Primary;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+@Service
|
|
|
|
+@Slf4j
|
|
|
|
+@Primary
|
|
|
|
+public class BytedanceInterfaceServiceImpl implements IBytedanceInterfaceService {
|
|
|
|
+ @Autowired
|
|
|
|
+ private IByteDanceCampaignService byteDanceCampaignService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private IByteDanceAdvertisePlanService byteDanceAdvertisePlanService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private BytedanceOperationRecordMapper bytedanceOperationRecordMapper;
|
|
|
|
+ @Autowired
|
|
|
|
+ private IByteDanceCreativeService byteDanceCreativeService;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void searchLog(Long accountId, CtopOauthToken token, Integer operationTarget, String startDate, String endDate){
|
|
|
|
+
|
|
|
|
+ //int campaignCount = byteDanceCampaignService.count(parameterQueryWrapper);
|
|
|
|
+ //int campaignPageSize = 20;
|
|
|
|
+ //int campaignPage = 1;
|
|
|
|
+ //
|
|
|
|
+ //Integer campaignTotalPage = (campaignCount - 1) / campaignPageSize + 1;
|
|
|
|
+ //if (campaignPage < campaignTotalPage) {
|
|
|
|
+ // QueryWrapper<ByteDanceCampaign> parameterQueryWrapper2 = new QueryWrapper<>();
|
|
|
|
+ // parameterQueryWrapper2.eq("account_id", accountId);
|
|
|
|
+ // parameterQueryWrapper2.last("limit" + (campaignPage-1) + "," + campaignPageSize);
|
|
|
|
+ // parameterQueryWrapper2.select("id");
|
|
|
|
+ //
|
|
|
|
+ //}
|
|
|
|
+ int pageNum = 1;
|
|
|
|
+ int pageSize = 20;
|
|
|
|
+ String startTime = startDate + " 00:00:00";
|
|
|
|
+ String endTime = endDate + " 23:59:59";
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ if(operationTarget == 2){
|
|
|
|
+ QueryWrapper<ByteDanceCampaign> parameterQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ parameterQueryWrapper.eq("account_id", accountId);
|
|
|
|
+ parameterQueryWrapper.select("id");
|
|
|
|
+ List<ByteDanceCampaign> list = byteDanceCampaignService.list(parameterQueryWrapper);
|
|
|
|
+ for (ByteDanceCampaign campaign:list){
|
|
|
|
+ logSearchByPage(pageNum, pageSize, token, accountId, campaign.getId(), 2, startTime, endTime); //组
|
|
|
|
+ }
|
|
|
|
+ }else if(operationTarget == 3){
|
|
|
|
+ QueryWrapper<ByteDanceAdvertisePlan> parameterQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ parameterQueryWrapper.eq("account_id", accountId);
|
|
|
|
+ parameterQueryWrapper.select("id");
|
|
|
|
+ List<ByteDanceAdvertisePlan> list = byteDanceAdvertisePlanService.list(parameterQueryWrapper);
|
|
|
|
+ for (ByteDanceAdvertisePlan plan:list){
|
|
|
|
+ logSearchByPage(pageNum, pageSize, token, accountId, plan.getId(), 3, startTime, endTime); //计划
|
|
|
|
+ }
|
|
|
|
+ }else if(operationTarget == 4){
|
|
|
|
+ QueryWrapper<ByteDanceCreative> parameterQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ parameterQueryWrapper.eq("account_id", accountId);
|
|
|
|
+ parameterQueryWrapper.select("id");
|
|
|
|
+ List<ByteDanceCreative> list = byteDanceCreativeService.list(parameterQueryWrapper);
|
|
|
|
+ for(ByteDanceCreative creative:list){
|
|
|
|
+ logSearchByPage(pageNum, pageSize, token, accountId, creative.getId(), 4, startTime, endTime); //创意
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ log.error("获取头条操作记录失败:accountId:{},startDate:{},endDate:{}", token.getAccountId(), startDate, endDate);
|
|
|
|
+ log.error(e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void logSearchByPage(Integer pageNum, Integer pageSize, CtopOauthToken token, Long accountId, Long objectId, Integer operationType, String startTime, String endTime) {
|
|
|
|
+ log.info("当前页数:" + pageNum);
|
|
|
|
+ String access_token = token.getAccessToken();
|
|
|
|
+
|
|
|
|
+ // 请求地址
|
|
|
|
+ String open_api_domain = "https://ad.oceanengine.com";
|
|
|
|
+ String path = "/open_api/2/tools/log_search/";
|
|
|
|
+
|
|
|
|
+ // 请求参数
|
|
|
|
+ Map data = new HashMap();
|
|
|
|
+ data.put("advertiser_id", accountId);
|
|
|
|
+ data.put("start_time", startTime);
|
|
|
|
+ data.put("end_time", endTime);
|
|
|
|
+ data.put("page", pageNum);
|
|
|
|
+ data.put("page_size", pageSize);
|
|
|
|
+
|
|
|
|
+ List<Long> list = new ArrayList<>();
|
|
|
|
+ list.add(objectId);
|
|
|
|
+ data.put("object_id", list);
|
|
|
|
+
|
|
|
|
+ JSONObject json = HttpUtils.bytedanceGetRequest(access_token, open_api_domain + path, JSONObject.parseObject(JSONObject.toJSONString(data)));
|
|
|
|
+ if (json == null) {
|
|
|
|
+ log.error("头条日志查询返回为空,请求有误:" + JSONObject.toJSONString(data));
|
|
|
|
+ return ;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ int code = json.getInteger("code");
|
|
|
|
+ if(code == 0){
|
|
|
|
+ List<ByteDanceOperationRecord> records = new ArrayList<>();
|
|
|
|
+
|
|
|
|
+ JSONObject dataResultJson = json.getJSONObject("data");
|
|
|
|
+ JSONObject pageInfoJson = dataResultJson.getJSONObject("page_info");
|
|
|
|
+ int totalPage = pageInfoJson.getInteger("total_number");
|
|
|
|
+ int page = pageInfoJson.getInteger("page");
|
|
|
|
+
|
|
|
|
+ JSONArray logsArray = dataResultJson.getJSONArray("logs");
|
|
|
|
+ int logsSize = logsArray.size();
|
|
|
|
+ for (int i=0; i<logsSize ;i++){
|
|
|
|
+ JSONObject logJson = logsArray.getJSONObject(i);
|
|
|
|
+ ByteDanceOperationRecord record = new ByteDanceOperationRecord();
|
|
|
|
+ record.setAccountId(accountId);
|
|
|
|
+ record.setContentTitle(logJson.getString("content_title"));
|
|
|
|
+ record.setContentLog(logJson.getString("content_log"));
|
|
|
|
+ record.setObjectId(logJson.getLong("object_id"));
|
|
|
|
+ record.setObjectName(logJson.getString("object_name"));
|
|
|
|
+ record.setObjectType(logJson.getString("object_type"));
|
|
|
|
+ record.setOperationCreateTime(logJson.getDate("create_time"));
|
|
|
|
+ record.setStatDate(DateUtils.formatDate(logJson.getDate("create_time")));
|
|
|
|
+ record.setOperator(logJson.getString("operator"));
|
|
|
|
+ record.setOptIp(logJson.getString("opt_id"));
|
|
|
|
+ record.setOperationTarget(operationType);
|
|
|
|
+
|
|
|
|
+ records.add(record);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //按类型入库
|
|
|
|
+ if(records !=null && records.size()!=0){
|
|
|
|
+ insertLog(operationType, records);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(page < totalPage){
|
|
|
|
+ logSearchByPage(page+1, pageSize, token, accountId, objectId, operationType, startTime, endTime);
|
|
|
|
+ }
|
|
|
|
+ }else{
|
|
|
|
+ log.error("头条日志查询返回为空,请求有误:" + JSONObject.toJSONString(data),";;;返回值为:"+ json);
|
|
|
|
+ return ;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void insertLog(Integer operationTarget, List<ByteDanceOperationRecord> records){
|
|
|
|
+ if(operationTarget == 2){
|
|
|
|
+ bytedanceOperationRecordMapper.insertOperationRecordCampaign(records);
|
|
|
|
+ }else if(operationTarget == 3){
|
|
|
|
+ bytedanceOperationRecordMapper.insertOperationRecordPlan(records);
|
|
|
|
+ }else if(operationTarget == 4){
|
|
|
|
+ bytedanceOperationRecordMapper.insertOperationRecordCreative(records);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|