You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.8 KiB

3 years ago
package setting
import (
"errors"
"git.oa00.com/go/mysql"
"recook/internal/libs/bean"
"recook/internal/v2/logic/command"
3 years ago
"recook/internal/v2/model"
3 years ago
"time"
)
var InviteLogic = &inviteLogic{}
type inviteLogic struct {
}
type inviteItem struct {
3 years ago
Id uint `json:"id"`
3 years ago
Code string `json:"code"`
CreateTime int64 `json:"create_time"`
EndTime int64 `json:"end_time"`
}
// Lists @Title 返回邀请码列表
func (i *inviteLogic) Lists(page bean.Page) (lists []inviteItem, total int64) {
lists = []inviteItem{}
3 years ago
var inviteCodes []model.TopCode
3 years ago
mysql.Db.Model(&inviteCodes).Count(&total)
if page.HasPage(total) {
mysql.Db.Offset(page.GetStart()).Limit(page.Limit).Find(&inviteCodes)
for _, inviteCode := range inviteCodes {
lists = append(lists, inviteItem{
3 years ago
Id: inviteCode.Id,
Code: inviteCode.Code,
CreateTime: inviteCode.CreatedTime.Unix(),
EndTime: inviteCode.EndTime.Unix(),
3 years ago
})
}
}
return
}
// Gen @Title 创建生成码
func (i *inviteLogic) Gen(nowTime string) error {
random := command.CommandLogic.Random(6)
location, err := time.ParseInLocation("2006-01-02 15:04:05", nowTime, time.Local)
if err != nil {
return errors.New("时间格式错误")
}
3 years ago
inviete := model.TopCode{
Code: random,
CreatedTime: time.Now(),
EndTime: location,
3 years ago
}
if mysql.Db.Create(&inviete).Error != nil {
return errors.New("生成失败")
}
return nil
}
// Cancellation @Title 作废邀请码
func (i *inviteLogic) Cancellation(inviteId int64) error {
3 years ago
inviete := model.TopCode{}
3 years ago
if mysql.Db.Where("id = ?", inviteId).First(&inviete).Error != nil {
return errors.New("邀请码不存在")
}
if mysql.Db.Model(&inviete).Update("end_time", time.Now()).Error != nil {
return errors.New("作废失败")
}
return nil
}