package com.qmth.sop.business.entity;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.qmth.sop.business.bean.dto.CrmDetailImportDto;
import com.qmth.sop.common.base.BaseEntity;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.CrmStatusEnum;
import com.qmth.sop.common.enums.ServiceScopeEnum;
import com.qmth.sop.common.enums.SopStatusEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Objects;

/**
 * <p>
 * 派单明细表
 * </p>
 *
 * @author wangliang
 * @since 2024-05-14
 */
@ApiModel(value = "TBCrmDetail对象", description = "派单明细")
public class TBCrmDetail extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "派单号")
    @NotNull(message = "派单号不能为空")
    private String crmNo;

    @ApiModelProperty(value = "流程部署id")
    private String flowDeploymentId;

    @ApiModelProperty(value = "sop单号")
    private String sopNo;

    @ApiModelProperty(value = "状态，UN_PUBLISH：未发布，PUBLISH：发布")
    private CrmStatusEnum status;

    @ApiModelProperty(value = "科目名称")
    @NotNull(message = "科目名称不能为空")
    private String courseName;

    @ApiModelProperty(value = "考生人数")
    @NotNull(message = "考生人数不能为空")
    private Integer examStudentCount;

    @ApiModelProperty(value = "联系人")
    private String contacts;

    @ApiModelProperty(value = "电话")
    private String mobileNumber;

    @ApiModelProperty(value = "服务范围，SCAN：仅扫描，SCAN_MARK：扫描+阅卷")
    @NotNull(message = "服务范围不能为空")
    private ServiceScopeEnum serviceScope;

    @ApiModelProperty(value = "扫描开始时间")
    @NotNull(message = "扫描开始时间不能为空")
    private Long scanStartTime;

    @ApiModelProperty(value = "扫描结束时间")
    @NotNull(message = "扫描结束时间不能为空")
    private Long scanEndTime;

    @ApiModelProperty(value = "阅卷开始时间")
    @NotNull(message = "阅卷开始时间不能为空")
    private Long markPaperStartTime;

    @ApiModelProperty(value = "阅卷结束时间")
    @NotNull(message = "阅卷结束时间不能为空")
    private Long markPaperEndTime;

    @ApiModelProperty(value = "sop状态")
    @TableField(exist = false)
    String sopStatusStr;

    @ApiModelProperty(value = "状态，DRAFT：草稿，START：提交")
    @TableField(exist = false)
    private SopStatusEnum sopStatus;

    public TBCrmDetail() {

    }

    public TBCrmDetail(String crmNo, String flowDeploymentId, CrmDetailImportDto crmDetailImportDto, Long userId) {
        setId(SystemConstant.getDbUuid());
        this.crmNo = crmNo;
        this.flowDeploymentId = flowDeploymentId;
        this.courseName = crmDetailImportDto.getCourseName();
        this.examStudentCount = crmDetailImportDto.getExamStudentCount();
        this.contacts = crmDetailImportDto.getContacts();
        this.mobileNumber = crmDetailImportDto.getMobileNumber();
        this.serviceScope = ServiceScopeEnum.convertToName(crmDetailImportDto.getServiceScope());
        this.scanStartTime = DateUtil.parse(crmDetailImportDto.getScanStartTime(),
                SystemConstant.DEFAULT_DATE_CRM_DETAIL_SOP_PATTERN).getTime();
        this.scanEndTime = DateUtil.parse(crmDetailImportDto.getScanEndTime(),
                SystemConstant.DEFAULT_DATE_CRM_DETAIL_SOP_PATTERN).getTime();
        this.markPaperStartTime = DateUtil.parse(crmDetailImportDto.getMarkPaperStartTime(),
                SystemConstant.DEFAULT_DATE_CRM_DETAIL_SOP_PATTERN).getTime();
        this.markPaperEndTime = DateUtil.parse(crmDetailImportDto.getMarkPaperEndTime(),
                SystemConstant.DEFAULT_DATE_CRM_DETAIL_SOP_PATTERN).getTime();
        this.status = CrmStatusEnum.UN_PUBLISH;
        insertInfo(userId);
    }

    /**
     * 创建sop
     *
     * @param flowDeploymentId
     * @param userId
     */
    public void createSop(String flowDeploymentId, Long userId) {
        setId(SystemConstant.getDbUuid());
        this.flowDeploymentId = flowDeploymentId;
        this.status = CrmStatusEnum.UN_PUBLISH;
        insertInfo(userId);
    }

    /**
     * 修改状态
     *
     * @param crmStatusEnum
     * @param userId
     */
    public void updateStatus(CrmStatusEnum crmStatusEnum, Long userId) {
        this.status = crmStatusEnum;
        updateInfo(userId);
    }

    public String getSopStatusStr() {
        if (Objects.nonNull(this.sopStatus)) {
            return this.sopStatus.getTitle();
        } else {
            return sopStatusStr;
        }
    }

    public void setSopStatusStr(String sopStatusStr) {
        this.sopStatusStr = sopStatusStr;
    }

    public SopStatusEnum getSopStatus() {
        return sopStatus;
    }

    public void setSopStatus(SopStatusEnum sopStatus) {
        this.sopStatus = sopStatus;
    }

    public String getCrmNo() {
        return crmNo;
    }

    public void setCrmNo(String crmNo) {
        this.crmNo = crmNo;
    }

    public String getFlowDeploymentId() {
        return flowDeploymentId;
    }

    public void setFlowDeploymentId(String flowDeploymentId) {
        this.flowDeploymentId = flowDeploymentId;
    }

    public String getSopNo() {
        return sopNo;
    }

    public void setSopNo(String sopNo) {
        this.sopNo = sopNo;
    }

    public CrmStatusEnum getStatus() {
        return status;
    }

    public void setStatus(CrmStatusEnum status) {
        this.status = status;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public Integer getExamStudentCount() {
        return examStudentCount;
    }

    public void setExamStudentCount(Integer examStudentCount) {
        this.examStudentCount = examStudentCount;
    }

    public String getContacts() {
        return contacts;
    }

    public void setContacts(String contacts) {
        this.contacts = contacts;
    }

    public String getMobileNumber() {
        return mobileNumber;
    }

    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }

    public ServiceScopeEnum getServiceScope() {
        return serviceScope;
    }

    public void setServiceScope(ServiceScopeEnum serviceScope) {
        this.serviceScope = serviceScope;
    }

    public Long getScanStartTime() {
        return scanStartTime;
    }

    public void setScanStartTime(Long scanStartTime) {
        this.scanStartTime = scanStartTime;
    }

    public Long getScanEndTime() {
        return scanEndTime;
    }

    public void setScanEndTime(Long scanEndTime) {
        this.scanEndTime = scanEndTime;
    }

    public Long getMarkPaperStartTime() {
        return markPaperStartTime;
    }

    public void setMarkPaperStartTime(Long markPaperStartTime) {
        this.markPaperStartTime = markPaperStartTime;
    }

    public Long getMarkPaperEndTime() {
        return markPaperEndTime;
    }

    public void setMarkPaperEndTime(Long markPaperEndTime) {
        this.markPaperEndTime = markPaperEndTime;
    }
}
