|
@@ -1,36 +1,46 @@
|
|
|
-package com.tzld.piaoquan.api.service.impl;
|
|
|
+package com.tzld.piaoquan.growth.common.service.Impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.tzld.piaoquan.api.dao.mapper.GuaranteesVideoMapper;
|
|
|
-import com.tzld.piaoquan.api.dao.mapper.MessageAttachmentMapper;
|
|
|
-import com.tzld.piaoquan.api.service.MessageAttachmentService;
|
|
|
-import com.tzld.piaoquan.common.common.base.CommonResponse;
|
|
|
-import com.tzld.piaoquan.common.common.enums.MessageAttachmentTypeEnum;
|
|
|
-import com.tzld.piaoquan.common.component.HttpPoolClient;
|
|
|
-import com.tzld.piaoquan.common.model.bo.VideoDetail;
|
|
|
-import com.tzld.piaoquan.common.model.bo.VideoParam;
|
|
|
-import com.tzld.piaoquan.common.model.po.GuaranteesVideo;
|
|
|
-import com.tzld.piaoquan.common.model.po.GuaranteesVideoExample;
|
|
|
-import com.tzld.piaoquan.common.model.po.MessageAttachment;
|
|
|
-import com.tzld.piaoquan.common.model.po.MessageAttachmentExample;
|
|
|
-import com.tzld.piaoquan.common.model.vo.GuaranteedParam;
|
|
|
-import com.tzld.piaoquan.common.utils.DateUtil;
|
|
|
-import com.tzld.piaoquan.common.utils.LarkRobotUtil;
|
|
|
+import com.tzld.piaoquan.growth.common.common.base.CommonResponse;
|
|
|
+import com.tzld.piaoquan.growth.common.common.enums.MessageAttachmentTypeEnum;
|
|
|
+import com.tzld.piaoquan.growth.common.component.HttpPoolClient;
|
|
|
+import com.tzld.piaoquan.growth.common.dao.mapper.GuaranteesVideoMapper;
|
|
|
+import com.tzld.piaoquan.growth.common.dao.mapper.MessageAttachmentMapper;
|
|
|
+import com.tzld.piaoquan.growth.common.model.bo.AdPutFlowParam;
|
|
|
+import com.tzld.piaoquan.growth.common.model.bo.VideoDetail;
|
|
|
+import com.tzld.piaoquan.growth.common.model.bo.VideoParam;
|
|
|
+import com.tzld.piaoquan.growth.common.model.po.*;
|
|
|
+import com.tzld.piaoquan.growth.common.model.vo.GuaranteedParam;
|
|
|
+import com.tzld.piaoquan.growth.common.service.WeComAccessTokenService;
|
|
|
+import com.tzld.piaoquan.growth.common.service.MessageAttachmentService;
|
|
|
+import com.tzld.piaoquan.growth.common.utils.DateUtil;
|
|
|
+import com.tzld.piaoquan.growth.common.utils.LarkRobotUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
+import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Paths;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
-import static com.tzld.piaoquan.common.common.constant.MessageConstant.MAX_VIDEO_NUM;
|
|
|
-import static com.tzld.piaoquan.common.common.constant.MessageConstant.appid;
|
|
|
-import static com.tzld.piaoquan.common.common.constant.OtherServerURL.POST_VIDEO_DETAIL_URL;
|
|
|
-import static com.tzld.piaoquan.common.common.constant.TimeConstant.MILLISECOND_DAY;
|
|
|
+import static com.tzld.piaoquan.growth.common.common.constant.MessageConstant.MAX_VIDEO_NUM;
|
|
|
+import static com.tzld.piaoquan.growth.common.common.constant.MessageConstant.appid;
|
|
|
+import static com.tzld.piaoquan.growth.common.common.constant.OtherServerURL.POST_ADD_TENCENT;
|
|
|
+import static com.tzld.piaoquan.growth.common.common.constant.OtherServerURL.POST_VIDEO_DETAIL_URL;
|
|
|
+import static com.tzld.piaoquan.growth.common.common.constant.TimeConstant.MILLISECOND_DAY;
|
|
|
+import static com.tzld.piaoquan.growth.common.common.constant.WeComConstant.POST_WE_COM_MEDIA_UPLOAD;
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
@@ -40,9 +50,15 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
|
|
|
@Autowired
|
|
|
private HttpPoolClient httpPoolClient;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private WeComAccessTokenService weComAccessTokenService;
|
|
|
+
|
|
|
@Autowired
|
|
|
private MessageAttachmentMapper messageAttachmentMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate<String, Object> redisTemplate;
|
|
|
+
|
|
|
@Autowired
|
|
|
private GuaranteesVideoMapper guaranteesVideoMapper;
|
|
|
|
|
@@ -196,6 +212,28 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
|
|
|
guaranteesVideoMapper.insertSelective(guaranteesVideo);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public GuaranteedParam getGuaranteedVideo(String date) {
|
|
|
+ GuaranteesVideoExample example = new GuaranteesVideoExample();
|
|
|
+ example.createCriteria().andDateEqualTo(date).andIsDeleteEqualTo(0);
|
|
|
+ List<GuaranteesVideo> guaranteesVideos = guaranteesVideoMapper.selectByExample(example);
|
|
|
+ if (CollectionUtils.isEmpty(guaranteesVideos)) {
|
|
|
+ LarkRobotUtil.sendMessage("获保底视频空,@薛一鸣");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ GuaranteedParam guaranteedParam = new GuaranteedParam();
|
|
|
+ guaranteedParam.setDate(date);
|
|
|
+ List<VideoParam> videoParams = new ArrayList<>();
|
|
|
+ for (GuaranteesVideo guaranteesVideo : guaranteesVideos) {
|
|
|
+ VideoParam videoParam = new VideoParam();
|
|
|
+ videoParam.setStaffId(guaranteesVideo.getStaffId());
|
|
|
+ List<Long> videoIds = JSONArray.parseArray(guaranteesVideo.getVideoIds(), Long.class);
|
|
|
+ videoParam.setVideoIds(videoIds);
|
|
|
+ videoParams.add(videoParam);
|
|
|
+ }
|
|
|
+ guaranteedParam.setVideoParamList(videoParams);
|
|
|
+ return guaranteedParam;
|
|
|
+ }
|
|
|
|
|
|
public Map<Long, VideoDetail> getVideoDetail(Set<Long> videoIdList) {
|
|
|
try {
|
|
@@ -222,5 +260,136 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
|
|
|
return new HashMap<>();
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public String getPicMediaId(String cover, Long corpId) {
|
|
|
+ String mediaId = (String) redisTemplate.opsForValue().get(cover);
|
|
|
+ if (StringUtils.isNotEmpty(mediaId)) {
|
|
|
+ return mediaId;
|
|
|
+ }
|
|
|
+ String filePath = UUID.randomUUID() + ".jpg"; // 临时文件路径
|
|
|
+ try {
|
|
|
+ HttpURLConnection httpUrl = (HttpURLConnection) new URL(cover).openConnection();
|
|
|
+ httpUrl.connect();
|
|
|
+ InputStream inputStream = httpUrl.getInputStream();
|
|
|
+
|
|
|
+ // 将文件内容写入临时文件
|
|
|
+ try (OutputStream outputStream = Files.newOutputStream(Paths.get(filePath))) {
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
+ int bytesRead;
|
|
|
+ while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
+ outputStream.write(buffer, 0, bytesRead);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ inputStream.close();
|
|
|
+ httpUrl.disconnect();
|
|
|
+ File file = new File(filePath);
|
|
|
+ String weComAccessToken = weComAccessTokenService.getWeComAccessToken(corpId);
|
|
|
+ String url = String.format(POST_WE_COM_MEDIA_UPLOAD + "?access_token=%s&type=%s", weComAccessToken, "image");
|
|
|
+ String res = httpPoolClient.post(url, file);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
+ if (jsonObject != null && jsonObject.getInteger("errcode") == 0) {
|
|
|
+ mediaId = jsonObject.getString("media_id");
|
|
|
+ redisTemplate.opsForValue().set(cover, mediaId, 2, TimeUnit.DAYS);
|
|
|
+ }
|
|
|
+ Files.delete(Paths.get(filePath));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("getPicMediaId error", e);
|
|
|
+ }
|
|
|
+ return mediaId;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
+ @Override
|
|
|
+ public String getPage(Staff staff, Long videoId) {
|
|
|
+ try {
|
|
|
+ if (staff == null || staff.getCarrierId() == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ String page = selectPage(videoId, staff.getCarrierId(), "企微", "日常推送");
|
|
|
+ if (StringUtils.isNotEmpty(page)) {
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+ return insertPage(staff.getCarrierId(), videoId, staff.getRemark());
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("MessageAttachmentService getPage error", e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String insertPage(String carrierId, Long videoId, String remark) throws IOException {
|
|
|
+ AdPutFlowParam param = new AdPutFlowParam();
|
|
|
+ param.setChannel("tencentqw");
|
|
|
+ param.setPath("pages/category.html");
|
|
|
+ param.setPutCarrierId(carrierId);
|
|
|
+ param.setPutScene("touliu");
|
|
|
+ param.setPutStartDate(DateUtil.getDayDateString("yyyy-MM-dd"));
|
|
|
+ param.setPutTypeOne("企微");
|
|
|
+ param.setPutTypeTwo("日常推送");
|
|
|
+ param.setPutTypeThree("位置X");
|
|
|
+ param.setRemark(remark);
|
|
|
+ param.setVideoId(videoId);
|
|
|
+ Map<String, String> requestParam = new HashMap<>();
|
|
|
+ String jumpPage = "pages/user-videos?id=${videoId}&fromGzh=1&rootShareId=${uuid}&shareId=${uuid}&rootSourceId=[rootSourceId]"
|
|
|
+ .replace("${videoId}", "" + videoId)
|
|
|
+ .replace("${uuid}", "" + UUID.randomUUID());
|
|
|
+ requestParam.put("jumpPage", jumpPage);
|
|
|
+ param.setRequestParam(requestParam);
|
|
|
+ String res = httpPoolClient.post(POST_ADD_TENCENT, JSONObject.toJSONString(param));
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
+ JSONObject data = jsonObject.getJSONObject("data");
|
|
|
+ return data.getString("url");
|
|
|
+ }
|
|
|
+
|
|
|
+ public String selectPage(Long videoId, String carrierId, String typeOne, String typeTwo) {
|
|
|
+ int totalPage = 1;
|
|
|
+ for (int n = 1; n <= totalPage; n++) {
|
|
|
+ String url = getUrl(videoId, n);
|
|
|
+ try {
|
|
|
+ String res = httpPoolClient.get(url);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
+ JSONObject data = jsonObject.getJSONObject("data");
|
|
|
+ if (data == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (data.getInteger("totalPage") != null) {
|
|
|
+ totalPage = data.getInteger("totalPage");
|
|
|
+ }
|
|
|
+ JSONArray objs = data.getJSONArray("objs");
|
|
|
+ for (int i = 0; i < objs.size(); i++) {
|
|
|
+ JSONObject obj = objs.getJSONObject(i);
|
|
|
+ String putCarrierId = obj.getString("putCarrierId");
|
|
|
+ String page = obj.getString("url");
|
|
|
+ String putTypeOne = obj.getString("putTypeOne");
|
|
|
+ String putTypeTwo = obj.getString("putTypeTwo");
|
|
|
+
|
|
|
+ if (StringUtils.isEmpty(putCarrierId) || StringUtils.isEmpty(page) || StringUtils.isEmpty(putTypeOne) || StringUtils.isEmpty(putTypeTwo)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (putCarrierId.equals(carrierId) && putTypeOne.equals(typeOne) && putTypeTwo.equals(typeTwo)) {
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("Error fetching data from API for videoId: " + videoId + ", page: " + n, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getUrl(Long videoId, int n) {
|
|
|
+ String baseUrl = "https://api.piaoquantv.com/ad/put/flow/list/tencent";
|
|
|
+ String putScene = "touliu";
|
|
|
+ String channel = "tencentqw";
|
|
|
+ int pageSize = 20;
|
|
|
+ return String.format(
|
|
|
+ "%s?videoId=%d&putScene=%s&channel=%s¤tPage=%d&pageSize=%d",
|
|
|
+ baseUrl,
|
|
|
+ videoId,
|
|
|
+ putScene,
|
|
|
+ channel,
|
|
|
+ n,
|
|
|
+ pageSize
|
|
|
+ );
|
|
|
+ }
|
|
|
}
|