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

     1  package models
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  //好友关联表
     8  //包含  用户 ID,好友 ID
     9  type Friend struct {
    10  	Id      int64
    11  	Uid     int64  `xorm:"index"`
    12  	Fid     int64  `xorm:"index"`
    13  	Content string `xorm:"index"`         //申请留言
    14  	Group   string `xorm:"index"`         //好友分组
    15  	Accept  int64  `xorm:"index"`         //是否通过好友申请,即是大于0就是所有好友 3为特别关注  2为普通 1为待处理 0为缺省值 -1为拒绝  -2黑名单
    16  	Created int64  `xorm:"created index"` //何时相交
    17  }
    18  
    19  type Friendjuser struct {
    20  	Friend `xorm:"extends",json:"Friend"`
    21  	User   `xorm:"extends",json:"User"`
    22  }
    23  
    24  func DelFriend(uid int64, fid int64) (int64, error) {
    25  	Engine.Where("uid=? and fid=?", fid, uid).Delete(new(Friend))
    26  	return Engine.Where("uid=? and fid=?", uid, fid).Delete(new(Friend))
    27  }
    28  
    29  func SetFriend(uid, fid, accept int64, content, group string) (int64, error) {
    30  	DelFriend(uid, fid)
    31  
    32  	f := new(Friend)
    33  	f.Uid = uid
    34  	f.Fid = fid
    35  	f.Content = content
    36  	f.Group = group
    37  	f.Accept = accept
    38  	f.Created = time.Now().Unix()
    39  	rows, err := Engine.Insert(f)
    40  	//2rd
    41  	f.Id = 0
    42  	f.Uid = fid
    43  	f.Fid = uid
    44  	rows, err = Engine.Insert(f)
    45  	return rows, err
    46  }
    47  
    48  func SetFriendTo(fromUid, toFid, accept int64, content, group string) (int64, error) {
    49  
    50  	f := new(Friend)
    51  	f.Uid = toFid
    52  	f.Fid = fromUid
    53  	f.Content = content
    54  	f.Group = group
    55  	f.Accept = accept
    56  	f.Created = time.Now().Unix()
    57  	rows, err := Engine.Insert(f)
    58  	return rows, err
    59  }
    60  
    61  func GetRelationship(uid, fid int64) *Friend {
    62  	f := new(Friend)
    63  	if has, err := Engine.Where("uid=? and fid=?", uid, fid).Get(f); err != nil {
    64  		return nil
    65  	} else {
    66  		if has {
    67  			return f
    68  		} else {
    69  			return nil
    70  		}
    71  	}
    72  }
    73  
    74  func IsFriend(uid, fid int64) bool {
    75  
    76  	f := new(Friend)
    77  	if has, err := Engine.Where("uid=? and fid=? and accept>=?", uid, fid, 2).Get(f); err != nil {
    78  		return false
    79  	} else {
    80  		if has {
    81  			return true
    82  		} else {
    83  			return false
    84  		}
    85  	}
    86  
    87  }
    88  
    89  func GetFriends(offset int, limit int, field string) (*[]*Friend, error) {
    90  	friends := new([]*Friend)
    91  	err := Engine.Limit(limit, offset).Desc(field).Find(friends)
    92  	return friends, err
    93  
    94  }
    95  
    96  func GetFriendsByUid(uid int64, offset int, limit int, group string, field string) *[]*Friend {
    97  	fr := new([]*Friend)
    98  	if field == "asc" {
    99  
   100  		if uid == 0 {
   101  			if len(group) > 0 {
   102  				Engine.Table("friend").Where("friend.group=?", group).And("friend.accept>=?", 1).Limit(limit, offset).Asc("friend.id").Find(fr)
   103  			} else {
   104  				Engine.Table("friend").Where("friend.accept>=?", 1).Limit(limit, offset).Asc("friend.id").Find(fr)
   105  			}
   106  
   107  		} else {
   108  
   109  			if len(group) == 0 {
   110  				Engine.Table("friend").Where("friend.uid=?", uid).And("friend.accept>=?", 1).Limit(limit, offset).Asc("friend.id").Find(fr)
   111  
   112  			} else {
   113  
   114  				Engine.Table("friend").Where("friend.group=? and friend.uid=?", group, uid).And("friend.accept>=?", 1).Limit(limit, offset).Asc("friend.id").Find(fr)
   115  			}
   116  		}
   117  	} else {
   118  
   119  		if uid == 0 {
   120  			if len(group) > 0 {
   121  				Engine.Table("friend").Where("friend.group=? and friend.accept>=?", group, 1).Limit(limit, offset).Desc("friend." + field).Find(fr)
   122  			} else {
   123  				Engine.Table("friend").Where("friend.accept>=?", 1).Limit(limit, offset).Desc("friend." + field).Find(fr)
   124  			}
   125  
   126  		} else {
   127  
   128  			if len(group) == 0 {
   129  				Engine.Table("friend").Where("friend.uid=? and friend.accept>=?", uid, 1).Limit(limit, offset).Desc("friend." + field).Find(fr)
   130  
   131  			} else {
   132  
   133  				Engine.Table("friend").Where("friend.group=? and friend.uid=? and friend.accept>=?", group, uid, 1).Limit(limit, offset).Desc("friend." + field).Find(fr)
   134  			}
   135  		}
   136  	}
   137  	return fr
   138  }
   139  
   140  func GetFriendsByUidJoinUser(uid int64, offset int, limit int, group string, field string) *[]*Friendjuser {
   141  	fr := new([]*Friendjuser)
   142  	if field == "asc" {
   143  
   144  		if uid == 0 {
   145  			if len(group) > 0 {
   146  				Engine.Table("friend").Where("friend.group=?", group).And("friend.accept>=?", 1).Limit(limit, offset).Asc("friend.id").Join("LEFT", "user", "user.id = friend.fid").Find(fr)
   147  			} else {
   148  				Engine.Table("friend").Where("friend.accept>=?", 1).Limit(limit, offset).Asc("friend.id").Join("LEFT", "user", "user.id = friend.fid").Find(fr)
   149  			}
   150  
   151  		} else {
   152  
   153  			if len(group) == 0 {
   154  				Engine.Table("friend").Where("friend.uid=?", uid).And("friend.accept>=?", 1).Limit(limit, offset).Asc("friend.id").Join("LEFT", "user", "user.id = friend.fid").Find(fr)
   155  
   156  			} else {
   157  
   158  				Engine.Table("friend").Where("friend.group=? and friend.uid=?", group, uid).And("friend.accept>=?", 1).Limit(limit, offset).Asc("friend.id").Join("LEFT", "user", "user.id = friend.fid").Find(fr)
   159  			}
   160  		}
   161  	} else {
   162  
   163  		if uid == 0 {
   164  			if len(group) > 0 {
   165  				Engine.Table("friend").Where("friend.group=? and friend.accept>=?", group, 1).Limit(limit, offset).Desc("friend."+field).Join("LEFT", "user", "user.id = friend.fid").Find(fr)
   166  			} else {
   167  				Engine.Table("friend").Where("friend.accept>=?", 1).Limit(limit, offset).Desc("friend."+field).Join("LEFT", "user", "user.id = friend.fid").Find(fr)
   168  			}
   169  
   170  		} else {
   171  
   172  			if len(group) == 0 {
   173  				Engine.Table("friend").Where("friend.uid=? and friend.accept>=?", uid, 1).Limit(limit, offset).Desc("friend."+field).Join("LEFT", "user", "user.id = friend.fid").Find(fr)
   174  
   175  			} else {
   176  
   177  				Engine.Table("friend").Where("friend.group=? and friend.uid=? and friend.accept>=?", group, uid, 1).Limit(limit, offset).Desc("friend."+field).Join("LEFT", "user", "user.id = friend.fid").Find(fr)
   178  			}
   179  		}
   180  	}
   181  	return fr
   182  }