headwear.go 6.8 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 65 66 67 68 69 70 71 72 73 74 75 76 77 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 105 106 107 108 109 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
package user_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
	"gorm.io/gorm"
	"hilo-group/_const/enum/headwear_e"
	"hilo-group/domain/model/res_m"
	"hilo-group/myerr"
	"hilo-group/myerr/bizerr"
	"strconv"
	"time"
)

//用户头饰
type UserHeadwear struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	UserId        mysql.ID
	HeadwearId    mysql.ID
	EndTime       time.Time
	Using         headwear_e.UserHeadwearUsing
}

//获取用户头饰,不存在则新建
func GetUserHeadwearOrInit(model *domain.Model, userId mysql.ID, headwearId mysql.ID) (*UserHeadwear, error) {
	//检查headwearId是否存在/有效
	if flag, err := res_m.CheckHeadwearValidById(model, headwearId); err != nil {
		return nil, err
	} else {
		if flag == false {
			return nil, myerr.NewSysError("headwearId: " + strconv.FormatUint(headwearId, 10) + " 无效")
		}
	}

	userHeadwear := UserHeadwear{}
	if err := model.Db.Model(&UserHeadwear{}).Where(&UserHeadwear{
		UserId:     userId,
		HeadwearId: headwearId,
	}).First(&userHeadwear).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return &UserHeadwear{
				Model:      model,
				UserId:     userId,
				HeadwearId: headwearId,
				EndTime:    time.Now(),
			}, nil
		} else {
			return nil, myerr.WrapErr(err)
		}
	}
	userHeadwear.Model = model
	return &userHeadwear, nil
}

func GetUserHeadwearOrErr(model *domain.Model, userId mysql.ID, headwearId mysql.ID) (*UserHeadwear, error) {
	userHeadwear := UserHeadwear{}
	if err := model.Db.Model(&UserHeadwear{}).Where(&UserHeadwear{
		UserId:     userId,
		HeadwearId: headwearId,
	}).First(&userHeadwear).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	userHeadwear.Model = model
	return &userHeadwear, nil
}

func GetUserHeadwearUsing(model *domain.Model, userId mysql.ID) (*UserHeadwear, error) {
	userHeadwear := UserHeadwear{}
	if err := model.Db.Model(&UserHeadwear{}).Where(&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)
		}
	}
	userHeadwear.Model = model
	return &userHeadwear, nil
}

func BatchGetUserHeadwearUsing(model *domain.Model, userIds []mysql.ID) (map[uint64]UserHeadwear, error) {
	userHeadwears := []UserHeadwear{}
	if err := model.Db.Model(&UserHeadwear{}).Where("end_time > ?", time.Now()).Where("user_id in (?)", userIds).Order("`using` ASC, updated_time ASC").Find(&userHeadwears).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		} else {
			return nil, myerr.WrapErr(err)
		}
	}
	userHeadwearMap := map[uint64]UserHeadwear{}
	for i, r := range userHeadwears {
		userHeadwears[i].Model = model
		userHeadwearMap[r.UserId] = userHeadwears[i]
	}
	return userHeadwearMap, nil
}

func (userHeadwear *UserHeadwear) Give(receiveUserId mysql.ID) (*UserHeadwear, *UserHeadwear, uint32, error) {
	remainSecond := userHeadwear.EndTime.Unix() - time.Now().Unix()
	if remainSecond <= 0 {
		return nil, nil, 0, bizerr.UserHeadwearHasEnd
	}
	receiveUserHeadwear, err := GetUserHeadwearOrInit(userHeadwear.Model, receiveUserId, userHeadwear.HeadwearId)
	if err != nil {
		return nil, nil, 0, err
	}

	receiveUserHeadwear, _, err = receiveUserHeadwear.AddEndTime(headwear_e.Give, uint32(remainSecond), userHeadwear.UserId)
	if err != nil {
		return nil, nil, 0, err
	}

	_, err = addUserHeadwearLog(userHeadwear.Model, userHeadwear.UserId, userHeadwear.HeadwearId, headwear_e.Give, headwear_e.Del, nil, nil, userHeadwear.UserId)
	if err != nil {
		return nil, nil, 0, err
	}
	userHeadwear.SetDel()
	return userHeadwear, receiveUserHeadwear, uint32(remainSecond), nil
}

//设置为使用中
func (userHeadwear *UserHeadwear) SetUsing() (*UserHeadwear, error) {
	if err := resetAllUserHeadwearNoUsing(userHeadwear.Model, userHeadwear.UserId); err != nil {
		return nil, err
	}
	userHeadwear.Using = headwear_e.YesUsing
	return userHeadwear, nil
}

//增加结束时间
func (userHeadwear *UserHeadwear) AddEndTime(t headwear_e.UserHeadwearLogOrginType, second uint32, operateUserId mysql.ID) (*UserHeadwear, mysql.ID, error) {
	logId, err := addUserHeadwearLog(userHeadwear.Model, userHeadwear.UserId, userHeadwear.HeadwearId, t, headwear_e.AddSecond, &second, nil, operateUserId)
	if err != nil {
		return nil, 0, myerr.WrapErr(err)
	}
	//if err := resetAllUserHeadwearNoUsing(userHeadwear.Model, userHeadwear.UserId); err != nil {
	//	return nil, logId, err
	//}
	nowTime := time.Now()
	if userHeadwear.EndTime.After(nowTime) {
		nowTime = userHeadwear.EndTime
	}
	userHeadwear.EndTime = nowTime.Add(time.Duration(second) * time.Second)
	return userHeadwear, logId, nil
}

//修改结束时间
func (userHeadwear *UserHeadwear) UpdateEndTime(t headwear_e.UserHeadwearLogOrginType, time time.Time, operateUserId mysql.ID) (*UserHeadwear, mysql.ID, error) {
	logId, err := addUserHeadwearLog(userHeadwear.Model, userHeadwear.UserId, userHeadwear.HeadwearId, t, headwear_e.UpdateEndTime, nil, &time, operateUserId)
	if err != nil {
		return nil, 0, myerr.WrapErr(err)
	}
	//if err := resetAllUserHeadwearNoUsing(userHeadwear.Model, userHeadwear.UserId); err != nil {
	//	return nil, logId, nil
	//}
	userHeadwear.EndTime = time
	return userHeadwear, logId, nil
}

//删除
func (userHeadwear *UserHeadwear) MgrDel(mgrId mysql.ID) (*UserHeadwear, mysql.ID, error) {
	logId, err := addUserHeadwearLog(userHeadwear.Model, userHeadwear.UserId, userHeadwear.HeadwearId, headwear_e.Mgr, headwear_e.Del, nil, nil, mgrId)
	if err != nil {
		return nil, 0, myerr.WrapErr(err)
	}
	userHeadwear.SetDel()
	return userHeadwear, logId, nil
}

//重置所有的座驾均为不使用状态
func resetAllUserHeadwearNoUsing(model *domain.Model, userId mysql.ID) error {
	if err := model.Db.Model(&UserHeadwear{}).Where(&UserHeadwear{
		UserId: userId,
	}).UpdateColumn("using", headwear_e.NoUsing).Error; err != nil {
		return myerr.WrapErr(err)
	}
	return nil
}

//用户装饰日志
type UserHeadwearLog struct {
	mysql.Entity
	UserId        mysql.ID
	OperateUserId mysql.ID
	HeadwearId    mysql.ID
	OriginType    headwear_e.UserHeadwearLogOrginType
	Type          headwear_e.UserHeadwearLogType
	AddSecond     *mysql.Num
	UpdateEndTime *time.Time
}

//增加修改日志
func addUserHeadwearLog(model *domain.Model, userId mysql.ID, headwearId mysql.ID, originType headwear_e.UserHeadwearLogOrginType, t headwear_e.UserHeadwearLogType, addSecond *uint32, UpdateEndTime *time.Time, operateUserId mysql.ID) (mysql.ID, error) {
	userHeadwearLog := UserHeadwearLog{
		UserId:        userId,
		OperateUserId: operateUserId,
		HeadwearId:    headwearId,
		OriginType:    originType,
		Type:          t,
		AddSecond:     addSecond,
		UpdateEndTime: UpdateEndTime,
	}
	if err := model.Db.Create(&userHeadwearLog).Error; err != nil {
		return 0, myerr.WrapErr(err)
	}
	return userHeadwearLog.ID, nil
}