|
- // Copyright 2017 The Gitea Authors. All rights reserved.
- // Use of this source code is governed by a MIT-style
- // license that can be found in the LICENSE file.
-
- package models
-
- import (
- "fmt"
- "code.gitea.io/gitea/modules/setting"
- "code.gitea.io/gitea/modules/timeutil"
- "xorm.io/builder"
- )
-
- type Transfer struct {
- ID int64 `xorm:"pk autoincr"`
- FromID string
- ToID string
- Why string
- Qty int
- CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
- }
-
- // SearchTransOptions contains the options for searching
- type SearchTransOptions struct {
- FromId string
- ToId string
- Why string
- Keyword string
- OrderBy SearchOrderBy
- Page int
- PageSize int // Can be smaller than or equal to setting.UI.ExplorePagingNum
- }
-
- func (opts *SearchTransOptions) toConds() builder.Cond {
- var cond builder.Cond = builder.Gt{"ID": 0}
- return cond
- }
-
- func SearchTrans(opts *SearchTransOptions) (trans []*Transfer, _ int64, _ error) {
- condition := Transfer{
- FromID: opts.FromId,
- ToID: opts.ToId,
- }
- x2 := x.Where("why like ?","%"+ opts.Why+"%")
- count, err := x2.Count(new(Transfer),condition)
- if err != nil {
- return nil, 0, fmt.Errorf("Count: %v", err)
- }
-
- if opts.PageSize == 0 || opts.PageSize > setting.UI.ExplorePagingNum {
- opts.PageSize = setting.UI.ExplorePagingNum
- }
- if opts.Page <= 0 {
- opts.Page = 1
- }
- if len(opts.OrderBy) == 0 {
- opts.OrderBy = SearchOrderByIDReverse
- }
-
- sess := x.NewSession()
- sess = sess.Where("why like ?","%"+ opts.Why+"%")
- if opts.PageSize > 0 {
- sess = sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
- }
- if opts.PageSize == -1 {
- opts.PageSize = int(count)
- }
- trans = make([]*Transfer, 0, opts.PageSize)
- return trans, count, sess.OrderBy(opts.OrderBy.String()).Find(&trans,condition)
- }
-
- func TransferPoint(FromID string, Why string, ToID string, Qty int) (err error) {
-
- sess := x.NewSession()
-
- defer sess.Close()
- if err = sess.Begin(); err != nil {
- return err
- }
-
- if _, err = sess.Insert(&Transfer{FromID: FromID, ToID: ToID, Why: Why, Qty: Qty}); err != nil {
- return err
- }
-
- if _, err = sess.Exec("UPDATE `user` SET point = point + ? WHERE name = ?", Qty, ToID); err != nil {
- return err
- }
-
- if _, err = sess.Exec("UPDATE `user` SET point = point - ? WHERE name = ?", Qty, FromID); err != nil {
- return err
- }
- return sess.Commit()
- }
-
- // Fund contains the fund information
- type Fund struct {
- ID int64 `xorm:"pk autoincr"`
- Name string
- RepoID int64 `xorm:"INDEX"`
- Qty int64
- }
-
- type FundList []*Fund
-
- // GetFunds returns a list of Funds of given repository.
- func GetFunds(repoID int64, page int) (FundList, error) {
- funds := make([]*Fund, 0, setting.UI.IssuePagingNum)
- sess := x.Where("repo_id = ? ", repoID)
- if page > 0 {
- sess = sess.Limit(setting.UI.IssuePagingNum, (page-1)*setting.UI.IssuePagingNum)
- }
-
- return funds, sess.Find(&funds)
- }
-
- func NewFund(name string, repoID int64, qty int64)(err error){
- sess := x.NewSession()
-
- defer sess.Close()
- if err = sess.Begin(); err != nil {
- return err
- }
-
- if _, err = sess.Insert(&Fund{Name: name, RepoID: repoID, Qty: qty}); err != nil {
- return err
- }
-
- if _, err = sess.Exec("UPDATE `repository` SET point = point + ? WHERE id = ?", qty, repoID); err != nil {
- return err
- }
-
- if _, err = sess.Exec("UPDATE `repository` SET next_point = point * percent * 0.01 WHERE id = ?", repoID); err != nil {
- return err
- }
- return sess.Commit()
- }
-
- func GetRepoPointByUserID(userID int64) (int, error){
- repos, _, err := SearchRepository(&SearchRepoOptions{OwnerID:userID})
- if err != nil {
- return 0,err
- }
- var repoPoint int
- for _,v:= range repos{
- repoPoint += int(v.Point)
- }
- return repoPoint, nil
- }
|