product.go 2.3 KB
package domain

import "time"

// 产品信息
type Product struct {
	// 企业id
	CompanyId int `json:"companyId,omitempty"`
	// 组织ID
	OrgId int `json:"orgId,omitempty"`
	// 产品ID
	ProductId int `json:"productId,omitempty"`
	// 产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001
	ProductCode string `json:"productCode,omitempty"`
	// 产品名称
	ProductName string `json:"productName,omitempty"`
	// 产品类别
	ProductCategory string `json:"productCategory,omitempty"`
	// 产品规格
	ProductSpec *UnitQuantity `json:"productSpec,omitempty"`
	// 创建时间
	CreatedAt time.Time `json:"createdAt,omitempty"`
	// 更新时间
	UpdatedAt time.Time `json:"updatedAt,omitempty"`
	// 删除时间
	DeletedAt time.Time `json:"deletedAt,omitempty"`
}

type ProductRepository interface {
	Save(product *Product) (*Product, error)
	Remove(product *Product) (*Product, error)
	FindOne(queryOptions map[string]interface{}) (*Product, error)
	Find(queryOptions map[string]interface{}) (int64, []*Product, error)
}

func (product *Product) Identify() interface{} {
	if product.ProductId == 0 {
		return nil
	}
	return product.ProductId
}

func (product *Product) Update(data map[string]interface{}) error {
	if companyId, ok := data["companyId"]; ok {
		product.CompanyId = companyId.(int)
	}
	if orgId, ok := data["orgId"]; ok {
		product.OrgId = orgId.(int)
	}
	if productCode, ok := data["productCode"]; ok {
		product.ProductCode = productCode.(string)
	}
	if productName, ok := data["productName"]; ok {
		product.ProductName = productName.(string)
	}
	if productCategory, ok := data["productCategory"]; ok {
		product.ProductCategory = productCategory.(string)
	}
	if quantity, ok := data["quantity"]; ok {
		product.ProductSpec.Quantity = quantity.(float64)
	}
	if unit, ok := data["unit"]; ok {
		product.ProductSpec.Unit = unit.(string)
	}
	if unitWeight, ok := data["unitWeight"]; ok {
		product.ProductSpec.UnitWeight = unitWeight.(float64)
	}
	if weight, ok := data["weight"]; ok {
		product.ProductSpec.Weight = weight.(float64)
	}
	if createdAt, ok := data["createdAt"]; ok {
		product.CreatedAt = createdAt.(time.Time)
	}
	if updatedAt, ok := data["updatedAt"]; ok {
		product.UpdatedAt = updatedAt.(time.Time)
	}
	if deletedAt, ok := data["deletedAt"]; ok {
		product.DeletedAt = deletedAt.(time.Time)
	}
	return nil
}