pg_goods_repository.go 4.8 KB
package repository

import (
	"fmt"
	"github.com/go-pg/pg/v10"

	"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"github.com/linmadan/egglib-go/utils/snowflake"
	"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg/transform"
)

type GoodRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

func (repository *GoodRepository) nextIdentify() (int64, error) {
	IdWorker, err := snowflake.NewIdWorker(1)
	if err != nil {
		return 0, err
	}
	id, err := IdWorker.NextId()
	return id, err
}
func (repository *GoodRepository) Save(good *domain.Good) (*domain.Good, error) {
	sqlBuildFields := []string{
		"good_id",
		"good_num",
		"good_dividend",
		"good_dividend_status",
		"good_name",
		"good_total_price",
		"remarks",
		"order_no",
		"good_detail",
		"create_at",
		"update_at",
		"delete_at",
	}
	insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
	insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
	returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
	updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "goods_id")
	updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
	tx := repository.transactionContext.PgTx
	if good.Identify() == nil {
		goodId, err := repository.nextIdentify()
		if err != nil {
			return good, err
		} else {
			good.GoodId = goodId
		}
		if _, err := tx.QueryOne(
			pg.Scan(
				&good.GoodId,
				&good.GoodNum,
				&good.GoodDividend,
				&good.GoodDividendStatus,
				&good.GoodName,
				&good.GoodTotalPrice,
				&good.Remarks,
				&good.OrderNo,
				&good.GoodDetail,
				&good.CreateAt,
				&good.UpdateAt,
				&good.DeleteAt,
			),
			fmt.Sprintf("INSERT INTO good (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
			good.GoodId,
			good.GoodNum,
			good.GoodDividend,
			good.GoodDividendStatus,
			good.GoodName,
			good.GoodTotalPrice,
			good.Remarks,
			good.OrderNo,
			good.GoodDetail,
			good.CreateAt,
			good.UpdateAt,
			good.DeleteAt,
		); err != nil {
			return good, err
		}
	} else {
		if _, err := tx.QueryOne(
			pg.Scan(
				&good.GoodId,
				&good.GoodNum,
				&good.GoodDividend,
				&good.GoodDividendStatus,
				&good.GoodName,
				&good.GoodTotalPrice,
				&good.Remarks,
				&good.OrderNo,
				&good.GoodDetail,
				&good.CreateAt,
				&good.UpdateAt,
				&good.DeleteAt,
			),
			fmt.Sprintf("UPDATE good SET %s WHERE goods_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
			good.GoodId,
			good.GoodNum,
			good.GoodDividend,
			good.GoodDividendStatus,
			good.GoodName,
			good.GoodTotalPrice,
			good.Remarks,
			good.OrderNo,
			good.GoodDetail,
			good.CreateAt,
			good.UpdateAt,
			good.DeleteAt,
			good.Identify(),
		); err != nil {
			return good, err
		}
	}
	return good, nil
}
func (repository *GoodRepository) Remove(good *domain.Good) (*domain.Good, error) {
	tx := repository.transactionContext.PgTx
	goodModel := new(models.Good)
	goodModel.GoodId = good.Identify().(int64)
	if _, err := tx.Model(goodModel).WherePK().Delete(); err != nil {
		return good, err
	}
	return good, nil
}
func (repository *GoodRepository) FindOne(queryOptions map[string]interface{}) (*domain.Good, error) {
	tx := repository.transactionContext.PgTx
	goodModel := new(models.Good)
	query := sqlbuilder.BuildQuery(tx.Model(goodModel), queryOptions)
	query.SetWhereByQueryOption("good.goods_id = ?", "goodsId")
	if err := query.First(); err != nil {
		if err.Error() == "pg: no rows in result set" {
			return nil, fmt.Errorf("没有此资源")
		} else {
			return nil, err
		}
	}
	if goodModel.GoodId == 0 {
		return nil, nil
	} else {
		return transform.TransformToGoodDomainModelFromPgModels(goodModel)
	}
}
func (repository *GoodRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Good, error) {
	tx := repository.transactionContext.PgTx
	var goodModels []*models.Good
	goods := make([]*domain.Good, 0)
	query := sqlbuilder.BuildQuery(tx.Model(&goodModels), queryOptions)
	query.SetOffsetAndLimit(20)
	query.SetOrderDirect("goods_id", "DESC")
	if count, err := query.SelectAndCount(); err != nil {
		return 0, goods, err
	} else {
		for _, goodModel := range goodModels {
			if good, err := transform.TransformToGoodDomainModelFromPgModels(goodModel); err != nil {
				return 0, goods, err
			} else {
				goods = append(goods, good)
			}
		}
		return int64(count), goods, nil
	}
}
func NewGoodsRepository(transactionContext *pgTransaction.TransactionContext) (*GoodRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &GoodRepository{
			transactionContext: transactionContext,
		}, nil
	}
}