headwear.go 5.63 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3
package headwear_cv

import (
hujiebin's avatar
hujiebin committed
4
	"git.hilo.cn/hilo-common/domain"
hujiebin's avatar
hujiebin committed
5 6 7
	"git.hilo.cn/hilo-common/resource/mysql"
	"gorm.io/gorm"
	"hilo-user/_const/enum/headwear_e"
hujiebin's avatar
hujiebin committed
8
	"hilo-user/domain/model/cp_m"
hujiebin's avatar
hujiebin committed
9 10 11 12 13 14 15 16
	"hilo-user/domain/model/res_m"
	"hilo-user/domain/model/user_m"
	"hilo-user/myerr"
	"strconv"
	"time"
)

type CvHeadwear struct {
hujiebin's avatar
hujiebin committed
17 18 19 20 21 22 23
	Id               uint64    `json:"id"`
	Using            bool      `json:"using"`
	PicUrl           mysql.Str `json:"picUrl"`
	EffectUrl        mysql.Str `json:"effectUrl"`
	ReverseEffectUrl mysql.Str `json:"reverseEffectUrl"`
	TimeLeft         int64     `json:"timeLeft"` // 离到期还有多少秒(过期则是负数)
	HeadwearIcon     string    `json:"headwearIcon"`
hujiebin's avatar
hujiebin committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
}

type CvHeadwearDiamond struct {
	Id         uint64 `json:"id"`
	PicUrl     string `json:"picUrl"`
	EffectUrl  string `json:"effectUrl"`
	DiamondNum uint32 `json:"diamondNum"`
	Second     uint32 `json:"second"`
	Days       string `json:"days"`
}

func GetCvHeadwearDiamond(pageSize int, pageIndex int) ([]CvHeadwearDiamond, error) {
	headwearDiamonds := []CvHeadwearDiamond{}
	if err := mysql.Db.Raw("SELECT d.id, r.pic_url, r.effect_url, d.`second`, d.diamond_num from res_headwear r, res_headwear_diamond d where r.id = d.res_headwear_id and d.`status` = ? ORDER BY d.diamond_num asc LIMIT ?, ?", mysql.USER, (pageIndex-1)*pageSize, pageSize).Scan(&headwearDiamonds).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	for i, _ := range headwearDiamonds {
		headwearDiamonds[i].Days = strconv.FormatUint(uint64(headwearDiamonds[i].Second/(24*60*60)), 10)
	}
	return headwearDiamonds, nil
}

func GetHeadwearList(db *gorm.DB, userId uint64) ([]CvHeadwear, error) {
	rows := make([]user_m.UserHeadwear, 0)
	if err := db.Where(&user_m.UserHeadwear{
		UserId: userId,
	}).Where("end_time >= ?", time.Now()).Order("`using` DESC, updated_time DESC").Find(&rows).Error; err != nil {
		return nil, err
	}
	resHwMap, err := res_m.GetResHeadwearMap(db)
	if err != nil {
		return nil, myerr.WrapErr(err)
	}

	result := make([]CvHeadwear, 0)
	now := time.Now()
	hasUsing := false
	for _, i := range rows {
		//  TODO: 没过期并且有设置using的,才算是,因为写入方不维护using状态的更新
		isUsing := i.Using == headwear_e.YesUsing && i.EndTime.After(now)
		result = append(result, CvHeadwear{
hujiebin's avatar
hujiebin committed
65 66 67 68 69 70
			Id:               i.HeadwearId,
			PicUrl:           resHwMap[i.HeadwearId].PicUrl,
			EffectUrl:        resHwMap[i.HeadwearId].EffectUrl,
			ReverseEffectUrl: resHwMap[i.HeadwearId].ReverseEffectUrl,
			Using:            isUsing,
			TimeLeft:         i.EndTime.Unix() - now.Unix(),
hujiebin's avatar
hujiebin committed
71 72 73 74 75 76
		})
		if isUsing {
			hasUsing = true
		}
	}
	// 如果没有一个using,则找第一个没过期的充当
hujiebin's avatar
hujiebin committed
77
	if !hasUsing {
hujiebin's avatar
hujiebin committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
		for i, e := range result {
			if e.TimeLeft > 0 {
				result[i].Using = true
				break
			}
		}
	}

	return result, nil
}

func GetCvHeadwear(userId uint64) (*CvHeadwear, error) {
	userHeadwear := user_m.UserHeadwear{}
	if err := mysql.Db.Model(&user_m.UserHeadwear{}).Where(&user_m.UserHeadwear{
		UserId: userId,
	}).Where("end_time >= ?", time.Now()).Order("`using` DESC, updated_time DESC").First(&userHeadwear).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		} else {
			return nil, myerr.WrapErr(err)
		}
	}
	resHeadwear := res_m.ResHeadwear{}
	if err := mysql.Db.Model(&res_m.ResHeadwear{}).First(&resHeadwear, userHeadwear.HeadwearId).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	return &CvHeadwear{
hujiebin's avatar
hujiebin committed
105 106 107 108 109
		Id:               userHeadwear.HeadwearId,
		PicUrl:           resHeadwear.PicUrl,
		EffectUrl:        resHeadwear.EffectUrl,
		ReverseEffectUrl: resHeadwear.ReverseEffectUrl,
		Using:            userHeadwear.Using == headwear_e.YesUsing,
hujiebin's avatar
hujiebin committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	}, nil
}

func BatchGetCvHeadwears(userIds []uint64) (map[uint64]CvHeadwear, error) {
	if len(userIds) == 0 {
		return map[uint64]CvHeadwear{}, nil
	}
	rows := make([]user_m.UserHeadwear, 0)
	//asc 进行覆盖,保证了updated_time 最大的是最后的输出
	if err := mysql.Db.Where("user_id IN ?", userIds).Where("end_time >= ?", time.Now()).
		Order("`using` ASC, updated_time ASC").Find(&rows).Error; err != nil {
		return nil, err
	}
	//
	resHeadwearIds := make([]uint64, 0, len(rows))
	for i, _ := range rows {
		resHeadwearIds = append(resHeadwearIds, rows[i].HeadwearId)
	}
	//获取头饰资源,然后转换成map结构
	resHeadwearMap := map[uint64]res_m.ResHeadwear{}
	resHeadwears := []res_m.ResHeadwear{}
	if err := mysql.Db.Where("id IN ?", resHeadwearIds).Find(&resHeadwears).Error; err != nil {
		return nil, err
	}
	for i, _ := range resHeadwears {
		resHeadwearMap[resHeadwears[i].ID] = resHeadwears[i]
	}
hujiebin's avatar
hujiebin committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	// cp 相关
	var model = domain.CreateModelNil()
	cpRelations := cp_m.MGetCpRelation(model, userIds)
	var m = make(map[uint64]cp_m.CpRelation)
	var cpUserIds []uint64
	for i, v := range cpRelations {
		m[v.UserId1] = cpRelations[i]
		m[v.UserId2] = cpRelations[i]
		cpUserIds = append(cpUserIds, v.UserId1)
		cpUserIds = append(cpUserIds, v.UserId2)
	}
	users, _ := user_m.GetUserMapByIds(model, cpUserIds)
	var response = make(map[uint64]string)
	for _, uid := range userIds {
		if cpRelation, ok := m[uid]; ok {
			cpUserId := cpRelation.UserId2
			if cpUserId == uid {
				cpUserId = cpRelation.UserId1
			}
			response[uid] = users[cpUserId].Avatar
		}
	}
hujiebin's avatar
hujiebin committed
159 160 161 162 163 164

	result := make(map[uint64]CvHeadwear, 0)
	for _, r := range rows {
		headwear, flag := resHeadwearMap[r.HeadwearId]
		if flag {
			result[r.UserId] = CvHeadwear{
hujiebin's avatar
hujiebin committed
165 166 167 168 169 170
				Id:               headwear.ID,
				PicUrl:           headwear.PicUrl,
				EffectUrl:        headwear.EffectUrl,
				ReverseEffectUrl: headwear.ReverseEffectUrl,
				Using:            r.Using == headwear_e.YesUsing,
				HeadwearIcon:     response[r.UserId],
hujiebin's avatar
hujiebin committed
171 172 173 174 175 176 177
			}
		}

	}
	return result, nil

}