package com.izouma.nineth.domain;

import com.izouma.nineth.annotations.Searchable;
import com.izouma.nineth.converter.FileObjectConverter;
import com.izouma.nineth.converter.FileObjectListConverter;
import com.izouma.nineth.converter.PrivilegeListConverter;
import com.izouma.nineth.converter.PropertyListConverter;
import com.izouma.nineth.enums.CollectionSource;
import com.izouma.nineth.enums.CollectionType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Formula;

import javax.persistence.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Data
@Entity
@Table(name = "collection_info", indexes = {
        @Index(columnList = "type,source"),
        @Index(columnList = "minterId"),
        @Index(columnList = "onShelf"),
        @Index(columnList = "ownerId"),
        @Index(columnList = "assetId")
})
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ApiModel("藏品")
public class Collection extends BaseEntity {

    @ApiModelProperty("名称")
    @Searchable
    private String name;

    @ApiModelProperty("图片")
    @Column(columnDefinition = "TEXT")
    @Convert(converter = FileObjectListConverter.class)
    private List<FileObject> pic;

    @Column(columnDefinition = "TEXT")
    @Convert(converter = FileObjectConverter.class)
    private FileObject model3d;

    @ApiModelProperty("铸造者")
//    @Searchable
    private String minter;

    @ApiModelProperty("铸造者ID")
    private Long minterId;

    @ApiModelProperty("铸造者头像")
    private String minterAvatar;

    @ApiModelProperty("拥有者")
//    @Searchable
    private String owner;

    @ApiModelProperty("拥有者ID")
    private Long ownerId;

    @ApiModelProperty("拥有者头像")
    private String ownerAvatar;

    @ApiModelProperty("详情")
    @Column(columnDefinition = "TEXT")
    private String detail;

    @ApiModelProperty("类型")
    @Enumerated(EnumType.STRING)
    private CollectionType type;

    @Column(columnDefinition = "TEXT")
    @Convert(converter = PropertyListConverter.class)
    @ApiModelProperty("特性")
    private List<CollectionProperty> properties;

    @Column(columnDefinition = "TEXT")
    @Convert(converter = PrivilegeListConverter.class)
    @ApiModelProperty("特权")
    private List<Privilege> privileges;

    @ApiModelProperty("编号")
    private Integer number;

    @ApiModelProperty("是否可转售")
    private boolean canResale;

    @ApiModelProperty("版税比例")
    private int royalties;

    @ApiModelProperty("手续费比例")
    private int serviceCharge;

    @ApiModelProperty("分类")
    private String category;

    @ApiModelProperty("来源")
    @Enumerated(EnumType.STRING)
    private CollectionSource source;

    @ApiModelProperty("已售")
    private int sale;

    @ApiModelProperty("库存")
    private int stock;

    @ApiModelProperty("发行数量")
    private int total;

    @ApiModelProperty("点赞")
    private int likes;

    @ApiModelProperty("上架")
    private boolean onShelf;

    @ApiModelProperty("可售")
    private boolean salable;

    @ApiModelProperty("价格")
    @Column(precision = 10, scale = 2)
    private BigDecimal price;

    @ApiModelProperty("盲盒开售时间")
    private LocalDateTime startTime;

    /**
     * 是否定时发售
     */
    private boolean scheduleSale;

    private int sort;

    private Long assetId;

    private BigDecimal originalPrice;

    private Integer currentNumber;

    @Formula(value = "if(stock <= 0, 1, 0)")
    private int soldOut;

    private int projectId;

    /**
     * 主图模式，用于随机选取图片到资产
     */
    private String picPattern;

    /**
     * 限购数量
     */
    private Integer limitBuyCount;

    /**
     *藏品目录信息
     */
    @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH})
    @JoinColumn(name = "collection_category_id")
    private CollectionCategory collectionCategory;

    /**
     * 藏品分类表ID
     */
    @Column(name = "collection_category_id",insertable = false, updatable = false)
    private Long collectionCategoryId;

    /**
     *搜索集合 为一级菜单ID,二级菜单ID,三级菜单ID 这样存
     */
    private String categories;


    @ApiModelProperty("盲盒是否自动打开，默认自动打开，true用户手动打开")
    private boolean variableType ;

    @ApiModelProperty("可变资产ID")
    private Long variableCollectionId ;
}
