pg_device_collection_repository.go 6.7 KB
package repository

import (
	"fmt"
	"github.com/go-pg/pg/v10"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
	"time"

	"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"
)

type DeviceCollectionRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

func (repository *DeviceCollectionRepository) nextIdentify() (int64, error) {
	//IdWorker, err := snowflake.NewIdWorker(1)
	//if err != nil {
	//	return 0, err
	//}
	//id, err := IdWorker.NextId()
	return utils.NewSnowflakeId()
}
func (repository *DeviceCollectionRepository) Save(deviceCollection *domain.DeviceCollection) (*domain.DeviceCollection, error) {
	sqlBuildFields := []string{
		"device_collection_id",
		"work_shop_name",
		"device_sn",
		"device_type",
		"collection_time",
		"startup_status",
		"com_status",
		"values",
		"count",
		"latest_update_time",
	}
	insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
	insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
	returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
	updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "device_collection_id")
	updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
	tx := repository.transactionContext.PgTx
	if deviceCollection.Identify() == nil {
		deviceCollectionId, err := repository.nextIdentify()
		if err != nil {
			return deviceCollection, err
		} else {
			deviceCollection.DeviceCollectionId = deviceCollectionId
		}
		if _, err := tx.QueryOne(
			pg.Scan(
				&deviceCollection.DeviceCollectionId,
				&deviceCollection.WorkShopName,
				&deviceCollection.DeviceSn,
				&deviceCollection.DeviceType,
				&deviceCollection.CollectionTime,
				&deviceCollection.StartupStatus,
				&deviceCollection.ComStatus,
				&deviceCollection.Values,
				&deviceCollection.Count,
				&deviceCollection.LatestUpdateTime,
			),
			fmt.Sprintf("INSERT INTO manufacture.device_collections (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
			deviceCollection.DeviceCollectionId,
			deviceCollection.WorkShopName,
			deviceCollection.DeviceSn,
			deviceCollection.DeviceType,
			deviceCollection.CollectionTime,
			deviceCollection.StartupStatus,
			deviceCollection.ComStatus,
			deviceCollection.Values,
			deviceCollection.Count,
			deviceCollection.LatestUpdateTime,
		); err != nil {
			return deviceCollection, err
		}
	} else {
		if _, err := tx.QueryOne(
			pg.Scan(
				&deviceCollection.DeviceCollectionId,
				&deviceCollection.WorkShopName,
				&deviceCollection.DeviceSn,
				&deviceCollection.DeviceType,
				&deviceCollection.CollectionTime,
				&deviceCollection.StartupStatus,
				&deviceCollection.ComStatus,
				&deviceCollection.Values,
				&deviceCollection.Count,
				&deviceCollection.LatestUpdateTime,
			),
			fmt.Sprintf("UPDATE manufacture.device_collections SET %s WHERE device_collection_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
			//deviceCollection.DeviceCollectionId,
			deviceCollection.WorkShopName,
			deviceCollection.DeviceSn,
			deviceCollection.DeviceType,
			deviceCollection.CollectionTime,
			deviceCollection.StartupStatus,
			deviceCollection.ComStatus,
			deviceCollection.Values,
			deviceCollection.Count,
			deviceCollection.LatestUpdateTime,
			deviceCollection.Identify(),
		); err != nil {
			return deviceCollection, err
		}
	}
	return deviceCollection, nil
}
func (repository *DeviceCollectionRepository) Remove(deviceCollection *domain.DeviceCollection) (*domain.DeviceCollection, error) {
	tx := repository.transactionContext.PgTx
	deviceCollectionModel := new(models.DeviceCollection)
	deviceCollectionModel.DeviceCollectionId = deviceCollection.Identify().(int64)
	if _, err := tx.Model(deviceCollectionModel).WherePK().Delete(); err != nil {
		return deviceCollection, err
	}
	return deviceCollection, nil
}
func (repository *DeviceCollectionRepository) FindOne(queryOptions map[string]interface{}) (*domain.DeviceCollection, error) {
	tx := repository.transactionContext.PgTx
	deviceCollectionModel := new(models.DeviceCollection)
	query := sqlbuilder.BuildQuery(tx.Model(deviceCollectionModel), queryOptions)
	query.SetWhereByQueryOption("device_collection.device_collection_id = ?", "deviceCollectionId")
	if err := query.First(); err != nil {
		if err.Error() == "pg: no rows in result set" {
			return nil, fmt.Errorf("没有此资源")
		} else {
			return nil, err
		}
	}
	if deviceCollectionModel.DeviceCollectionId == 0 {
		return nil, nil
	} else {
		return transform.TransformToDeviceCollectionDomainModelFromPgModels(deviceCollectionModel)
	}
}
func (repository *DeviceCollectionRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.DeviceCollection, error) {
	tx := repository.transactionContext.PgTx
	var deviceCollectionModels []*models.DeviceCollection
	deviceCollections := make([]*domain.DeviceCollection, 0)
	query := sqlbuilder.BuildQuery(tx.Model(&deviceCollectionModels), queryOptions)
	query.SetOffsetAndLimit(20)
	query.SetOrderDirect("device_collection_id", "DESC")
	if deviceType, ok := queryOptions["deviceType"]; ok && deviceType != "" {
		query.SetWhereByQueryOption("device_type = ?", "deviceType")
	}
	if workShopName, ok := queryOptions["workShopName"]; ok && workShopName.(string) != "" {
		query.SetWhereByQueryOption("work_shop_name = ?", "workShopName")
	}
	if v, ok := queryOptions["productBeginTime"]; ok && !((v.(time.Time)).IsZero()) {
		query.Where("collection_time>=?", v.(time.Time))
	}
	if v, ok := queryOptions["productEndTime"]; ok && !((v.(time.Time)).IsZero()) {
		query.Where("collection_time<?", v.(time.Time))
	}
	if count, err := query.SelectAndCount(); err != nil {
		return 0, deviceCollections, err
	} else {
		for _, deviceCollectionModel := range deviceCollectionModels {
			if deviceCollection, err := transform.TransformToDeviceCollectionDomainModelFromPgModels(deviceCollectionModel); err != nil {
				return 0, deviceCollections, err
			} else {
				deviceCollections = append(deviceCollections, deviceCollection)
			}
		}
		return int64(count), deviceCollections, nil
	}
}
func NewDeviceCollectionRepository(transactionContext *pgTransaction.TransactionContext) (*DeviceCollectionRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &DeviceCollectionRepository{
			transactionContext: transactionContext,
		}, nil
	}
}