2022-08-30 07:03:49 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-10-10 04:11:39 +00:00
|
|
|
"git.selly.red/Selly-Modules/natsio"
|
|
|
|
"git.selly.red/Selly-Modules/natsio/model"
|
|
|
|
"git.selly.red/Selly-Modules/natsio/subject"
|
2022-08-30 07:03:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DistinctWithField ...
|
2022-08-30 08:15:11 +00:00
|
|
|
func (w Warehouse) DistinctWithField(p model.DistinctWithField) ([]interface{}, error) {
|
2022-08-30 07:03:49 +00:00
|
|
|
msg, err := natsio.GetServer().Request(subject.Warehouse.Distinct, bsonToBytes(p))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var r struct {
|
|
|
|
Data []interface{} `json:"data"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(msg.Data, &r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if r.Error != "" {
|
|
|
|
return nil, errors.New(r.Error)
|
|
|
|
}
|
|
|
|
return r.Data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindOneByCondition ...
|
2022-08-30 07:44:48 +00:00
|
|
|
func (w Warehouse) FindOneByCondition(p model.FindOneCondition) (*model.WarehouseNatsResponse, error) {
|
2022-08-30 07:03:49 +00:00
|
|
|
msg, err := natsio.GetServer().Request(subject.Warehouse.FindOne, bsonToBytes(p))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var r struct {
|
|
|
|
Data *model.WarehouseNatsResponse `json:"data"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(msg.Data, &r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if r.Error != "" {
|
|
|
|
return nil, errors.New(r.Error)
|
|
|
|
}
|
|
|
|
return r.Data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CountByCondition ...
|
|
|
|
func (w Warehouse) CountByCondition(p model.FindWithCondition) (int64, error) {
|
|
|
|
msg, err := natsio.GetServer().Request(subject.Warehouse.Count, bsonToBytes(p))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
var r struct {
|
|
|
|
Data int64 `json:"data"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(msg.Data, &r); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if r.Error != "" {
|
|
|
|
return 0, errors.New(r.Error)
|
|
|
|
}
|
|
|
|
return r.Data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindByCondition ...
|
|
|
|
func (w Warehouse) FindByCondition(p model.FindWithCondition) ([]*model.WarehouseNatsResponse, error) {
|
|
|
|
msg, err := natsio.GetServer().Request(subject.Warehouse.FindByCondition, bsonToBytes(p))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var r struct {
|
|
|
|
Data []*model.WarehouseNatsResponse `json:"data"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(msg.Data, &r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if r.Error != "" {
|
|
|
|
return nil, errors.New(r.Error)
|
|
|
|
}
|
|
|
|
return r.Data, nil
|
|
|
|
}
|