plan_dispatch_record_dao.go 2.0 KB
package dao

import (
	"fmt"
	"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/transform"
	"time"
)

type ProductPlanDispatchRecordDao struct {
	transactionContext *pgTransaction.TransactionContext
}

func NewProductPlanDispatchRecord(transactionContext *pgTransaction.TransactionContext) (*ProductPlanDispatchRecordDao, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &ProductPlanDispatchRecordDao{
			transactionContext: transactionContext,
		}, nil
	}
}

// 生产计划对应的批次
// 工段
// 日期
// 产品编号
// 调度状态 status
func (dao *ProductPlanDispatchRecordDao) DeviceProductPlan(companyId, orgId int, workStationId string, date time.Time, productCode string, status int) (*domain.ProductPlanDispatchRecord, error) {
	tx := dao.transactionContext.PgTx
	productPlanDispatchRecordModel := new(models.ProductPlanDispatchRecord)
	query := sqlbuilder.BuildQuery(tx.Model(productPlanDispatchRecordModel), map[string]interface{}{})
	query.Where("company_id = ?", companyId)
	query.Where("org_id = ?", orgId)
	query.Where("work_station->>'workStationId'=?", workStationId)
	query.Where("product_date = ?", date)
	query.Where("plan_dispatch_status = ?", status)
	query.Where("plan_dispatch_record_ext->>'productCode'=?", productCode)
	query.Order("updated_at desc")
	if err := query.First(); err != nil {
		if err.Error() == "pg: no rows in result set" {
			return nil, domain.ErrorNotFound
		} else {
			return nil, err
		}
	}
	if productPlanDispatchRecordModel.ProductPlanDispatchRecordId == 0 {
		return nil, nil
	} else {
		return transform.TransformToProductPlanDispatchRecordDomainModelFromPgModels(productPlanDispatchRecordModel)
	}
}