workshops.go 3.4 KB
package domain

import "strings"

/*车间列表*/
type Workshops []*Workshop

func (m Workshops) FindWorkStation(workshopId, lineId, sectionId int, flag ...int) *WorkStation {
	for i := range m {
		item := m[i]
		workstation, err := item.FindWorkStation(workshopId, lineId, sectionId)
		if len(flag) > 0 && flag[0] == 1 && workstation != nil {
			workstation.Principal = item.Principal
		}
		if err == nil && workstation != nil {
			return workstation
		}
	}
	return &WorkStation{} //返回空的对象
}

func (m Workshops) FindWorkStationOrNil(workshopId, lineId, sectionId int) *WorkStation {
	for i := range m {
		item := m[i]
		workstation, err := item.FindWorkStationOrNil(workshopId, lineId, sectionId)
		if err == nil && workstation != nil {
			return workstation
		}
	}
	return &WorkStation{} //返回空的对象
}

func (m Workshops) FindWorkshopsByName(workshopName string) []int {
	result := make([]int, 0)
	if len(workshopName) == 0 {
		return result
	}
	for i := range m {
		item := m[i]
		if strings.Contains(item.WorkshopName, workshopName) {
			result = append(result, item.WorkshopId)
		}
	}
	return result
}

func (m Workshops) ExistsWorkshops(workshopName string) (*Workshop, bool) {
	if len(workshopName) == 0 {
		return nil, false
	}
	for i := range m {
		item := m[i]
		if strings.EqualFold(item.WorkshopName, workshopName) {
			return item, true
		}
	}
	return nil, false
}

func (m Workshops) FindProductLinesByName(lineName string) []int {
	result := make([]int, 0)
	if len(lineName) == 0 {
		return result
	}
	for i := range m {
		item := m[i]
		for j := range item.ProductLines {
			line := item.ProductLines[j]
			if line.Removed == Deleted {
				continue
			}
			if strings.Contains(line.LineName, lineName) {
				result = append(result, line.LineId)
			}
		}
	}
	return result
}

func (m Workshops) FindProductSectionsByName(sectionName string) []int {
	result := make([]int, 0)
	if len(sectionName) == 0 {
		return result
	}
	for i := range m {
		item := m[i]
		for j := range item.ProductLines {
			line := item.ProductLines[j]
			if line.Removed == Deleted {
				continue
			}
			for z := range line.ProductSections {
				section := line.ProductSections[z]
				if section.Removed == Deleted {
					continue
				}
				if strings.Contains(section.SectionName, sectionName) {
					result = append(result, section.SectionId)
				}
			}
		}
	}
	return result
}

func (m Workshops) FindByName(workshopName, lineName, sectionName string) (workshops []int, lines []int, sections []int) {
	workshops = m.FindWorkshopsByName(workshopName)
	lines = m.FindProductLinesByName(lineName)
	sections = m.FindProductSectionsByName(sectionName)
	return
}

func (m Workshops) FindByNameWithQuery(query map[string]interface{}, workshopName, lineName, sectionName string) map[string]interface{} {
	var workshops, lines, sections []int
	workshops = m.FindWorkshopsByName(workshopName)
	lines = m.FindProductLinesByName(lineName)
	sections = m.FindProductSectionsByName(sectionName)
	defaultValue := []int{0}
	if len(workshops) > 0 {
		query["inWorkshopIds"] = workshops
	} else {
		if len(workshopName) > 0 {
			query["inWorkshopIds"] = defaultValue
		}
	}

	if len(lines) > 0 {
		query["inLineIds"] = lines
	} else {
		if len(lineName) > 0 {
			query["inLineIds"] = defaultValue
		}
	}
	if len(sections) > 0 {
		query["inSectionIds"] = sections
	} else {
		if len(sectionName) > 0 {
			query["inSectionIds"] = defaultValue
		}
	}
	return query
}