pg_reward_standard_repository.go 4.9 KB
package repository

import (
	"fmt"
	"time"

	"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 RewardStandardRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

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

func NewRewardStandardRepository(transactionContext *pgTransaction.TransactionContext) (*RewardStandardRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &RewardStandardRepository{
			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 *RewardStandardRepository) Save(param *domain.RewardStandard) (*domain.RewardStandard, error) {
	m := models.RewardStandard{
		Id:             param.Id,
		Workshop:       param.Workshop,
		ProductLine:    param.ProductLine,
		ProductSection: param.ProductSection,
		Remark:         param.Remark,
		TargetType:     param.TargetType,
		TargeVal1:      param.TargeVal1,
		TargeVal2:      param.TargeVal2,
		TargeVal3:      param.TargeVal3,
		TargeVal4:      param.TargeVal4,
		CreatedAt:      param.CreatedAt,
		UpdatedAt:      param.UpdatedAt,
		DeletedAt:      param.DeletedAt,
		CompanyId:      param.CompanyId,
		OrgId:          param.OrgId,
	}
	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 *RewardStandardRepository) 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 *RewardStandardRepository) FindOne(queryOptions map[string]interface{}) (*domain.RewardStandard, error) {
	tx := repo.transactionContext.PgTx
	m := new(models.RewardStandard)
	query := tx.Model(m).Where("deleted_at isnull")
	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 *RewardStandardRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.RewardStandard, error) {
	tx := repo.transactionContext.PgTx
	m := []models.RewardStandard{}
	query := tx.Model(&m).
		Where("deleted_at isnull").
		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["workshopId"]; ok {
		query.Where("workshop->>'workshopId'='?'", v)
	}
	if v, ok := queryOptions["lineId"]; ok {
		query.Where("product_line->>'lineId'='?'", v)
	}
	if v, ok := queryOptions["sectionId"]; ok {
		query.Where("product_section->>'sectionId'='?'", v)
	}

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

	if v, ok := queryOptions["workshopName"]; ok && len(v.(string)) > 0 {
		query.Where(fmt.Sprintf(`work_shop->>'workshopName' like '%%%v%%'`, v))
	}
	if v, ok := queryOptions["lineName"]; ok && len(v.(string)) > 0 {
		query.Where(fmt.Sprintf(`product_line->>'lineName' like '%%%v%%'`, v))
	}
	if v, ok := queryOptions["sectionName"]; ok && len(v.(string)) > 0 {
		query.Where(fmt.Sprintf(`product_section->>'sectionName' like '%%%v%%'`, v))
	}

	cnt, err := query.SelectAndCount()
	if err != nil {
		return 0, nil, err
	}

	var listData []*domain.RewardStandard
	for i := range m {
		temp := repo.TransformToDomain(&m[i])
		listData = append(listData, temp)
	}
	return int64(cnt), listData, nil
}

func (repo *RewardStandardRepository) TransformToDomain(param *models.RewardStandard) *domain.RewardStandard {
	return &domain.RewardStandard{
		Id:             param.Id,
		Workshop:       param.Workshop,
		ProductLine:    param.ProductLine,
		ProductSection: param.ProductSection,
		Remark:         param.Remark,
		TargetType:     param.TargetType,
		TargeVal1:      param.TargeVal1,
		TargeVal2:      param.TargeVal2,
		TargeVal3:      param.TargeVal3,
		TargeVal4:      param.TargeVal4,
		CreatedAt:      param.CreatedAt,
		UpdatedAt:      param.UpdatedAt,
		DeletedAt:      param.DeletedAt,
		CompanyId:      param.CompanyId,
		OrgId:          param.OrgId,
	}
}