product_record.go 2.6 KB
package domain

import (
	"errors"
	"fmt"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
	"time"
)

const (
	RecordTypeReceiveMaterial  = 1 // 领料
	RecordTypeReturnMaterial   = 2 // 退料
	RecordTypeWeigh            = 4 // 称重
	RecordTypeSecondLevelWeigh = 8 // 二级品称重
)

const (
	ProductRecordNotApprove = 1 // 未审核
	ProductRecordApproved   = 2 // 已审核
)

// 生产记录
type ProductRecord struct {
	// 生产记录ID
	ProductRecordId int `json:"productRecordId,omitempty"`
	// 企业id
	CompanyId int `json:"companyId,omitempty"`
	// 组织ID
	OrgId int `json:"orgId,omitempty"`
	// 生产记录类型:1:领料 2:退料  4:称重 8:二级品称重
	ProductRecordType int `json:"productRecordType,omitempty"`
	// 生产工人
	ProductWorker *User `json:"productWorker,omitempty"`
	// 工作位置
	WorkStation *WorkStation `json:"workStation,omitempty"`
	// 创建时间
	CreatedAt time.Time `json:"createdAt,omitempty"`
	// 更新时间
	UpdatedAt time.Time `json:"updatedAt,omitempty"`
	// 删除时间
	DeletedAt time.Time `json:"deletedAt,omitempty"`
	// 生产记录信息
	ProductRecordInfo *ProductRecordInfo `json:"productRecordInfo,omitempty"`
	// 扩展数据
	Ext *Ext `json:"ext,omitempty"`

	// 上一条记录
	PreRecord *ProductRecord `json:"-"`
}

type ProductRecordRepository interface {
	Save(productRecord *ProductRecord) (*ProductRecord, error)
	Remove(productRecord *ProductRecord) (*ProductRecord, error)
	FindOne(queryOptions map[string]interface{}) (*ProductRecord, error)
	Find(queryOptions map[string]interface{}) (int64, []*ProductRecord, error)
}

func (productRecord *ProductRecord) Identify() interface{} {
	if productRecord.ProductRecordId == 0 {
		return nil
	}
	return productRecord.ProductRecordId
}

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

func (productRecord *ProductRecord) Approve(approveUser *User, weighAfter float64, approveAt time.Time, status int) error {
	if productRecord.ProductRecordInfo.ApproveAt > 0 {
		return errors.New("已审核")
	}
	if weighAfter <= 0 {
		return errors.New("审核后重量必须大于0")
	}
	productRecord.ProductRecordInfo.ApproveAt = approveAt.Unix()
	productRecord.ProductRecordInfo.ApproveStatus = status
	productRecord.ProductRecordInfo.ApproveUser = approveUser
	productRecord.ProductRecordInfo.WeighAfter = weighAfter
	productRecord.ProductRecordInfo.Weigh = weighAfter
	productRecord.UpdatedAt = time.Now()
	return nil
}

func TaskKeyPatternProductRecordStatics() string {
	return fmt.Sprintf("%v:task:product-record:statics", constant.CACHE_PREFIX)
}