package com.izouma.zhirongip.domain.resource;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.izouma.zhirongip.annotations.EnumFormat;
import com.izouma.zhirongip.annotations.Searchable;
import com.izouma.zhirongip.converter.StringArrayConverter;
import com.izouma.zhirongip.domain.BaseEntity;
import com.izouma.zhirongip.domain.Setting;
import com.izouma.zhirongip.enums.PurchaseItem;
import com.izouma.zhirongip.enums.PurchaseType;
import com.izouma.zhirongip.utils.excel.EnumExcelConverter;
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.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.Where;

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

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity
@ApiModel(value = "军采信息")
@Where(clause = "del = 0")
public class Purchase extends BaseEntity {

    @ApiModelProperty(value = "项目名称")
    private String name;

    @EnumFormat(value = PurchaseType.class,
            fromExcel = {"公开招标", "邀请招标", "竞争性谈判", "询价"},
            toJavaEnum = {"PUBLIC_TENDER", "INVITATION_TO_TENDER", "COMPETITIVE_NEGOTIATION", "INQUIRY"})
    @ExcelProperty(value = "采购类型", converter = EnumExcelConverter.class)
    @Enumerated(EnumType.STRING)
    @ApiModelProperty(value = "采购类型")
    private PurchaseType purchaseType;

    @EnumFormat(value = PurchaseItem.class,
            fromExcel = {"工程", "货物", "服务"},
            toJavaEnum = {"ENGINEERING", "CARGO", "SERVICE"})
    @ExcelProperty(value = "采购项目", converter = EnumExcelConverter.class)
    @Enumerated(EnumType.STRING)
    @ApiModelProperty(value = "采购项目")
    private PurchaseItem purchaseItem;

    @ApiModelProperty(value = "报名截止时间")
    private LocalDate registerDeadline;

    @ExcelIgnore
    @ApiModelProperty(value = "专业领域")
    private Long field;

    @ApiModelProperty(value = "地点")
    private String address;

    @ApiModelProperty(value = "预算（万元）")
    private BigDecimal expectedPrice;

    @Searchable
    @ExcelIgnore
    @ApiModelProperty(value = "详情介绍")
    @Column(columnDefinition = "TEXT")
    private String content;

    @ApiModelProperty(value = "浏览量")
    private long view;

    @ExcelIgnore
    @Convert(converter = StringArrayConverter.class)
    @Column(columnDefinition = "TEXT")
    private List<String> img;

    @ExcelIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "field", insertable = false, updatable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    @NotFound(action = NotFoundAction.IGNORE)
    private Setting setting;

}
