product_group_employees_dto.go 3.9 KB
package dto

import (
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils/converter"
	"strings"
)

// 生产班组员工
type ProductGroupEmployeesDto struct {
	// 用户Id 用户唯一标识
	UserId int `json:"userId,omitempty"`
	// 用户姓名
	UserName string `json:"userName,omitempty"`
	// 生产小组ID
	ProductGroupId int `json:"productGroupId,omitempty"`
	// 班组名称
	GroupName string `json:"groupName,omitempty"`
	// 上班班次 1:全天  2:白班 4:中班  8:夜班
	WorkOnDescription string `json:"workOn,omitempty"`
	// 工作位置键值 (车间ID+'.'+生产线ID+'.'+工段ID)
	//WorkStationId string `json:"workStationId,omitempty"`
	UserNamePinyin string `json:"pinyin"`
	// 在线的
	Online bool `json:"online"`
}

func NewProductGroupEmployeesDto(group *domain.ProductGroup) []*ProductGroupEmployeesDto {
	employees := make([]*ProductGroupEmployeesDto, 0)
	if group.GroupLeader != nil {
		employees = append(employees, NewGroupEmployee(group, group.GroupLeader))
	}
	for i := range group.GroupMembers {
		employees = append(employees, NewGroupEmployee(group, group.GroupMembers[i]))
	}
	return employees
}

func NewGroupEmployee(group *domain.ProductGroup, u *domain.User) *ProductGroupEmployeesDto {
	item := &ProductGroupEmployeesDto{}
	item.UserId = u.UserId
	item.UserName = u.UserName
	item.ProductGroupId = group.ProductGroupId
	item.GroupName = group.GroupName
	workOns := domain.WorkOnDescription(group.WorkOn)
	item.WorkOnDescription = strings.Join(workOns, ",")
	//item.WorkStationId = group.WorkStation.WorkStationId
	if len(item.UserName) > 0 {
		item.UserNamePinyin = converter.ToPinYin(item.UserName, "")
	}
	return item
}

type ProductGroupEmployeesDtos struct {
	Result    []*ProductGroupEmployeesDto
	MapResult map[int]*ProductGroupEmployeesDto
}

func (d *ProductGroupEmployeesDtos) Append(items ...*ProductGroupEmployeesDto) {
	for i := range items {
		if _, ok := d.MapResult[items[i].UserId]; ok {
			continue
		}
		d.MapResult[items[i].UserId] = items[i]
		d.Result = append(d.Result, items[i])
	}
}

func (d *ProductGroupEmployeesDtos) LoadDto(groups ...*domain.ProductGroup) {
	for i := range groups {
		items := NewProductGroupEmployeesDto(groups[i])
		d.Append(items...)
	}
}

func (d *ProductGroupEmployeesDtos) LoadDtoV2(list []*domain.ProductAttendanceRecord, mapGroupUser map[string]*domain.User, keyFunc func(int) string) {
	var mapUser = make(map[int]int)
	// 打卡的用户
	for _, v := range list {
		item := &ProductGroupEmployeesDto{}
		item.UserId = v.ProductWorker.UserId
		item.UserName = v.ProductWorker.UserName
		item.ProductGroupId = 0
		item.GroupName = ""
		item.Online = true
		if v, ok := mapGroupUser[keyFunc(item.UserId)]; ok {
			item.GroupName = v.GroupName
			item.ProductGroupId = v.GroupId
			item.WorkOnDescription = domain.WorkOnDescriptions(v.WorkOn)
		}
		if len(item.UserName) > 0 {
			item.UserNamePinyin = converter.ToPinYin(item.UserName, "")
		}
		if _, ok := mapUser[item.UserId]; ok {
			continue
		}
		mapUser[item.UserId] = item.UserId
		d.Append(item)
	}
	// 未打卡的用户
	for _, v := range mapGroupUser {
		if _, ok := mapUser[v.UserId]; ok {
			continue
		}
		item := &ProductGroupEmployeesDto{}
		item.UserId = v.UserId
		item.UserName = v.UserName
		item.ProductGroupId = v.GroupId
		item.GroupName = v.GroupName
		mapUser[item.UserId] = item.UserId
		d.Append(item)
	}
}

func NewProductGroupEmployeesDtos() *ProductGroupEmployeesDtos {
	return &ProductGroupEmployeesDtos{
		Result:    make([]*ProductGroupEmployeesDto, 0),
		MapResult: make(map[int]*ProductGroupEmployeesDto),
	}
}

func (ms *ProductGroupEmployeesDtos) Len() int { return len(ms.Result) }
func (ms *ProductGroupEmployeesDtos) Less(i, j int) bool {
	return ms.Result[i].UserNamePinyin < ms.Result[j].UserNamePinyin
}
func (ms *ProductGroupEmployeesDtos) Swap(i, j int) {
	ms.Result[i], ms.Result[j] = ms.Result[j], ms.Result[i]
}