48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package product
|
|
|
|
type ItemStatus int8
|
|
|
|
func (p *ItemStatus) ToString() string {
|
|
s, _ := StatusToString(*p)
|
|
|
|
return s
|
|
}
|
|
|
|
const (
|
|
StatusActive ItemStatus = 1 + iota // 上架
|
|
StatusInactive // 下架
|
|
StatusOutOfStock // 缺貨
|
|
)
|
|
|
|
const (
|
|
StatusActiveStr = "active"
|
|
StatusInactiveStr = "inactive"
|
|
StatusOutOfStockStr = "out_of_stock"
|
|
)
|
|
|
|
var statusToStringMap = map[ItemStatus]string{
|
|
StatusActive: StatusActiveStr,
|
|
StatusInactive: StatusInactiveStr,
|
|
StatusOutOfStock: StatusOutOfStockStr,
|
|
}
|
|
|
|
var stringToStatusMap = map[string]ItemStatus{
|
|
StatusActiveStr: StatusActive,
|
|
StatusInactiveStr: StatusInactive,
|
|
StatusOutOfStockStr: StatusOutOfStock,
|
|
}
|
|
|
|
// StatusToString 將 ProductItemStatus 轉換為字串
|
|
func StatusToString(status ItemStatus) (string, bool) {
|
|
str, ok := statusToStringMap[status]
|
|
|
|
return str, ok
|
|
}
|
|
|
|
// StringToItemStatus 將字串轉換為 ProductItemStatus
|
|
func StringToItemStatus(statusStr string) (ItemStatus, bool) {
|
|
status, ok := stringToStatusMap[statusStr]
|
|
|
|
return status, ok
|
|
}
|