|
|
@@ -0,0 +1,175 @@
|
|
|
+package cn.com.ctop.job.bytedance.data.service.impl;
|
|
|
+
|
|
|
+import cn.com.ctop.job.bytedance.data.mapper.ReportsAdPushMapper;
|
|
|
+import cn.com.ctop.job.bytedance.data.service.IDateReportsPushService;
|
|
|
+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 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.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class DateReportsPushServiceImpl implements IDateReportsPushService {
|
|
|
+ public static final String APP_ID = "cli_a772633aaeff5013";
|
|
|
+ public static final String APP_KEY = "lEHaMdEoBVrrLNc5RtC11f8XsANnKgB4";
|
|
|
+ @Autowired
|
|
|
+ ReportsAdPushMapper reportsPushMapper;
|
|
|
+
|
|
|
+ private static final String TEMPLATE_VERSION = "1.0.2";
|
|
|
+ private static final String TEMPLATE_ID = "AAqRhJzMBoO0E";
|
|
|
+ @Override
|
|
|
+ public void send() {
|
|
|
+ String token;
|
|
|
+ try {
|
|
|
+ token = FeishuRobotUtil.getTenantToken(APP_ID, APP_KEY);
|
|
|
+ }catch (IOException e){
|
|
|
+ log.error("获取飞书token失败", e);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //日期时间 - 统统以昨日为准
|
|
|
+ String today = DateUtils.getNowAddDate("yyyy-MM-dd", -1);
|
|
|
+ String lastDay = DateUtils.getNowAddDate("yyyy-MM-dd", -2);
|
|
|
+ Long todayLong = Long.parseLong(today.replaceAll("-", ""));
|
|
|
+ Long lastDayLong = Long.parseLong(lastDay.replaceAll("-", ""));
|
|
|
+
|
|
|
+ Map<String, String> todayData = reportsPushMapper.tmpPushData(todayLong);
|
|
|
+ Map<String, String> lastDayData = reportsPushMapper.tmpPushData(lastDayLong);
|
|
|
+
|
|
|
+ String updateTime = String.valueOf(todayData.get("update_time"));
|
|
|
+ List<Pair<String, String>> columns = Arrays.asList(
|
|
|
+ new Pair<>("cost_amount", "消耗金额"),
|
|
|
+ new Pair<>("end_amount", "结算金额"),
|
|
|
+ new Pair<>("pay_amount", "充值金额"),
|
|
|
+ new Pair<>("water_roi", "流水ROI"),
|
|
|
+ new Pair<>("real_roi", "真实ROI"),
|
|
|
+ new Pair<>("water_profit", "流水利润"),
|
|
|
+ new Pair<>("real_profit", "真实利润")
|
|
|
+ );
|
|
|
+ JSONArray dateIndicatorTable = new JSONArray();
|
|
|
+ for (Pair<String, String> column : columns) {
|
|
|
+ String key = column.getKey();
|
|
|
+ String name = column.getValue();
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
+ if (!name.contains("ROI")){
|
|
|
+ String todayDataValue = String.valueOf(todayData.getOrDefault(key, "0"));
|
|
|
+ String lastDayDataValue = String.valueOf(lastDayData.getOrDefault(key, "0"));
|
|
|
+ obj.put("indicatorName", name);
|
|
|
+ obj.put("todayData", todayDataValue);
|
|
|
+ obj.put("lastDayData", lastDayDataValue);
|
|
|
+ double todayDataDouble = Double.parseDouble(todayDataValue);
|
|
|
+ double lastDayDataDouble = Double.parseDouble(lastDayDataValue);
|
|
|
+ if (lastDayDataDouble != 0) {
|
|
|
+ double rate = (todayDataDouble - lastDayDataDouble) / lastDayDataDouble * 100;
|
|
|
+ String rateStr = String.format("%.2f%%", rate);
|
|
|
+ obj.put("roundRate", rateStr);
|
|
|
+ if (rate > 0.0){
|
|
|
+ obj.put("rateColor", "red");
|
|
|
+ }else if (rate < 0.0){
|
|
|
+ obj.put("rateColor", "green");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ obj.put("roundRate", "0.00%");
|
|
|
+ obj.put("rateColor", "black");
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ String todayDataValue = String.valueOf(todayData.get(key));
|
|
|
+ String lastDayDataValue = String.valueOf(lastDayData.get(key));
|
|
|
+ obj.put("indicatorName", name);
|
|
|
+ obj.put("todayData", todayDataValue);
|
|
|
+ obj.put("lastDayData", lastDayDataValue);
|
|
|
+ obj.put("roundRate", "-");
|
|
|
+ obj.put("rateColor", "black");
|
|
|
+ }
|
|
|
+ dateIndicatorTable.add(obj);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Map<String, String>> movieData = reportsPushMapper.tmpTopMovie(todayLong);
|
|
|
+ JSONArray movieRankTable = new JSONArray();
|
|
|
+ for (Map<String, String> movie : movieData){
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
+ obj.put("rank", String.valueOf(movie.get("rk")));
|
|
|
+ obj.put("movieName", String.valueOf(movie.get("movie_name")));
|
|
|
+ obj.put("endAmount", String.valueOf(movie.get("end_amount")));
|
|
|
+ obj.put("realROI", String.valueOf(movie.get("real_roi")));
|
|
|
+ movieRankTable.add(obj);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Map<String, String>> deptData = reportsPushMapper.deptRank2(todayLong);
|
|
|
+ JSONArray deptRank = new JSONArray();
|
|
|
+ for (Map<String, String> dept : deptData){
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
+ obj.put("rank", String.valueOf(dept.get("rk")));
|
|
|
+ obj.put("deptName", String.valueOf(dept.get("dept_name")));
|
|
|
+ obj.put("todayCost", String.valueOf(dept.get("cost_amount")));
|
|
|
+ obj.put("ROI", String.valueOf(dept.get("real_roi")));
|
|
|
+ deptRank.add(obj);
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONObject template_variable = new JSONObject();
|
|
|
+ template_variable.put("updateTime", updateTime);
|
|
|
+ template_variable.put("todayDate", today);
|
|
|
+ template_variable.put("lastDayDate", lastDay);
|
|
|
+ template_variable.put("dateIndicatorTable", dateIndicatorTable);
|
|
|
+ template_variable.put("movieRankTable", movieRankTable);
|
|
|
+ template_variable.put("deptRank", deptRank);
|
|
|
+
|
|
|
+ List<String> userIds = Arrays.asList(
|
|
|
+ "44fbaed6" //zhaotx
|
|
|
+ ,"ebeeced9" //yumeng
|
|
|
+ ,"265g9ef1" //wangshu
|
|
|
+ ,"ee518161" //zhaozhicheng
|
|
|
+ ,"c5f6b59d" //yuyang
|
|
|
+ ,"d834552b" //hemenghao
|
|
|
+ ,"a3235a61" //louzijun
|
|
|
+ ,"58e7a98a" //xsq
|
|
|
+ );
|
|
|
+ for (String userId : userIds) {
|
|
|
+ sendToFeishu(token, template_variable, userId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|