|
@@ -0,0 +1,198 @@
|
|
|
|
+package org.jeecg.modules.ctop.service.impl;
|
|
|
|
+
|
|
|
|
+import cn.com.ctop.common.module.entity.CtopOauthToken;
|
|
|
|
+import cn.com.ctop.common.module.entity.MaterialInfo;
|
|
|
|
+import cn.com.ctop.common.module.entity.MaterialParameter;
|
|
|
|
+import cn.com.ctop.common.module.entity.UserAllocation;
|
|
|
|
+import cn.com.ctop.common.module.enums.MaterialEnum;
|
|
|
|
+import cn.com.ctop.common.module.mapper.CtopOauthTokenMapper;
|
|
|
|
+import cn.com.ctop.common.module.mapper.MaterialParameterMapper;
|
|
|
|
+import cn.com.ctop.common.module.service.IUserAllocationService;
|
|
|
|
+import cn.com.ctop.common.module.utils.Check;
|
|
|
|
+import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
|
|
|
|
+import cn.com.ctop.common.module.utils.LoadFileUtil;
|
|
|
|
+import cn.com.ctop.common.module.utils.PropertiesUtils;
|
|
|
|
+import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouVideoGet;
|
|
|
|
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouVideoGetService;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.http.ParseException;
|
|
|
|
+import org.jeecg.modules.ctop.service.IKuaiShouUploadService;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.core.io.FileSystemResource;
|
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
|
+import java.util.concurrent.Executors;
|
|
|
|
+
|
|
|
|
+@Slf4j
|
|
|
|
+@Service
|
|
|
|
+public class KuaiShouUploadServiceImpl implements IKuaiShouUploadService {
|
|
|
|
+ private static ExecutorService executorService = Executors.newFixedThreadPool(6);
|
|
|
|
+
|
|
|
|
+ @Value("${oss.replace.replace-value}")
|
|
|
|
+ private String replaceValue;
|
|
|
|
+ @Value("${oss.replace.replace-old-value}")
|
|
|
|
+ private String replaceOldValue;
|
|
|
|
+ @Value("${oss.replace.download}")
|
|
|
|
+ private String downloadUrl;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private IUserAllocationService allocationService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private CtopOauthTokenMapper oauthTokenMapper;
|
|
|
|
+ @Autowired
|
|
|
|
+ private MaterialParameterMapper parameterMapper;
|
|
|
|
+ @Autowired
|
|
|
|
+ private IKuaiShouVideoGetService kuaiShouVideoGetService;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void upload(MaterialInfo materialInfo) {
|
|
|
|
+ try {
|
|
|
|
+ String url = materialInfo.getUrl().replace(replaceOldValue, replaceValue);
|
|
|
|
+ log.info("replaceUrl:{}", url);
|
|
|
|
+ String localUrl = LoadFileUtil.downLoadFromUrl(url, downloadUrl);
|
|
|
|
+ FileSystemResource resource = new FileSystemResource(new File(localUrl));
|
|
|
|
+ Map<String, String> headerMap = new HashMap<>();
|
|
|
|
+ headerMap.put("Content-Type", "multipart/form-data");
|
|
|
|
+ JSONObject requestJson = new JSONObject();
|
|
|
|
+ requestJson.put("file", resource);
|
|
|
|
+ requestJson.put("signature", materialInfo.getCode());
|
|
|
|
+
|
|
|
|
+ QueryWrapper<MaterialParameter> parameterQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ parameterQueryWrapper.eq("material_id", materialInfo.getCode());
|
|
|
|
+ parameterQueryWrapper.last("limit 1");
|
|
|
|
+ Integer type = null;
|
|
|
|
+ MaterialParameter materialParameter = parameterMapper.selectOne(parameterQueryWrapper);
|
|
|
|
+ if (!Check.isNull(materialParameter)) {
|
|
|
|
+ String width = materialParameter.getWidth();
|
|
|
|
+ String height = materialParameter.getHeight();
|
|
|
|
+ type = MaterialEnum.getTypeBySize(Integer.valueOf(width), Integer.valueOf(height));
|
|
|
|
+ if (!Check.isNull(type)) {
|
|
|
|
+ requestJson.put("type", type);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ QueryWrapper<UserAllocation> allocationQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ allocationQueryWrapper.eq("project_id", materialInfo.getProjectId());
|
|
|
|
+ allocationQueryWrapper.eq("account_status", 0);
|
|
|
|
+ List<UserAllocation> list = allocationService.list(allocationQueryWrapper);
|
|
|
|
+ if (!Check.isNull(list)) {
|
|
|
|
+ for (UserAllocation allocation : list) {
|
|
|
|
+ Long accountId = allocation.getAccountId();
|
|
|
|
+ QueryWrapper<CtopOauthToken> tokenQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ tokenQueryWrapper.eq("account_id", accountId);
|
|
|
|
+ tokenQueryWrapper.eq("media_id", 2);
|
|
|
|
+ tokenQueryWrapper.orderByDesc("create_time");
|
|
|
|
+ tokenQueryWrapper.last("limit 1");
|
|
|
|
+ CtopOauthToken ctopOauthToken = oauthTokenMapper.selectOne(tokenQueryWrapper);
|
|
|
|
+ if (Check.isNull(ctopOauthToken)) {
|
|
|
|
+ log.error("快手账户信息为空,accountId:{}", accountId);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ QueryWrapper<KuaiShouVideoGet> videoGetQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ videoGetQueryWrapper.eq("account_id", accountId);
|
|
|
|
+ videoGetQueryWrapper.eq("signature", materialInfo.getCode());
|
|
|
|
+ videoGetQueryWrapper.last("limit 1");
|
|
|
|
+ KuaiShouVideoGet checkVideoGet = kuaiShouVideoGetService.getOne(videoGetQueryWrapper);
|
|
|
|
+ if (!Check.isNull(checkVideoGet)) {
|
|
|
|
+ log.info("此素材已同步,accountId:{},code:{}", accountId, materialInfo.getCode());
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ requestJson.put("advertiser_id", accountId);
|
|
|
|
+ headerMap.put("Access-Token", ctopOauthToken.getAccessToken());
|
|
|
|
+ Integer finalType = type;
|
|
|
|
+ executorService.submit(new Runnable() {
|
|
|
|
+ @Override
|
|
|
|
+ public void run() {
|
|
|
|
+ try {
|
|
|
|
+ log.info("快手素材自动同步,accountId:{},code:{}", accountId, materialInfo.getCode());
|
|
|
|
+ String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.VIDEO_UPLOAD;
|
|
|
|
+ String result = exceptInfoForRestTemplate(url, requestJson, headerMap);
|
|
|
|
+ JSONObject resultJson = JSONObject.parseObject(result);
|
|
|
|
+ if (!Check.isNull(resultJson)) {
|
|
|
|
+ if (resultJson.getInteger("code") == 0) {
|
|
|
|
+ JSONObject dataJson = resultJson.getJSONObject("data");
|
|
|
|
+ if (!Check.isNull(dataJson)) {
|
|
|
|
+ String photoId = dataJson.getString("photo_id");
|
|
|
|
+ String signature = dataJson.getString("signature");
|
|
|
|
+ KuaiShouVideoGet videoGet = new KuaiShouVideoGet();
|
|
|
|
+ videoGet.setAccountId(accountId);
|
|
|
|
+ videoGet.setId(accountId + photoId);
|
|
|
|
+ videoGet.setUrl(materialInfo.getUrl());
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ videoGet.setMaterialType(finalType);
|
|
|
|
+ videoGet.setPhotoId(photoId);
|
|
|
|
+ videoGet.setSignature(signature);
|
|
|
|
+ kuaiShouVideoGetService.saveOrUpdate(videoGet);
|
|
|
|
+ }
|
|
|
|
+ log.info("快手素材同步完成,accountId:{},code:{},返回信息:{}", accountId, materialInfo.getCode(), resultJson);
|
|
|
|
+ } else {
|
|
|
|
+ log.error("快手同步素材失败,返回信息:{},请求参数:{}", resultJson, requestJson);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ log.error("快手自动同步素材返回信息为空,accountId:{}", accountId);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*boolean isTrue = LoadFileUtil.delFile(localUrl);
|
|
|
|
+ if (isTrue) {
|
|
|
|
+ log.info("删除本地缓存素材成功,code:{}", materialInfo.getCode());
|
|
|
|
+ }*/
|
|
|
|
+
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private RestTemplate rest;
|
|
|
|
+
|
|
|
|
+ private String exceptInfoForRestTemplate(String url, Map<String, Object> paramMap, Map<String, String> headerMap) throws ParseException {
|
|
|
|
+ try {
|
|
|
|
+ MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
|
|
|
|
+ if (!Check.isNullMap(paramMap)) {
|
|
|
|
+ for (String key : paramMap.keySet()) {
|
|
|
|
+ param.add(key, paramMap.get(key));
|
|
|
|
+ }
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ if (!Check.isNullMap(headerMap)) {
|
|
|
|
+ for (String key : headerMap.keySet()) {
|
|
|
|
+ headers.add(key, headerMap.get(key));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(param, headers);
|
|
|
|
+ ResponseEntity<String> responseEntity = rest.exchange(url, HttpMethod.POST, httpEntity, String.class);
|
|
|
|
+ return responseEntity.getBody();
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|