|
|
package repository
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"time"
|
|
|
|
|
|
"github.com/go-pg/pg/v10"
|
|
|
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"
|
|
|
)
|
|
|
|
|
|
type ProductTroubleRepository struct {
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
var _ domain.ProductTroubleRepository = (*ProductTroubleRepository)(nil)
|
|
|
|
|
|
func NewProductTroubleRepository(transactionContext *pgTransaction.TransactionContext) (*ProductTroubleRepository, error) {
|
|
|
if transactionContext == nil {
|
|
|
return nil, fmt.Errorf("transactionContext参数不能为nil")
|
|
|
} else {
|
|
|
return &ProductTroubleRepository{
|
|
|
transactionContext: transactionContext,
|
|
|
}, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (repo *ProductTroubleRepository) Save(param *domain.ProductTrouble) (*domain.ProductTrouble, error) {
|
|
|
m := models.ProductTrouble{
|
|
|
Id: param.Id,
|
|
|
CompanyId: param.CompanyId,
|
|
|
OrgId: param.OrgId,
|
|
|
WorkStation: param.WorkStation,
|
|
|
ProductWorker: param.ProductWorker,
|
|
|
AmountLoss: param.AmountLoss,
|
|
|
Types: int(param.Types),
|
|
|
RecordData: param.RecordData,
|
|
|
Remark: param.Remark,
|
|
|
ApproveStatus: int(param.ApproveStatus),
|
|
|
ApproveAt: param.ApproveAt,
|
|
|
ApproveUser: param.ApproveUser,
|
|
|
CreatedAt: param.CreatedAt,
|
|
|
UpdatedAt: param.UpdatedAt,
|
|
|
DeletedAt: param.DeletedAt,
|
|
|
}
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
if param.Id == 0 {
|
|
|
_, err := tx.Model(&m).Insert()
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
param.Id = m.Id
|
|
|
} else {
|
|
|
_, err := tx.Model(&m).WherePK().Update()
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return param, nil
|
|
|
}
|
|
|
|
|
|
func (repo *ProductTroubleRepository) Remove(param *domain.ProductTrouble) (*domain.ProductTrouble, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
m := new(models.ProductTrouble)
|
|
|
m.Id = param.Id
|
|
|
nowTime := time.Now()
|
|
|
param.DeletedAt = &nowTime
|
|
|
_, err := tx.Model(m).
|
|
|
WherePK().Set("deleted_at=?", nowTime).
|
|
|
Update()
|
|
|
if err != nil {
|
|
|
return param, err
|
|
|
}
|
|
|
|
|
|
return nil, nil
|
|
|
}
|
|
|
|
|
|
func (repo *ProductTroubleRepository) FindOne(queryOptions map[string]interface{}) (*domain.ProductTrouble, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
m := new(models.ProductTrouble)
|
|
|
query := tx.Model(m).Where("deleted_at isnull")
|
|
|
if v, ok := queryOptions["id"]; ok {
|
|
|
query.Where("id=?", v)
|
|
|
}
|
|
|
err := query.First()
|
|
|
if err != nil {
|
|
|
if err == pg.ErrNoRows {
|
|
|
return nil, domain.ErrorNotFound
|
|
|
} else {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
result := repo.TransformToDomain(m)
|
|
|
return result, nil
|
|
|
}
|
|
|
|
|
|
func (repo *ProductTroubleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.ProductTrouble, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
m := []models.ProductTrouble{}
|
|
|
query := tx.Model(&m).
|
|
|
Where("deleted_at isnull").
|
|
|
Limit(20)
|
|
|
if v, ok := queryOptions["limit"].(int); ok {
|
|
|
query.Limit(v)
|
|
|
}
|
|
|
|
|
|
if v, ok := queryOptions["offset"].(int); ok {
|
|
|
query.Offset(v)
|
|
|
}
|
|
|
|
|
|
cnt, err := query.SelectAndCount()
|
|
|
if err != nil {
|
|
|
return 0, nil, err
|
|
|
}
|
|
|
|
|
|
var listData []*domain.ProductTrouble
|
|
|
for i := range m {
|
|
|
temp := repo.TransformToDomain(&m[i])
|
|
|
listData = append(listData, temp)
|
|
|
}
|
|
|
return int64(cnt), listData, nil
|
|
|
}
|
|
|
|
|
|
func (repo *ProductTroubleRepository) TransformToDomain(param *models.ProductTrouble) *domain.ProductTrouble {
|
|
|
return &domain.ProductTrouble{
|
|
|
Id: param.Id,
|
|
|
CompanyId: param.CompanyId,
|
|
|
OrgId: param.OrgId,
|
|
|
WorkStation: param.WorkStation,
|
|
|
ProductWorker: param.ProductWorker,
|
|
|
AmountLoss: param.AmountLoss,
|
|
|
Types: domain.TroubleType(param.Types),
|
|
|
RecordData: param.RecordData,
|
|
|
Remark: param.Remark,
|
|
|
ApproveStatus: domain.ProductTroubleApprove(param.ApproveStatus),
|
|
|
ApproveAt: param.ApproveAt,
|
|
|
ApproveUser: param.ApproveUser,
|
|
|
CreatedAt: param.CreatedAt,
|
|
|
UpdatedAt: param.UpdatedAt,
|
|
|
DeletedAt: param.DeletedAt,
|
|
|
}
|
|
|
} |
...
|
...
|
|