审查视图

pkg/infrastructure/repository/pg_log_sms_repository.go 3.0 KB
tangxvhui authored
1
package repository
tangxvhui authored
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

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)
tangxvhui authored
19 20 21 22 23 24 25 26 27
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,
tangxvhui authored
28 29 30
		Status:     string(param.Status),
		From:       param.From,
		Index:      param.Index,
tangxvhui authored
31
		ExecuteAt:  param.ExecuteAt,
tangxvhui authored
32 33
	}
	tx := repo.transactionContext.PgTx
tangxvhui authored
34 35 36 37 38
	if m.Id == 0 {
		_, err := tx.Model(&m).Insert()
		if err != nil {
			return err
		}
tangxvhui authored
39
		return nil
tangxvhui authored
40 41
	}
	_, err := tx.Model(&m).WherePK().Update()
tangxvhui authored
42 43 44
	if err != nil {
		return err
	}
tangxvhui authored
45
tangxvhui authored
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	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)
	}
tangxvhui authored
64
	if v, ok := queryOptions["executeAtBegin"]; ok {
tangxvhui authored
65
		query.Where("execute_at>=?", v)
tangxvhui authored
66 67 68
	}

	if v, ok := queryOptions["executeAtEnd"]; ok {
tangxvhui authored
69
		query.Where("execute_at<=?", v)
tangxvhui authored
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	}

	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,
	}
}
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

//批量添加

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
}