github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/central/mongo/service/impl/impl_org_vote_detail.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 VoteDetailImpl struct {
    11  	detail *entity.VoteDetail
    12  	leagueId string
    13  }
    14  
    15  func NewVoteDetailImpl(heightor *entity.VoteDetail,orgid string) *VoteDetailImpl  {
    16  	return &VoteDetailImpl{heightor,orgid}
    17  }
    18  
    19  func (svc *VoteDetailImpl) Insert() error {
    20  
    21  	orgkey := mongokey.NewVoteDetailPrefixKey(svc.leagueId)
    22  
    23  	session,col := mongo.KeyProvider(orgkey)
    24  	defer session.Close()
    25  
    26  	if svc.detail.Id == "" {
    27  		svc.detail.Id = svc.leagueId
    28  	}
    29  
    30  	return col.Insert(svc.detail)
    31  }
    32  
    33  func (svc *VoteDetailImpl) UpdateVoteDetailById(newHeight uint64) error {
    34  	orgkey := mongokey.NewVoteDetailPrefixKey(svc.leagueId)
    35  
    36  	session,col := mongo.KeyProvider(orgkey)
    37  	defer session.Close()
    38  
    39  	return col.UpdateId(svc.leagueId,svc.detail)
    40  }
    41  
    42  func (svc *VoteDetailImpl) GetLeagueVoteDetail(orgid,voteid string)(*entity.VoteDetail,error) {
    43  	orgkey := mongokey.NewVoteDetailPrefixKey(svc.leagueId)
    44  
    45  	session,col := mongo.KeyProvider(orgkey)
    46  	defer session.Close()
    47  
    48  	htor := new(entity.VoteDetail)
    49  	err := col.FindId(svc.leagueId).One(htor)
    50  	if err != nil {
    51  		return nil,err
    52  	}
    53  	return htor,nil
    54  }
    55  
    56  
    57  func (svc *VoteDetailImpl) GetVoteList(page,size int,height uint64,vstate uint8) (int,interface{}, error) {
    58  	orgkey := mongokey.NewVoteDetailPrefixKey(svc.leagueId)
    59  	session, col := mongo.KeyProvider(orgkey)
    60  	defer session.Close()
    61  
    62  	if page <= 0 {
    63  		page = 1
    64  	}
    65  
    66  	skip := size * (page-1)
    67  
    68  	bsm := bson.M{}
    69  
    70  	if vstate == 1 {
    71  		bsm = bson.M{
    72  			"endheight":bson.M{"$gt":vstate},
    73  		}
    74  	}
    75  
    76  	if vstate == 2 {
    77  		bsm = bson.M{
    78  			"endheight":bson.M{"$lte":vstate},
    79  		}
    80  	}
    81  
    82  	var htor []*entity.VoteDetail
    83  	query := col.Find(bsm)
    84  
    85  	total,err := query.Count()
    86  
    87  	if err != nil {
    88  		return 0,nil, err
    89  	}
    90  
    91  	err = query.Sort("-endheight").Skip(skip).Limit(size).All(&htor)
    92  	if err != nil {
    93  		return 0,nil, err
    94  	}
    95  
    96  
    97  	return total,htor, nil
    98  }