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.

184 lines
4.7 KiB

package upgrade
import (
"fmt"
"math/rand"
"recook/internal/define"
"recook/internal/libs/bean"
"recook/internal/service/baseCode"
"recook/internal/v2/model/recook/user"
"recook/internal/v2/model/upgrade"
"strings"
"time"
"git.oa00.com/go/mysql"
"github.com/golangkit/formatime"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type logic struct {
}
var Logic = logic{}
type ApplyList struct {
bean.Page
State upgrade.State `json:"state" validate:"oneof=0 1 2 3"`
Mobile string `json:"mobile"`
ApplyUserName string `json:"apply_user_name"`
Kind uint `json:"kind"`
}
func (o logic) List(args ApplyList) (res []upgrade.ApplyEntry, total int64, err error) {
query := mysql.Db.Table((&upgrade.ApplyEntry{}).TableName())
{
if args.State != 0 {
query = query.Where("state = ?", args.State)
}
if args.Mobile != "" {
query = query.Where("mobile = ?", args.Mobile)
}
if args.ApplyUserName != "" {
sub := mysql.Db.Table((&user.RecookUserInfoModel{}).TableName()).
Select("id").Where("nickname like ?", fmt.Sprintf("%%%s%%", args.ApplyUserName))
query = query.Where("apply_user_id in (?)", sub)
}
if args.Kind != 0 {
query = query.Where("kind=?", args.Kind)
}
}
query.Count(&total)
query.Preload(clause.Associations).Limit(args.GetLimit()).Offset(args.GetStart()).Find(&res)
return
}
type ApplySure struct {
ID uint `json:"id" validate:"required"`
UserID uint `json:"-"`
}
func (o logic) Sure(args ApplySure) error {
var ae upgrade.ApplyEntry
if err := mysql.Db.First(&ae, "id = ?", args.ID).Error; err != nil {
return err
}
var mu user.RecookUserInfoModel
mysql.Db.First(&mu, "mobile=?", ae.Mobile)
if mu.Level > 2 {
return nil
}
mobile := ae.Mobile
if len(mobile) != 11 || !strings.HasPrefix(mobile, "1") {
return fmt.Errorf("手机格式不正确")
}
offline := true
if ae.Kind == 1 {
offline = false
}
if mu.Id == 0 {
// 没有就创建
if err := mysql.Db.Transaction(func(tx *gorm.DB) error {
sql := `select max(id)+floor(1+rand()*5) from recook_user_info`
rd := fmt.Sprintf("%06v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(1000000))
var lastId int64
if err := tx.Raw(sql).Count(&lastId).Error; err != nil {
return err
}
info := user.RecookUserInfoModel{
Id: uint(lastId),
Nickname: "瑞库客" + mobile[7:],
Mobile: mobile,
Birthday: formatime.NewSecondFrom(define.DefaultBirthday),
HeadImgUrl: "",
Phone: mobile,
Identifier: time.Now().Format("060102") + rd,
Level: 2,
ParentID: 0,
RootID: ae.ParentID,
IsOffline: offline,
UpgradeTime2: formatime.NewSecondNow(),
InvitationNo: baseCode.Encode(uint64(lastId)),
}
if err := tx.Create(&info).Error; err != nil {
tx.Rollback()
err = fmt.Errorf("创建用户错误101:" + err.Error())
return err
}
// 生成邀请码和推荐码
info.InvitationNo = baseCode.Encode(uint64(info.Id))
if err := tx.Save(&info).Error; err != nil {
err = fmt.Errorf("创建用户错误102:" + err.Error())
return err
}
// 建立钱包
wallet := user.RecookUserWalletModel{
UserId: info.Id,
}
if err := tx.Create(&wallet).Error; err != nil {
tx.Rollback()
err = fmt.Errorf("创建用户错误103:" + err.Error())
return err
}
if err := tx.Table((&upgrade.ApplyEntry{}).TableName()).Where("id = ?", args.ID).Updates(upgrade.ApplyEntry{
State: 2,
ProcessTime: formatime.NewSecondNow(),
ProcessUserID: args.UserID,
}).Error; err != nil {
return err
}
return nil
}); err != nil {
return err
}
} else {
if err := mysql.Db.Transaction(func(tx *gorm.DB) error {
if err := tx.Table((&upgrade.ApplyEntry{}).TableName()).Where("id = ?", args.ID).Updates(upgrade.ApplyEntry{
State: 2,
ProcessTime: formatime.NewSecondNow(),
ProcessUserID: args.UserID,
}).Error; err != nil {
return err
}
if err := tx.Model(&mu).Updates(map[string]interface{}{
"level": 2,
"parent_id": 0,
"root_id": ae.ParentID,
"upgrade_time1": formatime.NewSecondNow(),
"is_offline": offline,
}).Error; err != nil {
return err
}
return nil
}); err != nil {
return err
}
}
return nil
}
type ApplyReject struct {
ApplySure
Reason string `json:"reason" validate:"required,lte=20"`
}
func (o logic) Reject(args ApplyReject) error {
mysql.Db.Table((&upgrade.ApplyEntry{}).TableName()).Where("id = ?", args.ID).Updates(
upgrade.ApplyEntry{
State: 3,
Reason: args.Reason,
ProcessTime: formatime.NewSecondNow(),
ProcessUserID: args.UserID,
})
return nil
}