|
@@ -1,616 +0,0 @@
|
|
|
-package cn.com.ctop.kuaishou.modules.ai.service.impl;
|
|
|
-
|
|
|
-import cn.com.ctop.common.module.entity.CtopOauthToken;
|
|
|
-import cn.com.ctop.common.module.entity.MaterialCutFrame;
|
|
|
-import cn.com.ctop.common.module.entity.MaterialImageInfo;
|
|
|
-import cn.com.ctop.common.module.service.ICtopOauthTokenService;
|
|
|
-import cn.com.ctop.common.module.service.IMaterialCutFrameService;
|
|
|
-import cn.com.ctop.common.module.service.IMaterialImageInfoService;
|
|
|
-import cn.com.ctop.kuaishou.modules.ai.entity.AiKuaishouAdvertiserStrategy;
|
|
|
-import cn.com.ctop.kuaishou.modules.ai.entity.AiKuaishouStrategyMapCreative;
|
|
|
-import cn.com.ctop.kuaishou.modules.ai.entity.KuaishouAccountCreativeOverrunInfo;
|
|
|
-import cn.com.ctop.kuaishou.modules.ai.service.IAiKuaishouNewCreateCampaign;
|
|
|
-import cn.com.ctop.kuaishou.modules.ai.service.IAiKuaishouStrategyMapCreativeService;
|
|
|
-import cn.com.ctop.kuaishou.modules.ai.service.IKuaishouAccountCreativeOverrunInfoService;
|
|
|
-import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouImageGet;
|
|
|
-import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouVideoGet;
|
|
|
-import cn.com.ctop.kuaishou.modules.batch.entity.KuaishouVideoRelateCreatives;
|
|
|
-import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouVideoGetMapper;
|
|
|
-import cn.com.ctop.kuaishou.modules.batch.service.*;
|
|
|
-import com.alibaba.fastjson.JSONArray;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.jeecg.common.util.DateUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-import javax.annotation.Resource;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-@Service
|
|
|
-@Slf4j
|
|
|
-public class AiKuaishouNewCreateCampaignServiceImpl implements IAiKuaishouNewCreateCampaign {
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private IKuaiShouVideoGetService videoGetService;
|
|
|
- @Autowired
|
|
|
- private IKuaishouInterfaceService kuaishouInterfaceService;
|
|
|
- @Autowired
|
|
|
- private ICtopOauthTokenService tokenService;
|
|
|
- @Autowired
|
|
|
- private IAiKuaishouStrategyMapCreativeService mapCreativeService;
|
|
|
- @Autowired
|
|
|
- private IMaterialCutFrameService materialCutFrameService;
|
|
|
- @Autowired
|
|
|
- private IMaterialImageInfoService materialImageInfoService;
|
|
|
- @Autowired
|
|
|
- private IKuaiShouImageGetService imageGetService;
|
|
|
- @Autowired
|
|
|
- private IKuaiShouMaterialUploadService materialUploadService;
|
|
|
- @Resource
|
|
|
- private KuaiShouVideoGetMapper videoGetMapper;
|
|
|
- @Autowired
|
|
|
- private IKuaishouVideoRelateCreativesService relateCreativesService;
|
|
|
- @Autowired
|
|
|
- private IKuaishouAccountCreativeOverrunInfoService overrunInfoService;
|
|
|
-
|
|
|
- /**
|
|
|
- * 单应用上新
|
|
|
- * @param strategy
|
|
|
- */
|
|
|
- @Override
|
|
|
- public void kuaishouAiAdsNewMaterialCreativityMultileAppids(AiKuaishouAdvertiserStrategy strategy) {
|
|
|
- Long accountId = strategy.getAccountId();
|
|
|
- CtopOauthToken token = tokenService.getOauthTokenByAccountId(accountId.toString());
|
|
|
- if(null == token){
|
|
|
- log.info("token获取失败=>accountId:{}",accountId);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- kuaishouInterfaceService.getVideoList(token, DateUtils.date2Str(),DateUtils.date2Str());
|
|
|
-
|
|
|
- // 2.2 执行 素造的 视频拉取服务,时间过滤执行 yyyy-MM-dd HH:mm:ss
|
|
|
- // 时间差,单位为分钟
|
|
|
- int timeStart = -10;
|
|
|
- int timeEnd = -5;
|
|
|
- Long now = System.currentTimeMillis();
|
|
|
- String startTime = DateUtils.formatDate(new Date(now + timeStart*60*1000),"yyyy-MM-dd HH:mm:ss");
|
|
|
- String endTime = DateUtils.formatDate(new Date(now + timeEnd*60*1000),"yyyy-MM-dd HH:mm:ss");
|
|
|
- if(null!=strategy.getSingleAppid()&&strategy.getSingleAppid() == 1){
|
|
|
- //单一应用
|
|
|
- }
|
|
|
- // 3、获取5分钟内,ctop_kuaishou_video_get 的视频数据,记为 N,如果有数据则执行下面步骤
|
|
|
- List<KuaiShouVideoGet> newVideos = videoGetService.getNewVideoBetweenTime(accountId,startTime,endTime,strategy.getChannelType(),null,null);
|
|
|
- int newVideosCnt = newVideos.size();
|
|
|
- if (newVideosCnt == 0){
|
|
|
- log.info("账户:{},{} 到 {} 这段时间没有上新素材",accountId, startTime, endTime);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- //2、判断该账户下当天有没有“上新”计划,没有则创建一个“上新”计划
|
|
|
- // time_filter_type 传入 "start_date",与 "end_date" 字段,且此字段为1时,按照创建时间进行筛选
|
|
|
- JSONArray campaignList = kuaishouInterfaceService.getCampaignList(token, new Date(), new Date(),1);
|
|
|
- boolean isHaveNewCampaign = false;
|
|
|
- Long newCampaignId = 0L;
|
|
|
- if (null!=campaignList&&campaignList.size()>0){
|
|
|
- for(int i = 0;i<campaignList.size();i++){
|
|
|
- JSONObject jsonObject = campaignList.getJSONObject(i);
|
|
|
- if (jsonObject.getString("campaign_name").contains("素材上新")){
|
|
|
- isHaveNewCampaign = true;
|
|
|
- newCampaignId = jsonObject.getLong("campaign_id");
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- // 不存在“上新”计划,则创建一个计划
|
|
|
- if (!isHaveNewCampaign){
|
|
|
- Date campaignDate = new Date();
|
|
|
- String timestamp = DateUtils.formatDate(campaignDate,"yyyyMMddHHmmss");
|
|
|
- JSONObject campaignParams = new JSONObject();
|
|
|
- campaignParams.put("advertiser_id",token.getAccountId());
|
|
|
- String campaignName = strategy.getCampaignName().replace("自定义","素材上新")+timestamp;
|
|
|
- campaignParams.put("campaign_name",campaignName);
|
|
|
- campaignParams.put("type",strategy.getCampaignType());
|
|
|
- //TODO 目前设定为不限
|
|
|
- campaignParams.put("day_budget",0L);
|
|
|
- Map<String, Object> campaignCreateResult = kuaishouInterfaceService.campaignCreate(token.getAccessToken(), token.getAccountId(), campaignParams);
|
|
|
- Integer code = (Integer) campaignCreateResult.get("code");
|
|
|
- String message = (String) campaignCreateResult.get("message");
|
|
|
- if(code != 0 ){
|
|
|
- //TODO 写入日志信息表
|
|
|
- log.info("计划创建失败=>message:{}",message);
|
|
|
- return;
|
|
|
- }
|
|
|
- newCampaignId = (Long) campaignCreateResult.get("campaignId");
|
|
|
- }
|
|
|
-
|
|
|
- //3、在“上新”计划中新增广告组,一个素材搭配5张封面,组成一个广告组
|
|
|
- String timestamp = DateUtils.formatDate(new Date(),"yyyyMMddHHmmss");
|
|
|
- String unitNamePrefix = strategy.getGroupName().replace("自定义","素材上新")+ timestamp;
|
|
|
- int unitCnt = 0;
|
|
|
- for(KuaiShouVideoGet videoItem: newVideos){
|
|
|
- KuaishouAccountCreativeOverrunInfo overrunInfo = overrunInfoService.getByParams(accountId,DateUtils.formatDate(new Date()));
|
|
|
- if(null!=overrunInfo){
|
|
|
- return;
|
|
|
- }
|
|
|
- unitCnt ++;
|
|
|
- String unitName = unitNamePrefix + "-" + unitCnt;
|
|
|
- JSONObject unitParams = createUnitParams(token, newCampaignId, strategy, unitName);
|
|
|
- Map<String, Object> unitCreateResult = kuaishouInterfaceService.adUnitCreate(token.getAccessToken(), token.getAccountId(), unitParams,1);
|
|
|
- Integer unitCode = (Integer) unitCreateResult.get("code");
|
|
|
- String unitMessage = (String) unitCreateResult.get("message");
|
|
|
- if(unitCode != 0 ){
|
|
|
- log.info("组创建失败=>accountId:{};message:{}",accountId,unitMessage);
|
|
|
- continue;
|
|
|
- }
|
|
|
- Long unitId = (Long) unitCreateResult.get("unitId");
|
|
|
-
|
|
|
- // 每个组,搭配5个创意
|
|
|
- List<MaterialCutFrame> cutFrameList = materialCutFrameService.getListByVideoSignature(videoItem.getSignature());
|
|
|
- if ((null == cutFrameList||cutFrameList.isEmpty())){
|
|
|
- log.info("未获取到视频封面=>videoSignature:{}",videoItem.getSignature());
|
|
|
- continue;
|
|
|
- }
|
|
|
- JSONObject creativeParams = creativeParams(token, unitId, videoItem, strategy);
|
|
|
- createCreativeByImage(cutFrameList,token, creativeParams, newCampaignId, unitId, videoItem,strategy.getImageCnt());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 单应用上新
|
|
|
- * @param strategy
|
|
|
- */
|
|
|
- @Override
|
|
|
- public void kuaishouAiAdsNewMaterialCreativity(AiKuaishouAdvertiserStrategy strategy) {
|
|
|
- Long accountId = strategy.getAccountId();
|
|
|
- CtopOauthToken token = tokenService.getOauthTokenByAccountId(accountId.toString());
|
|
|
- if(null == token){
|
|
|
- log.info("token获取失败=>accountId:{}",accountId);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- kuaishouInterfaceService.getVideoList(token, DateUtils.date2Str(),DateUtils.date2Str());
|
|
|
-
|
|
|
- // 2.2 执行 素造的 视频拉取服务,时间过滤执行 yyyy-MM-dd HH:mm:ss
|
|
|
- // 时间差,单位为分钟
|
|
|
- int timeStart = -10;
|
|
|
- int timeEnd = -5;
|
|
|
- Long now = System.currentTimeMillis();
|
|
|
- String startTime = DateUtils.formatDate(new Date(now + timeStart*60*1000),"yyyy-MM-dd HH:mm:ss");
|
|
|
- String endTime = DateUtils.formatDate(new Date(now + timeEnd*60*1000),"yyyy-MM-dd HH:mm:ss");
|
|
|
- // 3、获取5分钟内,ctop_kuaishou_video_get 的视频数据,记为 N,如果有数据则执行下面步骤
|
|
|
- List<KuaiShouVideoGet> newVideos = videoGetService.getNewVideoBetweenTime(accountId,startTime,endTime,strategy.getChannelType(),null,null);
|
|
|
- int newVideosCnt = newVideos.size();
|
|
|
- if (newVideosCnt == 0){
|
|
|
- log.info("账户:{},{} 到 {} 这段时间没有上新素材",accountId, startTime, endTime);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- //2、判断该账户下当天有没有“上新”计划,没有则创建一个“上新”计划
|
|
|
- // time_filter_type 传入 "start_date",与 "end_date" 字段,且此字段为1时,按照创建时间进行筛选
|
|
|
- JSONArray campaignList = kuaishouInterfaceService.getCampaignList(token, new Date(), new Date(),1);
|
|
|
- boolean isHaveNewCampaign = false;
|
|
|
- Long newCampaignId = 0L;
|
|
|
- if (null!=campaignList&&campaignList.size()>0){
|
|
|
- for(int i = 0;i<campaignList.size();i++){
|
|
|
- JSONObject jsonObject = campaignList.getJSONObject(i);
|
|
|
- if (jsonObject.getString("campaign_name").contains("素材上新")){
|
|
|
- isHaveNewCampaign = true;
|
|
|
- newCampaignId = jsonObject.getLong("campaign_id");
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- // 不存在“上新”计划,则创建一个计划
|
|
|
- if (!isHaveNewCampaign){
|
|
|
- Date campaignDate = new Date();
|
|
|
- String timestamp = DateUtils.formatDate(campaignDate,"yyyyMMddHHmmss");
|
|
|
- JSONObject campaignParams = new JSONObject();
|
|
|
- campaignParams.put("advertiser_id",token.getAccountId());
|
|
|
- String campaignName = strategy.getCampaignName().replace("自定义","素材上新")+timestamp;
|
|
|
- campaignParams.put("campaign_name",campaignName);
|
|
|
- campaignParams.put("type",strategy.getCampaignType());
|
|
|
- //TODO 目前设定为不限
|
|
|
- campaignParams.put("day_budget",0L);
|
|
|
- Map<String, Object> campaignCreateResult = kuaishouInterfaceService.campaignCreate(token.getAccessToken(), token.getAccountId(), campaignParams);
|
|
|
- Integer code = (Integer) campaignCreateResult.get("code");
|
|
|
- String message = (String) campaignCreateResult.get("message");
|
|
|
- if(code != 0 ){
|
|
|
- //TODO 写入日志信息表
|
|
|
- log.info("计划创建失败=>message:{}",message);
|
|
|
- return;
|
|
|
- }
|
|
|
- newCampaignId = (Long) campaignCreateResult.get("campaignId");
|
|
|
- }
|
|
|
-
|
|
|
- //3、在“上新”计划中新增广告组,一个素材搭配5张封面,组成一个广告组
|
|
|
- String timestamp = DateUtils.formatDate(new Date(),"yyyyMMddHHmmss");
|
|
|
- String unitNamePrefix = strategy.getGroupName().replace("自定义","素材上新")+ timestamp;
|
|
|
- int unitCnt = 0;
|
|
|
- for(KuaiShouVideoGet videoItem: newVideos){
|
|
|
- KuaishouAccountCreativeOverrunInfo overrunInfo = overrunInfoService.getByParams(accountId,DateUtils.formatDate(new Date()));
|
|
|
- if(null!=overrunInfo){
|
|
|
- return;
|
|
|
- }
|
|
|
- unitCnt ++;
|
|
|
- String unitName = unitNamePrefix + "-" + unitCnt;
|
|
|
- JSONObject unitParams = createUnitParams(token, newCampaignId, strategy, unitName);
|
|
|
- Map<String, Object> unitCreateResult = kuaishouInterfaceService.adUnitCreate(token.getAccessToken(), token.getAccountId(), unitParams,1);
|
|
|
- Integer unitCode = (Integer) unitCreateResult.get("code");
|
|
|
- String unitMessage = (String) unitCreateResult.get("message");
|
|
|
- if(unitCode != 0 ){
|
|
|
- log.info("组创建失败=>accountId:{};message:{}",accountId,unitMessage);
|
|
|
- continue;
|
|
|
- }
|
|
|
- Long unitId = (Long) unitCreateResult.get("unitId");
|
|
|
-
|
|
|
- // 每个组,搭配5个创意
|
|
|
- List<MaterialCutFrame> cutFrameList = materialCutFrameService.getListByVideoSignature(videoItem.getSignature());
|
|
|
- if ((null == cutFrameList||cutFrameList.isEmpty())){
|
|
|
- log.info("未获取到视频封面=>videoSignature:{}",videoItem.getSignature());
|
|
|
- continue;
|
|
|
- }
|
|
|
- JSONObject creativeParams = creativeParams(token, unitId, videoItem, strategy);
|
|
|
- createCreativeByImage(cutFrameList,token, creativeParams, newCampaignId, unitId, videoItem,strategy.getImageCnt());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void kuaishouAiAdsHotMaterialCreativity(AiKuaishouAdvertiserStrategy strategy) {
|
|
|
- Long accountId = strategy.getAccountId();
|
|
|
- CtopOauthToken token = tokenService.getOauthTokenByAccountId(accountId.toString());
|
|
|
- if(null == token){
|
|
|
- log.info("token获取失败=>accountId:{}",accountId);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- int days = 14;
|
|
|
- int highQualityVideoCnt = 100;
|
|
|
- List<KuaiShouVideoGet>highQualityVideos = videoGetMapper.getHighQualityVideo(accountId,days,highQualityVideoCnt);
|
|
|
- if (null == highQualityVideos||highQualityVideos.isEmpty()){
|
|
|
- log.info("此项目下无视频");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- JSONArray campaignList = kuaishouInterfaceService.getCampaignList(token, new Date(), new Date(),1);
|
|
|
- boolean isHaveHotCampaign = false;
|
|
|
- Long newCampaignId = 0L;
|
|
|
- if (null!=campaignList&&campaignList.size()>0){
|
|
|
- for(int i = 0;i<campaignList.size();i++){
|
|
|
- JSONObject jsonObject = campaignList.getJSONObject(i);
|
|
|
- if (jsonObject.getString("campaign_name").contains("爆量素材")){
|
|
|
- isHaveHotCampaign = true;
|
|
|
- newCampaignId = jsonObject.getLong("campaign_id");
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (!isHaveHotCampaign){
|
|
|
- Date campaignDate = new Date();
|
|
|
- String timestamp = DateUtils.formatDate(campaignDate,"yyyyMMddHHmmss");
|
|
|
- JSONObject campaignParams = new JSONObject();
|
|
|
- campaignParams.put("advertiser_id",token.getAccountId());
|
|
|
- String campaignName = strategy.getCampaignName().replace("自定义","爆量素材")+ timestamp;
|
|
|
- campaignParams.put("campaign_name",campaignName);
|
|
|
- campaignParams.put("type",strategy.getCampaignType());
|
|
|
- campaignParams.put("day_budget",0L);
|
|
|
- Map<String, Object> campaignCreateResult = kuaishouInterfaceService.campaignCreate(token.getAccessToken(), token.getAccountId(), campaignParams);
|
|
|
- Integer code = (Integer) campaignCreateResult.get("code");
|
|
|
- String message = (String) campaignCreateResult.get("message");
|
|
|
- if(code != 0 ){
|
|
|
- log.info("计划创建失败=>message:{}",message);
|
|
|
- return;
|
|
|
- }
|
|
|
- newCampaignId = (Long) campaignCreateResult.get("campaignId");
|
|
|
- }
|
|
|
-
|
|
|
- //3、在“上新”计划中新增广告组,一个素材搭配5张封面,组成一个广告组
|
|
|
- String timestamp = DateUtils.formatDate(new Date(),"yyyyMMddHHmmss");
|
|
|
- String unitNamePrefix = strategy.getGroupName().replace("自定义","爆量素材")+timestamp;
|
|
|
- int unitCnt = 0;
|
|
|
- for(KuaiShouVideoGet videoItem: highQualityVideos){
|
|
|
- KuaishouAccountCreativeOverrunInfo overrunInfo = overrunInfoService.getByParams(accountId,DateUtils.formatDate(new Date()));
|
|
|
- if(null!=overrunInfo){
|
|
|
- return;
|
|
|
- }
|
|
|
- unitCnt ++;
|
|
|
- String unitName = unitNamePrefix + "-" + unitCnt;
|
|
|
- JSONObject unitParams = createUnitParams(token, newCampaignId, strategy, unitName);
|
|
|
- Map<String, Object> unitCreateResult = kuaishouInterfaceService.adUnitCreate(token.getAccessToken(), token.getAccountId(), unitParams,1);
|
|
|
- Integer unitCode = (Integer) unitCreateResult.get("code");
|
|
|
- String unitMessage = (String) unitCreateResult.get("message");
|
|
|
- if(unitCode != 0 ){
|
|
|
- log.info("组创建失败=>accountId:{};message:{}",accountId,unitMessage);
|
|
|
- continue;
|
|
|
- }
|
|
|
- Long unitId = (Long) unitCreateResult.get("unitId");
|
|
|
-
|
|
|
- // 每个组,搭配5个创意
|
|
|
- List<MaterialCutFrame> cutFrameList = materialCutFrameService.getListByVideoSignature(videoItem.getSignature());
|
|
|
- List<MaterialImageInfo> orgFrameList = materialImageInfoService.getListByVideoSignature(videoItem.getSignature());
|
|
|
- if ((null == cutFrameList||cutFrameList.isEmpty())&&(null == orgFrameList|| orgFrameList.isEmpty())){
|
|
|
- log.info("未获取到视频封面=>videoSignature:{}",videoItem.getSignature());
|
|
|
- continue;
|
|
|
- }
|
|
|
- JSONObject creativeParams = creativeParams(token, unitId, videoItem, strategy);
|
|
|
- createCreativeByImage(cutFrameList,token, creativeParams, newCampaignId, unitId, videoItem,strategy.getImageCnt());
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void kuaishouAiAddZeroMaterialCreativity(AiKuaishouAdvertiserStrategy strategy,Integer matNum) {
|
|
|
- Long accountId = strategy.getAccountId();
|
|
|
- CtopOauthToken token = tokenService.getOauthTokenByAccountId(accountId.toString());
|
|
|
- if(null == token){
|
|
|
- log.info("token获取失败=>accountId:{}",accountId);
|
|
|
- return;
|
|
|
- }
|
|
|
- kuaishouInterfaceService.getVideoList(token, DateUtils.date2Str(),DateUtils.date2Str());
|
|
|
-
|
|
|
- //获取创意关联数为0的素材
|
|
|
- List<KuaishouVideoRelateCreatives> kuaishouVideoRelateCreatives = relateCreativesService.listByParams(accountId,matNum);
|
|
|
- if(null == kuaishouVideoRelateCreatives||kuaishouVideoRelateCreatives.isEmpty()){
|
|
|
- log.info("账户:{}, 不存在未创建创意素材",accountId);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- //2、判断该账户下当天有没有“上新”计划,没有则创建一个“上新”计划
|
|
|
- JSONArray campaignList = kuaishouInterfaceService.getCampaignList(token, new Date(), new Date(),1);
|
|
|
- boolean isHaveNewCampaign = false;
|
|
|
- Long newCampaignId = 0L;
|
|
|
- if (null!=campaignList&&campaignList.size()>0){
|
|
|
- for(int i = 0;i<campaignList.size();i++){
|
|
|
- JSONObject jsonObject = campaignList.getJSONObject(i);
|
|
|
- if (jsonObject.getString("campaign_name").contains("素材上新")){
|
|
|
- isHaveNewCampaign = true;
|
|
|
- newCampaignId = jsonObject.getLong("campaign_id");
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- // 不存在“上新”计划,则创建一个计划
|
|
|
- if (!isHaveNewCampaign){
|
|
|
- Date campaignDate = new Date();
|
|
|
- String timestamp = DateUtils.formatDate(campaignDate,"yyyyMMddHHmmss");
|
|
|
- JSONObject campaignParams = new JSONObject();
|
|
|
- campaignParams.put("advertiser_id",token.getAccountId());
|
|
|
- String campaignName = strategy.getCampaignName().replace("自定义","素材上新")+timestamp;
|
|
|
- campaignParams.put("campaign_name",campaignName);
|
|
|
- campaignParams.put("type",strategy.getCampaignType());
|
|
|
- campaignParams.put("day_budget",0L);
|
|
|
- Map<String, Object> campaignCreateResult = kuaishouInterfaceService.campaignCreate(token.getAccessToken(), token.getAccountId(), campaignParams);
|
|
|
- Integer code = (Integer) campaignCreateResult.get("code");
|
|
|
- String message = (String) campaignCreateResult.get("message");
|
|
|
- if(code != 0 ){
|
|
|
- log.info("计划创建失败=>message:{}",message);
|
|
|
- return;
|
|
|
- }
|
|
|
- newCampaignId = (Long) campaignCreateResult.get("campaignId");
|
|
|
- }
|
|
|
-
|
|
|
- //3、在“上新”计划中新增广告组,一个素材搭配n张封面,组成一个广告组
|
|
|
- String timestamp = DateUtils.formatDate(new Date(),"yyyyMMddHHmmss");
|
|
|
- String unitNamePrefix = strategy.getGroupName().replace("自定义","素材上新")+timestamp;
|
|
|
- int unitCnt = 0;
|
|
|
- for(KuaishouVideoRelateCreatives videoItem: kuaishouVideoRelateCreatives){
|
|
|
- KuaishouAccountCreativeOverrunInfo overrunInfo = overrunInfoService.getByParams(accountId,DateUtils.formatDate(new Date()));
|
|
|
- if(null!=overrunInfo){
|
|
|
- return;
|
|
|
- }
|
|
|
- unitCnt ++;
|
|
|
- KuaiShouVideoGet videoGet = videoGetService.getVideoByPhotoId(videoItem.getPhotoId());
|
|
|
- if(null == videoGet){
|
|
|
- log.info("未获取到视频信息=>photoId:{}",videoItem.getPhotoId());
|
|
|
- continue;
|
|
|
- }
|
|
|
- // 每个组,搭配n个创意
|
|
|
- List<MaterialCutFrame> cutFrameList = materialCutFrameService.getListByVideoSignature(videoGet.getSignature());
|
|
|
- List<MaterialImageInfo> orgFrameList = materialImageInfoService.getListByVideoSignature(videoGet.getSignature());
|
|
|
- if ((null == cutFrameList||cutFrameList.isEmpty())&&(null == orgFrameList|| orgFrameList.isEmpty())){
|
|
|
- log.info("未获取到视频封面=>videoSignature:{}",videoGet.getSignature());
|
|
|
- continue;
|
|
|
- }
|
|
|
- String unitName = unitNamePrefix + "-" + unitCnt;
|
|
|
- JSONObject unitParams = createUnitParams(token, newCampaignId, strategy, unitName);
|
|
|
- Map<String, Object> unitCreateResult = kuaishouInterfaceService.adUnitCreate(token.getAccessToken(), token.getAccountId(), unitParams,1);
|
|
|
- Integer unitCode = (Integer) unitCreateResult.get("code");
|
|
|
- String unitMessage = (String) unitCreateResult.get("message");
|
|
|
- if(unitCode != 0 ){
|
|
|
- log.info("组创建失败=>accountId:{};message:{}",accountId,unitMessage);
|
|
|
- continue;
|
|
|
- }
|
|
|
- Long unitId = (Long) unitCreateResult.get("unitId");
|
|
|
- JSONObject creativeParams = creativeParams(token, unitId, videoGet, strategy);
|
|
|
- createCreativeByImage(cutFrameList,token, creativeParams, newCampaignId, unitId, videoGet,strategy.getImageCnt());
|
|
|
- videoItem.setCreativeCount(videoItem.getCreativeCount()+strategy.getImageCnt());
|
|
|
- relateCreativesService.updateById(videoItem);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private JSONObject creativeParams(CtopOauthToken token,Long unitId,KuaiShouVideoGet videoItem,AiKuaishouAdvertiserStrategy strategy){
|
|
|
- JSONObject creativeParams = new JSONObject();
|
|
|
- creativeParams.put("advertiser_id",token.getAccountId());
|
|
|
- creativeParams.put("unit_id",unitId);
|
|
|
- creativeParams.put("photo_id",videoItem.getPhotoId());
|
|
|
- creativeParams.put("creative_material_type",videoItem.getMaterialType());
|
|
|
- creativeParams.put("action_bar_text",strategy.getActionBarText());
|
|
|
- creativeParams.put("description",strategy.getDescription());
|
|
|
- // 安卓下载中间页ID
|
|
|
- if(null!=strategy.getSiteId()){
|
|
|
- creativeParams.put("site_id",strategy.getSiteId());
|
|
|
- }
|
|
|
- if(null != strategy.getClickTrackUrl()&&!"".equals(strategy.getClickTrackUrl())){
|
|
|
- creativeParams.put("click_track_url",strategy.getClickTrackUrl());
|
|
|
- }
|
|
|
- if(null != strategy.getImpressionUrl()&&!"".equals(strategy.getImpressionUrl())){
|
|
|
- creativeParams.put("impression_url",strategy.getImpressionUrl());
|
|
|
- }
|
|
|
- if(null != strategy.getActionbarClickUrl()&&!"".equals(strategy.getActionbarClickUrl())){
|
|
|
- creativeParams.put("actionbar_click_url",strategy.getActionbarClickUrl());
|
|
|
- }
|
|
|
- if(null!=strategy.getCreativeCategory()){
|
|
|
- creativeParams.put("creative_category",strategy.getCreativeCategory());
|
|
|
- }
|
|
|
- if(null!=strategy.getCreativeTag()&&!"".equals(strategy.getCreativeTag())){
|
|
|
- creativeParams.put("creative_tag",JSONArray.parseArray(strategy.getCreativeTag()));
|
|
|
- }
|
|
|
- return creativeParams;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private void createCreative(CtopOauthToken token,JSONObject creativeParams,Long campaignId,Long unitId,
|
|
|
- int creativeCnt,KuaiShouVideoGet videoGet){
|
|
|
- creativeParams.put("creative_name","自定义创意_竖版视频_"+creativeCnt);
|
|
|
- Map<String, Object> creativeCreateResult = kuaishouInterfaceService.creativeCreate
|
|
|
- (token.getAccessToken(), token.getAccountId(), creativeParams,1);
|
|
|
- Integer creativeCode = (Integer) creativeCreateResult.get("code");
|
|
|
- String creativeMessage = (String) creativeCreateResult.get("message");
|
|
|
- if(creativeCode != 0 ){
|
|
|
- log.info("创意创建失败=>message:{}",creativeMessage);
|
|
|
- return;
|
|
|
- }
|
|
|
- Long creativeId = (Long) creativeCreateResult.get("creativeId");
|
|
|
- AiKuaishouStrategyMapCreative mapCreative = new AiKuaishouStrategyMapCreative();
|
|
|
- mapCreative.setAccountId(token.getAccountId());
|
|
|
- mapCreative.setCampaignId(campaignId);
|
|
|
- mapCreative.setUnitId(unitId);
|
|
|
- mapCreative.setVideoSignature(videoGet.getSignature());
|
|
|
- mapCreative.setCreativeId(creativeId);
|
|
|
- mapCreativeService.save(mapCreative);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private JSONObject createUnitParams(CtopOauthToken token, Long campaignId, AiKuaishouAdvertiserStrategy strategy,
|
|
|
- String unitName) {
|
|
|
- // 拼装创建组的入参 json
|
|
|
- JSONObject unitParams = new JSONObject();
|
|
|
- unitParams.put("advertiser_id",token.getAccountId());
|
|
|
- unitParams.put("campaign_id",campaignId);
|
|
|
- unitParams.put("unit_name", unitName);
|
|
|
- unitParams.put("bid_type",strategy.getBidType());
|
|
|
- unitParams.put("ocpx_action_type",strategy.getOcpxActionType());
|
|
|
-
|
|
|
- if(null!=strategy.getUseAppMarket()){
|
|
|
- unitParams.put("use_app_market",strategy.getUseAppMarket());
|
|
|
- }
|
|
|
- if(null!=strategy.getAppStore()&&!"".equals(strategy.getAppStore())){
|
|
|
- unitParams.put("app_store",JSONArray.parseArray(strategy.getAppStore()));
|
|
|
- }
|
|
|
-
|
|
|
- // 在本策略中,都是 OCPM 的出价方式
|
|
|
- unitParams.put("cpa_bid",strategy.getCpaBid());
|
|
|
- unitParams.put("scene_id",JSONArray.parseArray(strategy.getSceneId()));
|
|
|
- unitParams.put("unit_type",4);
|
|
|
-
|
|
|
- // 格式为yyyy-MM-dd,需大于等于当前时间
|
|
|
- unitParams.put("begin_time",DateUtils.formatDate(new Date()));
|
|
|
-
|
|
|
- JSONArray appIds = JSONArray.parseArray(strategy.getAppIds());
|
|
|
- if(null!=appIds&&!appIds.isEmpty()){
|
|
|
- unitParams.put("app_id",appIds.getLong(0));
|
|
|
- }
|
|
|
- unitParams.put("show_mode",2);
|
|
|
- unitParams.put("speed",1);
|
|
|
- //是否开启智能抽帧
|
|
|
- if(null!=strategy.getSmartCover()){
|
|
|
- unitParams.put("smart_cover",strategy.getSmartCover());
|
|
|
- }
|
|
|
- //是否开启素材挖掘
|
|
|
- if(null!=strategy.getAssetMining()){
|
|
|
- unitParams.put("asset_mining",strategy.getAssetMining());
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- // 设置定向内容
|
|
|
- JSONObject targetJsonObject = new JSONObject();
|
|
|
- if(null!=strategy.getRegion()&&!"".equals(strategy.getRegion())){
|
|
|
- targetJsonObject.put("region",JSONArray.parseArray(strategy.getRegion()));
|
|
|
- }
|
|
|
- if(null!=strategy.getAgesRange()&&!"".equals(strategy.getAgesRange())){
|
|
|
- targetJsonObject.put("ages_range",JSONArray.parseArray(strategy.getAgesRange()));
|
|
|
- }
|
|
|
- if(null!=strategy.getGender()){
|
|
|
- targetJsonObject.put("gender",strategy.getGender());
|
|
|
- }
|
|
|
- if(null!=strategy.getPlatformOs()){
|
|
|
- targetJsonObject.put("platform_os",strategy.getPlatformOs());
|
|
|
- }
|
|
|
- if(null!=strategy.getAndroidOsv()){
|
|
|
- targetJsonObject.put("android_osv",strategy.getAndroidOsv());
|
|
|
- }
|
|
|
- if(null!=strategy.getIosOsv()){
|
|
|
- targetJsonObject.put("ios_osv",strategy.getIosOsv());
|
|
|
- }
|
|
|
- if(null!=strategy.getDeviceBrand()&&!"".equals(strategy.getDeviceBrand())&&!"[]".equals(strategy.getDeviceBrand())){
|
|
|
- targetJsonObject.put("device_brand",JSONArray.parseArray(strategy.getDeviceBrand()));
|
|
|
- }
|
|
|
- if(null!=strategy.getDevicePrice()&&!"".equals(strategy.getDevicePrice())&&!"[]".equals(strategy.getDevicePrice())){
|
|
|
- targetJsonObject.put("device_price",JSONArray.parseArray(strategy.getDevicePrice()));
|
|
|
- }
|
|
|
- if(null!=strategy.getPopulation()&&!"".equals(strategy.getPopulation())){
|
|
|
- JSONArray population = JSONArray.parseArray(strategy.getPopulation());
|
|
|
- targetJsonObject.put("population",population);
|
|
|
- }
|
|
|
- if(null!=strategy.getFilterConvertedLevel()&&strategy.getFilterConvertedLevel()!=0){
|
|
|
- targetJsonObject.put("filter_converted_level",strategy.getFilterConvertedLevel());
|
|
|
- }
|
|
|
- if(null!=strategy.getExcludePopulation()&&!"".equals(strategy.getExcludePopulation())){
|
|
|
- JSONArray excludePopulation = JSONArray.parseArray(strategy.getExcludePopulation());
|
|
|
- targetJsonObject.put("exclude_population",excludePopulation);
|
|
|
- }
|
|
|
- //是否开启智能扩量
|
|
|
- if(null!=strategy.getIsOpen()&&strategy.getIsOpen().equals(1)){
|
|
|
- JSONObject intelliExtend = new JSONObject();
|
|
|
- intelliExtend.put("is_open",1);
|
|
|
- if(null!=strategy.getNoAgeBreak()&&strategy.getNoAgeBreak().equals(1)){
|
|
|
- intelliExtend.put("no_age_break",1);
|
|
|
- }
|
|
|
- if(null!=strategy.getNoGenderBreak()&&strategy.getNoGenderBreak().equals(1)){
|
|
|
- intelliExtend.put("no_gender_break",1);
|
|
|
- }
|
|
|
- if(null!=strategy.getNoAreaBreak()&&strategy.getNoAreaBreak().equals(1)){
|
|
|
- intelliExtend.put("no_area_break",1);
|
|
|
- }
|
|
|
- targetJsonObject.put("intelli_extend",intelliExtend);
|
|
|
- }
|
|
|
- unitParams.put("target",targetJsonObject);
|
|
|
- return unitParams;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private void createCreativeByImage(List<MaterialCutFrame> cutFrameList, CtopOauthToken token, JSONObject creativeParams,
|
|
|
- Long campaignId, Long unitId, KuaiShouVideoGet videoItem,
|
|
|
- int imageNumber){
|
|
|
- int creativeCnt = 1;
|
|
|
-
|
|
|
- if ((null!=cutFrameList&&!cutFrameList.isEmpty())){
|
|
|
- for (int i = 0; i < cutFrameList.size(); i++) {
|
|
|
- String imageCode = cutFrameList.get(i).getSignature();
|
|
|
- String imageUrl = cutFrameList.get(i).getUrl();
|
|
|
- String imageToken = getImageToken(imageUrl,imageCode,token.getAccountId(),token.getAccessToken());
|
|
|
- creativeParams.put("image_token",imageToken);
|
|
|
- createCreative(token, creativeParams, campaignId, unitId, creativeCnt,videoItem);
|
|
|
- creativeCnt ++;
|
|
|
- if (creativeCnt > imageNumber) {
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private String getImageToken(String imageUrl, String imageCode, Long accountId,String token){
|
|
|
- // 根据imageUrl 和 imageCode,获取 imageToken (创建创意用)
|
|
|
- KuaiShouImageGet imageGet = imageGetService.getOneByParams(null,imageCode);
|
|
|
- if ((null == imageGet) || ("".equals(imageGet.getImageToken()))){
|
|
|
- // 如果取不到image_token
|
|
|
- // 则调用上传url的方法 kuauiShouImageUpload(url, signature, accountId, oauthToken.getAccessToken());
|
|
|
- return materialUploadService.kuauiShouImageUpload(imageUrl,imageCode,accountId,token);
|
|
|
- }
|
|
|
- return imageGet.getImageToken();
|
|
|
- }
|
|
|
-}
|