github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/central/mongo/service/impl/impl_org_tx.go (about)

     1  package impl
     2  
     3  import (
     4  	"github.com/sixexorg/magnetic-ring/central/entity"
     5  	"github.com/sixexorg/magnetic-ring/central/mongo"
     6  	"github.com/sixexorg/magnetic-ring/central/mongokey"
     7  	"gopkg.in/mgo.v2/bson"
     8  )
     9  
    10  type OrgTxnsImpl struct {
    11  	Txs      []interface{}
    12  	LeagueId string
    13  }
    14  
    15  func NewOrgTxnsImpl(txs []interface{}, orgid string) *OrgTxnsImpl {
    16  	return &OrgTxnsImpl{txs, orgid}
    17  }
    18  
    19  func (svc *OrgTxnsImpl) Insert() error {
    20  	leaguetxnskey := mongokey.NewLeagueTxnsPrefixKey(svc.LeagueId)
    21  	session, col := mongo.KeyProvider(leaguetxnskey)
    22  	defer session.Close()
    23  	return col.Insert(svc.Txs...)
    24  }
    25  
    26  func (svc *OrgTxnsImpl) GetTransferTxList(accountaddr string,page,size int) (int,interface{}, error) {
    27  	leaguetxnskey := mongokey.NewLeagueTxnsPrefixKey(svc.LeagueId)
    28  	session, col := mongo.KeyProvider(leaguetxnskey)
    29  	defer session.Close()
    30  
    31  	var htor []*entity.OrgTxDto
    32  
    33  	querym := bson.M{
    34  		"$or":[]bson.M{
    35  			{"txdata.from":accountaddr},
    36  			{"txdata.froms.tis":bson.M{"address":accountaddr}},
    37  			{"txdata.tos.tos":bson.M{"address":accountaddr}},
    38  		},
    39  	}
    40  
    41  	pg := page
    42  	if page <= 0 {
    43  		pg =1
    44  	}
    45  
    46  	skip := (pg -1 )*size
    47  
    48  	qury := col.Find(querym)
    49  
    50  	total,err := qury.Count()
    51  	if err != nil {
    52  		return 0,nil,err
    53  	}
    54  
    55  	err = qury.Sort("-blocktime").Skip(skip).All(&htor)
    56  
    57  	if err != nil {
    58  		if err != nil {
    59  			return 0,nil,err
    60  		}
    61  	}
    62  	return total,htor, nil
    63  }