workshop.go 7.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 SimpleWorkshop struct {
	// 车间ID
	WorkshopId int `json:"workshopId"`
	// 车间名称
	WorkshopName string `json:"workshopName"`
}

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
	}
	sections := line.GetProductSections(NotDeleted)
	if len(sections) > 0 {
		return nil, fmt.Errorf("生产线:%v下存在工段,不可删除", line.LineName)
	}
	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)
	}
	for _, v := range workshop.ProductLines {
		if v.Removed == Deleted {
			continue
		}
		if v.LineName == lineName && v.LineId != lineId {
			return fmt.Errorf("生产线:%v已存在", 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)
	}

	// 当前线下面工段不能重复
	line, err := workshop.FindLine(lineId)
	if err != nil {
		return err
	}
	for _, v := range line.ProductSections {
		if v.Removed == Deleted {
			continue
		}
		if v.SectionName == sectionName && v.SectionId != sectionId {
			return fmt.Errorf("工段:%v已存在", 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,
	}
}

//SimpleWorkshop 输出简化的模型
func (workShop *Workshop) SimpleWorkshop() SimpleWorkshop {
	return SimpleWorkshop{
		WorkshopId:   workShop.WorkshopId,
		WorkshopName: workShop.WorkshopName,
	}
}