package org.jeecg.modules.bytedance.advertise.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.jeecg.common.aspect.annotation.Dict;
import org.jeecg.modules.bytedance.common.utils.CtopAdConstant;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 今日头条广告计划信息
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2019-07-22
 */
@Data
@TableName("ctop_bytedance_advertise_plan")
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value = "ctop_bytedance_advertise_plan对象", description = "今日头条广告计划信息")
public class ByteDanceAdvertisePlan {
    @TableId
    private Long id;

    @Dict(dicCode = "account_id",dictTable="ctop_user_allocation",dicText="auth_name")
    private String accountId;

    // 计划名称
    private String name;

    @TableField(exist=false)
    private String names;

    @TableField(exist=false)
    private String landingType;

    @Dict(dicCode = "id",dictTable="ctop_bytedance_campaign",dicText="name")
    private Long campaignId;

    private String operation;

    private String unionVideoType;

    // 上次修改时间戳(用于更新时提交,服务端判断是否基于最新信息修改)
    private String planModifyTime;

    // 投放范围
    private String deliveryRange;

    // 计划上次修改时间
    private String adModifyTime;

    // 计划创建时间
    private String adCreateTime;

    // 广告预算类型
    private String budgetMode;

    // 广告预算
    private BigDecimal budget;

    // 广告计划投放状态
    private String toutiaoStatus;

    //true:投放中 false:暂停
    private Boolean planStatus;

    // 广告计划操作状态
    private String optStatus;

    // 广告投放起始时间
    private String startTime;

    // 广告投放结束时间
    private String endTime;

    // 广告出价
    private BigDecimal bid;

    // 广告出价类型
    private String pricing;

    // 广告投放时间类型
    private String scheduleType;

    // 广告投放时段
    private String scheduleTime;

    // 广告投放速度类型
    private String flowControlMode;

    // 应用直达链接
    private String openUrl;

    // 应用下载方式
    private String downloadType;

    // 应用下载方式
    private String downloadUrl;

    // 广告落地页链接
    private String externalUrl;

    // 广告应用下载包名
    private String toutiaoPackage;

    // 广告应用下载类型
    private String appType;

    // 广告计划审核不通过原因
    private String auditRejectReason;

    // ocpc广告转化出价 目标转化出价/预期成本
    private BigDecimal cpaBid;

    // 深度优化出价
    private BigDecimal deepCpaBid;

    private String deepBidType;

    private Integer cpaSkipFirstPhrase;

    // 转化类型广告的转化工具id
    private Long convertId;

    // 过滤已转化用户类型字段
    private String hideIfConverted;

    // 过滤已安装，当推广目标为安卓应用下载时可填
    private Integer hideIfExists;

    private String convertedTimeDuration;


    // audience
    private String audience;

    private String modifyTime;

    private Date createTime;

    private Date updateTime;

    private BigDecimal lubanRoiGoal;

    private BigDecimal roiGoal;

    //定向包id
    private String audiencePackageId;

    /**
     *  受众公共参数
     */
    // 受众性别
    private String gender;
    // 受众平台
    @TableField(exist=false)
    private Object platform;
    @TableField(exist=false)
    private Object retargetingTags;
    @TableField(exist=false)
    private Object retargetingTagsExclude;
    @TableField(exist=false)
    private Object age;
    @TableField(exist=false)
    private Object carrier;
    @TableField(exist=false)
    private Object ac;
    @TableField(exist=false)
    private Object deviceBrand;
    @TableField(exist=false)
    private Object articleCategory;
    @TableField(exist=false)
    private Object activateType;
    @TableField(exist=false)
    private Object city;
    @TableField(exist=false)
    private Object businessIds;
    @TableField(exist=false)
    private String locationType;

    //媒体定向
    @TableField(exist=false)
    private Object superiorPopularityType;
    @TableField(exist=false)
    private Object flowPackage;
    @TableField(exist=false)
    private Object excludeFlowPackage;
    @TableField(exist=false)
    private Object deviceType;

    //是否启用智能放量 0、1。缺省为 0
    @TableField(exist=false)
    private Object autoExtendEnabled;
    @TableField(exist=false)
    private Object launchPrice;
    @TableField(exist=false)
    private Object interestActionMode;
    @TableField(exist=false)
    private Object actionScene;
    @TableField(exist=false)
    private Object actionDays;
    @TableField(exist=false)
    private Object actionCategories;
    @TableField(exist=false)
    private Object actionWords;
    @TableField(exist=false)
    private Object interestCategories;
    @TableField(exist=false)
    private Object interestWords;
    @TableField(exist=false)
    private Object geolocation;
    @TableField(exist=false)
    private String externalAction;
    @TableField(exist=false)
    private String useOpenUrl;

    public ByteDanceAdvertisePlan(JSONObject dataObject, String accountId) {
        this.id = dataObject.getLong("id");
        this.accountId = accountId;
        String name = dataObject.getString("name");
        if (null != name && !"".equals(name.trim())) {
            this.name = name;
        }
        JSONObject audience = dataObject.getJSONObject("audience");
        if (null != audience) {
            this.audience = audience.toJSONString();
            JSONArray platform = audience.getJSONArray("platform");
            if (null != platform && platform.size() > 0) {
                String plat = "";
                for (int i = 0; i < platform.size(); i++) {
                    String platformStr = platform.getString(i);
                    if ("IOS".equals(platformStr)) {
                        plat += "ios,";
                    } else {
                        plat += "安卓,";
                    }
                }
                this.platform = plat.substring(0, plat.length() - 1);
            }
            String gender = audience.getString("gender");
            if (null != gender) {
                if (CtopAdConstant.BYTEDANCE_GENDER_TYPE_NONE.equals(gender)) {
                    this.gender = "不限";
                } else if (CtopAdConstant.BYTEDANCE_GENDER_TYPE_GENDER_MALE.equals(gender)) {
                    this.gender = "男";
                } else {
                    this.gender = "女";
                }
            }
        }
        this.createTime = new Date();
        this.updateTime = new Date();
        Long advertiserId = dataObject.getLong("advertiser_id");

        if (null != advertiserId) {
            this.accountId = advertiserId+"";
        }
        Long campaignId = dataObject.getLong("campaign_id");
        if (null != campaignId) {
            this.campaignId = campaignId;
        }

        String budgetMode = dataObject.getString("budget_mode");
        if (null != budgetMode && !"".equals(budgetMode.trim())) {
            this.budgetMode = budgetMode;
        }
        String modifyTime = dataObject.getString("modify_time");
        if (null != modifyTime && !"".equals(modifyTime.trim())) {
            this.modifyTime = modifyTime;
        }
        String openUrl = dataObject.getString("open_url");
        if (null != openUrl && !"".equals(openUrl.trim())) {
            this.openUrl = openUrl;
        }

        String adCreateTime = dataObject.getString("ad_create_time");
        if (null != adCreateTime && !"".equals(adCreateTime.trim())) {
            this.adCreateTime = adCreateTime;
        }

        String adModifyTime = dataObject.getString("ad_modify_time");
        if (null != adModifyTime && !"".equals(adModifyTime.trim())) {
            this.adModifyTime = adModifyTime;
        }

        String auditRejectReason = dataObject.getString("audit_reject_reason");
        if (null != auditRejectReason && !"".equals(auditRejectReason.trim())) {
            this.auditRejectReason = auditRejectReason;
        }

        String pricing = dataObject.getString("pricing");
        if (null != pricing && !"".equals(pricing.trim())) {
            this.pricing = pricing;
        }


        Integer hideIfExists = dataObject.getInteger("hide_if_exists");
        if (null != hideIfExists) {
            this.hideIfExists = hideIfExists;
        }

        String appType = dataObject.getString("app_type");
        if (null != appType && !"".equals(appType.trim())) {
            this.appType = appType;
        }

        String flowControlMode = dataObject.getString("flow_control_mode");
        if (null != flowControlMode && !"".equals(flowControlMode.trim())) {
            this.flowControlMode = flowControlMode;
        }


        BigDecimal cpaBid = dataObject.getBigDecimal("cpa_bid");
        if (null != cpaBid) {
            this.cpaBid = cpaBid;
        }

        String startTime = dataObject.getString("start_time");
        if (null != startTime && !"".equals(startTime.trim())) {
            this.startTime = startTime;
        }

        String optStatus = dataObject.getString("opt_status");
        if (null != optStatus && !"".equals(optStatus.trim())) {
            this.optStatus = optStatus;
        }

        String endTime = dataObject.getString("end_time");
        if (null != endTime && !"".equals(endTime.trim())) {
            this.endTime = endTime;
        }

        String status = dataObject.getString("status");
        if (null != status && !"".equals(status.trim())) {
            this.toutiaoStatus = status;
        }

        Long convertId = dataObject.getLong("convert_id");
        if (null != convertId) {
            this.convertId = convertId;
        }

        BigDecimal bid = dataObject.getBigDecimal("bid");
        if (null != bid) {
            this.bid = bid;
        }

        String hideIfConverted = dataObject.getString("hide_if_converted");
        if (null != hideIfConverted && !"".equals(hideIfConverted.trim())) {
            this.hideIfConverted = hideIfConverted;
        }

        String toutiaoPackage = dataObject.getString("package");
        if (null != toutiaoPackage && !"".equals(toutiaoPackage.trim())) {
            this.toutiaoPackage = toutiaoPackage;
        }

        String deliveryRange = dataObject.getString("delivery_range");
        if (null != deliveryRange && !"".equals(deliveryRange.trim())) {
            this.deliveryRange = deliveryRange;
        }

        BigDecimal budget = dataObject.getBigDecimal("budget");
        if (null != budget) {
            this.budget = budget;
        }

        String scheduleType = dataObject.getString("schedule_type");
        if (null != scheduleType && !"".equals(scheduleType.trim())) {
            this.scheduleType = scheduleType;
        }

        String scheduleTime = dataObject.getString("schedule_time");
        if (null != scheduleTime && !"".equals(scheduleTime.trim())) {
            this.scheduleTime = scheduleTime;
        }

        String downloadType = dataObject.getString("download_type");
        if (null != downloadType && !"".equals(downloadType.trim())) {
            this.downloadType = downloadType;
        }

        String downloadUrl = dataObject.getString("download_url");
        if (null != downloadUrl && !"".equals(downloadUrl.trim())) {
            this.downloadUrl = downloadUrl;
        }

        String externalUrl = dataObject.getString("external_url");
        if (null != externalUrl && !"".equals(externalUrl.trim())) {
            this.externalUrl = externalUrl;
        }
    }

    public ByteDanceAdvertisePlan(String advertiserId, Long adId, JSONObject params) {
        this.id = adId;
        this.accountId = advertiserId;
        String name = params.getString("name");
        if (null != name && !"".equals(name.trim())) {
            this.name = name;
        }

        Long campaignId = params.getLong("campaign_id");
        if (null != campaignId) {
            this.campaignId = campaignId;
        }

        String budgetMode = params.getString("budget_mode");
        if (null != budgetMode && !"".equals(budgetMode.trim())) {
            this.budgetMode = budgetMode;
        }

        String openUrl = params.getString("open_url");
        if (null != openUrl && !"".equals(openUrl.trim())) {
            this.openUrl = openUrl;
        }

        String adCreateTime = params.getString("ad_create_time");
        if (null != adCreateTime && !"".equals(adCreateTime.trim())) {
            this.adCreateTime = adCreateTime;
        }

        String adModifyTime = params.getString("ad_modify_time");
        if (null != adModifyTime && !"".equals(adModifyTime.trim())) {
            this.adModifyTime = adModifyTime;
        }

        String auditRejectReason = params.getString("audit_reject_reason");
        if (null != auditRejectReason && !"".equals(auditRejectReason.trim())) {
            this.auditRejectReason = auditRejectReason;
        }

        String pricing = params.getString("pricing");
        if (null != pricing && !"".equals(pricing.trim())) {
            this.pricing = pricing;
        }

        Integer hideIfExists = params.getInteger("hide_if_exists");
        if (null != hideIfExists) {
            this.hideIfExists = hideIfExists;
        }

        String appType = params.getString("app_type");
        if (null != appType && !"".equals(appType.trim())) {
            this.appType = appType;
        }

        String flowControlMode = params.getString("flow_control_mode");
        if (null != flowControlMode && !"".equals(flowControlMode.trim())) {
            this.flowControlMode = flowControlMode;
        }

        BigDecimal cpaBid = params.getBigDecimal("cpa_bid");
        if (null != cpaBid) {
            this.cpaBid = cpaBid;
        }

        String startTime = params.getString("start_time");
        if (null != startTime && !"".equals(startTime.trim())) {
            this.startTime = startTime;
        }

        String optStatus = params.getString("opt_status");
        if (null != optStatus && !"".equals(optStatus.trim())) {
            this.optStatus = optStatus;
        }

        String endTime = params.getString("end_time");
        if (null != endTime && !"".equals(endTime.trim())) {
            this.endTime = endTime;
        }

        String status = params.getString("status");
        if (null != status && !"".equals(status.trim())) {
            this.toutiaoStatus = status;
        }

        Long convertId = params.getLong("convert_id");
        if (null != convertId) {
            this.convertId = convertId;
        }

        BigDecimal bid = params.getBigDecimal("bid");
        if (null != bid) {
            this.bid = bid;
        }

        String hideIfConverted = params.getString("hide_if_converted");
        if (null != hideIfConverted && !"".equals(hideIfConverted.trim())) {
            this.hideIfConverted = hideIfConverted;
        }

        String toutiaoPackage = params.getString("package");
        if (null != toutiaoPackage && !"".equals(toutiaoPackage.trim())) {
            this.toutiaoPackage = toutiaoPackage;
        }

        String deliveryRange = params.getString("delivery_range");
        if (null != deliveryRange && !"".equals(deliveryRange.trim())) {
            this.deliveryRange = deliveryRange;
        }

        BigDecimal budget = params.getBigDecimal("budget");
        if (null != budget) {
            this.budget = budget;
        }

        String scheduleType = params.getString("schedule_type");
        if (null != scheduleType && !"".equals(scheduleType.trim())) {
            this.scheduleType = scheduleType;
        }

        String scheduleTime = params.getString("schedule_time");
        if (null != scheduleTime && !"".equals(scheduleTime.trim())) {
            this.scheduleTime = scheduleTime;
        }

        String downloadType = params.getString("download_type");
        if (null != downloadType && !"".equals(downloadType.trim())) {
            this.downloadType = downloadType;
        }

        String externalUrl = params.getString("external_url");
        if (null != externalUrl && !"".equals(externalUrl.trim())) {
            this.externalUrl = externalUrl;
        }
    }

    public ByteDanceAdvertisePlan() {
    }
}
