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.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.jeecg.common.util.JsonResourceUtil;
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;

    @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 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);
        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 = targetTemplate.getTitlePrefix() + "素材上新"+ targetTemplate.getTitleMiddle()+ 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 unitNamePrefix = targetTemplate.getTitlePrefix() + "素材上新"+ targetTemplate.getTitleMiddle()+ 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, 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());
        }
    }

    @Override
    public void kuaishouAiAdsHotMaterialCreativity(AiKuaishouAccountTargetTemplate targetTemplate) {
        Long accountId = targetTemplate.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 = targetTemplate.getTitlePrefix() + "爆量素材"+ targetTemplate.getTitleMiddle() + 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 unitNamePrefix = targetTemplate.getTitlePrefix() + "爆量素材"+ targetTemplate.getTitleMiddle()+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, 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());
        }

    }
    @Override
    public void kuaishouAiAddNewMaterialCreativity(AiKuaishouAccountTargetTemplate targetTemplate,Integer matNum) {
        Long accountId = targetTemplate.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 = targetTemplate.getTitlePrefix() + "素材上新"+ targetTemplate.getTitleMiddle()+ 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 unitNamePrefix = targetTemplate.getTitlePrefix() + "素材上新"+ targetTemplate.getTitleMiddle()+ 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;
            }
            // 每个组，搭配5个创意
            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, 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");
            JSONObject creativeParams = creativeParams(token, unitId, videoGet, targetTemplate);
            createCreativeByImage(cutFrameList, orgFrameList,token, creativeParams, newCampaignId, unitId, videoGet,targetTemplate.getImageNumber());
            videoItem.setCreativeCount(5);
            relateCreativesService.updateById(videoItem);
        }
    }


    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",targetTemplate.getActionBarText());
        creativeParams.put("description",targetTemplate.getCreativeDescription());
        // 安卓下载中间页ID
        JSONArray siteIdArray = JSONArray.parseArray(targetTemplate.getSiteId());
        if(null == siteIdArray||!siteIdArray.isEmpty()){
            JSONObject siteObject = siteIdArray.getJSONObject(0);
            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);
        }
        if(null!=targetTemplate.getFilterConvertedLevel()&&targetTemplate.getFilterConvertedLevel()!=0){
            targetJsonObject.put("filter_converted_level",targetTemplate.getFilterConvertedLevel());
        }
        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();
    }
}
