package com.ruixuan.live.entity;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import lombok.Data;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 快手直播账户对象 kuaishou_live_user_account
 *
 * @author ruoyi
 * @date 2022-05-31
 */
@Data
@ApiModel(value = "kuaishouLiveUserAccount", description = "快手直播账户对象")
public class KuaishouLiveUserAccount implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 主键ID
     */
    private String id;

    /**
     * 账户ID
     */
    private Long projectId;

    /**
     * 快手ID
     */
    private Long ksId;

    /**
     * 快手昵称
     */
    private String ksName;

    /**
     * 账户ID
     */
    private Long accountId;

    /**
     * 账户名称
     */
    private String accountName;

    /**
     * 账户类型
     */
    private String accountType;

    /**
     * 付款方式
     */
    private String paymentMethod;

    /**
     * 销售负责人Id
     */
    private String saleLeaderId;

    /**
     * 销售负责人
     */
    private String saleLeader;

    /**
     * 运营（优化）负责人Id
     */
    private String operatorLeaderId;

    /**
     * 运营（优化）负责人
     */
    private String operatorLeader;

    /**
     * 设计负责人Id
     */
    private String designerLeaderId;

    /**
     * 设计负责人
     */
    private String designerLeader;

    /**
     * 企业名称
     */
    private String enterpriseName;

    /**
     * 一级行业
     */
    private String firstIndustry;

    /**
     * 二级行业
     */
    private String secondaryIndustry;

    /**
     * 资质状态
     */
    private String qualificationStatus;

    /**
     * 账户状态
     */
    private String accountStatus;

    /**
     * 端口名称
     */
    private String portName;

    /**
     * 创建人ID
     */
    private Long createrId;
    private Long userId;
    private String projectName;

    /**
     * 协作人集
     */
    private String collaboratorList;
    /**
     * 更新时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    private Integer count;
    private List<Long> accountIds;

    public KuaishouLiveUserAccount() {

    }

    public KuaishouLiveUserAccount(JSONObject obj, Long createrId) {
        this.setId(UUID.randomUUID().toString().replace("-", ""));
        this.setAccountName(obj.getString("0"));//账户名称
        this.setAccountId(obj.getLong("1"));//广告主 ID
        this.setQualificationStatus(obj.getString("2"));//资质状态
        //快手昵称与快手 ID
        String ksIdAndName = obj.getString("3").replace("(", "（").replace(")", "）").trim();
        String[] split = ksIdAndName.split("（");
        this.setKsName(ksIdAndName.substring(0, ksIdAndName.lastIndexOf("（")));
        this.setKsId(Long.valueOf(ksIdAndName.substring(ksIdAndName.lastIndexOf("（") + 1).replace("）", "")));
        this.setAccountType(AccountTypeEnum.getByName(obj.getString("4")));//账户类型
        this.setPaymentMethod(obj.getString("5"));//付款方式
        this.setProjectName(obj.getString("6"));//产品名
        this.setEnterpriseName(obj.getString("7"));//企业名称
        this.setFirstIndustry(obj.getString("9"));//一级行业
        this.setSecondaryIndustry(obj.getString("10"));//二级行业
        this.setAccountStatus(obj.getString("21"));//账户状态
        this.setOperatorLeader(getUserName(obj.getString("24")));//优化师责任人
        this.setSaleLeader(getUserName(obj.getString("25")));//销售责任人
        this.setCollaboratorList(obj.getString("26"));//协作人列表
        this.setPortName(obj.getString("29"));//端口名称
        this.setCreaterId(createrId);
    }

    private String getUserName(String name) {
        if (name == null) {
            return "";
        }
        name = name.replace("(", "（").replace(")", "）").trim();
        return name.split("（")[0];
    }

    private enum AccountTypeEnum {
        CLJN("cljn", "磁力金牛"),
        CLJN_YD("cljnydd", "磁力金牛移动端"),
        INFOR("xxl", "信息流");

        private final String code;
        private final String name;

        AccountTypeEnum(String code, String name) {
            this.code = code;
            this.name = name;
        }

        // 每次取枚举用for循环遍历
        public static String getByName(String name) {
            for (AccountTypeEnum it : AccountTypeEnum.values()) {
                if (it.getName().equals(name)) {
                    return it.getCode();
                }
            }
            return "";
        }

        public String getCode() {
            return code;
        }

        public String getName() {
            return name;
        }
    }

}
