pg_log_sms_repository.go 3.1 KB
package repository

import (
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
)

type LogSmsRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

func NewLogSmsRepository(transactionContext *pgTransaction.TransactionContext) *LogSmsRepository {
	return &LogSmsRepository{transactionContext: transactionContext}
}

var _ domain.LogSmsRepository = (*LogSmsRepository)(nil)

func (repo *LogSmsRepository) Save(param *domain.LogSms) error {
	m := models.LogSms{
		Id:         param.Id,
		Phone:      param.Phone,
		TemplateId: param.TemplateId,
		Template:   param.Template,
		Value:      param.Value,
		CreatedAt:  param.CreatedAt,
		Result:     param.Result,
		Status:     string(param.Status),
		From:       param.From,
		Index:      param.Index,
		ExecuteAt:  param.ExecuteAt,
	}
	tx := repo.transactionContext.PgTx
	if m.Id == 0 {
		_, err := tx.Model(&m).Insert()
		if err != nil {
			return err
		}
		return nil
	}
	_, err := tx.Model(&m).WherePK().Update()
	if err != nil {
		return err
	}

	return nil
}

func (repo *LogSmsRepository) Find(queryOptions map[string]interface{}) (int, []*domain.LogSms, error) {
	tx := repo.transactionContext.PgTx
	var m []*models.LogSms
	query := tx.Model(&m)
	if v, ok := queryOptions["limit"].(int); ok {
		query.Limit(v)
	}
	if v, ok := queryOptions["offset"].(int); ok {
		query.Offset(v)
	}

	if v, ok := queryOptions["status"]; ok {
		query.Where("status=?", v)
	}

	if v, ok := queryOptions["executeAtBegin"]; ok {
		query.Where("execute_at>=?", v)
	}

	if v, ok := queryOptions["executeAtEnd"]; ok {
		query.Where("execute_at<=?", v)
	}
	if v, ok := queryOptions["from"]; ok {
		query.Where("from=?", v)
	}
	if v, ok := queryOptions["index"]; ok {
		query.Where("index=?", v)
	}
	count, err := query.SelectAndCount()
	if err != nil {
		return 0, nil, err
	}
	var datas []*domain.LogSms
	for _, v := range m {
		d := repo.TransformToDomain(v)
		datas = append(datas, d)
	}
	return count, datas, nil
}

func (repo *LogSmsRepository) TransformToDomain(d *models.LogSms) *domain.LogSms {
	return &domain.LogSms{
		Id:         d.Id,
		Phone:      d.Phone,
		TemplateId: d.TemplateId,
		Template:   d.Template,
		Value:      d.Value,
		Result:     d.Result,
		Status:     domain.SmsStatus(d.Status),
		From:       d.From,
		Index:      d.Index,
		ExecuteAt:  d.ExecuteAt,
		CreatedAt:  d.CreatedAt,
	}
}

//批量添加

func (repo *LogSmsRepository) BatchInsert(params []*domain.LogSms) error {
	smsList := []models.LogSms{}
	for _, param := range params {
		m := models.LogSms{
			Id:         param.Id,
			Phone:      param.Phone,
			TemplateId: param.TemplateId,
			Template:   param.Template,
			Value:      param.Value,
			CreatedAt:  param.CreatedAt,
			Result:     param.Result,
			Status:     string(param.Status),
			From:       param.From,
			Index:      param.Index,
			ExecuteAt:  param.ExecuteAt,
		}
		smsList = append(smsList, m)
	}

	tx := repo.transactionContext.PgTx

	_, err := tx.Model(&smsList).Insert()
	if err != nil {
		return err
	}
	return nil
}