|
|
@@ -3,26 +3,73 @@ package cn.com.ctop.job.bytedance.data.service.impl;
|
|
|
import cn.com.ctop.job.bytedance.data.entity.BytedanceErrorLog;
|
|
|
import cn.com.ctop.job.bytedance.data.mapper.BytedancePromoterReportMapper;
|
|
|
import cn.com.ctop.job.bytedance.data.mapper.DianZhongMapper;
|
|
|
+import cn.com.ctop.job.bytedance.data.service.ICacheService;
|
|
|
import cn.com.ctop.job.bytedance.data.service.IDianZhongService;
|
|
|
+import cn.com.ctop.job.bytedance.data.utils.Check;
|
|
|
+import cn.com.ctop.job.bytedance.data.utils.DateUtils;
|
|
|
import cn.com.ctop.job.bytedance.data.utils.HttpUtils;
|
|
|
+import cn.com.ctop.job.bytedance.utils.doris.sink.DorisMultiTableStreamLoadManager;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.util.NumberUtil;
|
|
|
import cn.hutool.crypto.SecureUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.client.ResponseExtractor;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
public class DianZhongServiceImpl implements IDianZhongService {
|
|
|
@Autowired
|
|
|
+ private ICacheService cacheService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DorisMultiTableStreamLoadManager dorisLoadManager;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
private BytedancePromoterReportMapper promoterReportMapper;
|
|
|
|
|
|
@Autowired
|
|
|
+ private RestTemplate downloadFileRestTemplate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
private DianZhongMapper dianZhongMapper;
|
|
|
|
|
|
+ private static final Map<Integer, Integer> domain2Platform = new HashMap<Integer, Integer>() {{
|
|
|
+ put(14, 2);//抖小
|
|
|
+ put(16, 1);//微小
|
|
|
+ put(17, 3);//快小
|
|
|
+ }};
|
|
|
+
|
|
|
+ private static final Map<String, String> clientDomain2AppId = new HashMap<String, String>() {{
|
|
|
+ put("10003129" + "14", "dzdyIAP");//抖小
|
|
|
+ }};
|
|
|
+
|
|
|
+ private static final Map<String, String> media2Id = new HashMap<String, String>() {{
|
|
|
+ put("adq", "3");//腾讯广点通
|
|
|
+ put("dy", "1");//字节巨量
|
|
|
+ put("ks", "2");//快手-磁力
|
|
|
+ }};
|
|
|
+
|
|
|
+ public List<JSONObject> getDZRetry() {
|
|
|
+ return dianZhongMapper.getDZRetry();
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ 获取点众IAP数据下载url
|
|
|
+ */
|
|
|
public void getDownloadUrl(String dataType, String clientId, String token,
|
|
|
- String statDay, String startTime, String endTime) {
|
|
|
+ String startTime, String endTime, Long errId, boolean isLog) {
|
|
|
long l = System.currentTimeMillis();
|
|
|
JSONObject param = new JSONObject()
|
|
|
.fluentPut("clientId", clientId)
|
|
|
@@ -30,47 +77,250 @@ public class DianZhongServiceImpl implements IDianZhongService {
|
|
|
.fluentPut("edate", String.valueOf(DateUtil.parse(endTime, "yyyy-MM-dd HH:mm:ss").getTime() / 1000))
|
|
|
.fluentPut("timestamp", l)
|
|
|
.fluentPut("signKey", getSignKey(clientId, token, l));
|
|
|
+ if ("user".equals(dataType)) {
|
|
|
+ param.put("queryField", "loginTime");
|
|
|
+ }
|
|
|
try {
|
|
|
String url = "https://routine.wqxsw.com/flames/channel/query/" + dataType;
|
|
|
String response = HttpUtils.httpPostRequest(url, param, null);
|
|
|
JSONObject responseJson = JSONObject.parseObject(response);
|
|
|
- System.out.println(response);
|
|
|
if ("0".equals(responseJson.getString("retCode"))) {
|
|
|
String taskId = responseJson.getString("data");
|
|
|
JSONObject jsonObject = new JSONObject()
|
|
|
- .fluentPut("stat_date", statDay.replace("-", ""))
|
|
|
+ .fluentPut("stat_date", DateUtils.transformFormat(endTime, "yyyy-MM-dd HH:mm:ss", "yyyyMMdd"))
|
|
|
.fluentPut("task_id", taskId)
|
|
|
.fluentPut("type", dataType)
|
|
|
- .fluentPut("client_id", clientId);
|
|
|
+ .fluentPut("client_id", clientId)
|
|
|
+ .fluentPut("start_time", startTime)
|
|
|
+ .fluentPut("end_time", endTime);
|
|
|
dianZhongMapper.insertDownloadTask(jsonObject);
|
|
|
- log.info("获取点众数据下载地址成功 {} {} {}~{} ", dataType, clientId, startTime, endTime);
|
|
|
+ log.info("获取点众{}数据下载地址成功 {} {}~{} taskId:{}", dataType, clientId, startTime, endTime, taskId);
|
|
|
+
|
|
|
+ if (errId != null) {
|
|
|
+ promoterReportMapper.deleteInfoByErrorId(errId);
|
|
|
+ }
|
|
|
} else {
|
|
|
- log.error("获取点众数据下载地址错误 {} {} {}~{} msg:{}", dataType, clientId, startTime, endTime, responseJson.getString("retMsg"));
|
|
|
+ log.error("获取点众{}数据下载地址错误 {} {}~{} msg:{}", dataType, clientId, startTime, endTime, responseJson.getString("retMsg"));
|
|
|
+ if (isLog) {
|
|
|
+ promoterReportMapper.replaceErrorInfo(
|
|
|
+ BytedanceErrorLog.builder()
|
|
|
+ .msg(responseJson.getString("retMsg"))
|
|
|
+ .advertiserId(Long.valueOf(clientId))
|
|
|
+ .dataDimension("dianzhong_url")
|
|
|
+ .dataType(dataType)
|
|
|
+ .startDate(startTime)
|
|
|
+ .endDate(endTime)
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取点众{}数据下载地址错误 {} {}~{} ", dataType, clientId, startTime, endTime, e);
|
|
|
+ if (isLog) {
|
|
|
promoterReportMapper.replaceErrorInfo(
|
|
|
BytedanceErrorLog.builder()
|
|
|
- .msg(responseJson.getString("retMsg"))
|
|
|
+ .msg(e.getMessage())
|
|
|
.advertiserId(Long.valueOf(clientId))
|
|
|
- .dataDimension("dianzhong")
|
|
|
+ .dataDimension("dianzhong_url")
|
|
|
.dataType(dataType)
|
|
|
.startDate(startTime)
|
|
|
.endDate(endTime)
|
|
|
.build());
|
|
|
}
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("获取点众数据下载地址错误 {} {} {}~{} ", dataType, clientId, startTime, endTime, e);
|
|
|
- promoterReportMapper.replaceErrorInfo(
|
|
|
- BytedanceErrorLog.builder()
|
|
|
- .msg(e.getMessage())
|
|
|
- .advertiserId(Long.valueOf(clientId))
|
|
|
- .dataDimension("dianzhong")
|
|
|
- .dataType(dataType)
|
|
|
- .startDate(startTime)
|
|
|
- .endDate(endTime)
|
|
|
- .build());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private static String getSignKey(String clientId, String token, long timeStamp) {
|
|
|
+ /*
|
|
|
+ 下载点众IAP数据
|
|
|
+ */
|
|
|
+ public void getUrlData(String statDate) {
|
|
|
+ List<JSONObject> tasks = dianZhongMapper.getTaskUrl(statDate.replace("-", ""));
|
|
|
+ if (!Check.isNull(tasks)) {
|
|
|
+ for (JSONObject task : tasks) {
|
|
|
+ String taskId = task.getString("task_id");
|
|
|
+ String url = task.getString("url");
|
|
|
+ String type = task.getString("type");
|
|
|
+ String clientId = task.getString("client_id");
|
|
|
+
|
|
|
+ downloadFileRestTemplate.execute(url, org.springframework.http.HttpMethod.GET, null,
|
|
|
+ (ResponseExtractor<Void>) response -> {
|
|
|
+ try (BufferedReader reader = new BufferedReader(
|
|
|
+ new InputStreamReader(response.getBody(), StandardCharsets.UTF_8))) {
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ if ("user".equals(type)) {
|
|
|
+ JSONObject dzUser = getDZUser(line, clientId);
|
|
|
+ dorisLoadManager.add("hr_playlet", "user_event", dzUser);
|
|
|
+ JSONObject dzLogin = getDZLogin(line, clientId);
|
|
|
+ dorisLoadManager.add("hr_playlet", "server_event", dzLogin);
|
|
|
+ } else if ("order".equals(type)) {
|
|
|
+ JSONObject dzOrder = getDZOrder(line, clientId);
|
|
|
+ dorisLoadManager.add("hr_playlet", "server_event", dzOrder);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ });
|
|
|
+ dianZhongMapper.updateTaskDownloaded(taskId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getSignKey(String clientId, String token, long timeStamp) {
|
|
|
return SecureUtil.md5(clientId + token + timeStamp).toLowerCase();
|
|
|
}
|
|
|
+
|
|
|
+ private JSONObject getDZUser(String line, String clientId) {
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(line);
|
|
|
+ JSONObject userJson = new JSONObject();
|
|
|
+ int domain = jsonObject.getIntValue("domain");
|
|
|
+ String appId = clientId + domain;
|
|
|
+ Long ctime = jsonObject.getLong("ctime");
|
|
|
+ String statDate = DateUtils.formatDate(ctime * 1000, "yyyyMMdd");
|
|
|
+ String registerTime = DateUtils.formatDate(ctime * 1000, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ String adId = jsonObject.getString("adid");
|
|
|
+ String promotionId = jsonObject.getString("promotionId");
|
|
|
+ adId = StringUtils.isNotBlank(promotionId) ? promotionId : adId;
|
|
|
+ Long dyeTime = jsonObject.getLong("dyeTime");
|
|
|
+ String activationTime = DateUtils.formatDate(dyeTime * 1000, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ String fromMedia = jsonObject.getString("from");
|
|
|
+ userJson.put("app_id", clientDomain2AppId.getOrDefault(appId, appId));
|
|
|
+ userJson.put("uid", jsonObject.getString("userId"));
|
|
|
+ userJson.put("stat_date", statDate);
|
|
|
+ userJson.put("register_time", registerTime);
|
|
|
+ userJson.put("created_at", ctime);
|
|
|
+ userJson.put("open_id", jsonObject.getString("openId"));
|
|
|
+ userJson.put("platform", domain2Platform.getOrDefault(domain, domain));
|
|
|
+ userJson.put("official_account_open_id", jsonObject.getString("thirdWxId"));
|
|
|
+ userJson.put("channel_id", jsonObject.getString("channelId"));
|
|
|
+ userJson.put("link_id", jsonObject.getString("referralId"));
|
|
|
+ userJson.put("expiration_time", jsonObject.getString("vipEndDate"));
|
|
|
+ userJson.put("pay_amount", jsonObject.getString("payAmount"));
|
|
|
+ userJson.put("pay_count", jsonObject.getString("payCount"));
|
|
|
+ userJson.put("last_login_time", jsonObject.getString("loginTime"));
|
|
|
+ userJson.put("ip", jsonObject.getString("ip"));
|
|
|
+ userJson.put("mobile_system", jsonObject.getIntValue("os") == 0 ? "Android" : "ios");
|
|
|
+ userJson.put("person_id", jsonObject.getString("kdrId"));
|
|
|
+ userJson.put("a_id", jsonObject.getString("projectId"));
|
|
|
+ userJson.put("ad_id", adId);
|
|
|
+ userJson.put("activation_time", activationTime);
|
|
|
+ userJson.put("media_id", media2Id.getOrDefault(fromMedia, fromMedia));
|
|
|
+
|
|
|
+ if (NumberUtil.isLong(adId) && !"0".equals(adId)) {
|
|
|
+ String anotherDay = DateUtils.getAnotherDay("yyyy-MM-dd", registerTime.substring(0, 10), -15);
|
|
|
+ Map<String, String> ad2Advertiser = cacheService.getBytedanceAdvertiserByAd(adId, DateUtils.getDateInteger(anotherDay));
|
|
|
+ if (!Check.isNullMap(ad2Advertiser)) {
|
|
|
+ String advertiserId = ad2Advertiser.get("advertiser_id");
|
|
|
+ userJson.put("advertiser_id", advertiserId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return userJson;
|
|
|
+ }
|
|
|
+
|
|
|
+ private JSONObject getDZLogin(String line, String clientId) {
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(line);
|
|
|
+ JSONObject loginJson = new JSONObject();
|
|
|
+ int domain = jsonObject.getIntValue("domain");
|
|
|
+ String appId = clientId + domain;
|
|
|
+ Long loginTime = jsonObject.getLong("loginTime");
|
|
|
+ String statDate = DateUtils.formatDate(loginTime * 1000, "yyyyMMdd");
|
|
|
+ String adId = jsonObject.getString("adid");
|
|
|
+ String promotionId = jsonObject.getString("promotionId");
|
|
|
+ adId = StringUtils.isNotBlank(promotionId) ? promotionId : adId;
|
|
|
+ Long dyeTime = jsonObject.getLong("dyeTime");
|
|
|
+ String activationTime = DateUtils.formatDate(dyeTime * 1000, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ String fromMedia = jsonObject.getString("from");
|
|
|
+ String userId = jsonObject.getString("userId");
|
|
|
+ loginJson.put("stat_date", statDate);
|
|
|
+ loginJson.put("event", "LOGIN");
|
|
|
+ loginJson.put("event_pk", SecureUtil.md5(String.format("dianzhong_login_%s_%s", userId, loginTime)));
|
|
|
+ loginJson.put("event_tag", "dianzhong_login");
|
|
|
+ loginJson.put("app_id", clientDomain2AppId.getOrDefault(appId, appId));
|
|
|
+
|
|
|
+ loginJson.put("uid", userId);
|
|
|
+ loginJson.put("open_id", jsonObject.getString("openId"));
|
|
|
+ loginJson.put("platform", domain2Platform.getOrDefault(domain, domain));
|
|
|
+ loginJson.put("official_account_open_id", jsonObject.getString("thirdWxId"));
|
|
|
+ loginJson.put("channel_id", jsonObject.getString("channelId"));
|
|
|
+ loginJson.put("link_id", jsonObject.getString("referralId"));
|
|
|
+ loginJson.put("register_date", jsonObject.getString("registerDate"));
|
|
|
+ loginJson.put("expiration_time", jsonObject.getString("vipEndDate"));
|
|
|
+ loginJson.put("last_login_time", jsonObject.getString("loginTime"));
|
|
|
+ loginJson.put("created_at", loginTime);
|
|
|
+ loginJson.put("ip", jsonObject.getString("ip"));
|
|
|
+ loginJson.put("mobile_system", jsonObject.getIntValue("os") == 0 ? "Android" : "ios");
|
|
|
+ loginJson.put("person_id", jsonObject.getString("kdrId"));
|
|
|
+ loginJson.put("a_id", jsonObject.getString("projectId"));
|
|
|
+ loginJson.put("ad_id", adId);
|
|
|
+ loginJson.put("activation_time", activationTime);
|
|
|
+ loginJson.put("media_id", media2Id.getOrDefault(fromMedia, fromMedia));
|
|
|
+
|
|
|
+ if (NumberUtil.isLong(adId) && !"0".equals(adId)) {
|
|
|
+ String anotherDay = DateUtils.getAnotherDay("yyyy-MM-dd", DateUtils.formatDate(loginTime * 1000, "yyyy-MM-dd"), -15);
|
|
|
+ Map<String, String> ad2Advertiser = cacheService.getBytedanceAdvertiserByAd(adId, DateUtils.getDateInteger(anotherDay));
|
|
|
+ if (!Check.isNullMap(ad2Advertiser)) {
|
|
|
+ String advertiserId = ad2Advertiser.get("advertiser_id");
|
|
|
+ loginJson.put("advertiser_id", advertiserId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return loginJson;
|
|
|
+ }
|
|
|
+
|
|
|
+ private JSONObject getDZOrder(String line, String clientId) {
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(line);
|
|
|
+ JSONObject orderJson = new JSONObject();
|
|
|
+ String orderId = jsonObject.getString("id");
|
|
|
+ int domain = jsonObject.getIntValue("domain");
|
|
|
+ String appId = clientId + domain;
|
|
|
+ Long ctime = jsonObject.getLong("ctime");
|
|
|
+ String statDate = DateUtils.formatDate(ctime * 1000, "yyyyMMdd");
|
|
|
+ String adId = jsonObject.getString("adid");
|
|
|
+ String promotionId = jsonObject.getString("promotionId");
|
|
|
+ adId = StringUtils.isNotBlank(promotionId) ? promotionId : adId;
|
|
|
+ Long dyeTime = jsonObject.getLong("dyeTime");
|
|
|
+ String activationTime = DateUtils.formatDate(dyeTime * 1000, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ String fromMedia = jsonObject.getString("from");
|
|
|
+ orderJson.put("stat_date", statDate);
|
|
|
+ orderJson.put("event", "PAY");
|
|
|
+ orderJson.put("event_pk", SecureUtil.md5("dianzhong_order_id:" + orderId));
|
|
|
+ orderJson.put("event_tag", "dianzhong_order");
|
|
|
+ orderJson.put("app_id", clientDomain2AppId.getOrDefault(appId, appId));
|
|
|
+ orderJson.put("order_no", orderId);
|
|
|
+ orderJson.put("platform_order_no", jsonObject.getString("outTradeNo"));
|
|
|
+ orderJson.put("total_fee", jsonObject.getString("discount"));
|
|
|
+ orderJson.put("type", jsonObject.getString("type"));
|
|
|
+ orderJson.put("status", jsonObject.getString("statusNotify"));
|
|
|
+ orderJson.put("created_at", ctime);
|
|
|
+ orderJson.put("pay_time", jsonObject.getLong("finishTime"));
|
|
|
+ orderJson.put("uid", jsonObject.getString("userId"));
|
|
|
+ orderJson.put("channel_id", jsonObject.getString("channelId"));
|
|
|
+ orderJson.put("platform", domain2Platform.getOrDefault(domain, domain));
|
|
|
+ orderJson.put("video_id", jsonObject.getString("sourceInfo"));
|
|
|
+ orderJson.put("video_detail_id", jsonObject.getString("chapterId"));
|
|
|
+ orderJson.put("video_name", jsonObject.getString("sourceDesc"));
|
|
|
+ orderJson.put("register_date", jsonObject.getString("registerDate"));
|
|
|
+ orderJson.put("open_id", jsonObject.getString("openId"));
|
|
|
+ orderJson.put("mobile_system", jsonObject.getIntValue("os") == 0 ? "Android" : "ios");
|
|
|
+ orderJson.put("link_id", jsonObject.getString("referralId"));
|
|
|
+ orderJson.put("a_id", jsonObject.getString("projectId"));
|
|
|
+ orderJson.put("ad_id", adId);
|
|
|
+ orderJson.put("person_id", jsonObject.getString("fromDrId") != null ? jsonObject.getString("fromDrId") : jsonObject.getString("kdrId"));
|
|
|
+ orderJson.put("scene", jsonObject.getString("scene"));
|
|
|
+ orderJson.put("official_account_open_id", jsonObject.getString("thirdWxId"));
|
|
|
+ orderJson.put("activation_time", activationTime);
|
|
|
+ orderJson.put("pay_type", jsonObject.getString("orderSubType"));
|
|
|
+ orderJson.put("media_id", media2Id.getOrDefault(fromMedia, fromMedia));
|
|
|
+ orderJson.put("money_benefit", jsonObject.getDouble("moneyBenefit"));
|
|
|
+ orderJson.put("params", jsonObject.toJSONString());
|
|
|
+
|
|
|
+ if (NumberUtil.isLong(adId) && !"0".equals(adId)) {
|
|
|
+ String anotherDay = DateUtils.getAnotherDay("yyyy-MM-dd", DateUtils.formatDate(ctime * 1000, "yyyy-MM-dd"), -15);
|
|
|
+ Map<String, String> ad2Advertiser = cacheService.getBytedanceAdvertiserByAd(adId, DateUtils.getDateInteger(anotherDay));
|
|
|
+ if (!Check.isNullMap(ad2Advertiser)) {
|
|
|
+ String advertiserId = ad2Advertiser.get("advertiser_id");
|
|
|
+ orderJson.put("advertiser_id", advertiserId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return orderJson;
|
|
|
+ }
|
|
|
}
|