product_plan.go 3.1 KB
package domain

import (
	"errors"
	"time"
)

const (
	PlanOnline  = 1 // 计划上线
	PlanOffline = 2 // 计划下线
)

// 生产计划
type ProductPlan struct {
	// 生产计划ID
	ProductPlanId int `json:"productPlanId,omitempty"`
	// 企业id
	CompanyId int `json:"companyId,omitempty"`
	// 组织ID
	OrgId int `json:"orgId,omitempty"`
	// 批号
	BatchNumber string `json:"batchNumber,omitempty"`
	// 生产日期
	ProductDate time.Time `json:"productDate,omitempty"`
	// 车间
	Workshop *Workshop `json:"workshop,omitempty"`
	// 上班班次 1:全天  2:白班 4:中班  8:夜班
	WorkOn int `json:"workOn,omitempty"`
	// 机台 (A、B、C、D 区分机器大小)
	Machine string `json:"machine,omitempty"`
	// 计划的产品名称
	PlanProductName string `json:"planProductName,omitempty"`
	// 计划投入
	PlanDevoted *UnitQuantity `json:"planDevoted,omitempty"`
	// 计划状态 (1:上线 2:下线  默认下线)
	PlanStatus int `json:"planStatus,omitempty"`
	// 工作位置
	WorkStation *WorkStation `json:"workStation,omitempty"`
	// 备注
	Remark string `json:"remark,omitempty"`
	// 创建时间
	CreatedAt time.Time `json:"createdAt,omitempty"`
	// 更新时间
	UpdatedAt time.Time `json:"updatedAt,omitempty"`
	// 删除时间
	DeletedAt time.Time `json:"deletedAt,omitempty"`
	// 扩展数据
	Ext *Ext `json:"ext,omitempty"`
}

type ProductPlanRepository interface {
	Save(productPlan *ProductPlan) (*ProductPlan, error)
	Remove(productPlan *ProductPlan) (*ProductPlan, error)
	FindOne(queryOptions map[string]interface{}) (*ProductPlan, error)
	Find(queryOptions map[string]interface{}) (int64, []*ProductPlan, error)
}

func (productPlan *ProductPlan) Identify() interface{} {
	if productPlan.ProductPlanId == 0 {
		return nil
	}
	return productPlan.ProductPlanId
}

func (productPlan *ProductPlan) Update(data map[string]interface{}) error {
	//if batchNumber, ok := data["batchNumber"]; ok {
	//	productPlan.BatchNumber = batchNumber.(string)
	//}
	if productDate, ok := data["productDateTime"]; ok {
		productPlan.ProductDate = productDate.(time.Time)
	}
	if workOn, ok := data["workOn"]; ok {
		productPlan.WorkOn = workOn.(int)
	}
	if machine, ok := data["machine"]; ok {
		productPlan.Machine = machine.(string)
	}
	if planProductName, ok := data["planProductName"]; ok {
		productPlan.PlanProductName = planProductName.(string)
	}
	if quantity, ok := data["quantity"]; ok {
		productPlan.PlanDevoted.Quantity = quantity.(float64)
	}
	if unit, ok := data["unit"]; ok {
		productPlan.PlanDevoted.Unit = unit.(string)
	}
	if weight, ok := data["weight"]; ok {
		productPlan.PlanDevoted.Weight = weight.(float64)
	}
	if remark, ok := data["remark"]; ok {
		productPlan.Remark = remark.(string)
	}
	productPlan.UpdatedAt = time.Now()
	return nil
}

func (productPlan *ProductPlan) ChangeStatus(status int) error {
	if productPlan.PlanStatus == status && status == PlanOnline {
		return errors.New("计划已经上线")
	}
	if productPlan.PlanStatus == status && status == PlanOffline {
		return errors.New("计划已经下线")
	}
	if !(status == PlanOnline || status == PlanOffline) {
		return errors.New("计划状态有误")
	}
	productPlan.PlanStatus = status
	return nil
}