friend.go 11.2 KB
package models

import (
	db "client/friendSystem/database"
	"errors"
	"log"
	"math/rand"
	"strconv"
	"time"
)

type FriendRelationship struct {
	RelationshipId        int
	FriendRelationshipAId string
	FriendRelationshipBId string
	RelationshipValue     int
}

type FriendNameList struct {
	FriendId              string
	FriendName            string
	FriendHeadPortraitUrl string
}

type RandomFriendInfo struct {
	Guid      string
	Username  string
	AvatorUrl string
}
type FriendAddRespones struct {
	Guid              string
	Username          string
	AvatorUrl         string
	RelationshipValue int
}
type FriendRanklistResult struct {
	RanklistUserId          string
	RanklistUserName        string
	RanklistHeadPortraitUrl string
	RanklistScore           float64
	RanklistIndex           int
	RanklistUserValueCount  map[string]string
}

type FriendRanklistRequest struct {
	RanklistGameId     string
	RanklistName       string
	RanklistUserId     string
	RanklistLength     int
	RanklistFieldIndex int
}

/*发送好友请求*/
func (f *FriendRelationship) AddFriend(addType string) (result interface{}, err error) {
	if err = CheckIdExist(f.FriendRelationshipAId); err != nil {
		return nil, err
	}
	if err = CheckIdExist(f.FriendRelationshipBId); err != nil {
		return nil, err
	}
	friendrelationship := make([]FriendRelationship, 0)
	sql := ` select relationship_id,relationship_value from ice_friend_relationship where  (friend_relationship_a_id=? and  friend_relationship_b_id=?) or (friend_relationship_b_id=? and friend_relationship_a_id =?) `
	if err = db.Engine.Sql(sql, f.FriendRelationshipAId, f.FriendRelationshipBId, f.FriendRelationshipAId, f.FriendRelationshipBId).Find(&friendrelationship); err != nil {
		return nil, err
	}

	if len(friendrelationship) != 0 {
		// if friendrelationship[0].RelationshipValue == 0 {
		// 	err = errors.New("已向该玩家发送请求")
		// } else if friendrelationship[0].RelationshipValue == 1 {
		// 	err = errors.New("该玩家已经是你的好友")
		// } else {

		// }
		// return nil, err
		switch friendrelationship[0].RelationshipValue {
		case 0:
			err = errors.New("已向该玩家发送请求")
			break
		case 1:
			err = errors.New("该玩家已经是你的好友")
			break
		case 2:
			log.Println("该玩家已经拒绝过你的请求")
			sql = ` delete from ice_friend_relationship where relationship_id = ? `
			_, err = db.Engine.Exec(sql, friendrelationship[0].RelationshipId)
			break
		case 3:
			err = errors.New("该玩家已经拒绝过你的请求")
			break
		default:
			err = errors.New("未知的好友关系类型")
			break
		}
	}
	if err != nil {
		return nil, err
	}
	var res string
	switch addType {
	case "addById":
		f.RelationshipValue = 0
		res = "成功发送请求"
	case "addByQrcode":
		f.RelationshipValue = 1
		res = "成功添加好友"
	default:
		err = errors.New("addtype not exists")
		return nil, err
	}
	friendrelationship = make([]FriendRelationship, 0)
	sql = ` insert into ice_friend_relationship (friend_relationship_a_id,friend_relationship_b_id,relationship_value) values(?,?,?) `
	err = db.Engine.Sql(sql, f.FriendRelationshipAId, f.FriendRelationshipBId, f.RelationshipValue).Find(&friendrelationship)
	if err != nil {
		return nil, err
	}
	return res, err
}

/*响应好友请求*/
func (f *FriendRelationship) RespondFriendRequest(respondResult string) (result interface{}, err error) {
	if err = CheckIdExist(f.FriendRelationshipAId); err != nil {
		return nil, err
	}
	if err = CheckIdExist(f.FriendRelationshipBId); err != nil {
		return nil, err
	}
	friendRelationship := make([]FriendRelationship, 0)
	sql := ` select relationship_value from ice_friend_relationship where friend_relationship_a_id=? and friend_relationship_b_id = ? limit 1 `
	err = db.Engine.Sql(sql, f.FriendRelationshipAId, f.FriendRelationshipBId).Find(&friendRelationship)
	if len(friendRelationship) == 0 {
		err = errors.New("不存在的好友申请")
		return nil, err
	}
	if friendRelationship[0].RelationshipValue == 1 {
		err = errors.New("已存在的好友关系")
		return nil, err
	}
	switch respondResult {
	case "accept":
		sql = `update ice_friend_relationship set relationship_value = 1 where friend_relationship_a_id=?`
		break
	case "refuse":
		sql = `update ice_friend_relationship set relationship_value = 2 where friend_relationship_a_id=?`
	default:
		err = errors.New("好友申请响应类型错误")
		return nil, err
	}
	_, err = db.Engine.Exec(sql, f.FriendRelationshipAId)
	if err != nil {
		return nil, err
	}
	return
}

/*删除好友*/
func (f *FriendRelationship) DeleteFriend() (result string, err error) {
	sql := ` select * from ice_friend_relationship where (friend_relationship_a_id =? and friend_relationship_b_id=?) or (friend_relationship_b_id =? and friend_relationship_a_id = ?) and relationship_value = 2 or relationship_value = 1 `
	r, err := db.Engine.Query(sql, f.FriendRelationshipAId, f.FriendRelationshipBId, f.FriendRelationshipAId, f.FriendRelationshipBId)
	if err != nil {
		return
	}
	if len(r) == 0 {
		err = errors.New("不存在的好友关系")
		return
	}
	sql = ` delete from ice_friend_relationship where  (friend_relationship_a_id =? and friend_relationship_b_id=?) or (friend_relationship_b_id =? and friend_relationship_a_id = ?)`
	_, err = db.Engine.Exec(sql, f.FriendRelationshipAId, f.FriendRelationshipBId, f.FriendRelationshipAId, f.FriendRelationshipBId)
	if err != nil {
		return
	}
	return "删除好友成功", err
}

/*查询是否有新的好友请求*/
func (f *FriendRelationship) CheckFriendRequest() (result interface{}, err error) {
	if err = CheckIdExist(f.FriendRelationshipBId); err != nil {
		return nil, err
	}
	sql := `SELECT guid, username, avator_url FROM users INNER JOIN ice_friend_relationship ON guid = friend_relationship_a_id WHERE friend_relationship_b_id =? and relationship_value =0`
	friendInfo := make([]RandomFriendInfo, 0)
	err = db.Engine.Sql(sql, f.FriendRelationshipBId).Find(&friendInfo)
	if err != nil {
		return nil, err
	}
	return friendInfo, err
}

/*获取二维码图片url*/
func GetQrCodeImg(uuid string) (result string, err error) {
	if err = SetQrCodeImage(uuid); err != nil {
		return
	}
	result = "http://iceplay.oss-cn-beijing.aliyuncs.com/userInfo/Qrcode/" + MD5(uuid)
	return
}

/*获得随机推荐好友*/
func GetRandomFriend(randomType string, randomLength int) (result interface{}, err error) {
	switch randomType {
	case "realrandom":
		randomFriendInfo := make([]RandomFriendInfo, 0)
		sql := `select count(*) from users `
		r, err := db.Engine.Query(sql)
		if err != nil {
			return nil, err
		}
		count, err := strconv.ParseInt(string(r[0]["count(*)"]), 10, 64)
		if err != nil {
			return nil, err
		}
		sql = ` select id from users order by id  limit 1 `
		r, err = db.Engine.Query(sql)
		if err != nil {
			return nil, err
		}
		start, err := strconv.ParseInt(string(r[0]["id"]), 10, 64)
		newRand := rand.New(rand.NewSource(time.Now().UnixNano()))
		sqlin := " where id in("
		for i := 0; i < randomLength; i++ {
			randUserId := strconv.FormatInt(newRand.Int63n(count)+start, 10)
			sqlin = sqlin + randUserId + ","
		}
		sqlin = sqlin[:len(sqlin)-1]
		sqlin = sqlin + ") "
		sql = `select guid,username,avator_url from users` + sqlin
		log.Println(sql)
		err = db.Engine.Sql(sql).Find(&randomFriendInfo)
		if err != nil {
			return nil, err
		}
		return randomFriendInfo, err
	default:
		err = errors.New("随机类型不存在")
		return nil, err
	}
}

/*获取好友列表*/
func GetFriendNameList(UserId string) (result interface{}, err error) {
	if err = CheckIdExist(UserId); err != nil {
		return nil, err
	}

	friendNameList := make([]FriendNameList, 0)
	sql := ` SELECT b.USERNAME AS friend_name,b.GUID AS friend_id ,b.avator_url as friend_head_portrait_url FROM ice_friend_relationship a,users b WHERE(a.friend_relationship_a_id = ? AND a.friend_relationship_b_id = b.GUID and a.relationship_value = 1) OR (a.friend_relationship_b_id = ? AND a.friend_relationship_a_id = b.GUID and a.relationship_value = 1) `
	err = db.Engine.Sql(sql, UserId, UserId).Find(&friendNameList)
	if err != nil {
		return nil, err
	}
	return friendNameList, err
}

/*获取好友排行榜*/
func (f *FriendRanklistRequest) GetFriendRanklist() (result interface{}, err error) {
	/*老版本获取好友排行榜
	// tableName, err := GetTableName(f.RanklistGameId)                     //通过游戏id获取排行榜表名
	tableName := "ice_tablename_" + f.RanklistGameId
	log.Println(tableName)
	fieldName, err := GetTableFieldName(tableName, f.RanklistFieldIndex) //通过索引位置获取要排序的字段
	if err != nil {
		return nil, err
	}
	friendList, err := GetFriendList(f.RanklistUserId) //通过用户uuid获取用户好友列表
	if err != nil {
		return nil, err
	}
	sql := `select ranklist_user_id,ranklist_user_name,ranklist_head_portrait_url,` + fieldName + ` from ` + tableName + ` where ranklist_user_id ` + friendList + " order by " + fieldName + " desc"
	log.Println(sql)
	log.Println(fieldName)
	r, err := db.Engine.Query(sql)
	friendRanklistResult := make([]FriendRanklistResult, len(r))
	for k, v := range r {
		friendRanklistResult[k].RanklistUserId = string(v["ranklist_user_id"])
		friendRanklistResult[k].RanklistUserName = string(v["ranklist_user_name"])
		friendRanklistResult[k].RanklistHeadPortraitUrl = string(v["ranklist_head_portrait_url"])
		friendRanklistResult[k].RanklistScore, err = strconv.ParseFloat(string(v[fieldName]), 64)
	}
	if err != nil {
		return nil, err
	}
	return friendRanklistResult, err
	*/

	//关联陈成排行榜
	friendList, err := GetFriendList(f.RanklistUserId) //通过用户uuid获取用户好友列表
	if err != nil {
		return nil, err
	}
	log.Println(friendList)
	log.Println(f)
	sql := ` select users.guid as ranklist_user_id,users.username as ranklist_user_name,users.avator_url as ranklist_head_portrait_url,mykey from ice_gametop,users where guid ` + friendList + ` and GTID=? and GID=? and ice_gametop.uid=users.guid order by cast(mykey as signed) desc`
	log.Println(sql, f.RanklistName, f.RanklistGameId)
	r, err := db.Engine.Query(sql, f.RanklistName, f.RanklistGameId)
	friendRanklistResult := make([]FriendRanklistResult, len(r))
	for k, v := range r {
		friendRanklistResult[k].RanklistUserId = string(v["ranklist_user_id"])
		friendRanklistResult[k].RanklistUserName = string(v["ranklist_user_name"])
		friendRanklistResult[k].RanklistHeadPortraitUrl = string(v["ranklist_head_portrait_url"])
		friendRanklistResult[k].RanklistScore, err = strconv.ParseFloat(string(v["mykey"]), 64)
		friendRanklistResult[k].RanklistIndex = k + 1
	}
	if err != nil {
		return nil, err
	}
	return friendRanklistResult, err

}

/*根据Gameid创建数据库表结构*/
func CreateDBTable(sql string) (result string, err error) {
	_, err = db.Engine.Exec(sql)
	if err != nil {
		return
	}
	result = "success"
	return
}

/*查看好友申请情况*/
func (f *FriendRelationship) CheckFriendRespones() (result interface{}, err error) {
	if err = CheckIdExist(f.FriendRelationshipAId); err != nil {
		return nil, err
	}
	friendAddRespones := make([]FriendAddRespones, 0)
	sql := `SELECT guid, username, avator_url, relationship_value FROM users inner join ice_friend_relationship  on guid = friend_relationship_b_id WHERE friend_relationship_a_id = ?`
	err = db.Engine.Sql(sql, f.FriendRelationshipAId).Find(&friendAddRespones)
	if err != nil {
		return nil, err
	}
	return friendAddRespones, err
}