package com.goafanti.common.enums;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

public enum OrderState {
	UNPAYED(0, "未支付"), PAYED(1, "已支付"), PAYING(2, "支付中"), COMPLETED(3, "完成"), CANCELLED(9, "取消"), INVALID(10, "无效");

	private OrderState(Integer code, String desc) {
		this.code = code;
		this.desc = desc;
	}

	private static Map<Integer, OrderState> status = new HashMap<Integer, OrderState>();

	static {
		for (OrderState value : OrderState.values()) {
			status.put(value.getCode(), value);
		}
	}

	public static boolean isPayed(OrderState os) {
		return os == PAYED || os == COMPLETED;
	}

	public static OrderState getStatus(Integer code) {
		if (containsType(code)) {
			return status.get(code);
		}
		return INVALID;
	}

	public static OrderState getStatus(String code) {
		if (StringUtils.isNumeric(code)) {
			return getStatus(Integer.parseInt(code));
		}
		return INVALID;
	}

	public static boolean containsType(Integer code) {
		return status.containsKey(code);
	}

	private Integer	code;
	private String	desc;

	public Integer getCode() {
		return code;
	}

	public String getDesc() {
		return desc;
	}

}
