package com.izouma.wenlvju.domain;

import cn.hutool.core.util.StrUtil;
import com.izouma.wenlvju.annotations.Searchable;
import com.izouma.wenlvju.converter.LongArrayConverter;
import com.izouma.wenlvju.converter.ReviewTimeListConverter;
import com.izouma.wenlvju.converter.StringArrayConverter;
import com.izouma.wenlvju.dto.ReviewTime;
import com.izouma.wenlvju.enums.OrganizationGrade;
import com.izouma.wenlvju.enums.RateStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity
@Where(clause = "del = 0")
@ApiModel(value = "等级评定")
public class Rate extends BaseEntity {
    @Searchable
    @ApiModelProperty(value = "承办单位名称")
    private String name;

    @ApiModelProperty(value = "负责人")
    private String owner;

    private String ownerPhone;

    private String ownerEmail;

    private String district;

    @Enumerated(EnumType.STRING)
    private RateStatus status;

    @ApiModelProperty(value = "是否承办考级活动")
    private boolean undertakeExamination;

    @Column(columnDefinition = "TEXT")
    @ApiModelProperty(value = "单位概况")
    private String introduction;

    @Column(columnDefinition = "TEXT")
    @Convert(converter = StringArrayConverter.class)
    @ApiModelProperty(value = "附件")
    private List<String> introductionAnnex;

    @ApiModelProperty(value = "是否提交")
    private boolean submit;

    @ApiModelProperty(value = "承办单位")
    private Long organizationId;

    @ApiModelProperty(value = "年度")
    private String year;

    @Column(columnDefinition = "TEXT")
    @Convert(converter = StringArrayConverter.class)
    @ApiModelProperty(value = "法人资格")
    private List<String> privacyPolicy;

    @Column(columnDefinition = "TEXT")
    @Convert(converter = StringArrayConverter.class)
    @ApiModelProperty(value = "业务内容")
    private List<String> business;

    @Column(columnDefinition = "TEXT")
    @Convert(converter = StringArrayConverter.class)
    @ApiModelProperty(value = "社会信誉")
    private List<String> credits;

    @Column(columnDefinition = "TEXT")
    @Convert(converter = StringArrayConverter.class)
    @ApiModelProperty(value = "消防安全")
    private List<String> fire;

    @Column(columnDefinition = "TEXT")
    @Convert(converter = StringArrayConverter.class)
    @ApiModelProperty(value = "卫生防疫")
    private List<String> hygiene;

    @Column(columnDefinition = "TEXT")
    @Convert(converter = StringArrayConverter.class)
    @ApiModelProperty(value = "财务报表")
    private List<String> finance;

    @Column(columnDefinition = "TEXT")
    @Convert(converter = StringArrayConverter.class)
    @ApiModelProperty(value = "房产证明")
    private List<String> property;

    @ApiModelProperty(value = "专家组长")
    private Long expertUserId;

    @Transient
    private Map<String, String> expertUser;

    @Convert(converter = LongArrayConverter.class)
    @ApiModelProperty(value = "专家组员")
    private List<Long> expertMemberUserId;

    @ApiModelProperty(value = "申请时间")
    private LocalDateTime applyTime;

    @ApiModelProperty(value = "审核开始时间")
    private LocalDateTime reviewStartTime;

    @ApiModelProperty(value = "审核结束时间")
    private LocalDateTime reviewEndTime;

    @ApiModelProperty(value = "审查时间")
    private LocalDateTime reviewTime;

    /*
    复审结果
    市级录入成绩
    */
    @ApiModelProperty(value = "分数")
    private int score;

    /*
    优秀/合格/不合格
     */
    @Enumerated(EnumType.STRING)
    @ApiModelProperty(value = "等级")
    private OrganizationGrade grade;

    @Transient
    private String organizer;

    @Transient
    private String address;

    @Column(columnDefinition = "TEXT")
    @Convert(converter = ReviewTimeListConverter.class)
    private List<ReviewTime> reviewTimes = new ArrayList<>();

    private String remark;

    private boolean isRejected;

    private int sort;

    @ApiModelProperty(value = "驳回时间")
    private LocalDateTime rejectedAt;

    @ApiModelProperty(value = "是否变更地址")
    private boolean isChange;

    @ApiModelProperty(value = "变更地址")
    private String changeAddress;

    public String getDetailAddress() {
        String str = "江苏省南京市" + this.district;

        if (this.isChange && StrUtil.isNotBlank(this.changeAddress)) {
            str += this.changeAddress;
        } else if (StrUtil.isNotBlank(this.address)) {
            str += this.address;
        }
        return str;
    }
}
