product_record_employee.go 3.6 KB
package domain

import "time"

const (
	ParticipateNormal  = 1 // 正常
	ParticipateSupport = 2 // 支援
)

const (
	ProductSection1 = "打料"
	ProductSection2 = "成型"
	ProductSection3 = "穿串"
	ProductSection4 = "包装"
)

// 员工生产记录
type EmployeeProductRecord struct {
	// 企业id
	CompanyId int `json:"companyId,omitempty"`
	// 组织ID
	OrgId int `json:"orgId,omitempty"`
	// 员工产能记录ID
	EmployeeProductRecordId int `json:"employeeProductRecordId"`
	// 工作位置
	WorkStation *WorkStation `json:"workStation"`
	// 上班班次 1:全天  2:白班 4:中班  8:夜班
	WorkOn int `json:"workOn"`
	// 参与类型 1:正常 2:支援
	ParticipateType int `json:"participateType"`
	// 初始产能
	ProductWeigh float64 `json:"productWeigh"`
	// 二级品产能
	SecondLevelWeigh float64 `json:"secondLevelWeigh"`
	// 创建时间
	CreatedAt time.Time `json:"createdAt"`
	// 更新时间
	UpdatedAt time.Time `json:"updatedAt"`
	// 删除时间
	DeletedAt time.Time `json:"deletedAt"`
	// 扩展
	Ext *Ext `json:"ext"`
	// 版本号
	Version int `json:"version"`
	// 生产记录统计信息
	ProductRecordInfo *ProductRecordStaticInfo `json:"productRecordInfo"`
	// 生产工人
	ProductWorker *User `comment:"生产工人"`
}

type EmployeeProductRecordRepository interface {
	Save(employeeProductRecord *EmployeeProductRecord) (*EmployeeProductRecord, error)
	Remove(employeeProductRecord *EmployeeProductRecord) (*EmployeeProductRecord, error)
	FindOne(queryOptions map[string]interface{}) (*EmployeeProductRecord, error)
	Find(queryOptions map[string]interface{}) (int64, []*EmployeeProductRecord, error)
}

func (employeeProductRecord *EmployeeProductRecord) Identify() interface{} {
	if employeeProductRecord.EmployeeProductRecordId == 0 {
		return nil
	}
	return employeeProductRecord.EmployeeProductRecordId
}

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

// 更新生产量
func (employeeProductRecord *EmployeeProductRecord) UpdateProductWeigh(weigh float64, productRecordType int, sectionName string, yesterdayWeight float64, bestWeight float64) {
	if employeeProductRecord.ProductRecordInfo.OtherSectionSecondLevelWeigh == nil {
		employeeProductRecord.ProductRecordInfo.OtherSectionSecondLevelWeigh = make(map[string]float64)
	}
	if productRecordType == RecordTypeSecondLevelWeigh {
		// 当前工段
		if employeeProductRecord.WorkStation.SectionName == sectionName {
			employeeProductRecord.SecondLevelWeigh += weigh
		} else {
			// 其他工段的二级品
			employeeProductRecord.ProductRecordInfo.AddSectionWeight(sectionName, weigh)
		}
	}
	if productRecordType == RecordTypeReceiveMaterial {
		employeeProductRecord.ProductWeigh += weigh
	}
	if productRecordType == RecordTypeReturnMaterial {
		employeeProductRecord.ProductWeigh -= weigh
	}
	employeeProductRecord.ProductRecordInfo.PreStatistics(employeeProductRecord.ProductWeigh, employeeProductRecord.SecondLevelWeigh, yesterdayWeight, bestWeight)
	employeeProductRecord.UpdatedAt = time.Now()
	employeeProductRecord.Version += 1
}

// 产能
func (employeeProductRecord *EmployeeProductRecord) RealProductWeigh() float64 {
	// 产能 = 总投入量 - 本阶段二级品重量 - 其他上级二级品重量
	return employeeProductRecord.ProductWeigh - employeeProductRecord.SecondLevelWeigh - employeeProductRecord.ProductRecordInfo.OtherSecondLevelWeigh()
}

// 合格率
func (employeeProductRecord *EmployeeProductRecord) QualificationRate() int {
	realProductWeigh := employeeProductRecord.RealProductWeigh()
	if realProductWeigh == 0 {
		return 0
	}
	result := int(realProductWeigh * 100 / employeeProductRecord.ProductWeigh)
	return result
}