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.9 KiB
184 lines
4.9 KiB
package upgrade
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"recook/internal/cache"
|
|
"recook/internal/v2/model/recook/user"
|
|
"recook/internal/v2/model/upgrade"
|
|
"strings"
|
|
|
|
"git.oa00.com/go/mysql"
|
|
"github.com/golangkit/formatime"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type logic struct {
|
|
}
|
|
|
|
var Logic = logic{}
|
|
|
|
type ApplyList struct {
|
|
LastID uint `json:"last_id"`
|
|
Size int `json:"size" validate:"required,lte=10"`
|
|
UserID uint `json:"-"`
|
|
State upgrade.State `json:"state"`
|
|
}
|
|
|
|
func (o logic) List(args ApplyList) (res []upgrade.ApplyEntry) {
|
|
query := mysql.Db.Table((&upgrade.ApplyEntry{}).TableName()).Where("apply_user_id = ?", args.UserID)
|
|
{
|
|
if args.LastID != 0 {
|
|
query = query.Where("id < ?", args.LastID)
|
|
}
|
|
if args.State != 0 {
|
|
query = query.Where("state = ?", args.State)
|
|
}
|
|
}
|
|
query.Order("id desc").Limit(args.Size).Find(&res)
|
|
return
|
|
}
|
|
|
|
type ApplyCreate struct {
|
|
Mobile string `json:"mobile" validate:"required"`
|
|
UserID uint `json:"-"`
|
|
Kind uint `json:"kind" validate:"required,oneof=1 2"`
|
|
Code string `json:"code" validate:"required"`
|
|
BusinessPhoto string `json:"business_photo"`
|
|
MainPhoto string `json:"main_photo"`
|
|
Address string `json:"address"`
|
|
}
|
|
|
|
func (o logic) Create(args ApplyCreate) error {
|
|
var u1 user.RecookUserInfoModel
|
|
mysql.Db.First(&u1, "id = ?", args.UserID)
|
|
|
|
if u1.Level < 2 {
|
|
return errors.New("无法发起申请")
|
|
}
|
|
|
|
// if args.Kind == 1 && u1.Level < 2 {
|
|
// return errors.New("无法发起云体店申请")
|
|
// }
|
|
|
|
if args.Kind == 2 && (u1.Level != 10 && !u1.IsOffline) {
|
|
return errors.New("无法发起实体店申请")
|
|
}
|
|
|
|
if len(args.Mobile) != 11 || !strings.HasPrefix(args.Mobile, "1") {
|
|
return fmt.Errorf("手机格式不正确")
|
|
}
|
|
|
|
var u2 user.RecookUserInfoModel
|
|
mysql.Db.First(&u2, "mobile = ?", args.Mobile)
|
|
if u2.Level >= 2 {
|
|
return errors.New("用户已是店铺")
|
|
}
|
|
flag := u2.Id == 0 || u2.ParentID == u1.Id || (u2.Level == 0 && u2.ParentID == 0)
|
|
if !flag {
|
|
return errors.New("用户链路不正确")
|
|
}
|
|
|
|
parentID := u1.Id
|
|
if u1.Level != 10 {
|
|
parentID = u1.RootID
|
|
}
|
|
state := upgrade.Apply
|
|
// if args.Kind == 1 {
|
|
// state = upgrade.Suc
|
|
// }
|
|
if args.Code != cache.GetUpgradeSMSCode(u1.Id, args.Mobile) {
|
|
return fmt.Errorf("验证码不正确")
|
|
}
|
|
if err := mysql.Db.Transaction(func(tx *gorm.DB) error {
|
|
// now := formatime.NewSecondNow()
|
|
// if args.Kind == 2 {
|
|
// now = formatime.Second{}
|
|
// }
|
|
obj := upgrade.ApplyEntry{
|
|
Mobile: args.Mobile,
|
|
ApplyUserID: args.UserID,
|
|
Kind: upgrade.Kind(args.Kind),
|
|
CreatedAt: formatime.NewSecondNow(),
|
|
State: state,
|
|
BusinessAddress: args.Address,
|
|
BusinessPhoto: args.BusinessPhoto,
|
|
MainPhoto: args.MainPhoto,
|
|
ParentID: parentID,
|
|
// ProcessTime: now,
|
|
}
|
|
if err := tx.Create(&obj).Error; err != nil {
|
|
return err
|
|
}
|
|
// 需求变更 审核通过
|
|
// if args.Kind == 1 {
|
|
// var u1 user.RecookUserInfoModel
|
|
// if err := tx.First(&u1, "mobile = ?", args.Mobile).Error; err != nil {
|
|
// if err != gorm.ErrRecordNotFound {
|
|
// return err
|
|
// }
|
|
// }
|
|
// if u1.Id == 0 {
|
|
// mobile := args.Mobile
|
|
// sql := `select max(id)+floor(1+rand()*5) from recook_user_info`
|
|
// 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") + baseCode.Encode(uint64(lastId)),
|
|
// Level: 2,
|
|
// ParentID: 0,
|
|
// RootID: parentID,
|
|
// UpgradeTime2: formatime.NewSecondNow(),
|
|
// }
|
|
|
|
// 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
|
|
// }
|
|
// } else {
|
|
// if u1.Level < 2 {
|
|
// if err := tx.Model(&u1).Updates(map[string]interface{}{
|
|
// "level": 2,
|
|
// "root_id": parentID,
|
|
// "parent_id": 0,
|
|
// "upgrade_time2": formatime.NewSecondNow(),
|
|
// }).Error; err != nil {
|
|
// return err
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
return nil
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|