|
|
package repository
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"github.com/go-pg/pg/v10"
|
|
|
|
|
|
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
|
|
|
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
|
|
"github.com/linmadan/egglib-go/utils/snowflake"
|
|
|
"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 DeviceDailyRunningRecordRepository struct {
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
func (repository *DeviceDailyRunningRecordRepository) nextIdentify() (int64, error) {
|
|
|
IdWorker, err := snowflake.NewIdWorker(1)
|
|
|
if err != nil {
|
|
|
return 0, err
|
|
|
}
|
|
|
id, err := IdWorker.NextId()
|
|
|
return id, err
|
|
|
}
|
|
|
func (repository *DeviceDailyRunningRecordRepository) Save(deviceDailyRunningRecord *domain.DeviceDailyRunningRecord) (*domain.DeviceDailyRunningRecord, error) {
|
|
|
sqlBuildFields := []string{
|
|
|
"device_daily_running_record_id",
|
|
|
"company_id",
|
|
|
"org_id",
|
|
|
"work_station",
|
|
|
"device_id",
|
|
|
"device_code",
|
|
|
"device_running_record_info",
|
|
|
"created_at",
|
|
|
"updated_at",
|
|
|
"deleted_at",
|
|
|
}
|
|
|
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
|
|
|
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
|
|
|
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
|
|
|
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "deviceDailyRunningRecord_id")
|
|
|
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
if deviceDailyRunningRecord.Identify() == nil {
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(
|
|
|
&deviceDailyRunningRecord.DeviceDailyRunningRecordId,
|
|
|
&deviceDailyRunningRecord.CompanyId,
|
|
|
&deviceDailyRunningRecord.OrgId,
|
|
|
&deviceDailyRunningRecord.WorkStation,
|
|
|
&deviceDailyRunningRecord.DeviceId,
|
|
|
&deviceDailyRunningRecord.DeviceCode,
|
|
|
&deviceDailyRunningRecord.DeviceRunningRecordInfo,
|
|
|
&deviceDailyRunningRecord.CreatedAt,
|
|
|
&deviceDailyRunningRecord.UpdatedAt,
|
|
|
&deviceDailyRunningRecord.DeletedAt,
|
|
|
),
|
|
|
fmt.Sprintf("INSERT INTO manufacture.device_daily_running_record (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
|
|
|
deviceDailyRunningRecord.DeviceDailyRunningRecordId,
|
|
|
deviceDailyRunningRecord.CompanyId,
|
|
|
deviceDailyRunningRecord.OrgId,
|
|
|
deviceDailyRunningRecord.WorkStation,
|
|
|
deviceDailyRunningRecord.DeviceId,
|
|
|
deviceDailyRunningRecord.DeviceCode,
|
|
|
deviceDailyRunningRecord.DeviceRunningRecordInfo,
|
|
|
deviceDailyRunningRecord.CreatedAt,
|
|
|
deviceDailyRunningRecord.UpdatedAt,
|
|
|
deviceDailyRunningRecord.DeletedAt,
|
|
|
); err != nil {
|
|
|
return deviceDailyRunningRecord, err
|
|
|
}
|
|
|
} else {
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(
|
|
|
&deviceDailyRunningRecord.DeviceDailyRunningRecordId,
|
|
|
&deviceDailyRunningRecord.CompanyId,
|
|
|
&deviceDailyRunningRecord.OrgId,
|
|
|
&deviceDailyRunningRecord.WorkStation,
|
|
|
&deviceDailyRunningRecord.DeviceId,
|
|
|
&deviceDailyRunningRecord.DeviceCode,
|
|
|
&deviceDailyRunningRecord.DeviceRunningRecordInfo,
|
|
|
&deviceDailyRunningRecord.CreatedAt,
|
|
|
&deviceDailyRunningRecord.UpdatedAt,
|
|
|
&deviceDailyRunningRecord.DeletedAt,
|
|
|
),
|
|
|
fmt.Sprintf("UPDATE manufacture.device_daily_running_record SET %s WHERE device_daily_running_record_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
|
|
|
deviceDailyRunningRecord.DeviceDailyRunningRecordId,
|
|
|
deviceDailyRunningRecord.CompanyId,
|
|
|
deviceDailyRunningRecord.OrgId,
|
|
|
deviceDailyRunningRecord.WorkStation,
|
|
|
deviceDailyRunningRecord.DeviceId,
|
|
|
deviceDailyRunningRecord.DeviceCode,
|
|
|
deviceDailyRunningRecord.DeviceRunningRecordInfo,
|
|
|
deviceDailyRunningRecord.CreatedAt,
|
|
|
deviceDailyRunningRecord.UpdatedAt,
|
|
|
deviceDailyRunningRecord.DeletedAt,
|
|
|
deviceDailyRunningRecord.Identify(),
|
|
|
); err != nil {
|
|
|
return deviceDailyRunningRecord, err
|
|
|
}
|
|
|
}
|
|
|
return deviceDailyRunningRecord, nil
|
|
|
}
|
|
|
func (repository *DeviceDailyRunningRecordRepository) Remove(deviceDailyRunningRecord *domain.DeviceDailyRunningRecord) (*domain.DeviceDailyRunningRecord, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
deviceDailyRunningRecordModel := new(models.DeviceDailyRunningRecord)
|
|
|
deviceDailyRunningRecordModel.DeviceDailyRunningRecordId = deviceDailyRunningRecord.Identify().(int)
|
|
|
if _, err := tx.Model(deviceDailyRunningRecordModel).WherePK().Delete(); err != nil {
|
|
|
return deviceDailyRunningRecord, err
|
|
|
}
|
|
|
return deviceDailyRunningRecord, nil
|
|
|
}
|
|
|
func (repository *DeviceDailyRunningRecordRepository) FindOne(queryOptions map[string]interface{}) (*domain.DeviceDailyRunningRecord, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
deviceDailyRunningRecordModel := new(models.DeviceDailyRunningRecord)
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(deviceDailyRunningRecordModel), queryOptions)
|
|
|
query.SetWhereByQueryOption("device_daily_running_record.device_daily_running_record_id = ?", "deviceDailyRunningRecordId")
|
|
|
if err := query.First(); err != nil {
|
|
|
if err.Error() == "pg: no rows in result set" {
|
|
|
return nil, fmt.Errorf("没有此资源")
|
|
|
} else {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
if deviceDailyRunningRecordModel.DeviceDailyRunningRecordId == 0 {
|
|
|
return nil, nil
|
|
|
} else {
|
|
|
return transform.TransformToDeviceDailyRunningRecordDomainModelFromPgModels(deviceDailyRunningRecordModel)
|
|
|
}
|
|
|
}
|
|
|
func (repository *DeviceDailyRunningRecordRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.DeviceDailyRunningRecord, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
var deviceDailyRunningRecordModels []*models.DeviceDailyRunningRecord
|
|
|
deviceDailyRunningRecords := make([]*domain.DeviceDailyRunningRecord, 0)
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(&deviceDailyRunningRecordModels), queryOptions)
|
|
|
query.SetOffsetAndLimit(20)
|
|
|
query.SetOrderDirect("device_daily_running_record_id", "DESC")
|
|
|
if count, err := query.SelectAndCount(); err != nil {
|
|
|
return 0, deviceDailyRunningRecords, err
|
|
|
} else {
|
|
|
for _, deviceDailyRunningRecordModel := range deviceDailyRunningRecordModels {
|
|
|
if deviceDailyRunningRecord, err := transform.TransformToDeviceDailyRunningRecordDomainModelFromPgModels(deviceDailyRunningRecordModel); err != nil {
|
|
|
return 0, deviceDailyRunningRecords, err
|
|
|
} else {
|
|
|
deviceDailyRunningRecords = append(deviceDailyRunningRecords, deviceDailyRunningRecord)
|
|
|
}
|
|
|
}
|
|
|
return int64(count), deviceDailyRunningRecords, nil
|
|
|
}
|
|
|
}
|
|
|
func NewDeviceDailyRunningRecordRepository(transactionContext *pgTransaction.TransactionContext) (*DeviceDailyRunningRecordRepository, error) {
|
|
|
if transactionContext == nil {
|
|
|
return nil, fmt.Errorf("transactionContext参数不能为nil")
|
|
|
} else {
|
|
|
return &DeviceDailyRunningRecordRepository{
|
|
|
transactionContext: transactionContext,
|
|
|
}, nil
|
|
|
}
|
|
|
} |
...
|
...
|
|