|
|
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"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
type ExchangeCashActivityRepository struct {
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
func (repository *ExchangeCashActivityRepository) Save() (*ExchangeCashActivityRepository, error) {
|
|
|
|
|
|
return nil, nil
|
|
|
func (repository *ExchangeCashActivityRepository) Save(exchangeCashActivity *domain.ExchangeCashActivity) (*domain.ExchangeCashActivity, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
if exchangeCashActivity.Identity() == nil {
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(&exchangeCashActivity.ExchangeCashActivityId, &exchangeCashActivity.ExchangeActivityName),
|
|
|
"INSERT INTO exchange_cash_activity () VALUES () RETURNING id, " ,
|
|
|
exchangeCashActivity.ExchangeActivityName); err != nil {
|
|
|
return exchangeCashActivity, err
|
|
|
}
|
|
|
} else {
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(&exchangeCashActivity.ExchangeCashActivityId, &exchangeCashActivity.ExchangeActivityName),
|
|
|
"UPDATE exchange_cash_activity SET exchange_activity_name=?, WHERE id=? RETURNING id, exchange_cash_activity_name",
|
|
|
exchangeCashActivity.ExchangeActivityName); err != nil {
|
|
|
return exchangeCashActivity, err
|
|
|
}
|
|
|
}
|
|
|
return exchangeCashActivity, nil
|
|
|
}
|
|
|
|
|
|
func (repository *ExchangeCashActivityRepository) FindOne(queryOptions map[string]interface{}) (*ExchangeCashActivityRepository, error) {
|
|
|
|
|
|
func (repository *ExchangeCashActivityRepository) FindOne(queryOptions map[string]interface{}) (*domain.ExchangeCashActivity, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
exchangeCashActivityModel := new(models.ExchangeCashActivity)
|
|
|
query := tx.Model(exchangeCashActivityModel)
|
|
|
if exchangeCashActivityId, ok := queryOptions["exchangeCashActivityId"]; ok {
|
|
|
query = query.Where("exchange_cash_list.id = ?", exchangeCashActivityId)
|
|
|
}
|
|
|
if err := query.First(); err != nil {
|
|
|
if err.Error() == "pg: no rows in result set" {
|
|
|
return nil, fmt.Errorf("没有此资源")
|
|
|
} else {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
if exchangeCashActivityModel.Id == 0 {
|
|
|
return nil, nil
|
|
|
} else {
|
|
|
return repository.transformPgModelToDomainModel(exchangeCashActivityModel)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (repository *ExchangeCashActivityRepository) Find(queryOptions map[string]interface{}) (*ExchangeCashActivityRepository, error) {
|
|
|
|
|
|
return nil, nil
|
|
|
func (repository *ExchangeCashActivityRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.ExchangeCashActivity, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
var exchangeCashActivityModels []*models.ExchangeCashActivity
|
|
|
exchangeCashActivities := make([]*domain.ExchangeCashActivity, 0)
|
|
|
query := tx.Model(&exchangeCashActivityModels)
|
|
|
// 公司相关活动搜索
|
|
|
if companyId, ok := queryOptions["companyId"]; ok {
|
|
|
query = query.Where(`exchange_cash_activity.company_id = ?`, companyId)
|
|
|
}
|
|
|
// 活动名称模糊搜索
|
|
|
if exchangeCashActivityNameMatch, ok := queryOptions["exchangeCashActivityNameMatch"]; ok && (exchangeCashActivityNameMatch != "") {
|
|
|
query = query.Where(`exchange_cash_activity.exchange_cash_activity_name LIKE ?`, fmt.Sprintf("%%%s%%", exchangeCashActivityNameMatch.(string)))
|
|
|
}
|
|
|
// 活动截止时间搜索
|
|
|
if deadline, ok := queryOptions["exchangeCashActivityDeadline"]; ok && !deadline.(time.Time).IsZero() {
|
|
|
query = query.Where(`exchange_cash_activity.deadline < ?`, deadline)
|
|
|
}
|
|
|
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, exchangeCashActivities, err
|
|
|
} else {
|
|
|
for _, exchangeCashActivityModel := range exchangeCashActivityModels {
|
|
|
if taskNature, err := repository.transformPgModelToDomainModel(exchangeCashActivityModel); err != nil {
|
|
|
return 0, exchangeCashActivities, err
|
|
|
} else {
|
|
|
exchangeCashActivities = append(exchangeCashActivities, taskNature)
|
|
|
}
|
|
|
}
|
|
|
return int64(count), exchangeCashActivities, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (repository *ExchangeCashActivityRepository) Remove() (*ExchangeCashActivityRepository, error) {
|
|
|
|
|
|
return nil, nil
|
|
|
func (repository *ExchangeCashActivityRepository) Remove(exchangeCashActivity *domain.ExchangeCashActivity) (*domain.ExchangeCashActivity, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
exchangeCashActivityModel := new(models.ExchangeCashActivity)
|
|
|
exchangeCashActivity.ExchangeCashActivityId = exchangeCashActivity.Identity().(int64)
|
|
|
if _, err := tx.Model(exchangeCashActivityModel).WherePK().Delete(); err != nil {
|
|
|
return exchangeCashActivity, err
|
|
|
}
|
|
|
return exchangeCashActivity, nil
|
|
|
}
|
|
|
|
|
|
//func (repository *ExchangeCashActivityRepository) transformPgModelToDomainModel(customerValueModel *models.CustomerValue) (*domain.CustomerValue, error) {
|
|
|
// return &domain.CustomerValue{
|
|
|
// CustomerValueId: customerValueModel.Id,
|
|
|
// CustomerValueName: customerValueModel.CustomerValueName,
|
|
|
// CompanyId: customerValueModel.CompanyId,
|
|
|
// }, nil
|
|
|
//}
|
|
|
//func NewExchangeCashActivityRepository(transactionContext *pgTransaction.TransactionContext) (*CustomerValueRepository, error) {
|
|
|
// if transactionContext == nil {
|
|
|
// return nil, fmt.Errorf("transactionContext参数不能为nil")
|
|
|
// } else {
|
|
|
// return &CustomerValueRepository{
|
|
|
// transactionContext: transactionContext,
|
|
|
// }, nil
|
|
|
// }
|
|
|
//} |
|
|
\ No newline at end of file |
|
|
func (repository *ExchangeCashActivityRepository) transformPgModelToDomainModel(exchangeCashActivity *models.ExchangeCashActivity) (*domain.ExchangeCashActivity, error) {
|
|
|
return &domain.ExchangeCashActivity{
|
|
|
ExchangeCashActivityId: exchangeCashActivity.Id,
|
|
|
ExchangeActivityName: exchangeCashActivity.ActivityName,
|
|
|
CompanyId: exchangeCashActivity.CompanyId,
|
|
|
ExchangedCash: exchangeCashActivity.ExchangedCash,
|
|
|
ExchangedSuMoney: exchangeCashActivity.ExchangedSuMoney,
|
|
|
CreateTime: exchangeCashActivity.CreateTime,
|
|
|
Deadline: exchangeCashActivity.Deadline,
|
|
|
CountDown: exchangeCashActivity.CountDown,
|
|
|
Rate: exchangeCashActivity.ExchangeRate,
|
|
|
LastRate: exchangeCashActivity.LastExchangeRate,
|
|
|
Operator: exchangeCashActivity.Sponsor,
|
|
|
ExchangeCashPeople: exchangeCashActivity.ExchangeSuMoneyList,
|
|
|
}, nil
|
|
|
}
|
|
|
func NewExchangeCashActivityRepository(transactionContext *pgTransaction.TransactionContext) (*ExchangeCashActivityRepository, error) {
|
|
|
if transactionContext == nil {
|
|
|
return nil, fmt.Errorf("transactionContext参数不能为nil")
|
|
|
} else {
|
|
|
return &ExchangeCashActivityRepository{
|
|
|
transactionContext: transactionContext,
|
|
|
}, nil
|
|
|
}
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|