pg_unit_conversion_repository.go 6.7 KB
package repository

import (
	"fmt"
	"github.com/go-pg/pg/v10"
	"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"

	"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"github.com/linmadan/egglib-go/utils/snowflake"
)

type UnitConversionRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

func (repository *UnitConversionRepository) nextIdentify() (int64, error) {
	IdWorker, err := snowflake.NewIdWorker(1)
	if err != nil {
		return 0, err
	}
	id, err := IdWorker.NextId()
	return id, err
}
func (repository *UnitConversionRepository) Save(unitConversion *domain.UnitConversion) (*domain.UnitConversion, error) {
	sqlBuildFields := []string{
		"unit_conversion_id",
		"company_id",
		"org_id",
		"work_station",
		"material",
		"from_unit_quantity",
		"to_unit_quantity",
		"intelligent_weighing_flag",
		"created_at",
		"updated_at",
		"deleted_at",
		"ext",
	}
	insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "unit_conversion_id", "deleted_at"))
	insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "unit_conversion_id", "deleted_at"))
	returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
	updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "unit_conversion_id", "deleted_at")
	updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
	tx := repository.transactionContext.PgTx
	if unitConversion.Identify() == nil {
		if _, err := tx.QueryOne(
			pg.Scan(
				&unitConversion.UnitConversionId,
				&unitConversion.CompanyId,
				&unitConversion.OrgId,
				&unitConversion.WorkStation,
				&unitConversion.Material,
				&unitConversion.FromUnitQuantity,
				&unitConversion.ToUnitQuantity,
				&unitConversion.IntelligentWeighingFlag,
				&unitConversion.CreatedAt,
				&unitConversion.UpdatedAt,
				&unitConversion.DeletedAt,
				&unitConversion.Ext,
			),
			fmt.Sprintf("INSERT INTO manufacture.unit_conversion (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
			unitConversion.CompanyId,
			unitConversion.OrgId,
			unitConversion.WorkStation,
			unitConversion.Material,
			unitConversion.FromUnitQuantity,
			unitConversion.ToUnitQuantity,
			unitConversion.IntelligentWeighingFlag,
			unitConversion.CreatedAt,
			unitConversion.UpdatedAt,
			unitConversion.Ext,
		); err != nil {
			return unitConversion, err
		}
	} else {
		if _, err := tx.QueryOne(
			pg.Scan(
				&unitConversion.UnitConversionId,
				&unitConversion.CompanyId,
				&unitConversion.OrgId,
				&unitConversion.WorkStation,
				&unitConversion.Material,
				&unitConversion.FromUnitQuantity,
				&unitConversion.ToUnitQuantity,
				&unitConversion.IntelligentWeighingFlag,
				&unitConversion.CreatedAt,
				&unitConversion.UpdatedAt,
				&unitConversion.DeletedAt,
				&unitConversion.Ext,
			),
			fmt.Sprintf("UPDATE manufacture.unit_conversion SET %s WHERE unit_conversion_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
			unitConversion.CompanyId,
			unitConversion.OrgId,
			unitConversion.WorkStation,
			unitConversion.Material,
			unitConversion.FromUnitQuantity,
			unitConversion.ToUnitQuantity,
			unitConversion.IntelligentWeighingFlag,
			unitConversion.CreatedAt,
			unitConversion.UpdatedAt,
			unitConversion.Ext,
			unitConversion.Identify(),
		); err != nil {
			return unitConversion, err
		}
	}
	return unitConversion, nil
}
func (repository *UnitConversionRepository) Remove(unitConversion *domain.UnitConversion) (*domain.UnitConversion, error) {
	tx := repository.transactionContext.PgTx
	unitConversionModel := new(models.UnitConversion)
	unitConversionModel.UnitConversionId = unitConversion.Identify().(int)
	if _, err := tx.Model(unitConversionModel).WherePK().Delete(); err != nil {
		return unitConversion, err
	}
	return unitConversion, nil
}
func (repository *UnitConversionRepository) FindOne(queryOptions map[string]interface{}) (*domain.UnitConversion, error) {
	tx := repository.transactionContext.PgTx
	unitConversionModel := new(models.UnitConversion)
	query := sqlbuilder.BuildQuery(tx.Model(unitConversionModel), queryOptions)
	query.SetWhereByQueryOption("unit_conversion.unit_conversion_id = ?", "unitConversionId")
	if v, ok := queryOptions["includeDeleted"]; ok && v.(bool) {
		query.AllWithDeleted()
	}
	if err := query.First(); err != nil {
		if err.Error() == "pg: no rows in result set" {
			return nil, domain.ErrorNotFound
		} else {
			return nil, err
		}
	}
	if unitConversionModel.UnitConversionId == 0 {
		return nil, nil
	} else {
		return transform.TransformToUnitConversionDomainModelFromPgModels(unitConversionModel)
	}
}
func (repository *UnitConversionRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.UnitConversion, error) {
	tx := repository.transactionContext.PgTx
	var unitConversionModels []*models.UnitConversion
	unitConversions := make([]*domain.UnitConversion, 0)
	query := sqlbuilder.BuildQuery(tx.Model(&unitConversionModels), queryOptions)
	query.SetWhereByQueryOption("company_id = ?", "companyId")
	query.SetWhereByQueryOption("org_id = ?", "orgId")
	query.SetWhereByQueryOption("work_station->>'workshopId'='?'", "workshopId")
	query.SetWhereByQueryOption("work_station->>'lineId'='?'", "lineId")
	query.SetWhereByQueryOption("work_station->>'sectionId'='?'", "sectionId")
	if v, ok := queryOptions["inOrgIds"]; ok && len(v.([]int)) > 0 {
		query.Where(`org_id in (?)`, pg.In(v))
	}
	query.SetWhereByQueryOption("intelligent_weighing_flag = ?", "intelligentWeighingFlag")
	if v, ok := queryOptions["materialName"]; ok && len(v.(string)) > 0 {
		query.Where(fmt.Sprintf(`material->>'materialName' like '%%%v%%'`, v))
	}
	query.SetOffsetAndLimit(domain.MaxQueryRow)
	query.SetOrderDirect("unit_conversion_id", "DESC")
	if count, err := query.SelectAndCount(); err != nil {
		return 0, unitConversions, err
	} else {
		for _, unitConversionModel := range unitConversionModels {
			if unitConversion, err := transform.TransformToUnitConversionDomainModelFromPgModels(unitConversionModel); err != nil {
				return 0, unitConversions, err
			} else {
				unitConversions = append(unitConversions, unitConversion)
			}
		}
		return int64(count), unitConversions, nil
	}
}
func NewUnitConversionRepository(transactionContext *pgTransaction.TransactionContext) (*UnitConversionRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &UnitConversionRepository{
			transactionContext: transactionContext,
		}, nil
	}
}