|
|
@@ -0,0 +1,194 @@
|
|
|
+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.ICutGroupRankPushService;
|
|
|
+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 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;
|
|
|
+
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class CutGroupRankPushServiceImpl implements ICutGroupRankPushService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ReportsAdPushMapper reportsPushMapper;
|
|
|
+
|
|
|
+ public static final String TEMPLATE_ID = "AAq4GJEHIXd03";
|
|
|
+ public static final String TEMPLATE_VERSION = "1.0.5";
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void send() {
|
|
|
+ List<String> userIds = Arrays.asList(
|
|
|
+ "44fbaed6"
|
|
|
+ ,"c5f6b59d" //yuyang
|
|
|
+ ,"58e7a98a" //xsq
|
|
|
+ ,"ebeeced9" //yumeng
|
|
|
+ ,"aca3ef75" //renyupeng
|
|
|
+ );
|
|
|
+
|
|
|
+ List<String> chatids = Arrays.asList(
|
|
|
+ );
|
|
|
+ send(userIds, chatids);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param userIds 单聊用户列表
|
|
|
+ * @param chatids 群聊用户列表
|
|
|
+ */
|
|
|
+ private void send(List<String> userIds, List<String> chatids){
|
|
|
+ List<JSONObject> users = reportsPushMapper.selectUserList();
|
|
|
+ List<String> mobiles = users.stream()
|
|
|
+ .filter(user -> (user.getLong("dept_id")==30205L || user.getLong("dept_id")==207L) && user.getString("phonenumber") != null)
|
|
|
+ .map(user -> user.getString("phonenumber"))
|
|
|
+ .filter(s -> s != null && s.length() == 11)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ String token;
|
|
|
+ Map<String, String> mobileMap;
|
|
|
+ try {
|
|
|
+ token = FeishuRobotUtil.getFsZtTenantToken();
|
|
|
+ mobileMap = FeishuRobotUtil.getFsUserIds(token, mobiles); //mobile -> userId
|
|
|
+ if (mobileMap == null){
|
|
|
+ log.error("获取飞书用户ID失败");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }catch (IOException e){
|
|
|
+ log.error("获取飞书token失败", e);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String yesterday = DateUtils.getNowAddDate("yyyy-MM-dd", -1);
|
|
|
+ Long yesterdayLong = Long.parseLong(yesterday.replace("-", ""));
|
|
|
+ List<Map<String, String>> cutDeptRank = reportsPushMapper.cutDeptRank(yesterdayLong);
|
|
|
+ JSONArray object_list_1 = new JSONArray();
|
|
|
+ for (Map<String, String> cutDept : cutDeptRank) {
|
|
|
+ JSONObject object = new JSONObject();
|
|
|
+ object.put("dept_name", cutDept.get("dept_name"));
|
|
|
+ object.put("todayData", cutDept.get("todayData"));
|
|
|
+ object.put("ranks", cutDept.get("ranks"));
|
|
|
+ object_list_1.add(object);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Map<String, String>> cutUserRank1 = reportsPushMapper.cutUserRank(yesterdayLong, 30205L);
|
|
|
+ JSONArray object_list_2 = new JSONArray();
|
|
|
+ for (Map<String, String> cutUser : cutUserRank1) {
|
|
|
+ JSONObject object = new JSONObject();
|
|
|
+ String phoneNumber = cutUser.get("phonenumber");
|
|
|
+ object.put("user_id", Arrays.asList(mobileMap.get(phoneNumber)));
|
|
|
+ object.put("rankTodayData", cutUser.get("rankTodayData"));
|
|
|
+ object.put("rankRank", cutUser.get("rankRank"));
|
|
|
+ object_list_2.add(object);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Map<String, String>> cutUserRank2 = reportsPushMapper.cutUserRank(yesterdayLong, 207L);
|
|
|
+ JSONArray object_list_3 = new JSONArray();
|
|
|
+ for (Map<String, String> cutUser : cutUserRank2) {
|
|
|
+ JSONObject object = new JSONObject();
|
|
|
+ String phoneNumber = cutUser.get("phonenumber");
|
|
|
+ object.put("user_id", Arrays.asList(mobileMap.get(phoneNumber)));
|
|
|
+ object.put("rankTodayData", cutUser.get("rankTodayData"));
|
|
|
+ object.put("rankRank", cutUser.get("rankRank"));
|
|
|
+ object_list_3.add(object);
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONObject template_variable = new JSONObject();
|
|
|
+ template_variable.put("object_list_1", object_list_1);
|
|
|
+ template_variable.put("object_list_2", object_list_2);
|
|
|
+ template_variable.put("object_list_3", object_list_3);
|
|
|
+ template_variable.put("todaydate", yesterday.substring(5, 10));
|
|
|
+ template_variable.put("dataDate", reportsPushMapper.updateTime(yesterdayLong));
|
|
|
+
|
|
|
+ //发送给剪辑组用户
|
|
|
+ for (String userId : mobileMap.values()){
|
|
|
+ System.out.println("发送给用户: "+userId);
|
|
|
+ if (userId == null || userId.isEmpty()){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ sendToFeishu(token, template_variable, userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ //发送给各个负责人
|
|
|
+ for (String userId : userIds) {
|
|
|
+ sendToFeishu(token, template_variable, userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (String chatid : chatids) {
|
|
|
+ sendToChatGroup(token, template_variable, chatid);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|