|
@@ -0,0 +1,255 @@
|
|
|
+package cn.com.ctop.kuaishou.modules.batch.service.impl;
|
|
|
+
|
|
|
+import cn.com.ctop.common.module.utils.Check;
|
|
|
+import cn.com.ctop.kuaishou.modules.batch.entity.KuaishouBatchCampaignPreview;
|
|
|
+import cn.com.ctop.kuaishou.modules.batch.entity.KuaishouBatchCreativePreview;
|
|
|
+import cn.com.ctop.kuaishou.modules.batch.entity.KuaishouBatchGroupPreview;
|
|
|
+import cn.com.ctop.kuaishou.modules.batch.mapper.KuaishouBatchCampaignPreviewMapper;
|
|
|
+import cn.com.ctop.kuaishou.modules.batch.mapper.KuaishouBatchCreativePreviewMapper;
|
|
|
+import cn.com.ctop.kuaishou.modules.batch.mapper.KuaishouBatchGroupPreviewMapper;
|
|
|
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouBatchCampaignPreviewService;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.xxl.job.core.enums.NoEn;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 快手-广告计划预览信息
|
|
|
+ *
|
|
|
+ * @author jeecg-boot
|
|
|
+ * @version V1.0
|
|
|
+ * @date 2021-01-26
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class KuaishouBatchCampaignPreviewServiceImpl extends ServiceImpl<KuaishouBatchCampaignPreviewMapper, KuaishouBatchCampaignPreview> implements IKuaishouBatchCampaignPreviewService {
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ KuaishouBatchCampaignPreviewMapper campaignPreviewMapper;
|
|
|
+ @Autowired
|
|
|
+ KuaishouBatchGroupPreviewMapper groupPreviewMapper;
|
|
|
+ @Autowired
|
|
|
+ KuaishouBatchCreativePreviewMapper creativePreviewMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void addPreviewData(JSONObject data) throws Exception {
|
|
|
+ String batchId = data.getString("batchId");
|
|
|
+ //计划
|
|
|
+ JSONObject campaignJson = data.getJSONObject("campaignJson");
|
|
|
+ if (Check.isNull(campaignJson)) {
|
|
|
+ throw new Exception("数据异常,未获取到广告计划数据");
|
|
|
+ }
|
|
|
+ KuaishouBatchCampaignPreview campaignPreview = commonCampaign(campaignJson, batchId);
|
|
|
+ //组
|
|
|
+ JSONObject groupJson = data.getJSONObject("unitJson");
|
|
|
+ if (Check.isNull(groupJson)) {
|
|
|
+ throw new Exception("数据异常,未获取到广告组数据");
|
|
|
+ }
|
|
|
+ KuaishouBatchGroupPreview groupPreview = commonGroup(groupJson, batchId, campaignJson);
|
|
|
+ //创意
|
|
|
+ JSONObject creativeJson = data.getJSONObject("creativeJson");
|
|
|
+ if (Check.isNull(creativeJson)) {
|
|
|
+ throw new Exception("数据异常,未获取到广告创意数据");
|
|
|
+ }
|
|
|
+ KuaishouBatchCreativePreview creativePreview = commonCreative(creativeJson, batchId);
|
|
|
+ //素材数据
|
|
|
+ JSONObject materialArray = data.getJSONObject("materialArray");
|
|
|
+ if (Check.isNull(materialArray)) {
|
|
|
+ throw new Exception("数据异常,未获取到素材数据");
|
|
|
+ }
|
|
|
+ //创意组
|
|
|
+ JSONArray materialList = materialArray.getJSONArray("materialList");
|
|
|
+ if (Check.isNull(materialList)) {
|
|
|
+ throw new Exception("数据异常,未获取到创意组数据");
|
|
|
+ }
|
|
|
+ //应用数据
|
|
|
+ JSONObject appList = data.getJSONObject("appList");
|
|
|
+ if (Check.isNull(appList)) {
|
|
|
+ throw new Exception("数据异常,未获取到应用数据");
|
|
|
+ }
|
|
|
+ //定向包
|
|
|
+ JSONArray targetIds = data.getJSONArray("targetIds");
|
|
|
+ if (Check.isNull(targetIds)) {
|
|
|
+ throw new Exception("数据异常,未获取到应用数据");
|
|
|
+ }
|
|
|
+ //分组规则 1 定向 2 创意 3 落地页
|
|
|
+ Integer type = campaignJson.getInteger("groupRule");
|
|
|
+ insertCampaignsAndGroups(type, targetIds, materialArray, campaignPreview, groupPreview, creativePreview);
|
|
|
+ }
|
|
|
+
|
|
|
+ private JSONObject insertCampaignsAndGroups(Integer type, JSONArray targetIds, JSONObject materialArray, KuaishouBatchCampaignPreview campaignPreview, KuaishouBatchGroupPreview groupPreview, KuaishouBatchCreativePreview creativePreview) {
|
|
|
+ //创意组
|
|
|
+ JSONArray materialList = materialArray.getJSONArray("materialList");
|
|
|
+
|
|
|
+ JSONObject materialInfo = materialArray.getJSONObject("materialInfo");
|
|
|
+ //广告语匹配方式 1自定义,2按创意,3按创意组
|
|
|
+ Integer matchingMethod = materialInfo.getInteger("matchingMethod");
|
|
|
+ JSONArray descriptionList = materialArray.getJSONArray("descriptionList");
|
|
|
+ if (!Check.isNull(descriptionList)) {
|
|
|
+ //创意组匹配
|
|
|
+ descriptionList = advertisingSlogan(descriptionList, materialList);
|
|
|
+ }
|
|
|
+ creativePreview.setAccountId(campaignPreview.getAccountId());
|
|
|
+ //分组规则 1定向,2创意组
|
|
|
+ if (NoEn.NO1.valueInt() == type) {
|
|
|
+ for (int i = 0; i < targetIds.size(); i++) {
|
|
|
+ campaignPreviewMapper.insert(campaignPreview);
|
|
|
+ for (int j = 0; j < materialList.size(); j++) {
|
|
|
+ groupPreview.setPlanId(campaignPreview.getId());
|
|
|
+ groupPreview.setTemplateId(targetIds.getLong(i));
|
|
|
+ groupPreviewMapper.insert(groupPreview);
|
|
|
+ JSONObject material = materialList.getJSONObject(j);
|
|
|
+ JSONArray photoArr = material.getJSONArray("photoArr");
|
|
|
+ if (matchingMethod == 2) {
|
|
|
+ //创意匹配
|
|
|
+ descriptionList = advertisingSlogan(descriptionList, photoArr);
|
|
|
+ }
|
|
|
+ for (int k = 0; k < photoArr.size(); k++) {
|
|
|
+ JSONObject photo = photoArr.getJSONObject(k);
|
|
|
+ JSONObject image = photo.getJSONObject("image");
|
|
|
+ creativePreview.setPlanId(campaignPreview.getId());
|
|
|
+ creativePreview.setGroupId(groupPreview.getId());
|
|
|
+ creativePreview.setPhotoId(photo.getString("photoId"));
|
|
|
+ creativePreview.setCreativeName(photo.getString("creativeName"));
|
|
|
+ creativePreview.setImageUrl(image.getString("url"));
|
|
|
+ creativePreview.setImageSignature(image.getString("signature"));
|
|
|
+ if (matchingMethod == 1) {
|
|
|
+ creativePreview.setDescription(photo.getString("description"));
|
|
|
+ } else if (matchingMethod == 2) {
|
|
|
+ creativePreview.setDescription(descriptionList.getString(k));
|
|
|
+ } else if (matchingMethod == 3) {
|
|
|
+ creativePreview.setDescription(descriptionList.getString(j));
|
|
|
+ }
|
|
|
+ creativePreviewMapper.insert(creativePreview);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (NoEn.NO2.valueInt() == type) {
|
|
|
+
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 广告语整理
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return com.alibaba.fastjson.JSONArray
|
|
|
+ * @throws
|
|
|
+ * @author ZHAOXA
|
|
|
+ */
|
|
|
+ private static JSONArray advertisingSlogan(JSONArray descriptionList, JSONArray materialList) {
|
|
|
+ if (materialList.size() <= descriptionList.size()) {
|
|
|
+ return descriptionList;
|
|
|
+ }
|
|
|
+ JSONArray other = new JSONArray();
|
|
|
+ if (materialList.size() > descriptionList.size()) {
|
|
|
+ int num = materialList.size() / descriptionList.size();
|
|
|
+ for (int i = 0; i <= num; i++) {
|
|
|
+ other.addAll(descriptionList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return other;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 整理通用广告计划数据
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return cn.com.ctop.kuaishou.modules.batch.entity.KuaishouBatchCampaignPreview
|
|
|
+ * @throws
|
|
|
+ * @author ZHAOXA
|
|
|
+ */
|
|
|
+ private KuaishouBatchCampaignPreview commonCampaign(JSONObject campaignJson, String batchId) {
|
|
|
+// KuaishouBatchCampaignPreview sd =JSONObject.toJavaObject(campaignJson, KuaishouBatchCampaignPreview.class);
|
|
|
+ KuaishouBatchCampaignPreview campaignPreview = new KuaishouBatchCampaignPreview();
|
|
|
+ campaignPreview.setAccountId(campaignJson.getLong("accountId"));
|
|
|
+ campaignPreview.setCampaignName(campaignJson.getString("campaignName"));
|
|
|
+ campaignPreview.setCampaignType(campaignJson.getInteger("campaignType"));
|
|
|
+ campaignPreview.setPutStatus(campaignJson.getInteger("campaignStatus"));
|
|
|
+ campaignPreview.setDayBudget(campaignJson.getInteger("dayBudget"));
|
|
|
+ campaignPreview.setDayBudgetSchedule(campaignJson.getString("dayBudgetSchedule"));
|
|
|
+ campaignPreview.setTimeStart(campaignJson.getString("timeStart"));
|
|
|
+ campaignPreview.setIsTime(campaignJson.getInteger("isTime"));
|
|
|
+ campaignPreview.setBatchId(batchId);
|
|
|
+ return campaignPreview;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 整理通用广告组数据
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return cn.com.ctop.kuaishou.modules.batch.entity.KuaishouBatchGroupPreview
|
|
|
+ * @throws
|
|
|
+ * @author ZHAOXA
|
|
|
+ */
|
|
|
+ private KuaishouBatchGroupPreview commonGroup(JSONObject groupJson, String batchId, JSONObject campaignJson) {
|
|
|
+ KuaishouBatchGroupPreview groupPreview = new KuaishouBatchGroupPreview();
|
|
|
+ groupPreview.setAccountId(groupJson.getLong("accountId"));
|
|
|
+ groupPreview.setUnitName(groupJson.getString("unitName"));
|
|
|
+ groupPreview.setPutStatus(campaignJson.getInteger("unitStatus"));
|
|
|
+ groupPreview.setBidType(groupJson.getInteger("bidType"));
|
|
|
+ if (groupJson.getInteger("bidType") == 2) {
|
|
|
+ int bid = getRandom(groupJson.getInteger("bidMax"), groupJson.getInteger("bidMin"));
|
|
|
+ if (bid > 0) {
|
|
|
+ groupJson.put("bid", bid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (groupJson.getInteger("bidType") == 10) {
|
|
|
+ groupPreview.setCpaBid(groupJson.getInteger("cpaBid"));
|
|
|
+ } else {
|
|
|
+ groupPreview.setBid(groupJson.getInteger("bid"));
|
|
|
+ }
|
|
|
+ groupPreview.setOcpxActionType(groupJson.getInteger("ocpxActionType"));
|
|
|
+ groupPreview.setDeepConversionType(groupJson.getInteger("deepConversionType"));
|
|
|
+ groupPreview.setDeepConversionBid(groupJson.getInteger("deepConversionBid"));
|
|
|
+ groupPreview.setDayBudget(groupJson.getInteger("dayBudget"));
|
|
|
+ groupPreview.setSpeed(groupJson.getInteger("speed"));
|
|
|
+ groupPreview.setBeginTime(groupJson.getString("beginTime"));
|
|
|
+ groupPreview.setEndTime(groupJson.getString("endTime"));
|
|
|
+ groupPreview.setScheduleTime(groupJson.getString("scheduleTime"));
|
|
|
+ groupPreview.setSceneId(groupJson.getString("sceneId"));
|
|
|
+ groupPreview.setShowMode(groupJson.getInteger("showMode"));
|
|
|
+ groupPreview.setUseAppMarket(groupJson.getInteger("useAppMarket"));
|
|
|
+ groupPreview.setAppStore(groupJson.getString("appStore"));
|
|
|
+ groupPreview.setBatchId(batchId);
|
|
|
+ groupPreview.setUnitType(groupJson.getInteger("unitType"));
|
|
|
+ return groupPreview;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int getRandom(int x, int y) {
|
|
|
+ int num = -1;
|
|
|
+ //说明:两个数在合法范围内,并不限制输入的数哪个更大一些
|
|
|
+ if (x < 0 || y < 0) {
|
|
|
+ return num;
|
|
|
+ } else {
|
|
|
+ int max = Math.max(x, y);
|
|
|
+ int min = Math.min(x, y);
|
|
|
+ int mid = max - min;//求差
|
|
|
+ //产生随机数
|
|
|
+ return (int) (Math.random() * (mid + 1)) + min;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 整理通用创意数据
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return cn.com.ctop.kuaishou.modules.batch.entity.KuaishouBatchCreativePreview
|
|
|
+ * @throws
|
|
|
+ * @author ZHAOXA
|
|
|
+ */
|
|
|
+ private KuaishouBatchCreativePreview commonCreative(JSONObject creativeJson, String batchId) {
|
|
|
+ KuaishouBatchCreativePreview creativePreview = new KuaishouBatchCreativePreview();
|
|
|
+ creativePreview.setActionBarText(creativeJson.getString("actionBarText"));
|
|
|
+ creativePreview.setOverlayType(creativeJson.getString("overlayType"));
|
|
|
+ creativePreview.setStickerTitle(creativeJson.getString("stickerTitle"));
|
|
|
+ creativePreview.setCreativeTag(creativeJson.getString("creativeTag"));
|
|
|
+ creativePreview.setBatchId(batchId);
|
|
|
+ return creativePreview;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|