作者 陈志颖

feat:生成订单基础代码

1 package factory 1 package factory
  2 +
  3 +import (
  4 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  5 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain/service"
  6 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/domainService"
  7 +)
  8 +
  9 +func CreateShippingService(options map[string]interface{}) (service.ShippingService, error) {
  10 + var transactionContext *pgTransaction.TransactionContext
  11 + if value, ok := options["transactionContext"]; ok {
  12 + transactionContext = value.(*pgTransaction.TransactionContext)
  13 + }
  14 + return domainService.NewShippingService(transactionContext)
  15 +}
@@ -29,3 +29,19 @@ func CreateUserAuthRepository(options map[string]interface{}) (domain.UserAuthRe @@ -29,3 +29,19 @@ func CreateUserAuthRepository(options map[string]interface{}) (domain.UserAuthRe
29 } 29 }
30 return repository.NewUserAuthRepository(transactionContext) 30 return repository.NewUserAuthRepository(transactionContext)
31 } 31 }
  32 +
  33 +func CreateOrderRepository(options map[string]interface{}) (domain.OrderRepository, error) {
  34 + var transactionContext *pg.TransactionContext
  35 + if value, ok := options["transactionContext"]; ok {
  36 + transactionContext = value.(*pg.TransactionContext)
  37 + }
  38 + return repository.NewOrderRepository(transactionContext)
  39 +}
  40 +
  41 +func CreateGoodsRepository(options map[string]interface{}) (domain.GoodsRepository, error) {
  42 + var transactionContext *pg.TransactionContext
  43 + if value, ok := options["transactionContext"]; ok {
  44 + transactionContext = value.(*pg.TransactionContext)
  45 + }
  46 + return repository.NewGoodsRepository(transactionContext)
  47 +}
@@ -4,9 +4,9 @@ import "os" @@ -4,9 +4,9 @@ import "os"
4 4
5 var POSTGRESQL_DB_NAME = "partner01" 5 var POSTGRESQL_DB_NAME = "partner01"
6 var POSTGRESQL_USER = "postgres" 6 var POSTGRESQL_USER = "postgres"
7 -var POSTGRESQL_PASSWORD = "123456"  
8 -var POSTGRESQL_HOST = "127.0.0.1"  
9 -var POSTGRESQL_PORT = "5432" 7 +var POSTGRESQL_PASSWORD = "eagle1010"
  8 +var POSTGRESQL_HOST = "114.55.200.59"
  9 +var POSTGRESQL_PORT = "31543"
10 var DISABLE_CREATE_TABLE = false 10 var DISABLE_CREATE_TABLE = false
11 var DISABLE_SQL_GENERATE_PRINT = false 11 var DISABLE_SQL_GENERATE_PRINT = false
12 12
  1 +package domain
  2 +
  3 +import "time"
  4 +
  5 +// 订单商品实体
  6 +type Goods struct {
  7 + // 商品id
  8 + GoodsId int64 `json:"goodsId"`
  9 + // 预定的货品数量
  10 + GoodsNum int `json:"goodsNum"`
  11 + // 商品分红支付信息
  12 + GoodsDividend *Dividend `json:"goodsDividend"`
  13 + // 商品分红支付状态
  14 + GoodsDividendStatus int `json:"goodsDividendStatus"`
  15 + // 商品名称
  16 + GoodsName string `json:"goodsName"`
  17 + // 商品单价
  18 + GoodsTotalPrice float64 `json:"goodsTotalPrice"`
  19 + // 备注
  20 + Remarks string `json:"remarks"`
  21 + // 订单编号
  22 + OrderNo string `json:"orderNo"`
  23 + GoodsDetail string `json:"goodsDetail"`
  24 + // 创建时间
  25 + CreateAt time.Time `json:"createAt"`
  26 + // 更新时间
  27 + UpdateAt time.Time `json:"updateAt"`
  28 + // 删除时间
  29 + DeleteAt time.Time `json:"deleteAt"`
  30 +}
  31 +
  32 +type GoodsRepository interface {
  33 + Save(goods *Goods) (*Goods, error)
  34 + Remove(goods *Goods) (*Goods, error)
  35 + FindOne(queryOptions map[string]interface{}) (*Goods, error)
  36 + Find(queryOptions map[string]interface{}) (int64, []*Goods, error)
  37 +}
  38 +
  39 +func (goods *Goods) Identify() interface{} {
  40 + if goods.GoodsId == 0 {
  41 + return nil
  42 + }
  43 + return goods.GoodsId
  44 +}
  45 +
  46 +func (goods *Goods) Update(data map[string]interface{}) error {
  47 + if goodsId, ok := data["goodsId"]; ok {
  48 + goods.GoodsId = goodsId.(int64)
  49 + }
  50 + if goodsNum, ok := data["goodsNum"]; ok {
  51 + goods.GoodsNum = goodsNum.(int)
  52 + }
  53 + if planBonus, ok := data["planBonus"]; ok {
  54 + goods.GoodsDividend.PlanBonus = planBonus.(float64)
  55 + }
  56 + if useBonus, ok := data["useBonus"]; ok {
  57 + goods.GoodsDividend.UseBonus = useBonus.(float64)
  58 + }
  59 + if bonusHas, ok := data["bonusHas"]; ok {
  60 + goods.GoodsDividend.BonusHas = bonusHas.(float64)
  61 + }
  62 + if bonusNot, ok := data["bonusNot"]; ok {
  63 + goods.GoodsDividend.BonusNot = bonusNot.(float64)
  64 + }
  65 + if bonusExpense, ok := data["bonusExpense"]; ok {
  66 + goods.GoodsDividend.BonusExpense = bonusExpense.(float64)
  67 + }
  68 + if bonusStatus, ok := data["bonusStatus"]; ok {
  69 + goods.GoodsDividend.BonusStatus = bonusStatus.(int)
  70 + }
  71 + if partnerBonusPercent, ok := data["partnerBonusPercent"]; ok {
  72 + goods.GoodsDividend.PartnerBonusPercent = partnerBonusPercent.(float64)
  73 + }
  74 + if salesmanBonusPercent, ok := data["salesmanBonusPercent"]; ok {
  75 + goods.GoodsDividend.SalesmanBonusPercent = salesmanBonusPercent.(float64)
  76 + }
  77 + if salesmanBonus, ok := data["salesmanBonus"]; ok {
  78 + goods.GoodsDividend.SalesmanBonus = salesmanBonus.(float64)
  79 + }
  80 + if goodsDividendStatus, ok := data["goodsDividendStatus"]; ok {
  81 + goods.GoodsDividendStatus = goodsDividendStatus.(int)
  82 + }
  83 + if goodsName, ok := data["goodsName"]; ok {
  84 + goods.GoodsName = goodsName.(string)
  85 + }
  86 + if goodsTotalPrice, ok := data["goodsTotalPrice"]; ok {
  87 + goods.GoodsTotalPrice = goodsTotalPrice.(float64)
  88 + }
  89 + if remarks, ok := data["remarks"]; ok {
  90 + goods.Remarks = remarks.(string)
  91 + }
  92 + if orderNo, ok := data["orderNo"]; ok {
  93 + goods.OrderNo = orderNo.(string)
  94 + }
  95 + if goodsDetail, ok := data["goodsDetail"]; ok {
  96 + goods.GoodsDetail = goodsDetail.(string)
  97 + }
  98 + if createAt, ok := data["createAt"]; ok {
  99 + goods.CreateAt = createAt.(time.Time)
  100 + }
  101 + if updateAt, ok := data["updateAt"]; ok {
  102 + goods.UpdateAt = updateAt.(time.Time)
  103 + }
  104 + if deleteAt, ok := data["deleteAt"]; ok {
  105 + goods.DeleteAt = deleteAt.(time.Time)
  106 + }
  107 + return nil
  108 +}
  1 +package domain
  2 +
  3 +import "time"
  4 +
  5 +// 订单实体
  6 +type Order struct {
  7 + // 订单id
  8 + OrderId int64 `json:"orderId"`
  9 + // 买家信息
  10 + Buyer *BuyerInfo `json:"buyer"`
  11 + // 公司Id
  12 + CompanyId int64 `json:"companyId"`
  13 + // 合伙人id
  14 + PartnerId int64 `json:"partnerId"`
  15 + // 发货单号
  16 + DeliveryCode string `json:"deliveryCode"`
  17 + // 是否关闭订单标志
  18 + IsDisable int `json:"isDisable"`
  19 + // 订单编号
  20 + OrderNo string `json:"orderNo"`
  21 + // 订单详情
  22 + OrderDetail *OrderDetail `json:"orderDetail"`
  23 + // 订单分红信息
  24 + OrderDividend *Dividend `json:"orderDividend"`
  25 + // 订单分红支付状态
  26 + OrderDividendStatus int `json:"orderDividendStatus"`
  27 + // 订单货品
  28 + OrderGoods []*Goods `json:"orderGoods"`
  29 + // 订单数据来源
  30 + OrderSource *OrderSource `json:"orderSource"`
  31 + // 订单类型(1.实际订单;2.意向订单)
  32 + OrderType int `json:"orderType"`
  33 + // 订单状态(1.发货;2.收货;3.退货;4.取消;5.完成)
  34 + OrderStatus int `json:"orderStatus"`
  35 + // 订单总价
  36 + TotalPrice float64 `json:"totalPrice"`
  37 + // 订单区域信息
  38 + RegionInfo *RegionInfo `json:"regionInfo"`
  39 + // 备注
  40 + Remarks string `json:"remarks"`
  41 + // 创建时间
  42 + CreateAt time.Time `json:"createAt"`
  43 + // 更新时间
  44 + UpdateAt time.Time `json:"updateAt"`
  45 + // 软删
  46 + DeleteAt time.Time `json:"deleteAt"`
  47 +}
  48 +
  49 +type OrderRepository interface {
  50 + Save(order *Order) (*Order, error)
  51 + Remove(order *Order) (*Order, error)
  52 + FindOne(queryOptions map[string]interface{}) (*Order, error)
  53 + Find(queryOptions map[string]interface{}) (int64, []*Order, error)
  54 +}
  55 +
  56 +func (order *Order) Identify() interface{} {
  57 + if order.OrderId == 0 {
  58 + return nil
  59 + }
  60 + return order.OrderId
  61 +}
  62 +
  63 +func (order *Order) Update(data map[string]interface{}) error {
  64 + if orderId, ok := data["orderId"]; ok {
  65 + order.OrderId = orderId.(int64)
  66 + }
  67 + if buyerName, ok := data["buyerName"]; ok {
  68 + order.Buyer.BuyerName = buyerName.(string)
  69 + }
  70 + if contactInfo, ok := data["contactInfo"]; ok {
  71 + order.Buyer.ContactInfo = contactInfo.(string)
  72 + }
  73 + if shippingAddress, ok := data["shippingAddress"]; ok {
  74 + order.Buyer.ShippingAddress = shippingAddress.(string)
  75 + }
  76 + if companyId, ok := data["companyId"]; ok {
  77 + order.CompanyId = companyId.(int64)
  78 + }
  79 + if partnerId, ok := data["partnerId"]; ok {
  80 + order.PartnerId = partnerId.(int64)
  81 + }
  82 + if deliveryCode, ok := data["deliveryCode"]; ok {
  83 + order.DeliveryCode = deliveryCode.(string)
  84 + }
  85 + if isDisable, ok := data["isDisable"]; ok {
  86 + order.IsDisable = isDisable.(int)
  87 + }
  88 + if orderNo, ok := data["orderNo"]; ok {
  89 + order.OrderNo = orderNo.(string)
  90 + }
  91 + if orderDetailId, ok := data["orderDetailId"]; ok {
  92 + order.OrderDetail.OrderDetailId = orderDetailId.(int64)
  93 + }
  94 + if orderNo, ok := data["orderNo"]; ok {
  95 + order.OrderDetail.OrderNo = orderNo.(string)
  96 + }
  97 + if orderNum, ok := data["orderNum"]; ok {
  98 + order.OrderDetail.OrderNum = orderNum.(int)
  99 + }
  100 + if totalPrice, ok := data["totalPrice"]; ok {
  101 + order.OrderDetail.TotalPrice = totalPrice.(float64)
  102 + }
  103 + if adjustedNum, ok := data["adjustedNum"]; ok {
  104 + order.OrderDetail.AdjustedNum = adjustedNum.(int)
  105 + }
  106 + if adjustedTotalPrice, ok := data["adjustedTotalPrice"]; ok {
  107 + order.OrderDetail.AdjustedTotalPrice = adjustedTotalPrice.(float64)
  108 + }
  109 + if planBonus, ok := data["planBonus"]; ok {
  110 + order.OrderDividend.PlanBonus = planBonus.(float64)
  111 + }
  112 + if useBonus, ok := data["useBonus"]; ok {
  113 + order.OrderDividend.UseBonus = useBonus.(float64)
  114 + }
  115 + if bonusHas, ok := data["bonusHas"]; ok {
  116 + order.OrderDividend.BonusHas = bonusHas.(float64)
  117 + }
  118 + if bonusNot, ok := data["bonusNot"]; ok {
  119 + order.OrderDividend.BonusNot = bonusNot.(float64)
  120 + }
  121 + if bonusExpense, ok := data["bonusExpense"]; ok {
  122 + order.OrderDividend.BonusExpense = bonusExpense.(float64)
  123 + }
  124 + if bonusStatus, ok := data["bonusStatus"]; ok {
  125 + order.OrderDividend.BonusStatus = bonusStatus.(int)
  126 + }
  127 + if partnerBonusPercent, ok := data["partnerBonusPercent"]; ok {
  128 + order.OrderDividend.PartnerBonusPercent = partnerBonusPercent.(float64)
  129 + }
  130 + if salesmanBonusPercent, ok := data["salesmanBonusPercent"]; ok {
  131 + order.OrderDividend.SalesmanBonusPercent = salesmanBonusPercent.(float64)
  132 + }
  133 + if salesmanBonus, ok := data["salesmanBonus"]; ok {
  134 + order.OrderDividend.SalesmanBonus = salesmanBonus.(float64)
  135 + }
  136 + if orderDividendStatus, ok := data["orderDividendStatus"]; ok {
  137 + order.OrderDividendStatus = orderDividendStatus.(int)
  138 + }
  139 + if orderGoods, ok := data["orderGoods"]; ok {
  140 + order.OrderGoods = orderGoods.([]*Goods)
  141 + }
  142 + if dataSourceId, ok := data["dataSourceId"]; ok {
  143 + order.OrderSource.DataSourceId = dataSourceId.(int64)
  144 + }
  145 + if platform, ok := data["platform"]; ok {
  146 + order.OrderSource.Platform = platform.(string)
  147 + }
  148 + if createAt, ok := data["createAt"]; ok {
  149 + order.OrderSource.CreateAt = createAt.(time.Time)
  150 + }
  151 + if orderType, ok := data["orderType"]; ok {
  152 + order.OrderType = orderType.(int)
  153 + }
  154 + if orderStatus, ok := data["orderStatus"]; ok {
  155 + order.OrderStatus = orderStatus.(int)
  156 + }
  157 + if totalPrice, ok := data["totalPrice"]; ok {
  158 + order.TotalPrice = totalPrice.(float64)
  159 + }
  160 + if regionName, ok := data["regionName"]; ok {
  161 + order.RegionInfo.RegionName = regionName.(string)
  162 + }
  163 + if remarks, ok := data["remarks"]; ok {
  164 + order.Remarks = remarks.(string)
  165 + }
  166 + if createAt, ok := data["createAt"]; ok {
  167 + order.CreateAt = createAt.(time.Time)
  168 + }
  169 + if updateAt, ok := data["updateAt"]; ok {
  170 + order.UpdateAt = updateAt.(time.Time)
  171 + }
  172 + if deleteAt, ok := data["deleteAt"]; ok {
  173 + order.DeleteAt = deleteAt.(time.Time)
  174 + }
  175 + return nil
  176 +}
  1 +package domain
  2 +
  3 +import "time"
  4 +
  5 +// 订单数据来源值对象
  6 +type OrderSource struct {
  7 + // 订单数据来源id
  8 + DataSourceId int64 `json:"dataSourceId"`
  9 + // 订单数据来源平台
  10 + Platform string `json:"platform"`
  11 + // 创建时间
  12 + CreateAt time.Time `json:"createAt"`
  13 +}
  1 +package service
  2 +
  3 +type ShippingService interface {
  4 +}
  1 +package domainService
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  7 +)
  8 +
  9 +type ShippingService struct {
  10 + transactionContext *pgTransaction.TransactionContext
  11 +}
  12 +
  13 +func NewShippingService(transactionContext *pgTransaction.TransactionContext) (*ShippingService, error) {
  14 + if transactionContext == nil {
  15 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  16 + } else {
  17 + return &ShippingService{
  18 + transactionContext: transactionContext,
  19 + }, nil
  20 + }
  21 +}
  1 +package models
  2 +
  3 +type Goods struct {
  4 + tableName string `pg:"goodss,alias:goods"`
  5 + // 商品id
  6 + GoodsId int64
  7 + // 预定的货品数量
  8 + GoodsNum int
  9 + // 商品分红支付信息
  10 + GoodsDividend *Dividend
  11 + // 商品分红支付状态
  12 + GoodsDividendStatus int
  13 + // 商品名称
  14 + GoodsName string
  15 + // 商品单价
  16 + GoodsTotalPrice float64
  17 + // 备注
  18 + Remarks string
  19 + // 订单编号
  20 + OrderNo string
  21 + GoodsDetail string
  22 + // 创建时间
  23 + CreateAt time.Time
  24 + // 更新时间
  25 + UpdateAt time.Time
  26 + // 删除时间
  27 + DeleteAt time.Time
  28 +}
  1 +package models
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
  5 + "time"
  6 +)
  7 +
  8 +type Order struct {
  9 + tableName string `pg:"orders,alias:order"`
  10 + // 订单id
  11 + OrderId int64
  12 + // 买家信息
  13 + Buyer *domain.BuyerInfo
  14 + // 公司Id
  15 + CompanyId int64
  16 + // 合伙人id
  17 + PartnerId int64
  18 + // 发货单号
  19 + DeliveryCode string
  20 + // 是否关闭订单标志
  21 + IsDisable int
  22 + // 订单编号
  23 + OrderNo string
  24 + // 订单详情
  25 + OrderDetail *domain.OrderDetail
  26 + // 订单分红信息
  27 + OrderDividend *domain.Dividend
  28 + // 订单分红支付状态
  29 + OrderDividendStatus int
  30 + // 订单货品
  31 + OrderGoods []*Goods `pg:",array"`
  32 + // 订单数据来源
  33 + OrderSource *domain.OrderSource
  34 + // 订单类型(1.实际订单;2.意向订单)
  35 + OrderType int
  36 + // 订单状态(1.发货;2.收货;3.退货;4.取消;5.完成)
  37 + OrderStatus int
  38 + // 订单总价
  39 + TotalPrice float64
  40 + // 订单区域信息
  41 + RegionInfo *domain.RegionInfo
  42 + // 备注
  43 + Remarks string
  44 + // 创建时间
  45 + CreateAt time.Time
  46 + // 更新时间
  47 + UpdateAt time.Time
  48 + // 软删
  49 + DeleteAt time.Time
  50 +}
  1 +package transform
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
  5 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg/models"
  6 +)
  7 +
  8 +func TransformToGoodsDomainModelFromPgModels(goodsModel *models.Goods) (*domain.Goods, error) {
  9 + return &domain.Goods{
  10 + GoodsId: goodsModel.GoodsId,
  11 + GoodsNum: goodsModel.GoodsNum,
  12 + GoodsDividend: goodsModel.GoodsDividend,
  13 + GoodsDividendStatus: goodsModel.GoodsDividendStatus,
  14 + GoodsName: goodsModel.GoodsName,
  15 + GoodsTotalPrice: goodsModel.GoodsTotalPrice,
  16 + Remarks: goodsModel.Remarks,
  17 + OrderNo: goodsModel.OrderNo,
  18 + GoodsDetail: goodsModel.GoodsDetail,
  19 + CreateAt: goodsModel.CreateAt,
  20 + UpdateAt: goodsModel.UpdateAt,
  21 + DeleteAt: goodsModel.DeleteAt,
  22 + }, nil
  23 +}
  1 +package transform
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
  5 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg/models"
  6 +)
  7 +
  8 +func TransformToOrderDomainModelFromPgModels(orderModel *models.Order) (*domain.Order, error) {
  9 + return &domain.Order{
  10 + OrderId: orderModel.OrderId,
  11 + Buyer: orderModel.Buyer,
  12 + CompanyId: orderModel.CompanyId,
  13 + PartnerId: orderModel.PartnerId,
  14 + DeliveryCode: orderModel.DeliveryCode,
  15 + IsDisable: orderModel.IsDisable,
  16 + OrderNo: orderModel.OrderNo,
  17 + OrderDetail: orderModel.OrderDetail,
  18 + OrderDividend: orderModel.OrderDividend,
  19 + OrderDividendStatus: orderModel.OrderDividendStatus,
  20 + OrderGoods: orderModel.OrderGoods,
  21 + OrderSource: orderModel.OrderSource,
  22 + OrderType: orderModel.OrderType,
  23 + OrderStatus: orderModel.OrderStatus,
  24 + TotalPrice: orderModel.TotalPrice,
  25 + RegionInfo: orderModel.RegionInfo,
  26 + Remarks: orderModel.Remarks,
  27 + CreateAt: orderModel.CreateAt,
  28 + UpdateAt: orderModel.UpdateAt,
  29 + DeleteAt: orderModel.DeleteAt,
  30 + }, nil
  31 +}
  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/mmm-go-pp/partner01/pkg/domain"
  11 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg/models"
  12 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg/transform"
  13 +)
  14 +
  15 +type GoodsRepository struct {
  16 + transactionContext *pgTransaction.TransactionContext
  17 +}
  18 +
  19 +func (repository *GoodsRepository) 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 *GoodsRepository) Save(goods *domain.Goods) (*domain.Goods, error) {
  28 + sqlBuildFields := []string{
  29 + "goods_id",
  30 + "goods_num",
  31 + "goods_dividend",
  32 + "goods_dividend_status",
  33 + "goods_name",
  34 + "goods_total_price",
  35 + "remarks",
  36 + "order_no",
  37 + "goods_detail",
  38 + "create_at",
  39 + "update_at",
  40 + "delete_at",
  41 + }
  42 + insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
  43 + insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
  44 + returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
  45 + updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "goods_id")
  46 + updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
  47 + tx := repository.transactionContext.PgTx
  48 + if goods.Identify() == nil {
  49 + goodsId, err := repository.nextIdentify()
  50 + if err != nil {
  51 + return goods, err
  52 + } else {
  53 + goods.GoodsId = goodsId
  54 + }
  55 + if _, err := tx.QueryOne(
  56 + pg.Scan(
  57 + &goods.GoodsId,
  58 + &goods.GoodsNum,
  59 + &goods.GoodsDividend,
  60 + &goods.GoodsDividendStatus,
  61 + &goods.GoodsName,
  62 + &goods.GoodsTotalPrice,
  63 + &goods.Remarks,
  64 + &goods.OrderNo,
  65 + &goods.GoodsDetail,
  66 + &goods.CreateAt,
  67 + &goods.UpdateAt,
  68 + &goods.DeleteAt,
  69 + ),
  70 + fmt.Sprintf("INSERT INTO goodss (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
  71 + goods.GoodsId,
  72 + goods.GoodsNum,
  73 + goods.GoodsDividend,
  74 + goods.GoodsDividendStatus,
  75 + goods.GoodsName,
  76 + goods.GoodsTotalPrice,
  77 + goods.Remarks,
  78 + goods.OrderNo,
  79 + goods.GoodsDetail,
  80 + goods.CreateAt,
  81 + goods.UpdateAt,
  82 + goods.DeleteAt,
  83 + ); err != nil {
  84 + return goods, err
  85 + }
  86 + } else {
  87 + if _, err := tx.QueryOne(
  88 + pg.Scan(
  89 + &goods.GoodsId,
  90 + &goods.GoodsNum,
  91 + &goods.GoodsDividend,
  92 + &goods.GoodsDividendStatus,
  93 + &goods.GoodsName,
  94 + &goods.GoodsTotalPrice,
  95 + &goods.Remarks,
  96 + &goods.OrderNo,
  97 + &goods.GoodsDetail,
  98 + &goods.CreateAt,
  99 + &goods.UpdateAt,
  100 + &goods.DeleteAt,
  101 + ),
  102 + fmt.Sprintf("UPDATE goodss SET %s WHERE goods_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
  103 + goods.GoodsId,
  104 + goods.GoodsNum,
  105 + goods.GoodsDividend,
  106 + goods.GoodsDividendStatus,
  107 + goods.GoodsName,
  108 + goods.GoodsTotalPrice,
  109 + goods.Remarks,
  110 + goods.OrderNo,
  111 + goods.GoodsDetail,
  112 + goods.CreateAt,
  113 + goods.UpdateAt,
  114 + goods.DeleteAt,
  115 + goods.Identify(),
  116 + ); err != nil {
  117 + return goods, err
  118 + }
  119 + }
  120 + return goods, nil
  121 +}
  122 +func (repository *GoodsRepository) Remove(goods *domain.Goods) (*domain.Goods, error) {
  123 + tx := repository.transactionContext.PgTx
  124 + goodsModel := new(models.Goods)
  125 + goodsModel.GoodsId = goods.Identify().(int64)
  126 + if _, err := tx.Model(goodsModel).WherePK().Delete(); err != nil {
  127 + return goods, err
  128 + }
  129 + return goods, nil
  130 +}
  131 +func (repository *GoodsRepository) FindOne(queryOptions map[string]interface{}) (*domain.Goods, error) {
  132 + tx := repository.transactionContext.PgTx
  133 + goodsModel := new(models.Goods)
  134 + query := sqlbuilder.BuildQuery(tx.Model(goodsModel), queryOptions)
  135 + query.SetWhereByQueryOption("goods.goods_id = ?", "goodsId")
  136 + if err := query.First(); err != nil {
  137 + if err.Error() == "pg: no rows in result set" {
  138 + return nil, fmt.Errorf("没有此资源")
  139 + } else {
  140 + return nil, err
  141 + }
  142 + }
  143 + if goodsModel.GoodsId == 0 {
  144 + return nil, nil
  145 + } else {
  146 + return transform.TransformToGoodsDomainModelFromPgModels(goodsModel)
  147 + }
  148 +}
  149 +func (repository *GoodsRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Goods, error) {
  150 + tx := repository.transactionContext.PgTx
  151 + var goodsModels []*models.Goods
  152 + goodss := make([]*domain.Goods, 0)
  153 + query := sqlbuilder.BuildQuery(tx.Model(&goodsModels), queryOptions)
  154 + query.SetOffsetAndLimit(20)
  155 + query.SetOrderDirect("goods_id", "DESC")
  156 + if count, err := query.SelectAndCount(); err != nil {
  157 + return 0, goodss, err
  158 + } else {
  159 + for _, goodsModel := range goodsModels {
  160 + if goods, err := transform.TransformToGoodsDomainModelFromPgModels(goodsModel); err != nil {
  161 + return 0, goodss, err
  162 + } else {
  163 + goodss = append(goodss, goods)
  164 + }
  165 + }
  166 + return int64(count), goodss, nil
  167 + }
  168 +}
  169 +func NewGoodsRepository(transactionContext *pgTransaction.TransactionContext) (*GoodsRepository, error) {
  170 + if transactionContext == nil {
  171 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  172 + } else {
  173 + return &GoodsRepository{
  174 + transactionContext: transactionContext,
  175 + }, nil
  176 + }
  177 +}
  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/mmm-go-pp/partner01/pkg/domain"
  11 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg/models"
  12 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg/transform"
  13 +)
  14 +
  15 +type OrderRepository struct {
  16 + transactionContext *pgTransaction.TransactionContext
  17 +}
  18 +
  19 +func (repository *OrderRepository) 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 *OrderRepository) Save(order *domain.Order) (*domain.Order, error) {
  28 + sqlBuildFields := []string{
  29 + "order_id",
  30 + "buyer",
  31 + "company_id",
  32 + "partner_id",
  33 + "delivery_code",
  34 + "is_disable",
  35 + "order_no",
  36 + "order_detail",
  37 + "order_dividend",
  38 + "order_dividend_status",
  39 + "order_goods",
  40 + "order_source",
  41 + "order_type",
  42 + "order_status",
  43 + "total_price",
  44 + "region_info",
  45 + "remarks",
  46 + "create_at",
  47 + "update_at",
  48 + "delete_at",
  49 + }
  50 + insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
  51 + insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
  52 + returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
  53 + updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "order_id")
  54 + updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
  55 + tx := repository.transactionContext.PgTx
  56 + if order.Identify() == nil {
  57 + orderId, err := repository.nextIdentify()
  58 + if err != nil {
  59 + return order, err
  60 + } else {
  61 + order.OrderId = orderId
  62 + }
  63 + if _, err := tx.QueryOne(
  64 + pg.Scan(
  65 + &order.OrderId,
  66 + &order.Buyer,
  67 + &order.CompanyId,
  68 + &order.PartnerId,
  69 + &order.DeliveryCode,
  70 + &order.IsDisable,
  71 + &order.OrderNo,
  72 + &order.OrderDetail,
  73 + &order.OrderDividend,
  74 + &order.OrderDividendStatus,
  75 + pg.Array(&order.OrderGoods),
  76 + &order.OrderSource,
  77 + &order.OrderType,
  78 + &order.OrderStatus,
  79 + &order.TotalPrice,
  80 + &order.RegionInfo,
  81 + &order.Remarks,
  82 + &order.CreateAt,
  83 + &order.UpdateAt,
  84 + &order.DeleteAt,
  85 + ),
  86 + fmt.Sprintf("INSERT INTO orders (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
  87 + order.OrderId,
  88 + order.Buyer,
  89 + order.CompanyId,
  90 + order.PartnerId,
  91 + order.DeliveryCode,
  92 + order.IsDisable,
  93 + order.OrderNo,
  94 + order.OrderDetail,
  95 + order.OrderDividend,
  96 + order.OrderDividendStatus,
  97 + pg.Array(order.OrderGoods),
  98 + order.OrderSource,
  99 + order.OrderType,
  100 + order.OrderStatus,
  101 + order.TotalPrice,
  102 + order.RegionInfo,
  103 + order.Remarks,
  104 + order.CreateAt,
  105 + order.UpdateAt,
  106 + order.DeleteAt,
  107 + ); err != nil {
  108 + return order, err
  109 + }
  110 + } else {
  111 + if _, err := tx.QueryOne(
  112 + pg.Scan(
  113 + &order.OrderId,
  114 + &order.Buyer,
  115 + &order.CompanyId,
  116 + &order.PartnerId,
  117 + &order.DeliveryCode,
  118 + &order.IsDisable,
  119 + &order.OrderNo,
  120 + &order.OrderDetail,
  121 + &order.OrderDividend,
  122 + &order.OrderDividendStatus,
  123 + pg.Array(&order.OrderGoods),
  124 + &order.OrderSource,
  125 + &order.OrderType,
  126 + &order.OrderStatus,
  127 + &order.TotalPrice,
  128 + &order.RegionInfo,
  129 + &order.Remarks,
  130 + &order.CreateAt,
  131 + &order.UpdateAt,
  132 + &order.DeleteAt,
  133 + ),
  134 + fmt.Sprintf("UPDATE orders SET %s WHERE order_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
  135 + order.OrderId,
  136 + order.Buyer,
  137 + order.CompanyId,
  138 + order.PartnerId,
  139 + order.DeliveryCode,
  140 + order.IsDisable,
  141 + order.OrderNo,
  142 + order.OrderDetail,
  143 + order.OrderDividend,
  144 + order.OrderDividendStatus,
  145 + pg.Array(order.OrderGoods),
  146 + order.OrderSource,
  147 + order.OrderType,
  148 + order.OrderStatus,
  149 + order.TotalPrice,
  150 + order.RegionInfo,
  151 + order.Remarks,
  152 + order.CreateAt,
  153 + order.UpdateAt,
  154 + order.DeleteAt,
  155 + order.Identify(),
  156 + ); err != nil {
  157 + return order, err
  158 + }
  159 + }
  160 + return order, nil
  161 +}
  162 +func (repository *OrderRepository) Remove(order *domain.Order) (*domain.Order, error) {
  163 + tx := repository.transactionContext.PgTx
  164 + orderModel := new(models.Order)
  165 + orderModel.OrderId = order.Identify().(int64)
  166 + if _, err := tx.Model(orderModel).WherePK().Delete(); err != nil {
  167 + return order, err
  168 + }
  169 + return order, nil
  170 +}
  171 +func (repository *OrderRepository) FindOne(queryOptions map[string]interface{}) (*domain.Order, error) {
  172 + tx := repository.transactionContext.PgTx
  173 + orderModel := new(models.Order)
  174 + query := sqlbuilder.BuildQuery(tx.Model(orderModel), queryOptions)
  175 + query.SetWhereByQueryOption("order.order_id = ?", "orderId")
  176 + if err := query.First(); err != nil {
  177 + if err.Error() == "pg: no rows in result set" {
  178 + return nil, fmt.Errorf("没有此资源")
  179 + } else {
  180 + return nil, err
  181 + }
  182 + }
  183 + if orderModel.OrderId == 0 {
  184 + return nil, nil
  185 + } else {
  186 + return transform.TransformToOrderDomainModelFromPgModels(orderModel)
  187 + }
  188 +}
  189 +func (repository *OrderRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Order, error) {
  190 + tx := repository.transactionContext.PgTx
  191 + var orderModels []*models.Order
  192 + orders := make([]*domain.Order, 0)
  193 + query := sqlbuilder.BuildQuery(tx.Model(&orderModels), queryOptions)
  194 + query.SetOffsetAndLimit(20)
  195 + query.SetOrderDirect("order_id", "DESC")
  196 + if count, err := query.SelectAndCount(); err != nil {
  197 + return 0, orders, err
  198 + } else {
  199 + for _, orderModel := range orderModels {
  200 + if order, err := transform.TransformToOrderDomainModelFromPgModels(orderModel); err != nil {
  201 + return 0, orders, err
  202 + } else {
  203 + orders = append(orders, order)
  204 + }
  205 + }
  206 + return int64(count), orders, nil
  207 + }
  208 +}
  209 +func NewOrderRepository(transactionContext *pgTransaction.TransactionContext) (*OrderRepository, error) {
  210 + if transactionContext == nil {
  211 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  212 + } else {
  213 + return &OrderRepository{
  214 + transactionContext: transactionContext,
  215 + }, nil
  216 + }
  217 +}