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

4 years ago
package jyy
import (
"errors"
"fmt"
"math/rand"
4 years ago
"recook/internal/define"
"recook/internal/libs/bean"
"recook/internal/service/baseCode"
3 years ago
"recook/internal/v2/model/company"
4 years ago
"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
3 years ago
Start string `json:"start"`
End string `json:"end"`
Kind uint `json:"kind"`
TaxBillType string `json:"taxBillType"`
IsEnterprise uint `json:"isEnterprise"` // 1公司 2 个人
4 years ago
}
type AppUserInfo struct {
3 years ago
Mobile string `json:"mobile"`
Nickname string `json:"nickname"`
Level uint `json:"level"`
IsOffline bool `json:"is_offline"`
CreatedAt formatime.Second `json:"created_at"`
3 years ago
TaxType string `json:"tax_type"`
3 years ago
IsEnterprise bool `json:"is_enterprise"`
4 years ago
}
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: // 云店铺
3 years ago
query = query.Where("level = 2 and is_offline = 0")
4 years ago
case 4: // vip店铺
query = query.Where("level = 2 and is_offline = 1")
3 years ago
case 5: // 合伙人
4 years ago
query = query.Where("level = 10")
}
}
3 years ago
if args.TaxType != "" {
query = query.Where("id in (?)", mysql.Db.Select("user_id").Model(&company.Info{}).Where("tax_type = ?", args.TaxType))
}
3 years ago
if args.IsEnterprise != 0 { // 判断公司还是个人
if args.IsEnterprise == 1 {
query = query.Where("is_enterprise = ? ", args.IsEnterprise)
} else {
query = query.Where("is_enterprise = ? ", 0)
}
3 years ago
}
4 years ago
}
query.Count(&total)
var temp []user.RecookUserInfoModel
3 years ago
query.Preload("CompanyInfo").Offset(args.GetStart()).Limit(args.GetLimit()).Find(&temp)
4 years ago
for _, v := range temp {
res = append(res, AppUserInfo{
3 years ago
Mobile: v.Mobile,
Nickname: v.Nickname,
Level: uint(v.Level),
IsOffline: v.IsOffline,
CreatedAt: v.CreatedAt,
3 years ago
TaxType: v.CompanyInfo.TaxType,
IsEnterprise: v.IsEnterprise,
4 years ago
})
}
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或子公司, 无法再次升级")
4 years ago
}
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))
4 years ago
var lastId int64
if err := tx.Raw(sql).Count(&lastId).Error; err != nil {
return err
}
info := user.RecookUserInfoModel{
Id: uint(lastId),
3 years ago
Nickname: "瑞库客" + mobile[7:],
4 years ago
Mobile: mobile,
Birthday: formatime.NewSecondFrom(define.DefaultBirthday),
HeadImgUrl: "",
Phone: mobile,
Identifier: time.Now().Format("060102") + rd,
4 years ago
Level: 2,
ParentID: 0,
RootID: 3238368,
4 years ago
IsOffline: true,
UpgradeTime2: formatime.NewSecondNow(),
InvitationNo: baseCode.Encode(uint64(lastId)),
4 years ago
}
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,
4 years ago
"upgrade_time1": formatime.NewSecondNow(),
"is_offline": true,
}).Error; err != nil {
return err
}
return nil
}); err != nil {
return err
}
}
return nil
}