workshop.go 6.9 KB
package domain

import (
	"fmt"
	"sort"
	"time"
)

const (
	// 所有
	All = 0
	// 未删除
	NotDeleted = 1
	// 已删除
	Deleted = 2
)

// 车间
type Workshop struct {
	// 企业id
	CompanyId int `json:"companyId,omitempty"`
	// 组织ID
	OrgId int `json:"orgId,omitempty"`
	// 车间ID
	WorkshopId int `json:"workshopId,omitempty"`
	// 车间名称
	WorkshopName string `json:"workshopName,omitempty"`
	// 负责人 (用户对象)
	Principal *User `json:"principal,omitempty"`
	// 生产线
	ProductLines []*ProductLine `json:"productLines,omitempty"`
	// 创建时间
	CreatedAt time.Time `json:"createdAt,omitempty"`
	// 更新时间
	UpdatedAt time.Time `json:"updatedAt,omitempty"`
	// 删除时间
	DeletedAt time.Time `json:"deletedAt,omitempty"`
	// 扩展数据
	Ext *Ext `json:"ext,omitempty"`
}

type WorkshopRepository interface {
	Save(workshop *Workshop) (*Workshop, error)
	Remove(workshop *Workshop) (*Workshop, error)
	FindOne(queryOptions map[string]interface{}) (*Workshop, error)
	Find(queryOptions map[string]interface{}) (int64, []*Workshop, error)
}

func (workshop *Workshop) Identify() interface{} {
	if workshop.WorkshopId == 0 {
		return nil
	}
	return workshop.WorkshopId
}

func (workshop *Workshop) Update(data map[string]interface{}) error {
	if workshopName, ok := data["workshopName"]; ok {
		workshop.WorkshopName = workshopName.(string)
	}
	if userId, ok := data["userId"]; ok {
		workshop.Principal.UserId = userId.(int)
	}
	if principal, ok := data["principal"]; ok {
		workshop.Principal = principal.(*User)
	}
	workshop.UpdatedAt = time.Now()
	return nil
}

// AddLine 添加生产线
func (workshop *Workshop) AddLine(line *ProductLine) error {
	for i := range workshop.ProductLines {
		item := workshop.ProductLines[i]
		if item.Removed == Deleted {
			continue
		}
		if item.LineName == line.LineName {
			return fmt.Errorf("生产线:%v已存在", line.LineName)
		}
	}
	workshop.ProductLines = append(workshop.ProductLines, line)
	return nil
}

// RemoveLine 移除生产线
func (workshop *Workshop) RemoveLine(lineId int) (*ProductLine, error) {
	line, err := workshop.FindLine(lineId)
	if err != nil {
		return nil, err
	}
	if line.Removed == Deleted {
		return nil, fmt.Errorf("生产线:%v已删除", line.LineName)
	}
	line.Removed = Deleted
	return line, nil
}

// RemoveLine 更新生产线
func (workshop *Workshop) UpdateLine(lineId int, lineName string) error {
	line, err := workshop.FindLine(lineId)
	if err != nil {
		return err
	}
	if line.Removed == Deleted {
		return fmt.Errorf("生产线:%v已删除", line.LineName)
	}
	line.LineName = lineName
	return nil
}

// FindLine 查询生产线
func (workshop *Workshop) FindLine(lineId int) (*ProductLine, error) {
	for i := range workshop.ProductLines {
		item := workshop.ProductLines[i]
		if item.LineId == lineId {
			return workshop.ProductLines[i], nil
		}
	}
	return nil, fmt.Errorf("生产线不存在")
}

// RemoveLine 车间是否可删除 (存在任意一个生产线时,可删除)
func (workshop *Workshop) CanRemove() bool {
	for i := range workshop.ProductLines {
		item := workshop.ProductLines[i]
		if item.Removed == NotDeleted {
			return false
		}
	}
	return true
}

// AddLine 添加生产线
func (workshop *Workshop) AddSection(lineId int, section *ProductSection) error {
	line, err := workshop.FindLine(lineId)
	if err != nil {
		return err
	}
	if line.Removed == Deleted {
		return fmt.Errorf("生产线:%v已删除", line.LineName)
	}
	for i := range line.ProductSections {
		item := line.ProductSections[i]
		if item.Removed == Deleted {
			continue
		}
		if item.SectionName == section.SectionName {
			return fmt.Errorf("工段:%v已存在", section.SectionName)
		}
	}
	line.ProductSections = append(line.ProductSections, section)
	return nil
}

// RemoveLine 移除生产线
func (workshop *Workshop) RemoveSection(lineId, sectionId int) error {
	section, err := workshop.FindSection(lineId, sectionId)
	if err != nil {
		return err
	}
	if section.Removed == Deleted {
		return fmt.Errorf("工段:%v已删除", section.SectionName)
	}
	section.Removed = Deleted
	return nil
}

// RemoveLine 更新生产线
func (workshop *Workshop) UpdateSection(lineId, sectionId int, sectionName string) error {
	section, err := workshop.FindSection(lineId, sectionId)
	if err != nil {
		return err
	}
	if section.Removed == Deleted {
		return fmt.Errorf("工段:%v已删除", section.SectionName)
	}
	section.SectionName = sectionName
	return nil
}

// 查询生产线
func (workshop *Workshop) FindSection(lineId, sectionId int) (*ProductSection, error) {
	line, err := workshop.FindLine(lineId)
	if err != nil {
		return nil, err
	}
	for i := range line.ProductSections {
		item := line.ProductSections[i]
		if item.SectionId == sectionId {
			return item, nil
		}
	}
	return nil, fmt.Errorf("工段不存在")
}

// 查询生产线
func (workshop *Workshop) FindWorkStation(workshopId, lineId, sectionId int) (*WorkStation, error) {
	if workshop.WorkshopId != workshopId {
		return nil, fmt.Errorf("不存在")
	}
	line, err := workshop.FindLine(lineId)
	if err != nil {
		return nil, err
	}
	section, err := workshop.FindSection(lineId, sectionId)
	if err != nil {
		return nil, err
	}
	return NewWorkStation(workshop, line, section), nil
}

func (workshop *Workshop) FindWorkStationOrNil(workshopId, lineId, sectionId int) (*WorkStation, error) {
	if workshop.WorkshopId != workshopId {
		return nil, fmt.Errorf("不存在")
	}
	line, _ := workshop.FindLine(lineId)
	//if err != nil {
	//	return nil, err
	//}
	section, _ := workshop.FindSection(lineId, sectionId)
	//if err != nil {
	//	return nil, err
	//}
	return NewWorkStation(workshop, line, section), nil
}

func (workshop *Workshop) GetProductLines(removed int) []*ProductLine {
	var result = make([]*ProductLine, 0)
	for i := range workshop.ProductLines {
		if removed > 0 && workshop.ProductLines[i].Removed != removed {
			continue
		}
		item := workshop.ProductLines[i]
		item.Removed = 0 // 隐藏
		item.ProductSections = item.GetProductSections(removed)
		result = append(result, item)
	}
	return result
}

func (workshop *Workshop) AllProductSections(removed int) []*ProductSection {
	var result ProductSections = make([]*ProductSection, 0)
	lines := workshop.GetProductLines(removed)
	for i := range lines {
		for j := range lines[i].ProductSections {
			section := lines[i].ProductSections[j]
			if removed > 0 && section.Removed != removed {
				continue
			}
			result = append(result, section.CloneSample())
		}
	}

	sort.Stable(result)
	return result
}

// 查询生产线
func (workshop *Workshop) FindSectionById(sectionId int) (*ProductLine, *ProductSection, error) {
	lines := workshop.GetProductLines(0)
	for k := range lines {
		line := lines[k]
		section, _ := line.FindSection(sectionId)
		if section != nil {
			return line, section, nil
		}
	}
	return nil, nil, fmt.Errorf("工段不存在")
}

func (workshop *Workshop) CloneSample() *Workshop {
	return &Workshop{
		WorkshopId:   workshop.WorkshopId,
		WorkshopName: workshop.WorkshopName,
	}
}