|
@@ -0,0 +1,299 @@
|
|
|
+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.AiKuaishouAccountTargetTemplate;
|
|
|
+import cn.com.ctop.kuaishou.modules.ai.entity.AiKuaishouStrategyMapCreative;
|
|
|
+import cn.com.ctop.kuaishou.modules.ai.service.IAiKuaishouNewCreateCampaign;
|
|
|
+import cn.com.ctop.kuaishou.modules.ai.service.IAiKuaishouStrategyMapCreativeService;
|
|
|
+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.service.IKuaiShouImageGetService;
|
|
|
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouMaterialUploadService;
|
|
|
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouVideoGetService;
|
|
|
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.jeecg.common.util.DateUtils;
|
|
|
+import org.jeecg.common.util.JsonResourceUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+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;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void kuaishouAiAdsNewMaterialCreativity(AiKuaishouAccountTargetTemplate targetTemplate) {
|
|
|
+ Long accountId = targetTemplate.getAccountId();
|
|
|
+ CtopOauthToken token = tokenService.getOauthTokenByAccountId(accountId.toString());
|
|
|
+ if(null == token){
|
|
|
+ log.info("token获取失败=>accountId:{}",accountId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2、执行素材的同步数据服务
|
|
|
+ // 2.1 执行 非素造的 视频拉取服务,时间过滤支持 yyyy-MM-dd, 拉取当天凌晨到此刻的数据
|
|
|
+ kuaishouInterfaceService.getVideoList(token, DateUtils.date2Str(),DateUtils.date2Str());
|
|
|
+
|
|
|
+ // 2.2 执行 素造的 视频拉取服务,时间过滤执行 yyyy-MM-dd HH:mm:ss
|
|
|
+ // 时间差,单位为分钟
|
|
|
+ int dimeDiff = 5;
|
|
|
+ Date now = new Date();
|
|
|
+ String startTime = DateUtils.formatDate(new Date(now.getTime() - dimeDiff*60*1000),"yyyy-MM-dd HH:mm:ss");
|
|
|
+ String endTime = DateUtils.formatDate(now,"yyyy-MM-dd HH:mm:ss");
|
|
|
+ kuaishouInterfaceService.getSuZaoList(token.getAccessToken(), token.getAccountId(), 1, startTime, endTime);
|
|
|
+
|
|
|
+
|
|
|
+ // 3、获取5分钟内,ctop_kuaishou_video_get 的视频数据,记为 N,如果有数据则执行下面步骤
|
|
|
+ List<KuaiShouVideoGet> newVideos = videoGetService.getNewVideoBetweenTime(accountId,startTime,endTime);
|
|
|
+ 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 (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 = "户14-上下滑&信息流" + "-素材上新-"+ "剪辑-低价-低价包邮-活动-年龄18岁以上-单出价-"+ timestamp;
|
|
|
+ campaignParams.put("campaign_name",campaignName);
|
|
|
+ campaignParams.put("type",Integer.parseInt(targetTemplate.getMarketingGoal()));
|
|
|
+ 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 unitName = "户14-上下滑&信息流" + "-素材上新-"+ "剪辑-低价-低价包邮-活动-年龄18岁以上-单出价-"+ timestamp;
|
|
|
+ int unitCnt = 0;
|
|
|
+ for(KuaiShouVideoGet videoItem: newVideos){
|
|
|
+ unitCnt ++;
|
|
|
+ unitName = unitName + "-" + unitCnt;
|
|
|
+ JSONObject unitParams = createUnitParams(token, newCampaignId, targetTemplate, 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, targetTemplate);
|
|
|
+ createCreativeByImage(cutFrameList, orgFrameList,token, creativeParams, newCampaignId, unitId, videoItem,targetTemplate.getImageNumber());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private JSONObject creativeParams(CtopOauthToken token,Long unitId,KuaiShouVideoGet videoItem,AiKuaishouAccountTargetTemplate targetTemplate){
|
|
|
+ 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","下载抢购");
|
|
|
+ creativeParams.put("description","不买亏大了!众多商品低至一元!看看你多花了多少冤枉钱!");
|
|
|
+ // 安卓下载中间页ID
|
|
|
+ JSONArray siteIdArray = JSONArray.parseArray(targetTemplate.getSiteId());
|
|
|
+ if(null == siteIdArray||!siteIdArray.isEmpty()){
|
|
|
+ JSONObject siteObject = siteIdArray.getJSONObject(0);
|
|
|
+ Long appId = siteObject.getLong("appId");
|
|
|
+ Long siteId = siteObject.getLong("siteId");
|
|
|
+ if(null!=siteId){
|
|
|
+ creativeParams.put("site_id",siteId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(null != targetTemplate.getClickTrackUrl()&&!"".equals(targetTemplate.getClickTrackUrl())){
|
|
|
+ creativeParams.put("click_track_url",targetTemplate.getClickTrackUrl());
|
|
|
+ }
|
|
|
+ if(null != targetTemplate.getImpressionUrl()&&!"".equals(targetTemplate.getImpressionUrl())){
|
|
|
+ creativeParams.put("impression_url",targetTemplate.getImpressionUrl());
|
|
|
+ }
|
|
|
+ if(null != targetTemplate.getActionbarClickUrl()&&!"".equals(targetTemplate.getActionbarClickUrl())){
|
|
|
+ creativeParams.put("actionbar_click_url",targetTemplate.getActionbarClickUrl());
|
|
|
+ }
|
|
|
+ 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, AiKuaishouAccountTargetTemplate targetTemplate,
|
|
|
+ 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",targetTemplate.getBidType());
|
|
|
+ unitParams.put("ocpx_action_type",targetTemplate.getOcpxActionType());
|
|
|
+
|
|
|
+ if(null!=targetTemplate.getUseAppMarket()&&!"".equals(targetTemplate.getUseAppMarket())){
|
|
|
+ unitParams.put("use_app_market",Integer.parseInt(targetTemplate.getUseAppMarket()));
|
|
|
+ }
|
|
|
+ if(null!=targetTemplate.getAppStore()&&!"".equals(targetTemplate.getAppStore())){
|
|
|
+ unitParams.put("app_store",JSONArray.parseArray(targetTemplate.getAppStore()));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 在本策略中,都是 OCPM 的出价方式
|
|
|
+ unitParams.put("cpa_bid",targetTemplate.getCpaBid());
|
|
|
+ unitParams.put("scene_id",JSONArray.parseArray(targetTemplate.getSceneId()));
|
|
|
+ unitParams.put("unit_type",4);
|
|
|
+
|
|
|
+ // 格式为yyyy-MM-dd,需大于等于当前时间
|
|
|
+ unitParams.put("begin_time",DateUtils.formatDate(new Date()));
|
|
|
+
|
|
|
+ JSONArray appIds = JSONArray.parseArray(targetTemplate.getAppIds());
|
|
|
+ if(null!=appIds&&!appIds.isEmpty()){
|
|
|
+ unitParams.put("app_id",appIds.getLong(0));
|
|
|
+ }
|
|
|
+ unitParams.put("show_mode",2);
|
|
|
+ unitParams.put("speed",1);
|
|
|
+
|
|
|
+ // 设置定向内容
|
|
|
+ JSONObject targetJsonObject = new JSONObject();
|
|
|
+ targetJsonObject.put("region",JSONArray.parseArray(targetTemplate.getRegion()));
|
|
|
+ targetJsonObject.put("ages_range",JSONArray.parseArray(targetTemplate.getAgesRange()));
|
|
|
+ targetJsonObject.put("gender",targetTemplate.getGender());
|
|
|
+ targetJsonObject.put("platform_os",targetTemplate.getPlatformOs());
|
|
|
+ JSONArray population = JSONArray.parseArray(targetTemplate.getPopulation());
|
|
|
+ if(null!=population&&!population.isEmpty()){
|
|
|
+ targetJsonObject.put("population",population);
|
|
|
+ }
|
|
|
+ JSONArray excludePopulation = JSONArray.parseArray(targetTemplate.getExcludePopulation());
|
|
|
+ if(null!=excludePopulation&&!excludePopulation.isEmpty()){
|
|
|
+ targetJsonObject.put("exclude_population",excludePopulation);
|
|
|
+ }
|
|
|
+ unitParams.put("target",targetJsonObject);
|
|
|
+
|
|
|
+ return unitParams;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void createCreativeByImage(List<MaterialCutFrame> cutFrameList, List<MaterialImageInfo> orgFrameList,
|
|
|
+ CtopOauthToken token, JSONObject creativeParams,
|
|
|
+ Long campaignId, Long unitId, KuaiShouVideoGet videoItem,
|
|
|
+ int imageNumber){
|
|
|
+ int creativeCnt = 1;
|
|
|
+ if (null!=orgFrameList&&!orgFrameList.isEmpty()) {
|
|
|
+ for (int i = 0; i < orgFrameList.size(); i++) {
|
|
|
+ String imageCode = orgFrameList.get(i).getCode();
|
|
|
+ String imageUrl = orgFrameList.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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ((null!=cutFrameList&&!cutFrameList.isEmpty()) && (creativeCnt<=15)){
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|