作者 陈志颖

合并分支 '3-project-init-order' 到 'dev'

Resolve "project-init-order"

Closes #3

查看合并请求 !4
... ... @@ -21,4 +21,10 @@ metadata:
- ref: orderNo
required: true
- name: goodsDetail
required: true
- ref: createAt
required: true
- ref: updateAt
required: true
- ref: deleteAt
required: true
\ No newline at end of file
... ...
... ... @@ -38,3 +38,10 @@ metadata:
required: true
- ref: remarks
required: true
- ref: createAt
required: true
- ref: updateAt
required: true
- ref: deleteAt
description: 软删
required: true
... ...
... ... @@ -8,3 +8,5 @@ metadata:
required: true
- ref: platform
required: true
- ref: createAt
required: true
... ...
# 合伙人练习项目
# 合伙人练习项目-仅供参考
## 代码以及文档
- [仓库地址](http://gitlab.fjmaimaimai.com/mmm-go-pp/partner01.git)
... ...
package factory
import (
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain/service"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/domainService"
)
func CreateShippingService(options map[string]interface{}) (service.ShippingService, error) {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pgTransaction.TransactionContext)
}
return domainService.NewShippingService(transactionContext)
}
... ...
... ... @@ -29,3 +29,19 @@ func CreateUserAuthRepository(options map[string]interface{}) (domain.UserAuthRe
}
return repository.NewUserAuthRepository(transactionContext)
}
func CreateOrderRepository(options map[string]interface{}) (domain.OrderRepository, error) {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewOrderRepository(transactionContext)
}
func CreateGoodsRepository(options map[string]interface{}) (domain.GoodsRepository, error) {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewGoodsRepository(transactionContext)
}
... ...
... ... @@ -4,9 +4,9 @@ import "os"
var POSTGRESQL_DB_NAME = "partner01"
var POSTGRESQL_USER = "postgres"
var POSTGRESQL_PASSWORD = "123456"
var POSTGRESQL_HOST = "127.0.0.1"
var POSTGRESQL_PORT = "5432"
var POSTGRESQL_PASSWORD = "eagle1010"
var POSTGRESQL_HOST = "114.55.200.59"
var POSTGRESQL_PORT = "31543"
var DISABLE_CREATE_TABLE = false
var DISABLE_SQL_GENERATE_PRINT = false
... ...
package domain
import "time"
// 订单商品实体
type Goods struct {
// 商品id
GoodsId int64 `json:"goodsId"`
// 预定的货品数量
GoodsNum int `json:"goodsNum"`
// 商品分红支付信息
GoodsDividend *Dividend `json:"goodsDividend"`
// 商品分红支付状态
GoodsDividendStatus int `json:"goodsDividendStatus"`
// 商品名称
GoodsName string `json:"goodsName"`
// 商品单价
GoodsTotalPrice float64 `json:"goodsTotalPrice"`
// 备注
Remarks string `json:"remarks"`
// 订单编号
OrderNo string `json:"orderNo"`
GoodsDetail string `json:"goodsDetail"`
// 创建时间
CreateAt time.Time `json:"createAt"`
// 更新时间
UpdateAt time.Time `json:"updateAt"`
// 删除时间
DeleteAt time.Time `json:"deleteAt"`
}
type GoodsRepository interface {
Save(goods *Goods) (*Goods, error)
Remove(goods *Goods) (*Goods, error)
FindOne(queryOptions map[string]interface{}) (*Goods, error)
Find(queryOptions map[string]interface{}) (int64, []*Goods, error)
}
func (goods *Goods) Identify() interface{} {
if goods.GoodsId == 0 {
return nil
}
return goods.GoodsId
}
func (goods *Goods) Update(data map[string]interface{}) error {
if goodsId, ok := data["goodsId"]; ok {
goods.GoodsId = goodsId.(int64)
}
if goodsNum, ok := data["goodsNum"]; ok {
goods.GoodsNum = goodsNum.(int)
}
if planBonus, ok := data["planBonus"]; ok {
goods.GoodsDividend.PlanBonus = planBonus.(float64)
}
if useBonus, ok := data["useBonus"]; ok {
goods.GoodsDividend.UseBonus = useBonus.(float64)
}
if bonusHas, ok := data["bonusHas"]; ok {
goods.GoodsDividend.BonusHas = bonusHas.(float64)
}
if bonusNot, ok := data["bonusNot"]; ok {
goods.GoodsDividend.BonusNot = bonusNot.(float64)
}
if bonusExpense, ok := data["bonusExpense"]; ok {
goods.GoodsDividend.BonusExpense = bonusExpense.(float64)
}
if bonusStatus, ok := data["bonusStatus"]; ok {
goods.GoodsDividend.BonusStatus = bonusStatus.(int)
}
if partnerBonusPercent, ok := data["partnerBonusPercent"]; ok {
goods.GoodsDividend.PartnerBonusPercent = partnerBonusPercent.(float64)
}
if salesmanBonusPercent, ok := data["salesmanBonusPercent"]; ok {
goods.GoodsDividend.SalesmanBonusPercent = salesmanBonusPercent.(float64)
}
if salesmanBonus, ok := data["salesmanBonus"]; ok {
goods.GoodsDividend.SalesmanBonus = salesmanBonus.(float64)
}
if goodsDividendStatus, ok := data["goodsDividendStatus"]; ok {
goods.GoodsDividendStatus = goodsDividendStatus.(int)
}
if goodsName, ok := data["goodsName"]; ok {
goods.GoodsName = goodsName.(string)
}
if goodsTotalPrice, ok := data["goodsTotalPrice"]; ok {
goods.GoodsTotalPrice = goodsTotalPrice.(float64)
}
if remarks, ok := data["remarks"]; ok {
goods.Remarks = remarks.(string)
}
if orderNo, ok := data["orderNo"]; ok {
goods.OrderNo = orderNo.(string)
}
if goodsDetail, ok := data["goodsDetail"]; ok {
goods.GoodsDetail = goodsDetail.(string)
}
if createAt, ok := data["createAt"]; ok {
goods.CreateAt = createAt.(time.Time)
}
if updateAt, ok := data["updateAt"]; ok {
goods.UpdateAt = updateAt.(time.Time)
}
if deleteAt, ok := data["deleteAt"]; ok {
goods.DeleteAt = deleteAt.(time.Time)
}
return nil
}
... ...
package domain
import "time"
// 订单实体
type Order struct {
// 订单id
OrderId int64 `json:"orderId"`
// 买家信息
Buyer *BuyerInfo `json:"buyer"`
// 公司Id
CompanyId int64 `json:"companyId"`
// 合伙人id
PartnerId int64 `json:"partnerId"`
// 发货单号
DeliveryCode string `json:"deliveryCode"`
// 是否关闭订单标志
IsDisable int `json:"isDisable"`
// 订单编号
OrderNo string `json:"orderNo"`
// 订单详情
OrderDetail *OrderDetail `json:"orderDetail"`
// 订单分红信息
OrderDividend *Dividend `json:"orderDividend"`
// 订单分红支付状态
OrderDividendStatus int `json:"orderDividendStatus"`
// 订单货品
OrderGoods []*Goods `json:"orderGoods"`
// 订单数据来源
OrderSource *OrderSource `json:"orderSource"`
// 订单类型(1.实际订单;2.意向订单)
OrderType int `json:"orderType"`
// 订单状态(1.发货;2.收货;3.退货;4.取消;5.完成)
OrderStatus int `json:"orderStatus"`
// 订单总价
TotalPrice float64 `json:"totalPrice"`
// 订单区域信息
RegionInfo *RegionInfo `json:"regionInfo"`
// 备注
Remarks string `json:"remarks"`
// 创建时间
CreateAt time.Time `json:"createAt"`
// 更新时间
UpdateAt time.Time `json:"updateAt"`
// 软删
DeleteAt time.Time `json:"deleteAt"`
}
type OrderRepository interface {
Save(order *Order) (*Order, error)
Remove(order *Order) (*Order, error)
FindOne(queryOptions map[string]interface{}) (*Order, error)
Find(queryOptions map[string]interface{}) (int64, []*Order, error)
}
func (order *Order) Identify() interface{} {
if order.OrderId == 0 {
return nil
}
return order.OrderId
}
func (order *Order) Update(data map[string]interface{}) error {
if orderId, ok := data["orderId"]; ok {
order.OrderId = orderId.(int64)
}
if buyerName, ok := data["buyerName"]; ok {
order.Buyer.BuyerName = buyerName.(string)
}
if contactInfo, ok := data["contactInfo"]; ok {
order.Buyer.ContactInfo = contactInfo.(string)
}
if shippingAddress, ok := data["shippingAddress"]; ok {
order.Buyer.ShippingAddress = shippingAddress.(string)
}
if companyId, ok := data["companyId"]; ok {
order.CompanyId = companyId.(int64)
}
if partnerId, ok := data["partnerId"]; ok {
order.PartnerId = partnerId.(int64)
}
if deliveryCode, ok := data["deliveryCode"]; ok {
order.DeliveryCode = deliveryCode.(string)
}
if isDisable, ok := data["isDisable"]; ok {
order.IsDisable = isDisable.(int)
}
if orderNo, ok := data["orderNo"]; ok {
order.OrderNo = orderNo.(string)
}
if orderDetailId, ok := data["orderDetailId"]; ok {
order.OrderDetail.OrderDetailId = orderDetailId.(int64)
}
if orderNo, ok := data["orderNo"]; ok {
order.OrderDetail.OrderNo = orderNo.(string)
}
if orderNum, ok := data["orderNum"]; ok {
order.OrderDetail.OrderNum = orderNum.(int)
}
if totalPrice, ok := data["totalPrice"]; ok {
order.OrderDetail.TotalPrice = totalPrice.(float64)
}
if adjustedNum, ok := data["adjustedNum"]; ok {
order.OrderDetail.AdjustedNum = adjustedNum.(int)
}
if adjustedTotalPrice, ok := data["adjustedTotalPrice"]; ok {
order.OrderDetail.AdjustedTotalPrice = adjustedTotalPrice.(float64)
}
if planBonus, ok := data["planBonus"]; ok {
order.OrderDividend.PlanBonus = planBonus.(float64)
}
if useBonus, ok := data["useBonus"]; ok {
order.OrderDividend.UseBonus = useBonus.(float64)
}
if bonusHas, ok := data["bonusHas"]; ok {
order.OrderDividend.BonusHas = bonusHas.(float64)
}
if bonusNot, ok := data["bonusNot"]; ok {
order.OrderDividend.BonusNot = bonusNot.(float64)
}
if bonusExpense, ok := data["bonusExpense"]; ok {
order.OrderDividend.BonusExpense = bonusExpense.(float64)
}
if bonusStatus, ok := data["bonusStatus"]; ok {
order.OrderDividend.BonusStatus = bonusStatus.(int)
}
if partnerBonusPercent, ok := data["partnerBonusPercent"]; ok {
order.OrderDividend.PartnerBonusPercent = partnerBonusPercent.(float64)
}
if salesmanBonusPercent, ok := data["salesmanBonusPercent"]; ok {
order.OrderDividend.SalesmanBonusPercent = salesmanBonusPercent.(float64)
}
if salesmanBonus, ok := data["salesmanBonus"]; ok {
order.OrderDividend.SalesmanBonus = salesmanBonus.(float64)
}
if orderDividendStatus, ok := data["orderDividendStatus"]; ok {
order.OrderDividendStatus = orderDividendStatus.(int)
}
if orderGoods, ok := data["orderGoods"]; ok {
order.OrderGoods = orderGoods.([]*Goods)
}
if dataSourceId, ok := data["dataSourceId"]; ok {
order.OrderSource.DataSourceId = dataSourceId.(int64)
}
if platform, ok := data["platform"]; ok {
order.OrderSource.Platform = platform.(string)
}
if createAt, ok := data["createAt"]; ok {
order.OrderSource.CreateAt = createAt.(time.Time)
}
if orderType, ok := data["orderType"]; ok {
order.OrderType = orderType.(int)
}
if orderStatus, ok := data["orderStatus"]; ok {
order.OrderStatus = orderStatus.(int)
}
if totalPrice, ok := data["totalPrice"]; ok {
order.TotalPrice = totalPrice.(float64)
}
if regionName, ok := data["regionName"]; ok {
order.RegionInfo.RegionName = regionName.(string)
}
if remarks, ok := data["remarks"]; ok {
order.Remarks = remarks.(string)
}
if createAt, ok := data["createAt"]; ok {
order.CreateAt = createAt.(time.Time)
}
if updateAt, ok := data["updateAt"]; ok {
order.UpdateAt = updateAt.(time.Time)
}
if deleteAt, ok := data["deleteAt"]; ok {
order.DeleteAt = deleteAt.(time.Time)
}
return nil
}
... ...
package domain
import "time"
// 订单数据来源值对象
type OrderSource struct {
// 订单数据来源id
DataSourceId int64 `json:"dataSourceId"`
// 订单数据来源平台
Platform string `json:"platform"`
// 创建时间
CreateAt time.Time `json:"createAt"`
}
... ...
package service
type ShippingService interface {
}
... ...
package domainService
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
)
type ShippingService struct {
transactionContext *pgTransaction.TransactionContext
}
func NewShippingService(transactionContext *pgTransaction.TransactionContext) (*ShippingService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &ShippingService{
transactionContext: transactionContext,
}, nil
}
}
... ...
... ... @@ -25,9 +25,11 @@ func init() {
}
if !constant.DISABLE_CREATE_TABLE {
for _, model := range []interface{}{
(*models.Permission)(nil),
(*models.PartnerCategory)(nil),
(*models.UserAuth)(nil),
&models.Permission{},
&models.PartnerCategory{},
&models.UserAuth{},
&models.Goods{},
&models.Order{},
} {
err := DB.Model(model).CreateTable(&orm.CreateTableOptions{
Temp: false,
... ...
package models
import (
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
"time"
)
type Goods struct {
tableName string `pg:"goodss,alias:goods"`
// 商品id
GoodsId int64
// 预定的货品数量
GoodsNum int
// 商品分红支付信息
GoodsDividend *domain.Dividend
// 商品分红支付状态
GoodsDividendStatus int
// 商品名称
GoodsName string
// 商品单价
GoodsTotalPrice float64
// 备注
Remarks string
// 订单编号
OrderNo string
// 商品详情
GoodsDetail string
// 创建时间
CreateAt time.Time
// 更新时间
UpdateAt time.Time
// 删除时间
DeleteAt time.Time
}
... ...
package models
import (
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
"time"
)
type Order struct {
tableName string `pg:"orders,alias:order"`
// 订单id
OrderId int64
// 买家信息
Buyer *domain.BuyerInfo
// 公司Id
CompanyId int64
// 合伙人id
PartnerId int64
// 发货单号
DeliveryCode string
// 是否关闭订单标志
IsDisable int
// 订单编号
OrderNo string
// 订单详情
OrderDetail *domain.OrderDetail
// 订单分红信息
OrderDividend *domain.Dividend
// 订单分红支付状态
OrderDividendStatus int
// 订单货品
OrderGoods []*domain.Goods
// 订单数据来源
OrderSource *domain.OrderSource
// 订单类型(1.实际订单;2.意向订单)
OrderType int
// 订单状态(1.发货;2.收货;3.退货;4.取消;5.完成)
OrderStatus int
// 订单总价
TotalPrice float64
// 订单区域信息
RegionInfo *domain.RegionInfo
// 备注
Remarks string
// 创建时间
CreateAt time.Time
// 更新时间
UpdateAt time.Time
// 软删
DeleteAt time.Time
}
... ...
package transform
import (
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg/models"
)
func TransformToGoodsDomainModelFromPgModels(goodsModel *models.Goods) (*domain.Goods, error) {
return &domain.Goods{
GoodsId: goodsModel.GoodsId,
GoodsNum: goodsModel.GoodsNum,
GoodsDividend: goodsModel.GoodsDividend,
GoodsDividendStatus: goodsModel.GoodsDividendStatus,
GoodsName: goodsModel.GoodsName,
GoodsTotalPrice: goodsModel.GoodsTotalPrice,
Remarks: goodsModel.Remarks,
OrderNo: goodsModel.OrderNo,
GoodsDetail: goodsModel.GoodsDetail,
CreateAt: goodsModel.CreateAt,
UpdateAt: goodsModel.UpdateAt,
DeleteAt: goodsModel.DeleteAt,
}, nil
}
... ...
package transform
import (
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg/models"
)
func TransformToOrderDomainModelFromPgModels(orderModel *models.Order) (*domain.Order, error) {
return &domain.Order{
OrderId: orderModel.OrderId,
Buyer: orderModel.Buyer,
CompanyId: orderModel.CompanyId,
PartnerId: orderModel.PartnerId,
DeliveryCode: orderModel.DeliveryCode,
IsDisable: orderModel.IsDisable,
OrderNo: orderModel.OrderNo,
OrderDetail: orderModel.OrderDetail,
OrderDividend: orderModel.OrderDividend,
OrderDividendStatus: orderModel.OrderDividendStatus,
OrderGoods: orderModel.OrderGoods,
OrderSource: orderModel.OrderSource,
OrderType: orderModel.OrderType,
OrderStatus: orderModel.OrderStatus,
TotalPrice: orderModel.TotalPrice,
RegionInfo: orderModel.RegionInfo,
Remarks: orderModel.Remarks,
CreateAt: orderModel.CreateAt,
UpdateAt: orderModel.UpdateAt,
DeleteAt: orderModel.DeleteAt,
}, nil
}
... ...
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 GoodsRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func (repository *GoodsRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
return id, err
}
func (repository *GoodsRepository) Save(goods *domain.Goods) (*domain.Goods, error) {
sqlBuildFields := []string{
"goods_id",
"goods_num",
"goods_dividend",
"goods_dividend_status",
"goods_name",
"goods_total_price",
"remarks",
"order_no",
"goods_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 goods.Identify() == nil {
goodsId, err := repository.nextIdentify()
if err != nil {
return goods, err
} else {
goods.GoodsId = goodsId
}
if _, err := tx.QueryOne(
pg.Scan(
&goods.GoodsId,
&goods.GoodsNum,
&goods.GoodsDividend,
&goods.GoodsDividendStatus,
&goods.GoodsName,
&goods.GoodsTotalPrice,
&goods.Remarks,
&goods.OrderNo,
&goods.GoodsDetail,
&goods.CreateAt,
&goods.UpdateAt,
&goods.DeleteAt,
),
fmt.Sprintf("INSERT INTO goodss (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
goods.GoodsId,
goods.GoodsNum,
goods.GoodsDividend,
goods.GoodsDividendStatus,
goods.GoodsName,
goods.GoodsTotalPrice,
goods.Remarks,
goods.OrderNo,
goods.GoodsDetail,
goods.CreateAt,
goods.UpdateAt,
goods.DeleteAt,
); err != nil {
return goods, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(
&goods.GoodsId,
&goods.GoodsNum,
&goods.GoodsDividend,
&goods.GoodsDividendStatus,
&goods.GoodsName,
&goods.GoodsTotalPrice,
&goods.Remarks,
&goods.OrderNo,
&goods.GoodsDetail,
&goods.CreateAt,
&goods.UpdateAt,
&goods.DeleteAt,
),
fmt.Sprintf("UPDATE goodss SET %s WHERE goods_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
goods.GoodsId,
goods.GoodsNum,
goods.GoodsDividend,
goods.GoodsDividendStatus,
goods.GoodsName,
goods.GoodsTotalPrice,
goods.Remarks,
goods.OrderNo,
goods.GoodsDetail,
goods.CreateAt,
goods.UpdateAt,
goods.DeleteAt,
goods.Identify(),
); err != nil {
return goods, err
}
}
return goods, nil
}
func (repository *GoodsRepository) Remove(goods *domain.Goods) (*domain.Goods, error) {
tx := repository.transactionContext.PgTx
goodsModel := new(models.Goods)
goodsModel.GoodsId = goods.Identify().(int64)
if _, err := tx.Model(goodsModel).WherePK().Delete(); err != nil {
return goods, err
}
return goods, nil
}
func (repository *GoodsRepository) FindOne(queryOptions map[string]interface{}) (*domain.Goods, error) {
tx := repository.transactionContext.PgTx
goodsModel := new(models.Goods)
query := sqlbuilder.BuildQuery(tx.Model(goodsModel), queryOptions)
query.SetWhereByQueryOption("goods.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 goodsModel.GoodsId == 0 {
return nil, nil
} else {
return transform.TransformToGoodsDomainModelFromPgModels(goodsModel)
}
}
func (repository *GoodsRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Goods, error) {
tx := repository.transactionContext.PgTx
var goodsModels []*models.Goods
goodss := make([]*domain.Goods, 0)
query := sqlbuilder.BuildQuery(tx.Model(&goodsModels), queryOptions)
query.SetOffsetAndLimit(20)
query.SetOrderDirect("goods_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, goodss, err
} else {
for _, goodsModel := range goodsModels {
if goods, err := transform.TransformToGoodsDomainModelFromPgModels(goodsModel); err != nil {
return 0, goodss, err
} else {
goodss = append(goodss, goods)
}
}
return int64(count), goodss, nil
}
}
func NewGoodsRepository(transactionContext *pgTransaction.TransactionContext) (*GoodsRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &GoodsRepository{
transactionContext: transactionContext,
}, nil
}
}
... ...
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,
pg.Array(&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,
pg.Array(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,
pg.Array(&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.OrderId,
order.Buyer,
order.CompanyId,
order.PartnerId,
order.DeliveryCode,
order.IsDisable,
order.OrderNo,
order.OrderDetail,
order.OrderDividend,
order.OrderDividendStatus,
pg.Array(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("order.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
}
}
... ...