package com.aegis.resource.requisition.domain;

import java.math.BigDecimal;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import com.aegis.common.core.domain.BaseEntity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 征用协议实体类 resource_requisition_agreement
 * 
 * @author aegis
 */
@Getter
@Setter
@ToString(callSuper = true)
public class RequisitionAgreement extends BaseEntity {
    
    private static final long serialVersionUID = 1L;

    /** 协议ID */
    private Long id;

    /** 征用ID */
    private Long requisitionId;

    /** 服务范围 */
    private String serviceScope;

    /** 计费方式：hour-按小时，day-按天，task-按任务 */
    private String billingType;

    /** 单价 */
    private BigDecimal unitPrice;

    /** 币种 */
    private String currency;

    /** 生效开始时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date effectiveFrom;

    /** 生效结束时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date effectiveTo;

    /** 协议条款 */
    private String terms;

    /** 附件URL（文件ID） */
    private String attachmentUrl;

    /**
     * 初始化默认值
     * 
     * @param operator 操作人
     */
    public void initializeDefaults(String operator) {
        if (StringUtils.isBlank(this.currency)) {
            this.currency = "CNY";
        }
    }

    /**
     * 获取计费方式枚举
     * 
     * @return 计费方式枚举
     */
    public BillingType getBillingTypeEnum() {
        return BillingType.fromCode(billingType);
    }

    /**
     * 计费方式枚举
     */
    public enum BillingType {
        HOUR("hour", "按小时"),
        DAY("day", "按天"),
        TASK("task", "按任务");

        private final String code;
        private final String label;

        BillingType(String code, String label) {
            this.code = code;
            this.label = label;
        }

        public String getCode() {
            return code;
        }

        public String getLabel() {
            return label;
        }

        public static BillingType fromCode(String code) {
            if (StringUtils.isBlank(code)) {
                return null;
            }
            for (BillingType value : values()) {
                if (value.code.equals(code)) {
                    return value;
                }
            }
            return null;
        }
    }
}