product_record_employee.go 4.2 KB
package domain

import (
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
	"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(productRecord *ProductRecord, yesterdayWeight float64, bestWeight float64) {
	var weigh float64 = productRecord.ProductRecordInfo.Weigh
	var productRecordType int = productRecord.ProductRecordType
	var sectionName string = productRecord.WorkStation.SectionName
	if employeeProductRecord.ProductRecordInfo.OtherSectionSecondLevelWeigh == nil {
		employeeProductRecord.ProductRecordInfo.OtherSectionSecondLevelWeigh = make(map[string]float64)
	}
	if productRecordType == RecordTypeSecondLevelWeigh {
		// 当前工段
		if employeeProductRecord.WorkStation.SectionName == sectionName && productRecord.PreRecord == nil {
			employeeProductRecord.SecondLevelWeigh += weigh
		} else if productRecord.PreRecord != nil {
			// 其他工段的二级品
			employeeProductRecord.ProductRecordInfo.AddSectionWeight(productRecord.PreRecord.WorkStation.SectionName, weigh)
		}
	}
	if productRecordType == RecordTypeReceiveMaterial {
		employeeProductRecord.ProductWeigh += weigh
	}
	if productRecordType == RecordTypeReturnMaterial {
		employeeProductRecord.ProductWeigh -= weigh
	}
	if productRecordType == RecordTypeWeigh {
		employeeProductRecord.ProductWeigh += weigh
	}
	employeeProductRecord.ProductWeigh = utils.Round(employeeProductRecord.ProductWeigh, 1)
	employeeProductRecord.ProductRecordInfo.PreStatistics(employeeProductRecord.ProductWeigh, employeeProductRecord.SecondLevelWeigh, yesterdayWeight, bestWeight)
	employeeProductRecord.UpdatedAt = time.Now()
	employeeProductRecord.Version += 1
}

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

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