|
|
package repository
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
|
|
|
"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 RewardRuleRepository struct {
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
var _ domain.RewardRuleRepository = (*RewardRuleRepository)(nil)
|
|
|
|
|
|
func NewRewardRuleRepository(transactionContext *pgTransaction.TransactionContext) (*RewardRuleRepository, error) {
|
|
|
if transactionContext == nil {
|
|
|
return nil, fmt.Errorf("transactionContext参数不能为nil")
|
|
|
} else {
|
|
|
return &RewardRuleRepository{
|
|
|
transactionContext: transactionContext,
|
|
|
}, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// func (repo *RewardStandardRepository) nextIdentify() (int64, error) {
|
|
|
// IdWorker, err := snowflake.NewIdWorker(1)
|
|
|
// if err != nil {
|
|
|
// return 0, err
|
|
|
// }
|
|
|
// id, err := IdWorker.NextId()
|
|
|
// return id, err
|
|
|
// }
|
|
|
|
|
|
func (repo *RewardRuleRepository) Save(param *domain.RewardRule) (*domain.RewardRule, error) {
|
|
|
m := models.RewardRule{
|
|
|
Id: param.Id,
|
|
|
CompanyId: param.CompanyId,
|
|
|
OrgId: param.OrgId,
|
|
|
RewardTag: param.RewardTag,
|
|
|
RewardNum: param.RewardNum,
|
|
|
RewardAmount: param.RewardAmount,
|
|
|
FaultTag: param.FaultTag,
|
|
|
FaultNum: param.FaultNum,
|
|
|
FaultAmount: param.FaultAmount,
|
|
|
CreatedAt: param.CreatedAt,
|
|
|
UpdatedAt: param.UpdatedAt,
|
|
|
Remark: param.Remark,
|
|
|
}
|
|
|
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 *RewardRuleRepository) Remove(param *domain.RewardStandard) (*domain.RewardStandard, error) {
|
|
|
// tx := repo.transactionContext.PgTx
|
|
|
// m := new(models.RewardStandard)
|
|
|
// 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 *RewardRuleRepository) FindOne(queryOptions map[string]interface{}) (*domain.RewardRule, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
m := new(models.RewardRule)
|
|
|
query := tx.Model(m)
|
|
|
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 *RewardRuleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.RewardRule, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
m := []models.RewardRule{}
|
|
|
query := tx.Model(&m).
|
|
|
Limit(20)
|
|
|
if v, ok := queryOptions["limit"].(int); ok {
|
|
|
query.Limit(v)
|
|
|
}
|
|
|
if v, ok := queryOptions["offset"].(int); ok {
|
|
|
query.Offset(v)
|
|
|
}
|
|
|
if v, ok := queryOptions["companyId"]; ok {
|
|
|
query.Where("company_id=?", v)
|
|
|
}
|
|
|
cnt, err := query.SelectAndCount()
|
|
|
if err != nil {
|
|
|
return 0, nil, err
|
|
|
}
|
|
|
|
|
|
var listData []*domain.RewardRule
|
|
|
for i := range m {
|
|
|
temp := repo.TransformToDomain(&m[i])
|
|
|
listData = append(listData, temp)
|
|
|
}
|
|
|
return int64(cnt), listData, nil
|
|
|
}
|
|
|
|
|
|
func (repo *RewardRuleRepository) TransformToDomain(param *models.RewardRule) *domain.RewardRule {
|
|
|
return &domain.RewardRule{
|
|
|
Id: param.Id,
|
|
|
CompanyId: param.CompanyId,
|
|
|
OrgId: param.OrgId,
|
|
|
RewardTag: param.RewardTag,
|
|
|
RewardNum: param.RewardNum,
|
|
|
RewardAmount: param.RewardAmount,
|
|
|
FaultTag: param.FaultTag,
|
|
|
FaultNum: param.FaultNum,
|
|
|
FaultAmount: param.FaultAmount,
|
|
|
CreatedAt: param.CreatedAt,
|
|
|
UpdatedAt: param.UpdatedAt,
|
|
|
Remark: param.Remark,
|
|
|
}
|
|
|
} |
...
|
...
|
|