...
|
...
|
@@ -12,11 +12,11 @@ import ( |
|
|
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg/transform"
|
|
|
)
|
|
|
|
|
|
type GoodsRepository struct {
|
|
|
type GoodRepository struct {
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
func (repository *GoodsRepository) nextIdentify() (int64, error) {
|
|
|
func (repository *GoodRepository) nextIdentify() (int64, error) {
|
|
|
IdWorker, err := snowflake.NewIdWorker(1)
|
|
|
if err != nil {
|
|
|
return 0, err
|
...
|
...
|
@@ -24,17 +24,17 @@ func (repository *GoodsRepository) nextIdentify() (int64, error) { |
|
|
id, err := IdWorker.NextId()
|
|
|
return id, err
|
|
|
}
|
|
|
func (repository *GoodsRepository) Save(goods *domain.Goods) (*domain.Goods, error) {
|
|
|
func (repository *GoodRepository) Save(good *domain.Good) (*domain.Good, error) {
|
|
|
sqlBuildFields := []string{
|
|
|
"goods_id",
|
|
|
"goods_num",
|
|
|
"goods_dividend",
|
|
|
"goods_dividend_status",
|
|
|
"goods_name",
|
|
|
"goods_total_price",
|
|
|
"good_id",
|
|
|
"good_num",
|
|
|
"good_dividend",
|
|
|
"good_dividend_status",
|
|
|
"good_name",
|
|
|
"good_total_price",
|
|
|
"remarks",
|
|
|
"order_no",
|
|
|
"goods_detail",
|
|
|
"good_detail",
|
|
|
"create_at",
|
|
|
"update_at",
|
|
|
"delete_at",
|
...
|
...
|
@@ -45,94 +45,94 @@ func (repository *GoodsRepository) Save(goods *domain.Goods) (*domain.Goods, err |
|
|
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "goods_id")
|
|
|
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
if goods.Identify() == nil {
|
|
|
goodsId, err := repository.nextIdentify()
|
|
|
if good.Identify() == nil {
|
|
|
goodId, err := repository.nextIdentify()
|
|
|
if err != nil {
|
|
|
return goods, err
|
|
|
return good, err
|
|
|
} else {
|
|
|
goods.GoodsId = goodsId
|
|
|
good.GoodId = goodId
|
|
|
}
|
|
|
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,
|
|
|
&good.GoodId,
|
|
|
&good.GoodNum,
|
|
|
&good.GoodDividend,
|
|
|
&good.GoodDividendStatus,
|
|
|
&good.GoodName,
|
|
|
&good.GoodTotalPrice,
|
|
|
&good.Remarks,
|
|
|
&good.OrderNo,
|
|
|
&good.GoodDetail,
|
|
|
&good.CreateAt,
|
|
|
&good.UpdateAt,
|
|
|
&good.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,
|
|
|
fmt.Sprintf("INSERT INTO good (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
|
|
|
good.GoodId,
|
|
|
good.GoodNum,
|
|
|
good.GoodDividend,
|
|
|
good.GoodDividendStatus,
|
|
|
good.GoodName,
|
|
|
good.GoodTotalPrice,
|
|
|
good.Remarks,
|
|
|
good.OrderNo,
|
|
|
good.GoodDetail,
|
|
|
good.CreateAt,
|
|
|
good.UpdateAt,
|
|
|
good.DeleteAt,
|
|
|
); err != nil {
|
|
|
return goods, err
|
|
|
return good, 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,
|
|
|
&good.GoodId,
|
|
|
&good.GoodNum,
|
|
|
&good.GoodDividend,
|
|
|
&good.GoodDividendStatus,
|
|
|
&good.GoodName,
|
|
|
&good.GoodTotalPrice,
|
|
|
&good.Remarks,
|
|
|
&good.OrderNo,
|
|
|
&good.GoodDetail,
|
|
|
&good.CreateAt,
|
|
|
&good.UpdateAt,
|
|
|
&good.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(),
|
|
|
fmt.Sprintf("UPDATE good SET %s WHERE goods_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
|
|
|
good.GoodId,
|
|
|
good.GoodNum,
|
|
|
good.GoodDividend,
|
|
|
good.GoodDividendStatus,
|
|
|
good.GoodName,
|
|
|
good.GoodTotalPrice,
|
|
|
good.Remarks,
|
|
|
good.OrderNo,
|
|
|
good.GoodDetail,
|
|
|
good.CreateAt,
|
|
|
good.UpdateAt,
|
|
|
good.DeleteAt,
|
|
|
good.Identify(),
|
|
|
); err != nil {
|
|
|
return goods, err
|
|
|
return good, err
|
|
|
}
|
|
|
}
|
|
|
return goods, nil
|
|
|
return good, nil
|
|
|
}
|
|
|
func (repository *GoodsRepository) Remove(goods *domain.Goods) (*domain.Goods, error) {
|
|
|
func (repository *GoodRepository) Remove(good *domain.Good) (*domain.Good, 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
|
|
|
goodModel := new(models.Good)
|
|
|
goodModel.GoodId = good.Identify().(int64)
|
|
|
if _, err := tx.Model(goodModel).WherePK().Delete(); err != nil {
|
|
|
return good, err
|
|
|
}
|
|
|
return goods, nil
|
|
|
return good, nil
|
|
|
}
|
|
|
func (repository *GoodsRepository) FindOne(queryOptions map[string]interface{}) (*domain.Goods, error) {
|
|
|
func (repository *GoodRepository) FindOne(queryOptions map[string]interface{}) (*domain.Good, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
goodsModel := new(models.Goods)
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(goodsModel), queryOptions)
|
|
|
query.SetWhereByQueryOption("goods.goods_id = ?", "goodsId")
|
|
|
goodModel := new(models.Good)
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(goodModel), queryOptions)
|
|
|
query.SetWhereByQueryOption("good.goods_id = ?", "goodsId")
|
|
|
if err := query.First(); err != nil {
|
|
|
if err.Error() == "pg: no rows in result set" {
|
|
|
return nil, fmt.Errorf("没有此资源")
|
...
|
...
|
@@ -140,37 +140,37 @@ func (repository *GoodsRepository) FindOne(queryOptions map[string]interface{}) |
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
if goodsModel.GoodsId == 0 {
|
|
|
if goodModel.GoodId == 0 {
|
|
|
return nil, nil
|
|
|
} else {
|
|
|
return transform.TransformToGoodsDomainModelFromPgModels(goodsModel)
|
|
|
return transform.TransformToGoodDomainModelFromPgModels(goodModel)
|
|
|
}
|
|
|
}
|
|
|
func (repository *GoodsRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Goods, error) {
|
|
|
func (repository *GoodRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Good, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
var goodsModels []*models.Goods
|
|
|
goodss := make([]*domain.Goods, 0)
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(&goodsModels), queryOptions)
|
|
|
var goodModels []*models.Good
|
|
|
goods := make([]*domain.Good, 0)
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(&goodModels), queryOptions)
|
|
|
query.SetOffsetAndLimit(20)
|
|
|
query.SetOrderDirect("goods_id", "DESC")
|
|
|
if count, err := query.SelectAndCount(); err != nil {
|
|
|
return 0, goodss, err
|
|
|
return 0, goods, err
|
|
|
} else {
|
|
|
for _, goodsModel := range goodsModels {
|
|
|
if goods, err := transform.TransformToGoodsDomainModelFromPgModels(goodsModel); err != nil {
|
|
|
return 0, goodss, err
|
|
|
for _, goodModel := range goodModels {
|
|
|
if good, err := transform.TransformToGoodDomainModelFromPgModels(goodModel); err != nil {
|
|
|
return 0, goods, err
|
|
|
} else {
|
|
|
goodss = append(goodss, goods)
|
|
|
goods = append(goods, good)
|
|
|
}
|
|
|
}
|
|
|
return int64(count), goodss, nil
|
|
|
return int64(count), goods, nil
|
|
|
}
|
|
|
}
|
|
|
func NewGoodsRepository(transactionContext *pgTransaction.TransactionContext) (*GoodsRepository, error) {
|
|
|
func NewGoodsRepository(transactionContext *pgTransaction.TransactionContext) (*GoodRepository, error) {
|
|
|
if transactionContext == nil {
|
|
|
return nil, fmt.Errorf("transactionContext参数不能为nil")
|
|
|
} else {
|
|
|
return &GoodsRepository{
|
|
|
return &GoodRepository{
|
|
|
transactionContext: transactionContext,
|
|
|
}, nil
|
|
|
}
|
...
|
...
|
|