github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/models/balance.go (about)

     1  package models
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"time"
     7  )
     8  
     9  //时间 类型 数额 余额 描述
    10  //Time Ctype Amount Balance Description
    11  type Balance struct {
    12  	Id          int64  `xorm:"index"`
    13  	Uid         int64  `xorm:"index"`   //钱包拥有者
    14  	Time        int64  `xorm:"index"`   //发生时间 为保证时间的正确性 此处使用时间戳
    15  	Ctype       int64  `xorm:"index"`   //动作类型
    16  	Amount      int64  `xorm:"index"`   //数额
    17  	Balance     int64  `xorm:"index"`   //余额
    18  	Description string `xorm:"text"`    //描述
    19  	Version     int64  `xorm:"version"` //乐观锁
    20  }
    21  
    22  type Balancejuser struct {
    23  	Balance `xorm:"extends"`
    24  	User    `xorm:"extends"`
    25  }
    26  
    27  func GetBalancesByUidJoinUser(uid int64, ctype int64, offset int, limit int, field string) *[]*Balancejuser {
    28  	var balance = new([]*Balancejuser)
    29  	if field == "asc" {
    30  
    31  		if uid == 0 { //uid为0则查询所有用户
    32  			if ctype != 0 { //查询特定ctype
    33  				Engine.Table("balance").Where("balance.ctype=?", ctype).Limit(limit, offset).Asc("balance.id").Join("LEFT", "user", "user.id = balance.uid").Find(balance)
    34  			} else { //不限制ctype
    35  				Engine.Table("balance").Limit(limit, offset).Asc("balance.id").Join("LEFT", "user", "user.id = balance.uid").Find(balance)
    36  			}
    37  
    38  		} else { //查询特定uid
    39  
    40  			if ctype == 0 { //不限制ctype
    41  				Engine.Table("balance").Where("balance.uid=?", uid).Limit(limit, offset).Asc("balance.id").Join("LEFT", "user", "user.id = balance.uid").Find(balance)
    42  
    43  			} else { //查询特定ctype
    44  
    45  				Engine.Table("balance").Where("balance.ctype=? and balance.uid=?", ctype, uid).Limit(limit, offset).Asc("balance.id").Join("LEFT", "user", "user.id = balance.uid").Find(balance)
    46  			}
    47  		}
    48  	} else if field == "desc" {
    49  
    50  		if uid == 0 {
    51  			if ctype != 0 {
    52  				Engine.Table("balance").Where("balance.ctype=?", ctype).Limit(limit, offset).Desc("balance."+field).Join("LEFT", "user", "user.id = balance.uid").Find(balance)
    53  			} else {
    54  				Engine.Table("balance").Limit(limit, offset).Desc("balance."+field).Join("LEFT", "user", "user.id = balance.uid").Find(balance)
    55  			}
    56  
    57  		} else {
    58  
    59  			if ctype == 0 {
    60  				Engine.Table("balance").Where("balance.uid=?", uid).Limit(limit, offset).Desc("balance."+field).Join("LEFT", "user", "user.id = balance.uid").Find(balance)
    61  
    62  			} else {
    63  
    64  				Engine.Table("balance").Where("balance.ctype=? and balance.uid=?", ctype, uid).Limit(limit, offset).Desc("balance."+field).Join("LEFT", "user", "user.id = balance.uid").Find(balance)
    65  			}
    66  		}
    67  	}
    68  	return balance
    69  }
    70  
    71  func GetBalancesByUid(uid int64, ctype int64, offset int, limit int, field string) *[]*Balance {
    72  	var balance = new([]*Balance)
    73  	if field == "asc" {
    74  
    75  		if uid == 0 { //uid为0则查询所有用户
    76  			if ctype != 0 { //查询特定ctype
    77  				Engine.Table("balance").Where("balance.ctype=?", ctype).Limit(limit, offset).Asc("balance.id").Find(balance)
    78  			} else { //不限制ctype
    79  				Engine.Table("balance").Limit(limit, offset).Asc("balance.id").Find(balance)
    80  			}
    81  
    82  		} else { //查询特定uid
    83  
    84  			if ctype == 0 { //不限制ctype
    85  				Engine.Table("balance").Where("balance.uid=?", uid).Limit(limit, offset).Asc("balance.id").Find(balance)
    86  
    87  			} else { //查询特定ctype
    88  
    89  				Engine.Table("balance").Where("balance.ctype=? and balance.uid=?", ctype, uid).Limit(limit, offset).Asc("balance.id").Find(balance)
    90  			}
    91  		}
    92  	} else {
    93  
    94  		if uid == 0 {
    95  			if ctype != 0 {
    96  				Engine.Table("balance").Where("balance.ctype=?", ctype).Limit(limit, offset).Desc("balance." + field).Find(balance)
    97  			} else {
    98  				Engine.Table("balance").Limit(limit, offset).Desc("balance." + field).Find(balance)
    99  			}
   100  
   101  		} else {
   102  
   103  			if ctype == 0 {
   104  				Engine.Table("balance").Where("balance.uid=?", uid).Limit(limit, offset).Desc("balance." + field).Find(balance)
   105  
   106  			} else {
   107  
   108  				Engine.Table("balance").Where("balance.ctype=? and balance.uid=?", ctype, uid).Limit(limit, offset).Desc("balance." + field).Find(balance)
   109  			}
   110  		}
   111  	}
   112  	return balance
   113  }
   114  
   115  func GetBalancesByUsername(username string, ctype int64, offset int, limit int, field string) *[]*Balance {
   116  	usr, e := GetUserByUsername(username)
   117  	if e != nil {
   118  		return nil
   119  	} else if usr != nil {
   120  		return GetBalancesByUid(usr.Id, ctype, offset, limit, field)
   121  	}
   122  	return nil
   123  }
   124  
   125  func GetBalanceById(id int64) (*Balance, error) {
   126  
   127  	balancey := &Balance{}
   128  	has, err := Engine.Id(id).Get(balancey)
   129  	if has {
   130  		return balancey, err
   131  	} else {
   132  
   133  		return nil, err
   134  	}
   135  }
   136  
   137  func GetBalanceByUid(uid int64) *Balance {
   138  	balance := &Balance{}
   139  
   140  	if has, err := Engine.Where("uid=?", uid).Get(balance); err != nil || has == false {
   141  		return nil
   142  	}
   143  	return balance
   144  }
   145  
   146  func GetAllBalance() *[]*Balance {
   147  	balances := &[]*Balance{}
   148  	Engine.Desc("id").Find(balances)
   149  	return balances
   150  }
   151  
   152  func SetAmountById(id, uid, ctype, amount int64, description string) error {
   153  	balance := &Balance{}
   154  	var err error
   155  	if id > 0 { //大于0则更新指定id的数据
   156  		if balance, err = GetBalanceById(id); err != nil {
   157  			return errors.New(fmt.Sprintf("SetAmountById() GetBalance() Error:", err))
   158  		} else if balance != nil {
   159  			balance.Uid = uid
   160  			balance.Ctype = ctype
   161  			balance.Time = time.Now().Unix()
   162  			balance.Amount = amount
   163  			balance.Balance = balance.Balance + amount
   164  			balance.Description = description
   165  			return SetBalance(balance) //更新数据
   166  		}
   167  	} else { //等于0则插入新id数据
   168  		balance.Id = 0
   169  		balance.Uid = uid
   170  		balance.Ctype = ctype
   171  		balance.Time = time.Now().Unix()
   172  		balance.Amount = amount
   173  		balance.Balance = balance.Balance + amount
   174  		balance.Description = description
   175  		return SetBalance(balance) //插入数据
   176  	}
   177  
   178  	return errors.New("SetAmountById() Error")
   179  }
   180  
   181  func SetAmountByUid(uid, ctype, amount int64, description string) error {
   182  
   183  	if balance := GetBalanceByUid(uid); balance != nil {
   184  		balance.Ctype = ctype
   185  		balance.Time = time.Now().Unix()
   186  		balance.Amount = amount
   187  		balance.Balance = balance.Balance + amount
   188  		balance.Description = description
   189  		return SetBalance(balance) //更新数据
   190  	} else {
   191  		balance := &Balance{}
   192  		balance.Id = 0
   193  		balance.Uid = uid
   194  		balance.Ctype = ctype
   195  		balance.Time = time.Now().Unix()
   196  		balance.Amount = amount
   197  		balance.Balance = balance.Balance + amount
   198  		balance.Description = description
   199  		return SetBalance(balance) //插入数据
   200  	}
   201  	return errors.New("SetAmountByUid() Error")
   202  
   203  }
   204  
   205  func SetBalance(balance *Balance) error {
   206  
   207  	balancey := &Balance{}
   208  
   209  	//id大于0则执行更新
   210  	if balance.Id > 0 {
   211  
   212  		if _, err := Engine.Id(balance.Id).Get(balancey); err != nil {
   213  			return err
   214  		} else if balancey != nil {
   215  
   216  			balancey.Amount = balance.Amount
   217  			balancey.Balance = balancey.Balance + balance.Amount
   218  			balancey.Description = balance.Description
   219  			balancey.Time = balance.Time
   220  			balancey.Ctype = balance.Ctype
   221  
   222  			if row, err := Engine.Id(balancey.Id).Cols("time,amount,balance,ctype,description").Update(balancey); err != nil || row == 0 {
   223  				return err
   224  			} else { //更新成功后把数据设到user表的balance字段
   225  				return SetBalanceForUser(balance.Uid, balance.Balance)
   226  
   227  			}
   228  		}
   229  	} else { //id小于等于0则执行插入 version=1
   230  
   231  		if row, err := Engine.Insert(balance); err != nil || row == 0 {
   232  			return err
   233  		} else { //更新成功后把数据设到user表的balance字段
   234  
   235  			return SetBalanceForUser(balance.Uid, balance.Balance)
   236  
   237  		}
   238  
   239  	}
   240  	return nil
   241  
   242  }
   243  
   244  func DelBalance(bid int64) error {
   245  	if row, err := Engine.Id(bid).Delete(new(Balance)); err != nil || row == 0 {
   246  		return errors.New("DelBalance() Error!")
   247  	} else {
   248  		return nil
   249  	}
   250  }
   251  
   252  func DelBalanceByRole(id int64, uid int64, role int64) error {
   253  	allow := bool(false)
   254  	if anz, err := GetBalanceById(id); err == nil && anz != nil {
   255  		if anz.Uid == uid {
   256  			allow = true
   257  		} else if role < 0 {
   258  			allow = true
   259  		}
   260  		if allow {
   261  			if row, err := Engine.Id(id).Delete(new(Balance)); err != nil || row == 0 {
   262  				return errors.New("row, err := Engine.Id(rid).Delete(new(Balance)) Error!")
   263  			} else {
   264  				return nil
   265  			}
   266  		} else {
   267  			return errors.New("DelBalanceByRole() not allow!")
   268  		}
   269  	} else {
   270  		return errors.New("DelBalanceByRole() GetBalanceById Error")
   271  	}
   272  
   273  }
   274  
   275  func DelBalancesByUid(uid int64) error {
   276  	balancey := &[]Balance{}
   277  	if err := Engine.Where("uid=?", uid).Find(balancey); err == nil && balancey != nil {
   278  		for _, v := range *balancey {
   279  			if err := DelBalanceByRole(v.Id, v.Uid, -1000); err != nil {
   280  				fmt.Println("DelBalanceByRole:", err)
   281  			}
   282  		}
   283  		return nil
   284  	} else {
   285  		return err
   286  	}
   287  }