|
|
@@ -0,0 +1,166 @@
|
|
|
+package cn.com.ctop.job.bytedance.data.service.impl;
|
|
|
+
|
|
|
+import cn.com.ctop.job.bytedance.data.mapper.DistributorDataPushMapper;
|
|
|
+import cn.com.ctop.job.bytedance.data.service.IDistributionPushService;
|
|
|
+import cn.com.ctop.job.bytedance.data.utils.Check;
|
|
|
+import cn.com.ctop.job.bytedance.data.utils.DateUtils;
|
|
|
+import cn.com.ctop.job.bytedance.utils.FeishuRobotUtil;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.google.common.collect.ImmutableMap;
|
|
|
+import javafx.util.Pair;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class DistributionPushServiceImpl implements IDistributionPushService {
|
|
|
+ @Resource
|
|
|
+ DistributorDataPushMapper distributorDataPushMapper;
|
|
|
+
|
|
|
+ public static final String TEMPLATE_ID = "AAqhyWD5vfNFO";
|
|
|
+ public static final String TEMPLATE_VERSION = "1.0.6";
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void sendToday() {
|
|
|
+ String today = DateUtils.getNowDate("yyyy-MM-dd");
|
|
|
+ Long todayLong = Long.parseLong(today.replaceAll("-", ""));
|
|
|
+ int hour = Integer.parseInt(DateUtils.getNowDate("H"));
|
|
|
+ int lastHour = hour -1;
|
|
|
+ int lastLastHour = hour -2;
|
|
|
+
|
|
|
+ List<JSONObject> p1 = distributorDataPushMapper.todayData(todayLong,lastHour,lastLastHour);
|
|
|
+ List<JSONObject> p2 = distributorDataPushMapper.distributorTop(todayLong);
|
|
|
+ List<JSONObject> p3 = distributorDataPushMapper.movieTop(todayLong);
|
|
|
+ JSONObject template_variables = new JSONObject();
|
|
|
+
|
|
|
+
|
|
|
+ List<Pair<String, String>> columns = Arrays.asList(
|
|
|
+ new Pair<>("end_amount", "结算总收入"),
|
|
|
+ new Pair<>("pay_amount", "总付费金额"),
|
|
|
+ new Pair<>("iaa_amount", "广告预估收入")
|
|
|
+ );
|
|
|
+
|
|
|
+ Map<String, JSONObject> p1Time = p1.stream().collect(Collectors.toMap(o->o.getString("time"), o-> o));
|
|
|
+ JSONObject p1Today = p1Time.get("today");
|
|
|
+ JSONObject p1LastHour = p1Time.get("lastHour");
|
|
|
+ JSONObject p1ThisHour = p1Time.get("thisHour");
|
|
|
+
|
|
|
+ template_variables.put("updateTime", p1ThisHour.getString("update_time"));
|
|
|
+ template_variables.put("date", today.substring(5,10));
|
|
|
+ template_variables.put("thisHour", lastHour);
|
|
|
+ template_variables.put("lastHour", lastLastHour);
|
|
|
+
|
|
|
+ JSONArray object_list_1 = new JSONArray();
|
|
|
+ for (Pair<String, String> cp : columns) {
|
|
|
+ String c = cp.getKey();
|
|
|
+ String cn = cp.getValue();
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
+ obj.put("indicatorName", cn);
|
|
|
+ String todayNum = String.valueOf(p1Today.get(c));
|
|
|
+ obj.put("todayData", todayNum);
|
|
|
+ String lastHourNum = String.valueOf(p1ThisHour.get(c));
|
|
|
+ obj.put("thisHour", lastHourNum);
|
|
|
+ String lastLastHourNum = String.valueOf(p1LastHour.get(c));
|
|
|
+ obj.put("lastHour", lastLastHourNum);
|
|
|
+ object_list_1.add(obj);
|
|
|
+ }
|
|
|
+ template_variables.put("object_list_1", object_list_1);
|
|
|
+
|
|
|
+
|
|
|
+ JSONArray object_list_2 = new JSONArray();
|
|
|
+ for (JSONObject obj : p2) {
|
|
|
+ JSONObject o = new JSONObject();
|
|
|
+ o.put("distributorName", obj.getString("name"));
|
|
|
+ o.put("waterAmount", obj.getString("water_income"));
|
|
|
+ o.put("rank", obj.getString("rank"));
|
|
|
+ object_list_2.add(o);
|
|
|
+ }
|
|
|
+ template_variables.put("object_list_2", object_list_2);
|
|
|
+
|
|
|
+ JSONArray movieRankTable = new JSONArray();
|
|
|
+ for (JSONObject movie : p3){
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
+ String movieName = String.valueOf(movie.get("name"));
|
|
|
+ String movieProjectSource = String.valueOf(movie.get("movie_project_source"));
|
|
|
+ String movieProjectSourceName = String.valueOf(movie.get("project_type"));
|
|
|
+
|
|
|
+ String color = "blue";
|
|
|
+ if (!Check.isNull(movieProjectSource) && movieProjectSource.equals("1")){
|
|
|
+ color="grey";
|
|
|
+ }else if (!Check.isNull(movieProjectSource) && movieProjectSource.equals("2")){
|
|
|
+ color="green";
|
|
|
+ }else {
|
|
|
+ color="blue";
|
|
|
+ }
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ sb.append("<text_tag color='"+color+"'>"+movieProjectSourceName+"</text_tag>");
|
|
|
+ sb.append("<br>");
|
|
|
+ sb.append("<text_tag color='green'>"+movie.getString("project_ad_step")+"</text_tag>");
|
|
|
+ obj.put("project_type", sb.toString());
|
|
|
+ obj.put("rank", String.valueOf(movie.get("rank")));
|
|
|
+ obj.put("project_name", movieName);
|
|
|
+ obj.put("pay_amount", String.valueOf(movie.get("pay_amount")));
|
|
|
+ obj.put("iaa_amount", String.valueOf(movie.get("iaa_amount")));
|
|
|
+ movieRankTable.add(obj);
|
|
|
+ }
|
|
|
+ template_variables.put("table_raw_array_1", movieRankTable);
|
|
|
+
|
|
|
+
|
|
|
+ String token;
|
|
|
+ try {
|
|
|
+ token = FeishuRobotUtil.getFsZtTenantToken();
|
|
|
+ }catch (IOException e){
|
|
|
+ log.error("获取飞书token失败", e);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ sendToFeishu(token, template_variables, "44fbaed6");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void sendYesterday() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void sendToFeishu(String token, JSONObject template_variable, String receive_id) {
|
|
|
+ JSONObject content = new JSONObject();
|
|
|
+ JSONObject contentData = new JSONObject();
|
|
|
+ contentData.put("template_id",TEMPLATE_ID);
|
|
|
+ contentData.put("template_version_name", TEMPLATE_VERSION);
|
|
|
+ contentData.put("template_variable", template_variable);
|
|
|
+ content.put("type", "template");
|
|
|
+ content.put("data", contentData);
|
|
|
+
|
|
|
+ JSONObject request = new JSONObject();
|
|
|
+ request.put("receive_id", receive_id);
|
|
|
+ request.put("msg_type", "interactive");
|
|
|
+ request.put("content", content.toJSONString());
|
|
|
+ //发送
|
|
|
+ try (CloseableHttpClient client = HttpClients.createDefault()){
|
|
|
+ HttpPost httpPost = new HttpPost("https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=user_id");
|
|
|
+ httpPost.setHeader("Authorization", "Bearer "+token);
|
|
|
+ HttpEntity entity = new StringEntity(request.toJSONString(), "UTF-8");
|
|
|
+ httpPost.setEntity(entity);
|
|
|
+ httpPost.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
|
+ CloseableHttpResponse respon = client.execute(httpPost);
|
|
|
+ System.out.println("发送结果: "+ EntityUtils.toString(respon.getEntity(), "UTF-8"));
|
|
|
+ }catch (IOException e){
|
|
|
+ log.error("发送飞书消息失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|