product_material_group.go 2.9 KB
package domain

import (
	"fmt"
	"strconv"
	"time"
)

// 物料分组
type ProductMaterialGroup struct {
	// 物料分组ID
	ProductMaterialGroupId int `json:"productMaterialGroupId"`
	// 企业id
	CompanyId int `json:"companyId"`
	// 组织ID
	OrgId int `json:"orgId"`
	// 父级ID
	Pid int `json:"pid"`
	// 路径 (不使用,如果父级改变的话,子级的Path要做更新)
	//Path string `json:"path"`
	// 物料分组名称
	MaterialGroupName string `json:"materialGroupName"`
	// 物料分组编码
	MaterialGroupNumber string `json:"materialGroupNumber"`
	// 创建时间
	CreatedAt time.Time `json:"createdAt"`
	// 更新时间
	UpdatedAt time.Time `json:"updatedAt"`
	// 删除时间
	DeletedAt time.Time `json:"deletedAt"`
	// 扩展数据
	Ext *Ext `json:"ext,omitempty"`
}

type ProductMaterialGroupRepository interface {
	Save(productMaterialGroup *ProductMaterialGroup) (*ProductMaterialGroup, error)
	Remove(productMaterialGroup *ProductMaterialGroup) (*ProductMaterialGroup, error)
	FindOne(queryOptions map[string]interface{}) (*ProductMaterialGroup, error)
	Find(queryOptions map[string]interface{}) (int64, []*ProductMaterialGroup, error)
}

func (productMaterialGroup *ProductMaterialGroup) Identify() interface{} {
	if productMaterialGroup.ProductMaterialGroupId == 0 {
		return nil
	}
	return productMaterialGroup.ProductMaterialGroupId
}

func (productMaterialGroup *ProductMaterialGroup) Update(data map[string]interface{}) error {
	return nil
}

func (productMaterialGroup *ProductMaterialGroup) GroupNumberComposeUp(parent *ProductMaterialGroup, latestNumber string, total int) string {
	iNumber, err := strconv.ParseInt(latestNumber, 10, 64)

	if err == nil {
		iNumber += 1
		return formatFunc(iNumber)
	}
	if parent != nil {
		return parent.MaterialGroupNumber + formatFunc(int64(total+1))
	}
	return formatFunc(int64(total + 1))
}

func formatFunc(iNumber int64) string {
	sNumber := fmt.Sprintf("%d", iNumber)
	if len(sNumber)%2 == 1 {
		return "0" + sNumber
	}
	return sNumber
}

/*****   1.实现树  *****/

func (productMaterialGroup *ProductMaterialGroup) PID() string {
	return fmt.Sprintf("%d", productMaterialGroup.Pid)
}
func (productMaterialGroup *ProductMaterialGroup) ID() string {
	return fmt.Sprintf("%d", productMaterialGroup.ProductMaterialGroupId)
}

type ProductMaterialGroups []*ProductMaterialGroup

func (tree ProductMaterialGroups) Len() int {
	return len(tree)
}

func (tree ProductMaterialGroups) Less(i, j int) bool {
	if tree[i].Pid < tree[j].Pid {
		return true
	}
	if tree[i].Pid == tree[j].Pid {
		return tree[i].ProductMaterialGroupId < tree[j].ProductMaterialGroupId
	}
	return false
}

func (tree ProductMaterialGroups) Swap(i, j int) {
	tree[i], tree[j] = tree[j], tree[i]
}

func (tree ProductMaterialGroups) ToMapByGroupNumber() map[string]*ProductMaterialGroup {
	var result = make(map[string]*ProductMaterialGroup)
	for i := range tree {
		result[tree[i].MaterialGroupNumber] = tree[i]
	}
	return result
}