package cn.com.ctop.toutiao.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.jeecgframework.poi.excel.annotation.Excel;

import java.util.Date;

/**
 * 今日头条受众模板信息
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2019-07-23
 */
@Data
@TableName("ctop_bytedance_user_orientation_template")
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value = "ctop_bytedance_user_orientation_template对象", description = "今日头条受众模板信息")
public class ByteDanceUserOrientationTemplate {

    /**
     * id
     */
    @TableId(type = IdType.AUTO)
    @ApiModelProperty(value = "id")
    private Long id;
    /**
     * 平台广告主id
     * userId
     */
    @Excel(name = "用户id")
    @ApiModelProperty(value = "user_id")
    private String userId;
    /**
     * 模板名称
     */
    @Excel(name = "模板名称", width = 15)
    @ApiModelProperty(value = "模板名称")
    private String name;
    /**
     * 受众性别
     */
    @Excel(name = "受众性别", width = 15)
    @ApiModelProperty(value = "受众性别")
    private String gender;
    /**
     * 年龄
     */
    @Excel(name = "年龄", width = 15)
    @ApiModelProperty(value = "年龄")
    private String age;
    /**
     * 受众最低android版本
     */
    @Excel(name = "受众最低android版本", width = 15)
    @ApiModelProperty(value = "受众最低android版本")
    private String androidOsv;
    /**
     * 受众最低ios版本
     */
    @Excel(name = "受众最低ios版本", width = 15)
    @ApiModelProperty(value = "受众最低ios版本")
    private String iosOsv;
    /**
     * 运营商类型
     */
    @Excel(name = "运营商类型", width = 15)
    @ApiModelProperty(value = "运营商类型")
    private String carrier;
    /**
     * 受众网络类型
     */
    @Excel(name = "受众网络类型", width = 15)
    @ApiModelProperty(value = "受众网络类型")
    private String ac;
    /**
     * 手机品牌
     */
    @Excel(name = "手机品牌", width = 15)
    @ApiModelProperty(value = "手机品牌")
    private String deviceBrand;
    /**
     * 受众文章分类
     */
    @Excel(name = "受众文章分类", width = 15)
    @ApiModelProperty(value = "受众文章分类")
    private String articleCategory;
    /**
     * 用户首次激活时间
     */
    @Excel(name = "用户首次激活时间", width = 15)
    @ApiModelProperty(value = "用户首次激活时间")
    private String activateType;
    /**
     * 受众平台
     */
    @Excel(name = "受众平台", width = 15)
    @ApiModelProperty(value = "受众平台")
    private String platform;
    /**
     * 地域定向城市或者区县列表
     */
    @Excel(name = "地域定向城市或者区县列表", width = 15)
    @ApiModelProperty(value = "地域定向城市或者区县列表")
    private String city;
    /**
     * 商圈ID数组
     */
    @Excel(name = "商圈ID数组", width = 15)
    @ApiModelProperty(value = "商圈ID数组")
    private String businessIds;
    /**
     * 地域类型
     */
    @Excel(name = "地域类型", width = 15)
    @ApiModelProperty(value = "地域类型")
    private String district;
    /**
     * 受众位置类型
     */
    @Excel(name = "受众位置类型", width = 15)
    @ApiModelProperty(value = "受众位置类型")
    private String locationType;
    /**
     * 兴趣分类
     */
    @Excel(name = "兴趣分类", width = 15)
    @ApiModelProperty(value = "兴趣分类")
    private String adTag;
    /**
     * 兴趣关键词, 传入具体的词id，非兴趣词包id
     */
    @Excel(name = "兴趣关键词, 传入具体的词id，非兴趣词包id", width = 15)
    @ApiModelProperty(value = "兴趣关键词, 传入具体的词id，非兴趣词包id")
    private String interestTags;
    /**
     * 行为定向
     */
    @Excel(name = "行为定向", width = 15)
    @ApiModelProperty(value = "行为定向")
    private String appBehaviorTarget;
    /**
     * APP行为定向,分类集合
     */
    @Excel(name = "APP行为定向,分类集合", width = 15)
    @ApiModelProperty(value = "APP行为定向,分类集合")
    private String appCategory;
    /**
     * APP行为定向,APP集合
     */
    @Excel(name = "APP行为定向,APP集合", width = 15)
    @ApiModelProperty(value = "APP行为定向,APP集合")
    private String appIds;
    /**
     * 产品目录ID(ID由查询产品目录接口得到)
     */
    @Excel(name = "产品目录ID(ID由查询产品目录接口得到)", width = 15)
    @ApiModelProperty(value = "产品目录ID(ID由查询产品目录接口得到)")
    private String productPlatformId;
    /**
     * H5地址参数
     */
    @Excel(name = "H5地址参数", width = 15)
    @ApiModelProperty(value = "H5地址参数")
    private String externalUrlParams;
    /**
     * 直达链接参数
     */
    @Excel(name = "直达链接参数", width = 15)
    @ApiModelProperty(value = "直达链接参数")
    private String openUrlParams;
    /**
     * 是否自定义商品定向
     */
    @Excel(name = "是否自定义商品定向", width = 15)
    @ApiModelProperty(value = "是否自定义商品定向")
    private String dpaLocalAudience;
    /**
     * 排除人群包
     */
    @Excel(name = "排除人群包", width = 15)
    @ApiModelProperty(value = "排除人群包")
    private String excludeCustomActions;
    /**
     * 包含人群包
     */
    @Excel(name = "包含人群包", width = 15)
    @ApiModelProperty(value = "包含人群包")
    private String includeCustomActions;
    /**
     * 精选流量包
     */
    @Excel(name = "精选流量包", width = 15)
    @ApiModelProperty(value = "精选流量包")
    private String superiorPopularityType;
    /**
     * 定向流量包ID数组
     */
    @Excel(name = "定向流量包ID数组", width = 15)
    @ApiModelProperty(value = "定向流量包ID数组")
    private String flowPackage;
    /**
     * 排除流量包ID数组
     */
    @Excel(name = "排除流量包ID数组", width = 15)
    @ApiModelProperty(value = "排除流量包ID数组")
    private String excludeFlowPackage;

    /**
     * 智能放量
     */
    @Excel(name = "是否开启智能放量", width = 15)
    @ApiModelProperty(value = "是否开启智能放量")
    private String autoExtendEnabled;

    @Excel(name = "区域类型", width = 15)
    @ApiModelProperty(value = "区域类型")
    private String areaType;

    @Excel(name = "兴趣类型", width = 15)
    @ApiModelProperty(value = "兴趣类型")
    private String intrestType;

    @Excel(name = "文章分类类型", width = 15)
    @ApiModelProperty(value = "文章分类类型")
    private String articleCategoryType;

    @Excel(name = "手机品牌", width = 15)
    @ApiModelProperty(value = "手机品牌")
    private String mobileBrand;

    private String cityStr;

    private String districtStr;

    private String adTagStr;

    private String deviceType;

    /**
     * status
     */
    @Excel(name = "status", width = 15)
    @ApiModelProperty(value = "status")
    private Integer status;
    /**
     * createTime
     */
    @ApiModelProperty(value = "createTime")
    private Date createTime;
    /**
     * updateTime
     */
    @ApiModelProperty(value = "updateTime")
    private Date updateTime;

    public ByteDanceUserOrientationTemplate() {
    }

    public ByteDanceUserOrientationTemplate(String userId, JSONObject template) {
        Long id = template.getLong("id");
        if (null != id && id != 0) {
            this.id = id;
        }
        this.userId = userId;
        String name = template.getString("name");
        if (null != name && !"".equals(name)) {
            this.name = name;
        } else {
            this.name = "汇创思拓_" + System.currentTimeMillis() + "定向模板";
        }

        String gender = template.getString("genden");
        if (null != gender && !"".equals(gender)) {
            this.gender = gender;
        } else {
            this.gender = "NONE";
        }
        JSONArray ageRange = template.getJSONArray("ageRange");
        if (null != ageRange) {
            this.age = ageRange.toJSONString();
        }
        String deviceType = template.getString("deviceType");
        if (null != deviceType && !"".equals(deviceType.trim())) {
            this.deviceType = deviceType;
        }
        String superiorPopularityType = template.getString("superiorPopularityType");
        if (null != superiorPopularityType && !"".equals(superiorPopularityType.trim())) {
            this.superiorPopularityType = superiorPopularityType;
        }
        //兴趣定向类别
        String intrestType = template.getString("intrestType");
        if (null != intrestType && !"".equals(intrestType)) {
            this.intrestType = intrestType;
            if ("NONE".equals(intrestType)) {
                this.adTag = "[]";
            } else if ("SYSTEM_TJ".equals(intrestType)) {
                this.adTag = "[" + 0 + "]";
            } else {
                //兴趣分类
                JSONArray categoryList = template.getJSONArray("intrestCategoryList");
                JSONArray tags = new JSONArray();
                JSONArray adTagStr = new JSONArray();
                if (null != categoryList && categoryList.size() > 0) {
                    categoryList.forEach(category -> {
                        JSONObject categoryObject = JSONObject.parseObject(JSONObject.toJSONString(category));
                        adTagStr.add(categoryObject);
                        Long tagId = categoryObject.getLong("value");
                        tags.add(tagId);
                    });
                }
                this.adTag = tags.toJSONString();
                this.adTagStr = adTagStr.toJSONString();

            }
        }
        String platformType = template.getString("platformType");
        if (null != platformType && !"".equals(platformType)) {
            this.platform = platformType;
        }
        String areaType = template.getString("areaType");
        if (null != areaType && !"".equals(areaType)) {
            this.areaType = areaType;
        }
        JSONArray netType = template.getJSONArray("netType");
        if (null != netType) {
            this.ac = netType.toJSONString();
        }
        String smartFl = template.getString("smartFl");
        if (null != smartFl && !"".equals(smartFl)) {
            this.autoExtendEnabled = smartFl;
        }
        String isArticleCategory = template.getString("isArticleCategory");
        if (null != isArticleCategory && !"".equals(isArticleCategory)) {
            this.articleCategoryType = isArticleCategory;
            if ("CATEGORY".equals(isArticleCategory)) {
                this.articleCategory = template.getJSONArray("tags").toJSONString();
            }
        }
        String isMobileBrand = template.getString("isMobileBrand");
        if (null != isMobileBrand && !"".equals(isMobileBrand)) {
            this.mobileBrand = isMobileBrand;
            if ("BRAND".equals(isArticleCategory)) {
                this.deviceBrand = template.getJSONArray("mobileType").toJSONString();
            }
        }
        JSONArray cityList = template.getJSONArray("cityList");
        JSONArray cityArray = new JSONArray();
        JSONArray cityStr = new JSONArray();
        if (null != cityList && cityList.size() > 0) {
            cityList.forEach(city -> {
                JSONObject cityObject = JSONObject.parseObject(JSONObject.toJSONString(city));
                cityStr.add(cityObject);
                Long cityId = cityObject.getLong("value");
                cityArray.add(cityId);
            });
        }
        this.city = cityArray.toJSONString();
        this.cityStr = cityStr.toJSONString();

        JSONArray areaList = template.getJSONArray("areaList");
        JSONArray areaArray = new JSONArray();
        JSONArray areaStr = new JSONArray();
        if (null != areaList && areaList.size() > 0) {
            areaList.forEach(area -> {
                JSONObject areaObject = JSONObject.parseObject(JSONObject.toJSONString(area));
                areaStr.add(areaObject);
                Long areaId = areaObject.getLong("value");
                areaArray.add(areaId);
            });
        }
        this.district = areaArray.toJSONString();
        this.districtStr = areaStr.toJSONString();

        //兴趣关键词
        JSONArray intrestTagList = template.getJSONArray("intrestTagList");
        JSONArray intrestTagArray = new JSONArray();
        if (null != intrestTagList && intrestTagList.size() > 0) {
            intrestTagList.forEach(intrestCategory -> {
                JSONObject intrestCategoryObject = JSONObject.parseObject(JSONObject.toJSONString(intrestCategory));
                Long intrestId = intrestCategoryObject.getLong("value");
                intrestTagArray.add(intrestId);
            });
        }
        this.interestTags = areaArray.toJSONString();
    }
}
