|
@@ -1,35 +1,56 @@
|
|
|
package cn.com.ctop.kuaishou.modules.batch.service.impl;
|
|
|
|
|
|
import cn.com.ctop.common.module.entity.CtopOauthToken;
|
|
|
+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.UserAllocationMapper;
|
|
|
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
|
|
|
-import cn.com.ctop.common.module.utils.Check;
|
|
|
-import cn.com.ctop.common.module.utils.RandomUtil;
|
|
|
-import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouCampaign;
|
|
|
-import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouCreative;
|
|
|
-import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouGroup;
|
|
|
-import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouGroupTarget;
|
|
|
+import cn.com.ctop.common.module.service.IMaterialParameterService;
|
|
|
+import cn.com.ctop.common.module.utils.*;
|
|
|
+import cn.com.ctop.kuaishou.modules.batch.entity.*;
|
|
|
import cn.com.ctop.kuaishou.modules.batch.mapper.*;
|
|
|
import cn.com.ctop.kuaishou.modules.batch.service.IBatchService;
|
|
|
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouImageGetService;
|
|
|
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouUpdateService;
|
|
|
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
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.common.util.DateUtils;
|
|
|
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.math.BigDecimal;
|
|
|
import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class BatchServiceImpl implements IBatchService {
|
|
|
+
|
|
|
+ @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 BatchMapper batchMapper;
|
|
|
@Autowired
|
|
@@ -718,7 +739,7 @@ public class BatchServiceImpl implements IBatchService {
|
|
|
QueryWrapper<KuaiShouCreative> queryWrapper = new QueryWrapper<>();
|
|
|
queryWrapper.eq("account_id", accountId);
|
|
|
queryWrapper.eq("unit_id", copyUnitId);
|
|
|
- // queryWrapper.eq("status", 52);
|
|
|
+ queryWrapper.eq("status", 52);
|
|
|
List<KuaiShouCreative> creativeList = creativeMapper.selectList(queryWrapper);
|
|
|
if (!Check.isNull(creativeList)) {
|
|
|
for (KuaiShouCreative creative : creativeList) {
|
|
@@ -799,6 +820,10 @@ public class BatchServiceImpl implements IBatchService {
|
|
|
* @param requestJson
|
|
|
* @return
|
|
|
*/
|
|
|
+ @Autowired
|
|
|
+ private IKuaiShouImageGetService imageGetService;
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public JSONObject createCreative(JSONObject requestJson) throws Exception {
|
|
|
if (Check.isNull(requestJson)) {
|
|
@@ -845,20 +870,41 @@ public class BatchServiceImpl implements IBatchService {
|
|
|
if (!Check.isNull(dataJsons)) {
|
|
|
String description = dataJson.getString("description");
|
|
|
String photo_id = dataJson.getString("photoId");
|
|
|
- JSONArray imageTokens = dataJson.getJSONArray("imageTokens");
|
|
|
- if (!Check.isNull(imageTokens)) {
|
|
|
- for (int j = 0; j < imageTokens.size(); j++) {
|
|
|
- JSONObject imageJson = imageTokens.getJSONObject(j);
|
|
|
+ JSONArray imageArr = dataJson.getJSONArray("imageArr");
|
|
|
+ if (!Check.isNull(imageArr)) {
|
|
|
+ for (int j = 0; j < imageArr.size(); j++) {
|
|
|
+ JSONObject imageJson = imageArr.getJSONObject(j);
|
|
|
if (!Check.isNull(imageJson)) {
|
|
|
- String image = imageJson.getString("image");
|
|
|
+
|
|
|
String name = imageJson.getString("name");
|
|
|
creativeJson.put("action_bar_text", action_bar_text);
|
|
|
creativeJson.put("description", description);
|
|
|
- creativeJson.put("image_token", image);
|
|
|
+
|
|
|
creativeJson.put("creative_name", name);
|
|
|
creativeJson.put("photo_id", photo_id);
|
|
|
creativeJson.put("click_track_url", click_track_url);
|
|
|
|
|
|
+ String imageToken = null;
|
|
|
+ String signature = imageJson.getString("signature");
|
|
|
+ QueryWrapper<KuaiShouImageGet> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("account_id", accountId);
|
|
|
+ queryWrapper.eq("signature", signature);
|
|
|
+ queryWrapper.last("limit 1");
|
|
|
+ KuaiShouImageGet imageGet = imageGetService.getOne(queryWrapper);
|
|
|
+ if (!Check.isNull(imageGet)) {
|
|
|
+ imageToken = imageGet.getImageToken();
|
|
|
+ } else {
|
|
|
+ String url = imageGetService.getUrlByCode(signature);
|
|
|
+ imageToken = this.kuauiShouImageUpload(url, signature, accountId, oauthToken.getAccessToken());
|
|
|
+ }
|
|
|
+ if (Check.isNull(imageToken)) {
|
|
|
+ JSONObject failJson = new JSONObject();
|
|
|
+ failJson.put("creativeName", name);
|
|
|
+ failJson.put("failMessage", "获取图片文件失败");
|
|
|
+ failArr.add(failJson);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ creativeJson.put("image_token", imageToken);
|
|
|
Map<String, Object> returnUnitMap = kuaishouInterfaceService.creativeCreate(oauthToken.getAccessToken(), accountId, creativeJson);
|
|
|
if (!Check.isNullMap(returnUnitMap)) {
|
|
|
Integer code = (Integer) returnUnitMap.get("code");
|
|
@@ -1142,6 +1188,9 @@ public class BatchServiceImpl implements IBatchService {
|
|
|
* @param requestJson
|
|
|
* @return
|
|
|
*/
|
|
|
+ @Autowired
|
|
|
+ private IKuaiShouImageGetService iKuaiShouImageGetService;
|
|
|
+
|
|
|
@Override
|
|
|
public JSONObject updateCreative(JSONObject requestJson) throws Exception {
|
|
|
if (Check.isNull(requestJson)) {
|
|
@@ -1220,6 +1269,120 @@ public class BatchServiceImpl implements IBatchService {
|
|
|
return returnJson;
|
|
|
}
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IMaterialParameterService materialParameterService;
|
|
|
+
|
|
|
+ public String kuauiShouImageUpload(String url, String signature, Long accountId, String token) {
|
|
|
+ System.err.println(url);
|
|
|
+ System.err.println(signature);
|
|
|
+ try {
|
|
|
+ // String replaceUrl = url.replace(replaceOldValue, replaceValue);
|
|
|
+ // log.info("replaceUrl:{}", replaceUrl);
|
|
|
+ /* 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", signature);
|
|
|
+ requestJson.put("url", url);
|
|
|
+ requestJson.put("type", 2);
|
|
|
+ /* QueryWrapper<MaterialParameter> parameterQueryWrapper = new QueryWrapper<>();
|
|
|
+ parameterQueryWrapper.eq("material_id", signature);
|
|
|
+ parameterQueryWrapper.orderByDesc("create_time").last("limit 1");
|
|
|
+ MaterialParameter materialParameter = materialParameterService.getOne(parameterQueryWrapper);
|
|
|
+ if (!Check.isNull(materialParameter)) {
|
|
|
+ Integer type = MaterialEnum.getTypeBySize(Integer.valueOf(materialParameter.getWidth()), Integer.valueOf(materialParameter.getHeight()));
|
|
|
+ if (!Check.isNull(type) && type != 0) {
|
|
|
+ requestJson.put("type", type);
|
|
|
+ }
|
|
|
+ }*/
|
|
|
+
|
|
|
+ requestJson.put("advertiser_id", accountId);
|
|
|
+ headerMap.put("Access-Token", token);
|
|
|
+ String requestUrl = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.IMAGE_UPLOAD;
|
|
|
+ requestJson.put("upload_type", "2");
|
|
|
+ String result = exceptInfoForRestTemplate(requestUrl, requestJson, headerMap);
|
|
|
+ JSONObject resultJson = JSONObject.parseObject(result);
|
|
|
+ System.err.println(resultJson);
|
|
|
+ if (!Check.isNull(resultJson)) {
|
|
|
+ if (resultJson.getInteger("code") == 0) {
|
|
|
+ JSONObject dataJson = resultJson.getJSONObject("data");
|
|
|
+ if (!Check.isNull(dataJson)) {
|
|
|
+ String image_token = dataJson.getString("image_token");
|
|
|
+ KuaiShouImageGet imageGet = new KuaiShouImageGet();
|
|
|
+ imageGet.setId(accountId + image_token);
|
|
|
+ imageGet.setAccountId(accountId);
|
|
|
+ imageGet.setUrl(dataJson.getString("url"));
|
|
|
+ imageGet.setWidth(dataJson.getInteger("width"));
|
|
|
+ imageGet.setHeight(dataJson.getInteger("height"));
|
|
|
+ imageGet.setSize(dataJson.getInteger("size"));
|
|
|
+ imageGet.setFormat(dataJson.getString("format"));
|
|
|
+ Integer type = MaterialEnum.getTypeBySize(dataJson.getInteger("width"), dataJson.getInteger("height"));
|
|
|
+ if (!Check.isNull(type)) {
|
|
|
+ imageGet.setMaterialType(type);
|
|
|
+ }
|
|
|
+ imageGet.setSignature(signature);
|
|
|
+ imageGet.setImageToken(image_token);
|
|
|
+ iKuaiShouImageGetService.saveOrUpdate(imageGet);
|
|
|
+ return dataJson.getString("image_token");
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } else {
|
|
|
+ log.error("上传图片失败,accountId:{},code:{},data:{}", accountId, signature, resultJson);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } else {
|
|
|
+ log.error("上传图片,返回结果为空,accountId:{},code:{},data:{}", accountId, signature, resultJson);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) throws IOException {
|
|
|
+ String localUrl = LoadFileUtil.downLoadFromUrl("https://ctop-media.oss-cn-beijing.aliyuncs.com/image/2020-04-29/uuuuu-1588129352054.jpg", PropertiesUtils.getValue("kuaishou_config", "video_sava_path"));
|
|
|
+ String signature = LoadFileUtil.getMD5(localUrl);
|
|
|
+ System.err.println(signature);
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|