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
71 lines
1.8 KiB
package setting
|
|
|
|
import (
|
|
"errors"
|
|
"git.oa00.com/go/mysql"
|
|
"recook/internal/libs/bean"
|
|
"recook/internal/v2/logic/command"
|
|
"recook/internal/v2/model"
|
|
"time"
|
|
)
|
|
|
|
var InviteLogic = &inviteLogic{}
|
|
|
|
type inviteLogic struct {
|
|
}
|
|
type inviteItem struct {
|
|
Id uint `json:"id"`
|
|
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{}
|
|
var inviteCodes []model.TopCode
|
|
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{
|
|
Id: inviteCode.Id,
|
|
Code: inviteCode.Code,
|
|
CreateTime: inviteCode.CreatedTime.Unix(),
|
|
EndTime: inviteCode.EndTime.Unix(),
|
|
})
|
|
}
|
|
}
|
|
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("时间格式错误")
|
|
}
|
|
inviete := model.TopCode{
|
|
Code: random,
|
|
CreatedTime: time.Now(),
|
|
EndTime: location,
|
|
}
|
|
if mysql.Db.Create(&inviete).Error != nil {
|
|
return errors.New("生成失败")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Cancellation @Title 作废邀请码
|
|
func (i *inviteLogic) Cancellation(inviteId int64) error {
|
|
inviete := model.TopCode{}
|
|
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
|
|
}
|