github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/central/mongo/service/impl/impl_org_vtststcs.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 VoteStatisticsImpl struct {
    11  	vu *entity.VoteUnit
    12  	leagueId string
    13  }
    14  
    15  func NewVoteStatisticsImpl(vu *entity.VoteUnit,leagueId string) *VoteStatisticsImpl  {
    16  	return &VoteStatisticsImpl{vu:vu,leagueId:leagueId}
    17  }
    18  
    19  func (svc *VoteStatisticsImpl) Add() error {
    20  	leaguetxnskey := mongokey.NewVoteSatisticsKey(svc.leagueId,svc.vu.VoteId)
    21  	session, col := mongo.KeyProvider(leaguetxnskey)
    22  	defer session.Close()
    23  
    24  	return col.Insert(svc.vu)
    25  }
    26  
    27  func (svc *VoteStatisticsImpl) IsAccountVoted(account string) bool {
    28  	leaguetxnskey := mongokey.NewVoteSatisticsKey(svc.leagueId,svc.vu.VoteId)
    29  	session, col := mongo.KeyProvider(leaguetxnskey)
    30  	defer session.Close()
    31  
    32  	result := new(entity.VoteUnit)
    33  	err := col.Find(bson.M{"account":account}).One(result)
    34  	if err != nil {
    35  		return false
    36  	}
    37  
    38  	return true
    39  }
    40  
    41  
    42  func (svc *VoteStatisticsImpl) FindVoteUnitList(leagueId,voteId string,page,pgsize int)(int,interface{},error) {
    43  	leaguetxnskey := mongokey.NewVoteSatisticsKey(leagueId,voteId)
    44  	session, col := mongo.KeyProvider(leaguetxnskey)
    45  	defer session.Close()
    46  
    47  	bsonm := bson.M{
    48  		"voteid":voteId,
    49  	}
    50  
    51  	query := col.Find(bsonm)
    52  
    53  	total,err := query.Count()
    54  	if err != nil {
    55  		return 0,nil,err
    56  	}
    57  
    58  	if page <= 0 {
    59  		page = 1
    60  	}
    61  
    62  	if pgsize < 10{
    63  		pgsize=10
    64  	}
    65  
    66  	skip := pgsize * (page-1)
    67  	var result []entity.VoteUnit
    68  
    69  	err = query.Sort("-endblockheight").Skip(skip).Limit(pgsize).All(&result)
    70  	if err != nil {
    71  		return 0,nil,err
    72  	}
    73  
    74  	return total,result,nil
    75  }