|
|
@@ -0,0 +1,282 @@
|
|
|
+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.IReportsByBusinessPushService;
|
|
|
+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.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class ReportsByBusinessPushServiceImpl implements IReportsByBusinessPushService {
|
|
|
+
|
|
|
+
|
|
|
+ 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.0";
|
|
|
+ private static final String TEMPLATE_ID = "AAq4oeDXECopo";
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 10点正式推送
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void send() {
|
|
|
+ List<String> userIds = Arrays.asList(
|
|
|
+ "44fbaed6" //zhaotx
|
|
|
+// ,"ebeeced9" //yumeng
|
|
|
+// ,"265g9ef1" //wangshu
|
|
|
+// ,"ee518161" //zhaozhicheng
|
|
|
+// ,"c5f6b59d" //yuyang
|
|
|
+// ,"d834552b" //hemenghao
|
|
|
+// ,"a3235a61" //louzijun
|
|
|
+// ,"58e7a98a" //xsq
|
|
|
+ );
|
|
|
+ List<String> chatid = Arrays.asList(
|
|
|
+ "oc_275f17aa4bf8fae649ef281a2b9250f6"
|
|
|
+ );
|
|
|
+ sendParam("", userIds, chatid);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 9点预推送给开发人员
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void preSend() {
|
|
|
+ List<String> userIds = Arrays.asList(
|
|
|
+ "44fbaed6" //zhaotx
|
|
|
+ ,"ebeeced9" //yumeng
|
|
|
+ ,"58e7a98a" //xsq
|
|
|
+ ,"2ga9669f" //angaoqi
|
|
|
+ ,"43gb7fbc" //huangpingkang
|
|
|
+ );
|
|
|
+ List<String> chatid = Arrays.asList(
|
|
|
+ );
|
|
|
+ sendParam("暂不包含广点通当日数据", userIds, chatid);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void test(){
|
|
|
+ sendParam("", Arrays.asList("44fbaed6"), Collections.EMPTY_LIST);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void sendParam(String description, List<String> userIds, List<String> chatid){
|
|
|
+ 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.allBusinessPushData(todayLong);
|
|
|
+ Map<String, String> lastDayData = reportsPushMapper.allBusinessPushData(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<>("share_iaa_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.allBusinessTopMovie(todayLong);
|
|
|
+ JSONArray movieRankTable = new JSONArray();
|
|
|
+ for (Map<String, String> movie : movieData){
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
+ String movieSource = String.valueOf(movie.get("movie_source"));
|
|
|
+ String movieName = String.valueOf(movie.get("movie_name"));
|
|
|
+ if (!Check.isNull(movieSource) && movieSource.contains("1")){
|
|
|
+ obj.put("movieNameColor", "blue");
|
|
|
+ }else {
|
|
|
+ obj.put("movieNameColor", "grey");
|
|
|
+ }
|
|
|
+ obj.put("rank", String.valueOf(movie.get("rk")));
|
|
|
+ obj.put("movieName", movieName);
|
|
|
+ obj.put("endAmount", String.valueOf(movie.get("share_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 allBusinessData = reportsPushMapper.businessTypeData(todayLong, Arrays.asList(2,4,6));
|
|
|
+ JSONObject b6 = reportsPushMapper.businessTypeData(todayLong, Arrays.asList(6));
|
|
|
+ JSONObject b4 = reportsPushMapper.businessTypeData(todayLong, Arrays.asList(4));
|
|
|
+ JSONObject b2 = reportsPushMapper.businessTypeData(todayLong, Arrays.asList(2));
|
|
|
+ JSONArray businessTypeTable = new JSONArray();
|
|
|
+ JSONObject allObj = new JSONObject();
|
|
|
+ allObj.put("type", "整体");
|
|
|
+ allObj.put("iap_end_amount", allBusinessData.getString("iap_end_amount"));
|
|
|
+ allObj.put("iaa_end_amount", allBusinessData.getString("iaa_end_amount"));
|
|
|
+ businessTypeTable.add(allObj);
|
|
|
+ JSONObject b6Obj = new JSONObject();
|
|
|
+ b6Obj.put("type", "达人");
|
|
|
+ b6Obj.put("iap_end_amount", b6.getString("iap_end_amount"));
|
|
|
+ b6Obj.put("iaa_end_amount", b6.getString("iaa_end_amount"));
|
|
|
+ businessTypeTable.add(b6Obj);
|
|
|
+ JSONObject b4Obj = new JSONObject();
|
|
|
+ b4Obj.put("type", "端原生");
|
|
|
+ b4Obj.put("iap_end_amount", b4.getString("iap_end_amount"));
|
|
|
+ b4Obj.put("iaa_end_amount", b4.getString("iaa_end_amount"));
|
|
|
+ businessTypeTable.add(b4Obj);
|
|
|
+ JSONObject b2Obj = new JSONObject();
|
|
|
+ b2Obj.put("type", "自分销");
|
|
|
+ b2Obj.put("iap_end_amount", b2.getString("iap_end_amount"));
|
|
|
+ b2Obj.put("iaa_end_amount", b2.getString("iaa_end_amount"));
|
|
|
+ businessTypeTable.add(b2Obj);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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);
|
|
|
+ template_variable.put("description", description);
|
|
|
+ template_variable.put("businessTypeList", businessTypeTable);
|
|
|
+
|
|
|
+
|
|
|
+ for (String userId : userIds) {
|
|
|
+ sendToFeishu(token, template_variable, userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (String chat : chatid) {
|
|
|
+ sendToChatGroup(token, template_variable, chat);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void sendToChatGroup(String token, JSONObject template_variable, String chatid) {
|
|
|
+ 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", chatid);
|
|
|
+ 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=chat_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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|