|
|
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/allied-creation/allied-creation-cooperation/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/transform"
|
|
|
)
|
|
|
|
|
|
type DividendsReturnedOrderRepository struct {
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
func (repository *DividendsReturnedOrderRepository) nextIdentify() (int64, error) {
|
|
|
IdWorker, err := snowflake.NewIdWorker(1)
|
|
|
if err != nil {
|
|
|
return 0, err
|
|
|
}
|
|
|
id, err := IdWorker.NextId()
|
|
|
return id, err
|
|
|
}
|
|
|
func (repository *DividendsReturnedOrderRepository) Save(dividendsReturnedOrder *domain.DividendsReturnedOrder) (*domain.DividendsReturnedOrder, error) {
|
|
|
sqlBuildFields := []string{
|
|
|
"dividends_returned_order_id",
|
|
|
"dividends_returned_order_number",
|
|
|
"dividends_returned_order_refund",
|
|
|
"original_order_num",
|
|
|
"dividends_order_number",
|
|
|
"dividends_returned_customer_name",
|
|
|
"dividends_returned_date",
|
|
|
"region",
|
|
|
"remarks",
|
|
|
"dividend_status",
|
|
|
"dividend_time",
|
|
|
"org",
|
|
|
"company",
|
|
|
"created_at",
|
|
|
"deleted_at",
|
|
|
"updated_at",
|
|
|
"operator",
|
|
|
"operate_time",
|
|
|
}
|
|
|
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
|
|
|
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
|
|
|
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
|
|
|
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "dividendsReturnedOrder_id")
|
|
|
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
if dividendsReturnedOrder.Identify() == nil {
|
|
|
dividendsReturnedOrderId, err := repository.nextIdentify()
|
|
|
if err != nil {
|
|
|
return dividendsReturnedOrder, err
|
|
|
} else {
|
|
|
dividendsReturnedOrder.DividendsReturnedOrderId = dividendsReturnedOrderId
|
|
|
}
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(
|
|
|
÷ndsReturnedOrder.DividendsReturnedOrderId,
|
|
|
÷ndsReturnedOrder.DividendsReturnedOrderNumber,
|
|
|
÷ndsReturnedOrder.DividendsReturnedOrderRefund,
|
|
|
÷ndsReturnedOrder.OriginalOrderNum,
|
|
|
÷ndsReturnedOrder.DividendsOrderNumber,
|
|
|
÷ndsReturnedOrder.DividendsReturnedCustomerName,
|
|
|
÷ndsReturnedOrder.DividendsReturnedDate,
|
|
|
÷ndsReturnedOrder.Region,
|
|
|
÷ndsReturnedOrder.Remarks,
|
|
|
÷ndsReturnedOrder.DividendStatus,
|
|
|
÷ndsReturnedOrder.DividendTime,
|
|
|
÷ndsReturnedOrder.Org,
|
|
|
÷ndsReturnedOrder.Company,
|
|
|
÷ndsReturnedOrder.CreatedAt,
|
|
|
÷ndsReturnedOrder.DeletedAt,
|
|
|
÷ndsReturnedOrder.UpdatedAt,
|
|
|
÷ndsReturnedOrder.Operator,
|
|
|
÷ndsReturnedOrder.OperateTime,
|
|
|
),
|
|
|
fmt.Sprintf("INSERT INTO dividends_returned_orders (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
|
|
|
dividendsReturnedOrder.DividendsReturnedOrderId,
|
|
|
dividendsReturnedOrder.DividendsReturnedOrderNumber,
|
|
|
dividendsReturnedOrder.DividendsReturnedOrderRefund,
|
|
|
dividendsReturnedOrder.OriginalOrderNum,
|
|
|
dividendsReturnedOrder.DividendsOrderNumber,
|
|
|
dividendsReturnedOrder.DividendsReturnedCustomerName,
|
|
|
dividendsReturnedOrder.DividendsReturnedDate,
|
|
|
dividendsReturnedOrder.Region,
|
|
|
dividendsReturnedOrder.Remarks,
|
|
|
dividendsReturnedOrder.DividendStatus,
|
|
|
dividendsReturnedOrder.DividendTime,
|
|
|
dividendsReturnedOrder.Org,
|
|
|
dividendsReturnedOrder.Company,
|
|
|
dividendsReturnedOrder.CreatedAt,
|
|
|
dividendsReturnedOrder.DeletedAt,
|
|
|
dividendsReturnedOrder.UpdatedAt,
|
|
|
dividendsReturnedOrder.Operator,
|
|
|
dividendsReturnedOrder.OperateTime,
|
|
|
); err != nil {
|
|
|
return dividendsReturnedOrder, err
|
|
|
}
|
|
|
} else {
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(
|
|
|
÷ndsReturnedOrder.DividendsReturnedOrderId,
|
|
|
÷ndsReturnedOrder.DividendsReturnedOrderNumber,
|
|
|
÷ndsReturnedOrder.DividendsReturnedOrderRefund,
|
|
|
÷ndsReturnedOrder.OriginalOrderNum,
|
|
|
÷ndsReturnedOrder.DividendsOrderNumber,
|
|
|
÷ndsReturnedOrder.DividendsReturnedCustomerName,
|
|
|
÷ndsReturnedOrder.DividendsReturnedDate,
|
|
|
÷ndsReturnedOrder.Region,
|
|
|
÷ndsReturnedOrder.Remarks,
|
|
|
÷ndsReturnedOrder.DividendStatus,
|
|
|
÷ndsReturnedOrder.DividendTime,
|
|
|
÷ndsReturnedOrder.Org,
|
|
|
÷ndsReturnedOrder.Company,
|
|
|
÷ndsReturnedOrder.CreatedAt,
|
|
|
÷ndsReturnedOrder.DeletedAt,
|
|
|
÷ndsReturnedOrder.UpdatedAt,
|
|
|
÷ndsReturnedOrder.Operator,
|
|
|
÷ndsReturnedOrder.OperateTime,
|
|
|
),
|
|
|
fmt.Sprintf("UPDATE dividends_returned_orders SET %s WHERE dividends_returned_order_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
|
|
|
dividendsReturnedOrder.DividendsReturnedOrderId,
|
|
|
dividendsReturnedOrder.DividendsReturnedOrderNumber,
|
|
|
dividendsReturnedOrder.DividendsReturnedOrderRefund,
|
|
|
dividendsReturnedOrder.OriginalOrderNum,
|
|
|
dividendsReturnedOrder.DividendsOrderNumber,
|
|
|
dividendsReturnedOrder.DividendsReturnedCustomerName,
|
|
|
dividendsReturnedOrder.DividendsReturnedDate,
|
|
|
dividendsReturnedOrder.Region,
|
|
|
dividendsReturnedOrder.Remarks,
|
|
|
dividendsReturnedOrder.DividendStatus,
|
|
|
dividendsReturnedOrder.DividendTime,
|
|
|
dividendsReturnedOrder.Org,
|
|
|
dividendsReturnedOrder.Company,
|
|
|
dividendsReturnedOrder.CreatedAt,
|
|
|
dividendsReturnedOrder.DeletedAt,
|
|
|
dividendsReturnedOrder.UpdatedAt,
|
|
|
dividendsReturnedOrder.Operator,
|
|
|
dividendsReturnedOrder.OperateTime,
|
|
|
dividendsReturnedOrder.Identify(),
|
|
|
); err != nil {
|
|
|
return dividendsReturnedOrder, err
|
|
|
}
|
|
|
}
|
|
|
return dividendsReturnedOrder, nil
|
|
|
}
|
|
|
func (repository *DividendsReturnedOrderRepository) Remove(dividendsReturnedOrder *domain.DividendsReturnedOrder) (*domain.DividendsReturnedOrder, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
dividendsReturnedOrderModel := new(models.DividendsReturnedOrder)
|
|
|
dividendsReturnedOrderModel.DividendsReturnedOrderId = dividendsReturnedOrder.Identify().(int64)
|
|
|
if _, err := tx.Model(dividendsReturnedOrderModel).WherePK().Delete(); err != nil {
|
|
|
return dividendsReturnedOrder, err
|
|
|
}
|
|
|
return dividendsReturnedOrder, nil
|
|
|
}
|
|
|
func (repository *DividendsReturnedOrderRepository) FindOne(queryOptions map[string]interface{}) (*domain.DividendsReturnedOrder, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
dividendsReturnedOrderModel := new(models.DividendsReturnedOrder)
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(dividendsReturnedOrderModel), queryOptions)
|
|
|
query.SetWhereByQueryOption("dividends_returned_order.dividends_returned_order_id = ?", "dividendsReturnedOrderId")
|
|
|
if err := query.First(); err != nil {
|
|
|
if err.Error() == "pg: no rows in result set" {
|
|
|
return nil, fmt.Errorf("没有此资源")
|
|
|
} else {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
if dividendsReturnedOrderModel.DividendsReturnedOrderId == 0 {
|
|
|
return nil, nil
|
|
|
} else {
|
|
|
return transform.TransformToDividendsReturnedOrderDomainModelFromPgModels(dividendsReturnedOrderModel)
|
|
|
}
|
|
|
}
|
|
|
func (repository *DividendsReturnedOrderRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.DividendsReturnedOrder, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
var dividendsReturnedOrderModels []*models.DividendsReturnedOrder
|
|
|
dividendsReturnedOrders := make([]*domain.DividendsReturnedOrder, 0)
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(÷ndsReturnedOrderModels), queryOptions)
|
|
|
query.SetOffsetAndLimit(20)
|
|
|
query.SetOrderDirect("dividends_returned_order_id", "DESC")
|
|
|
if count, err := query.SelectAndCount(); err != nil {
|
|
|
return 0, dividendsReturnedOrders, err
|
|
|
} else {
|
|
|
for _, dividendsReturnedOrderModel := range dividendsReturnedOrderModels {
|
|
|
if dividendsReturnedOrder, err := transform.TransformToDividendsReturnedOrderDomainModelFromPgModels(dividendsReturnedOrderModel); err != nil {
|
|
|
return 0, dividendsReturnedOrders, err
|
|
|
} else {
|
|
|
dividendsReturnedOrders = append(dividendsReturnedOrders, dividendsReturnedOrder)
|
|
|
}
|
|
|
}
|
|
|
return int64(count), dividendsReturnedOrders, nil
|
|
|
}
|
|
|
}
|
|
|
func NewDividendsReturnedOrderRepository(transactionContext *pgTransaction.TransactionContext) (*DividendsReturnedOrderRepository, error) {
|
|
|
if transactionContext == nil {
|
|
|
return nil, fmt.Errorf("transactionContext参数不能为nil")
|
|
|
} else {
|
|
|
return &DividendsReturnedOrderRepository{
|
|
|
transactionContext: transactionContext,
|
|
|
}, nil
|
|
|
}
|
|
|
} |
...
|
...
|
|