作者 陈志颖

feat:添加订单产品领域模型

@@ -101,3 +101,11 @@ func CreateDividendsReturnedOrderRepository(options map[string]interface{}) (dom @@ -101,3 +101,11 @@ func CreateDividendsReturnedOrderRepository(options map[string]interface{}) (dom
101 } 101 }
102 return repository.NewDividendsReturnedOrderRepository(transactionContext) 102 return repository.NewDividendsReturnedOrderRepository(transactionContext)
103 } 103 }
  104 +
  105 +func CreateOrderGoodRepository(options map[string]interface{}) (domain.OrderGoodRepository, error) {
  106 + var transactionContext *pg.TransactionContext
  107 + if value, ok := options["transactionContext"]; ok {
  108 + transactionContext = value.(*pg.TransactionContext)
  109 + }
  110 + return repository.NewOrderGoodRepository(transactionContext)
  111 +}
  1 +package domain
  2 +
  3 +import "time"
  4 +
  5 +// OrderGood 订单产品领域实体(包括分红订单、分红退货单)
  6 +type OrderGood struct {
  7 + // 订单产品
  8 + OrderGoodId int64 `json:"orderGoodId,string"`
  9 + // 订单产品金额
  10 + OrderGoodAmount float64 `json:"orderGoodAmount"`
  11 + // 订单产品名称
  12 + OrderGoodName string `json:"orderGoodName"`
  13 + // 订单产品单价
  14 + OrderGoodPrice float64 `json:"orderGoodPrice"`
  15 + // 订单产品数量
  16 + OrderGoodQuantity int64 `json:"orderGoodQuantity,string"`
  17 + // 关联分红订单号
  18 + DividendsOrderNumber int64 `json:"dividendsOrderNumber,string"`
  19 + // 关联的共创合约编号
  20 + CooperationContractNumber string `json:"cooperationContractNumber"`
  21 + // 订单产品费用
  22 + OrderGoodExpense float64 `json:"orderGoodExpense"`
  23 + // 创建时间
  24 + CreatedAt time.Time `json:"createdAt"`
  25 + // 删除时间
  26 + DeletedAt time.Time `json:"deletedAt"`
  27 + // 更新时间
  28 + UpdatedAt time.Time `json:"updatedAt"`
  29 +}
  30 +
  31 +type OrderGoodRepository interface {
  32 + Save(orderGood *OrderGood) (*OrderGood, error)
  33 + Remove(orderGood *OrderGood) (*OrderGood, error)
  34 + FindOne(queryOptions map[string]interface{}) (*OrderGood, error)
  35 + Find(queryOptions map[string]interface{}) (int64, []*OrderGood, error)
  36 +}
  37 +
  38 +func (orderGood *OrderGood) Identify() interface{} {
  39 + if orderGood.OrderGoodId == 0 {
  40 + return nil
  41 + }
  42 + return orderGood.OrderGoodId
  43 +}
  44 +
  45 +func (orderGood *OrderGood) Update(data map[string]interface{}) error {
  46 + if orderGoodAmount, ok := data["orderGoodAmount"]; ok {
  47 + orderGood.OrderGoodAmount = orderGoodAmount.(float64)
  48 + }
  49 + if orderGoodName, ok := data["orderGoodName"]; ok {
  50 + orderGood.OrderGoodName = orderGoodName.(string)
  51 + }
  52 + if orderGoodPrice, ok := data["orderGoodPrice"]; ok {
  53 + orderGood.OrderGoodPrice = orderGoodPrice.(float64)
  54 + }
  55 + if orderGoodQuantity, ok := data["orderGoodQuantity"]; ok {
  56 + orderGood.OrderGoodQuantity = orderGoodQuantity.(int64)
  57 + }
  58 + if dividendsOrderNumber, ok := data["dividendsOrderNumber"]; ok {
  59 + orderGood.DividendsOrderNumber = dividendsOrderNumber.(int64)
  60 + }
  61 + if cooperationContractNumber, ok := data["cooperationContractNumber"]; ok {
  62 + orderGood.CooperationContractNumber = cooperationContractNumber.(string)
  63 + }
  64 + if orderGoodExpense, ok := data["orderGoodExpense"]; ok {
  65 + orderGood.OrderGoodExpense = orderGoodExpense.(float64)
  66 + }
  67 + if updatedAt, ok := data["updatedAt"]; ok {
  68 + orderGood.UpdatedAt = updatedAt.(time.Time)
  69 + }
  70 + return nil
  71 +}
  1 +package models
  2 +
  3 +import "time"
  4 +
  5 +type OrderGood struct {
  6 + tableName string `comment:"订单产品领域实体(包括分红订单、分红退货单)" pg:"order_goods,alias:order_good"`
  7 + // 订单产品
  8 + OrderGoodId int64 `comment:"订单产品" pg:"pk:order_good_id"`
  9 + // 订单产品金额
  10 + OrderGoodAmount float64 `comment:"订单产品金额"`
  11 + // 订单产品名称
  12 + OrderGoodName string `comment:"订单产品名称"`
  13 + // 订单产品单价
  14 + OrderGoodPrice float64 `comment:"订单产品单价"`
  15 + // 订单产品数量
  16 + OrderGoodQuantity int64 `comment:"订单产品数量"`
  17 + // 关联分红订单号
  18 + DividendsOrderNumber int64 `comment:"关联分红订单号"`
  19 + // 关联的共创合约编号
  20 + CooperationContractNumber string `comment:"关联的共创合约编号"`
  21 + // 订单产品费用
  22 + OrderGoodExpense float64 `comment:"订单产品费用"`
  23 + // 创建时间
  24 + CreatedAt time.Time `comment:"创建时间"`
  25 + // 删除时间
  26 + DeletedAt time.Time `comment:"删除时间"`
  27 + // 更新时间
  28 + UpdatedAt time.Time `comment:"更新时间"`
  29 +}
  1 +package transform
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
  6 +)
  7 +
  8 +func TransformToOrderGoodDomainModelFromPgModels(orderGoodModel *models.OrderGood) (*domain.OrderGood, error) {
  9 + return &domain.OrderGood{
  10 + OrderGoodId: orderGoodModel.OrderGoodId,
  11 + OrderGoodAmount: orderGoodModel.OrderGoodAmount,
  12 + OrderGoodName: orderGoodModel.OrderGoodName,
  13 + OrderGoodPrice: orderGoodModel.OrderGoodPrice,
  14 + OrderGoodQuantity: orderGoodModel.OrderGoodQuantity,
  15 + DividendsOrderNumber: orderGoodModel.DividendsOrderNumber,
  16 + CooperationContractNumber: orderGoodModel.CooperationContractNumber,
  17 + OrderGoodExpense: orderGoodModel.OrderGoodExpense,
  18 + CreatedAt: orderGoodModel.CreatedAt,
  19 + DeletedAt: orderGoodModel.DeletedAt,
  20 + UpdatedAt: orderGoodModel.UpdatedAt,
  21 + }, nil
  22 +}
  1 +package repository
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/go-pg/pg/v10"
  6 +
  7 + "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
  8 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  9 + "github.com/linmadan/egglib-go/utils/snowflake"
  10 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
  11 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
  12 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/transform"
  13 +)
  14 +
  15 +type OrderGoodRepository struct {
  16 + transactionContext *pgTransaction.TransactionContext
  17 +}
  18 +
  19 +func (repository *OrderGoodRepository) nextIdentify() (int64, error) {
  20 + IdWorker, err := snowflake.NewIdWorker(1)
  21 + if err != nil {
  22 + return 0, err
  23 + }
  24 + id, err := IdWorker.NextId()
  25 + return id, err
  26 +}
  27 +func (repository *OrderGoodRepository) Save(orderGood *domain.OrderGood) (*domain.OrderGood, error) {
  28 + sqlBuildFields := []string{
  29 + "order_good_id",
  30 + "order_good_amount",
  31 + "order_good_name",
  32 + "order_good_price",
  33 + "order_good_quantity",
  34 + "dividends_order_number",
  35 + "cooperation_contract_number",
  36 + "order_good_expense",
  37 + "created_at",
  38 + "deleted_at",
  39 + "updated_at",
  40 + }
  41 + insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
  42 + insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
  43 + returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
  44 + updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "orderGood_id")
  45 + updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
  46 + tx := repository.transactionContext.PgTx
  47 + if orderGood.Identify() == nil {
  48 + orderGoodId, err := repository.nextIdentify()
  49 + if err != nil {
  50 + return orderGood, err
  51 + } else {
  52 + orderGood.OrderGoodId = orderGoodId
  53 + }
  54 + if _, err := tx.QueryOne(
  55 + pg.Scan(
  56 + &orderGood.OrderGoodId,
  57 + &orderGood.OrderGoodAmount,
  58 + &orderGood.OrderGoodName,
  59 + &orderGood.OrderGoodPrice,
  60 + &orderGood.OrderGoodQuantity,
  61 + &orderGood.DividendsOrderNumber,
  62 + &orderGood.CooperationContractNumber,
  63 + &orderGood.OrderGoodExpense,
  64 + &orderGood.CreatedAt,
  65 + &orderGood.DeletedAt,
  66 + &orderGood.UpdatedAt,
  67 + ),
  68 + fmt.Sprintf("INSERT INTO order_goods (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
  69 + orderGood.OrderGoodId,
  70 + orderGood.OrderGoodAmount,
  71 + orderGood.OrderGoodName,
  72 + orderGood.OrderGoodPrice,
  73 + orderGood.OrderGoodQuantity,
  74 + orderGood.DividendsOrderNumber,
  75 + orderGood.CooperationContractNumber,
  76 + orderGood.OrderGoodExpense,
  77 + orderGood.CreatedAt,
  78 + orderGood.DeletedAt,
  79 + orderGood.UpdatedAt,
  80 + ); err != nil {
  81 + return orderGood, err
  82 + }
  83 + } else {
  84 + if _, err := tx.QueryOne(
  85 + pg.Scan(
  86 + &orderGood.OrderGoodId,
  87 + &orderGood.OrderGoodAmount,
  88 + &orderGood.OrderGoodName,
  89 + &orderGood.OrderGoodPrice,
  90 + &orderGood.OrderGoodQuantity,
  91 + &orderGood.DividendsOrderNumber,
  92 + &orderGood.CooperationContractNumber,
  93 + &orderGood.OrderGoodExpense,
  94 + &orderGood.CreatedAt,
  95 + &orderGood.DeletedAt,
  96 + &orderGood.UpdatedAt,
  97 + ),
  98 + fmt.Sprintf("UPDATE order_goods SET %s WHERE order_good_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
  99 + orderGood.OrderGoodId,
  100 + orderGood.OrderGoodAmount,
  101 + orderGood.OrderGoodName,
  102 + orderGood.OrderGoodPrice,
  103 + orderGood.OrderGoodQuantity,
  104 + orderGood.DividendsOrderNumber,
  105 + orderGood.CooperationContractNumber,
  106 + orderGood.OrderGoodExpense,
  107 + orderGood.CreatedAt,
  108 + orderGood.DeletedAt,
  109 + orderGood.UpdatedAt,
  110 + orderGood.Identify(),
  111 + ); err != nil {
  112 + return orderGood, err
  113 + }
  114 + }
  115 + return orderGood, nil
  116 +}
  117 +func (repository *OrderGoodRepository) Remove(orderGood *domain.OrderGood) (*domain.OrderGood, error) {
  118 + tx := repository.transactionContext.PgTx
  119 + orderGoodModel := new(models.OrderGood)
  120 + orderGoodModel.OrderGoodId = orderGood.Identify().(int64)
  121 + if _, err := tx.Model(orderGoodModel).WherePK().Delete(); err != nil {
  122 + return orderGood, err
  123 + }
  124 + return orderGood, nil
  125 +}
  126 +func (repository *OrderGoodRepository) FindOne(queryOptions map[string]interface{}) (*domain.OrderGood, error) {
  127 + tx := repository.transactionContext.PgTx
  128 + orderGoodModel := new(models.OrderGood)
  129 + query := sqlbuilder.BuildQuery(tx.Model(orderGoodModel), queryOptions)
  130 + query.SetWhereByQueryOption("order_good.order_good_id = ?", "orderGoodId")
  131 + if err := query.First(); err != nil {
  132 + if err.Error() == "pg: no rows in result set" {
  133 + return nil, fmt.Errorf("没有此资源")
  134 + } else {
  135 + return nil, err
  136 + }
  137 + }
  138 + if orderGoodModel.OrderGoodId == 0 {
  139 + return nil, nil
  140 + } else {
  141 + return transform.TransformToOrderGoodDomainModelFromPgModels(orderGoodModel)
  142 + }
  143 +}
  144 +func (repository *OrderGoodRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.OrderGood, error) {
  145 + tx := repository.transactionContext.PgTx
  146 + var orderGoodModels []*models.OrderGood
  147 + orderGoods := make([]*domain.OrderGood, 0)
  148 + query := sqlbuilder.BuildQuery(tx.Model(&orderGoodModels), queryOptions)
  149 + query.SetOffsetAndLimit(20)
  150 + query.SetOrderDirect("order_good_id", "DESC")
  151 + if count, err := query.SelectAndCount(); err != nil {
  152 + return 0, orderGoods, err
  153 + } else {
  154 + for _, orderGoodModel := range orderGoodModels {
  155 + if orderGood, err := transform.TransformToOrderGoodDomainModelFromPgModels(orderGoodModel); err != nil {
  156 + return 0, orderGoods, err
  157 + } else {
  158 + orderGoods = append(orderGoods, orderGood)
  159 + }
  160 + }
  161 + return int64(count), orderGoods, nil
  162 + }
  163 +}
  164 +func NewOrderGoodRepository(transactionContext *pgTransaction.TransactionContext) (*OrderGoodRepository, error) {
  165 + if transactionContext == nil {
  166 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  167 + } else {
  168 + return &OrderGoodRepository{
  169 + transactionContext: transactionContext,
  170 + }, nil
  171 + }
  172 +}