package com.microservices.pms.product.domain.enums;

import com.microservices.common.core.exception.ServiceException;
import com.microservices.pms.product.domain.PmsProductReqOperationRecord;
import com.microservices.system.api.domain.SimpleSysUser;
import lombok.Getter;

import java.text.SimpleDateFormat;

/**
 * 产品需求操作类型
 *
 * @author OTTO
 */
@Getter
public enum ProductReqOperationType {
    CONTENT_MODIFY("contentModify", "内容修改"),
    REVIEW_USER_REQ("reviewUserReq", "用户需求评审"),
    ACTIVE_PLAN("activePlan", "纳入计划"),
    REVIEW_REQ_SPECS("reviewReqSpecs", "需求规格评审");
    private final String key;
    private final String name;

    ProductReqOperationType(String key, String name) {
        this.key = key;
        this.name = name;
    }

    public static String genRecordStr(SimpleSysUser operator, PmsProductReqOperationRecord record) {
        ProductReqOperationType type = getByKey(record.getType());
        if (type == null) {
            throw new ServiceException("需求操作记录类型错误");
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String operationTimeStr = formatter.format(record.getOperatingTime());
        switch (type) {
            case CONTENT_MODIFY:
                return String.format("%s,由【%s】%s",
                        operationTimeStr,
                        operator.getNickName(),
                        ProductContentModifyOperation.getNameByKey(record.getOperation()));
            case REVIEW_USER_REQ:
                return String.format("%s,由【%s】进行了用户需求评审，评审结果为【%s】",
                        operationTimeStr,
                        operator.getNickName(),
                        ProductReqReviewStatus.getNameByKey(record.getOperation()));
            case ACTIVE_PLAN:
                return String.format("%s,由【%s】将需求纳入了计划",
                        operationTimeStr,
                        operator.getNickName());
            case REVIEW_REQ_SPECS:
                return String.format("%s,由【%s】进行了需求规格评审，评审结果为【%s】",
                        operationTimeStr,
                        operator.getNickName(),
                        ProductReqReviewStatus.getNameByKey(record.getOperation()));
            default:
                throw new ServiceException("需求操作记录类型错误");
        }
    }

    public static ProductReqOperationType getByKey(String key) {
        for (ProductReqOperationType type : ProductReqOperationType.values()) {
            if (type.getKey().equals(key)) {
                return type;
            }
        }
        return null;
    }
}
