package cn.palmte.work.model.process;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import org.hibernate.annotations.GenericGenerator;

import java.io.IOException;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;

import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PrePersist;
import javax.persistence.Table;

import cn.palmte.work.model.enums.ProcessStatus;
import cn.palmte.work.model.enums.ProcessType;
import lombok.Data;

/**
 * 项目相关的流程
 *
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
 * @since 2.0 2022/12/14 16:11
 */
@Data
@Entity
@Table(name = "project_process")
public class ProjectProcess implements Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @GenericGenerator(name = "persistenceGenerator", strategy = "increment")
  private Integer id;

  private Integer projectId;

  // 项目编号
  private String projectNo;

  @JsonFormat(pattern = "yyyy-MM-dd")
  private LocalDate applyDate;

  // 项目标题
  private String projectTitle;

  // 申请人
  private String applyPersonName;

  // 申请人 ID
  private Integer applyPersonId;

  // 申请部门 部门1,部门2,部门3 (逗号分割)
  private String applyDept;

  // 申请部门ID(逗号分割)
  private String applyDeptId;

  // 申请部门领导ID
  private Integer applyDeptLeaderId;

  // 申请部门领导
  private String applyDeptLeaderName;

  // 合同编号
  private String contractNo;

  // 合同名称
  private String contractName;

  // 用印类型
  @JsonSerialize(using = SealTypeArraySerializer.class)
  @Convert(converter = SealTypeArrayConverter.class)
  private SealTypeArray sealTypes;

  // 税率
  private String taxRate;

  // 状态
  @Enumerated(EnumType.STRING)
  private ProcessStatus status;

  // 当前审核人
  private String currentAudit;

  // 当前审核人ID
  private String currentAuditId;

  // 最后更新时间

  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  private LocalDateTime lastUpdateAt;

  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  private LocalDateTime createAt;

  @Enumerated(EnumType.STRING)
  private ProcessType processType;

  private String remark;

  // 附件 JSON Array
  private String attachmentUri;

  // 项目类型
//  @Enumerated(EnumType.STRING)
//  private ProjectType projectType;

  // 合作类型
//  @Enumerated(EnumType.STRING)
//  private CooperationType cooperationType;

  @PrePersist
  void prePersist() {
    this.createAt = LocalDateTime.now();
  }

  static class SealTypeArraySerializer extends JsonSerializer<SealTypeArray> {

    @Override
    public void serialize(SealTypeArray value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
      serializers.defaultSerializeValue(value.getList(), gen);
    }
  }

}
