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.
178 lines
4.8 KiB
178 lines
4.8 KiB
package jyy
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"math/rand"
|
|
"recook/internal/define"
|
|
"recook/internal/libs/bean"
|
|
"recook/internal/service/baseCode"
|
|
"recook/internal/v2/model/company"
|
|
"recook/internal/v2/model/recook/user"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.oa00.com/go/mysql"
|
|
"github.com/golangkit/formatime"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type AppUserReq struct {
|
|
bean.Page
|
|
AppUserInfo
|
|
Start string `json:"start"`
|
|
End string `json:"end"`
|
|
Kind uint `json:"kind"`
|
|
TaxBillType string `json:"taxBillType"`
|
|
IsEnterprise uint `json:"isEnterprise"` // 1公司 2 个人
|
|
}
|
|
|
|
type AppUserInfo struct {
|
|
Mobile string `json:"mobile"`
|
|
Nickname string `json:"nickname"`
|
|
Level uint `json:"level"`
|
|
IsOffline bool `json:"is_offline"`
|
|
CreatedAt formatime.Second `json:"created_at"`
|
|
TaxType string `json:"tax_type"`
|
|
IsEnterprise bool `json:"is_enterprise"`
|
|
}
|
|
|
|
func (o logic) AppUser(args AppUserReq) (res []AppUserInfo, total int64, err error) {
|
|
query := mysql.Db.Table((&user.RecookUserInfoModel{}).TableName()).Where("LENGTH(mobile) =11")
|
|
{
|
|
if args.Mobile != "" {
|
|
query = query.Where("mobile = ?", args.Mobile)
|
|
}
|
|
|
|
if args.Nickname != "" {
|
|
query = query.Where("nickname = ?", args.Nickname)
|
|
}
|
|
if args.Start != "" {
|
|
query = query.Where("created_at > ?", args.Start)
|
|
}
|
|
if args.End != "" {
|
|
query = query.Where("created_at < ?", args.End)
|
|
}
|
|
if args.Kind != 0 {
|
|
switch args.Kind {
|
|
case 1: // 会员
|
|
query = query.Where("level = 0")
|
|
case 2: // 店主
|
|
query = query.Where("level = 1")
|
|
case 3: // 云店铺
|
|
query = query.Where("level = 2 and is_offline = 0")
|
|
case 4: // vip店铺
|
|
query = query.Where("level = 2 and is_offline = 1")
|
|
case 5: // 合伙人
|
|
query = query.Where("level = 10")
|
|
}
|
|
}
|
|
if args.TaxType != "" {
|
|
query = query.Where("id in (?)", mysql.Db.Select("user_id").Model(&company.Info{}).Where("tax_type = ?", args.TaxType))
|
|
}
|
|
if args.IsEnterprise != 0 { // 判断公司还是个人
|
|
if args.IsEnterprise == 1 {
|
|
query = query.Where("is_enterprise = ? ", args.IsEnterprise)
|
|
} else {
|
|
query = query.Where("is_enterprise = ? ", 0)
|
|
}
|
|
}
|
|
}
|
|
query.Count(&total)
|
|
var temp []user.RecookUserInfoModel
|
|
query.Preload("CompanyInfo").Offset(args.GetStart()).Limit(args.GetLimit()).Find(&temp)
|
|
for _, v := range temp {
|
|
res = append(res, AppUserInfo{
|
|
Mobile: v.Mobile,
|
|
Nickname: v.Nickname,
|
|
Level: uint(v.Level),
|
|
IsOffline: v.IsOffline,
|
|
CreatedAt: v.CreatedAt,
|
|
TaxType: v.CompanyInfo.TaxType,
|
|
IsEnterprise: v.IsEnterprise,
|
|
})
|
|
}
|
|
return
|
|
}
|
|
|
|
func (o logic) Vip(args AppUserReq) error {
|
|
|
|
if len(args.Mobile) != 11 || !strings.HasPrefix(args.Mobile, "1") {
|
|
return errors.New("手机格式异常")
|
|
}
|
|
var u user.RecookUserInfoModel
|
|
mysql.Db.First(&u, "mobile = ?", args.Mobile)
|
|
if u.Level > 2 {
|
|
return errors.New("该账号身份已经是vip或子公司, 无法再次升级")
|
|
}
|
|
mobile := args.Mobile
|
|
if u.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: 3238368,
|
|
IsOffline: true,
|
|
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
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
} else {
|
|
if err := mysql.Db.Transaction(func(tx *gorm.DB) error {
|
|
if err := tx.Model(&u).Updates(map[string]interface{}{
|
|
"level": 2,
|
|
"parent_id": 0,
|
|
"root_id": 3238368,
|
|
"upgrade_time1": formatime.NewSecondNow(),
|
|
"is_offline": true,
|
|
}).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|