pg_order_repository.go 5.7 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 OrderRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

func (repository *OrderRepository) nextIdentify() (int64, error) {
	IdWorker, err := snowflake.NewIdWorker(1)
	if err != nil {
		return 0, err
	}
	id, err := IdWorker.NextId()
	return id, err
}
func (repository *OrderRepository) Save(order *domain.Order) (*domain.Order, error) {
	sqlBuildFields := []string{
		"order_id",
		"buyer",
		"company_id",
		"partner_id",
		"delivery_code",
		"is_disable",
		"order_no",
		"order_detail",
		"order_dividend",
		"order_dividend_status",
		"order_goods",
		"order_source",
		"order_type",
		"order_status",
		"total_price",
		"region_info",
		"remarks",
		"create_at",
		"update_at",
		"delete_at",
	}
	insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
	insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
	returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
	updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "order_id")
	updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
	tx := repository.transactionContext.PgTx
	if order.Identify() == nil {
		orderId, err := repository.nextIdentify()
		if err != nil {
			return order, err
		} else {
			order.OrderId = orderId
		}
		if _, err := tx.QueryOne(
			pg.Scan(
				&order.OrderId,
				&order.Buyer,
				&order.CompanyId,
				&order.PartnerId,
				&order.DeliveryCode,
				&order.IsDisable,
				&order.OrderNo,
				&order.OrderDetail,
				&order.OrderDividend,
				&order.OrderDividendStatus,
				&order.OrderGoods,
				&order.OrderSource,
				&order.OrderType,
				&order.OrderStatus,
				&order.TotalPrice,
				&order.RegionInfo,
				&order.Remarks,
				&order.CreateAt,
				&order.UpdateAt,
				&order.DeleteAt,
			),
			fmt.Sprintf("INSERT INTO orders (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
			order.OrderId,
			order.Buyer,
			order.CompanyId,
			order.PartnerId,
			order.DeliveryCode,
			order.IsDisable,
			order.OrderNo,
			order.OrderDetail,
			order.OrderDividend,
			order.OrderDividendStatus,
			order.OrderGoods,
			order.OrderSource,
			order.OrderType,
			order.OrderStatus,
			order.TotalPrice,
			order.RegionInfo,
			order.Remarks,
			order.CreateAt,
			order.UpdateAt,
			order.DeleteAt,
		); err != nil {
			return order, err
		}
	} else {
		if _, err := tx.QueryOne(
			pg.Scan(
				&order.OrderId,
				&order.Buyer,
				&order.CompanyId,
				&order.PartnerId,
				&order.DeliveryCode,
				&order.IsDisable,
				&order.OrderNo,
				&order.OrderDetail,
				&order.OrderDividend,
				&order.OrderDividendStatus,
				&order.OrderGoods,
				&order.OrderSource,
				&order.OrderType,
				&order.OrderStatus,
				&order.TotalPrice,
				&order.RegionInfo,
				&order.Remarks,
				&order.CreateAt,
				&order.UpdateAt,
				&order.DeleteAt,
			),
			fmt.Sprintf("UPDATE orders SET %s WHERE order_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
			order.Buyer,
			order.CompanyId,
			order.PartnerId,
			order.DeliveryCode,
			order.IsDisable,
			order.OrderNo,
			order.OrderDetail,
			order.OrderDividend,
			order.OrderDividendStatus,
			order.OrderGoods,
			order.OrderSource,
			order.OrderType,
			order.OrderStatus,
			order.TotalPrice,
			order.RegionInfo,
			order.Remarks,
			order.CreateAt,
			order.UpdateAt,
			order.DeleteAt,
			order.Identify(),
		); err != nil {
			return order, err
		}
	}
	return order, nil
}
func (repository *OrderRepository) Remove(order *domain.Order) (*domain.Order, error) {
	tx := repository.transactionContext.PgTx
	orderModel := new(models.Order)
	orderModel.OrderId = order.Identify().(int64)
	if _, err := tx.Model(orderModel).WherePK().Delete(); err != nil {
		return order, err
	}
	return order, nil
}
func (repository *OrderRepository) FindOne(queryOptions map[string]interface{}) (*domain.Order, error) {
	tx := repository.transactionContext.PgTx
	orderModel := new(models.Order)
	query := sqlbuilder.BuildQuery(tx.Model(orderModel), queryOptions)
	query.SetWhereByQueryOption("orders.order_id = ?", "orderId")
	if err := query.First(); err != nil {
		if err.Error() == "pg: no rows in result set" {
			return nil, fmt.Errorf("没有此资源")
		} else {
			return nil, err
		}
	}
	if orderModel.OrderId == 0 {
		return nil, nil
	} else {
		return transform.TransformToOrderDomainModelFromPgModels(orderModel)
	}
}
func (repository *OrderRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Order, error) {
	tx := repository.transactionContext.PgTx
	var orderModels []*models.Order
	orders := make([]*domain.Order, 0)
	query := sqlbuilder.BuildQuery(tx.Model(&orderModels), queryOptions)
	query.SetOffsetAndLimit(20)
	query.SetOrderDirect("order_id", "DESC")
	if count, err := query.SelectAndCount(); err != nil {
		return 0, orders, err
	} else {
		for _, orderModel := range orderModels {
			if order, err := transform.TransformToOrderDomainModelFromPgModels(orderModel); err != nil {
				return 0, orders, err
			} else {
				orders = append(orders, order)
			}
		}
		return int64(count), orders, nil
	}
}
func NewOrderRepository(transactionContext *pgTransaction.TransactionContext) (*OrderRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &OrderRepository{
			transactionContext: transactionContext,
		}, nil
	}
}