|
|
package repository
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
|
|
|
"github.com/go-pg/pg"
|
|
|
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
|
|
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg/models"
|
|
|
)
|
|
|
|
|
|
type SuMoneyTransactionRecordRepository struct {
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
func (repository *SuMoneyTransactionRecordRepository) nextIdentify() (int64, error) {
|
|
|
return 0, nil
|
|
|
}
|
|
|
func (repository *SuMoneyTransactionRecordRepository) Save(suMoneyTransactionRecord *domain.SuMoneyTransactionRecord) (*domain.SuMoneyTransactionRecord, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
if suMoneyTransactionRecord.Identify() == nil {
|
|
|
_, err := repository.nextIdentify()
|
|
|
if err != nil {
|
|
|
return suMoneyTransactionRecord, err
|
|
|
}
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(&suMoneyTransactionRecord.SuMoneyTransactionRecordId, &suMoneyTransactionRecord.RecordType, &suMoneyTransactionRecord.Employee, &suMoneyTransactionRecord.SuMoney, &suMoneyTransactionRecord.Operator, &suMoneyTransactionRecord.RecordDescription, &suMoneyTransactionRecord.CreateTime),
|
|
|
"INSERT INTO su_money_transaction_records (id, record_type, employee, su_money, operator, record_description, create_time) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING su_money_transaction_record_id, record_type, employee, su_money, operator, record_description, create_time",
|
|
|
suMoneyTransactionRecord.SuMoneyTransactionRecordId, suMoneyTransactionRecord.RecordType, suMoneyTransactionRecord.Employee, suMoneyTransactionRecord.SuMoney, suMoneyTransactionRecord.Operator, suMoneyTransactionRecord.RecordDescription, suMoneyTransactionRecord.CreateTime); err != nil {
|
|
|
return suMoneyTransactionRecord, err
|
|
|
}
|
|
|
} else {
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(&suMoneyTransactionRecord.SuMoneyTransactionRecordId, &suMoneyTransactionRecord.RecordType, &suMoneyTransactionRecord.Employee, &suMoneyTransactionRecord.SuMoney, &suMoneyTransactionRecord.Operator, &suMoneyTransactionRecord.RecordDescription, &suMoneyTransactionRecord.CreateTime),
|
|
|
"UPDATE su_money_transaction_records SET record_type=?, employee=?, su_money=?, operator=?, record_description=?, create_time=? WHERE id=? RETURNING su_money_transaction_record_id, record_type, employee, su_money, operator, record_description, create_time",
|
|
|
suMoneyTransactionRecord.RecordType, suMoneyTransactionRecord.Employee, suMoneyTransactionRecord.SuMoney, suMoneyTransactionRecord.Operator, suMoneyTransactionRecord.RecordDescription, suMoneyTransactionRecord.CreateTime, suMoneyTransactionRecord.SuMoneyTransactionRecordId); err != nil {
|
|
|
return suMoneyTransactionRecord, err
|
|
|
}
|
|
|
}
|
|
|
return suMoneyTransactionRecord, nil
|
|
|
}
|
|
|
func (repository *SuMoneyTransactionRecordRepository) Remove(suMoneyTransactionRecord *domain.SuMoneyTransactionRecord) (*domain.SuMoneyTransactionRecord, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
|
|
|
suMoneyTransactionRecordModel.Id = suMoneyTransactionRecord.Identify().(int64)
|
|
|
if _, err := tx.Model(suMoneyTransactionRecordModel).WherePK().Delete(); err != nil {
|
|
|
return suMoneyTransactionRecord, err
|
|
|
}
|
|
|
return suMoneyTransactionRecord, nil
|
|
|
}
|
|
|
func (repository *SuMoneyTransactionRecordRepository) FindOne(queryOptions map[string]interface{}) (*domain.SuMoneyTransactionRecord, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
|
|
|
query := tx.Model(suMoneyTransactionRecordModel)
|
|
|
if suMoneyTransactionRecordId, ok := queryOptions["suMoneyTransactionRecordId"]; ok {
|
|
|
query = query.Where("su_money_transaction_record.id = ?", suMoneyTransactionRecordId)
|
|
|
}
|
|
|
if err := query.First(); err != nil {
|
|
|
if err.Error() == "pg: no rows in result set" {
|
|
|
return nil, fmt.Errorf("没有此资源")
|
|
|
} else {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
if suMoneyTransactionRecordModel.Id == 0 {
|
|
|
return nil, nil
|
|
|
} else {
|
|
|
return repository.transformPgModelToDomainModel(suMoneyTransactionRecordModel)
|
|
|
}
|
|
|
}
|
|
|
func (repository *SuMoneyTransactionRecordRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.SuMoneyTransactionRecord, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
var suMoneyTransactionRecordModels []*models.SuMoneyTransactionRecord
|
|
|
suMoneyTransactionRecords := make([]*domain.SuMoneyTransactionRecord, 0)
|
|
|
query := tx.Model(&suMoneyTransactionRecordModels)
|
|
|
if employeeUid, ok := queryOptions["employeeUid"]; ok && (employeeUid != int64(0)) {
|
|
|
query = query.Where(`task.employee @> '{"uid":?}'`, employeeUid)
|
|
|
}
|
|
|
if offset, ok := queryOptions["offset"]; ok {
|
|
|
offset := offset.(int)
|
|
|
if offset > -1 {
|
|
|
query = query.Offset(offset)
|
|
|
}
|
|
|
} else {
|
|
|
query = query.Offset(0)
|
|
|
}
|
|
|
if limit, ok := queryOptions["limit"]; ok {
|
|
|
limit := limit.(int)
|
|
|
if limit > -1 {
|
|
|
query = query.Limit(limit)
|
|
|
}
|
|
|
} else {
|
|
|
query = query.Limit(20)
|
|
|
}
|
|
|
if count, err := query.Order("id DESC").SelectAndCount(); err != nil {
|
|
|
return 0, suMoneyTransactionRecords, err
|
|
|
} else {
|
|
|
for _, suMoneyTransactionRecordModel := range suMoneyTransactionRecordModels {
|
|
|
if suMoneyTransactionRecord, err := repository.transformPgModelToDomainModel(suMoneyTransactionRecordModel); err != nil {
|
|
|
return 0, suMoneyTransactionRecords, err
|
|
|
} else {
|
|
|
suMoneyTransactionRecords = append(suMoneyTransactionRecords, suMoneyTransactionRecord)
|
|
|
}
|
|
|
suMoneyTransactionRecords = append(suMoneyTransactionRecords, &domain.SuMoneyTransactionRecord{})
|
|
|
}
|
|
|
return int64(count), suMoneyTransactionRecords, nil
|
|
|
}
|
|
|
}
|
|
|
func (repository *SuMoneyTransactionRecordRepository) transformPgModelToDomainModel(suMoneyTransactionRecordModel *models.SuMoneyTransactionRecord) (*domain.SuMoneyTransactionRecord, error) {
|
|
|
return &domain.SuMoneyTransactionRecord{
|
|
|
SuMoneyTransactionRecordId: suMoneyTransactionRecordModel.Id,
|
|
|
RecordType: suMoneyTransactionRecordModel.RecordType,
|
|
|
Employee: suMoneyTransactionRecordModel.Employee,
|
|
|
SuMoney: suMoneyTransactionRecordModel.SuMoney,
|
|
|
Operator: suMoneyTransactionRecordModel.Operator,
|
|
|
RecordDescription: suMoneyTransactionRecordModel.RecordDescription,
|
|
|
CreateTime: suMoneyTransactionRecordModel.CreateTime,
|
|
|
}, nil
|
|
|
}
|
|
|
func NewSuMoneyTransactionRecordRepository(transactionContext *pgTransaction.TransactionContext) (*SuMoneyTransactionRecordRepository, error) {
|
|
|
if transactionContext == nil {
|
|
|
return nil, fmt.Errorf("transactionContext参数不能为nil")
|
|
|
} else {
|
|
|
return &SuMoneyTransactionRecordRepository{
|
|
|
transactionContext: transactionContext,
|
|
|
}, nil
|
|
|
}
|
|
|
} |
...
|
...
|
|