Browse Source

addNewSubAccount

Xingyu Xian 10 months ago
parent
commit
ef16b6c23b

+ 0 - 12
db/enterprise.go

@@ -215,15 +215,3 @@ func UpdateEnterprise(ctx context.Context, EnterpriseID string, BusinessName str
 	}
 	return nil
 }
-
-// CreateEnterpriseSubAccount ToDo
-// CreateEnterpriseSubAccount 创建商家子账号
-func CreateEnterpriseSubAccount(ctx context.Context, EnterpriseID string, BusinessName string) error {
-	db := GetReadDB(ctx)
-	err := db.Model(gorm_model.Enterprise{}).Where("enterprise_id=?", EnterpriseID).Update("business_name", BusinessName).Error
-	if err != nil {
-		fmt.Println("Update Enterprise Failed!")
-		return err
-	}
-	return nil
-}

+ 56 - 0
db/sub_account.go

@@ -0,0 +1,56 @@
+package db
+
+import (
+	"context"
+	"fmt"
+	"youngee_b_api/model/gorm_model"
+)
+
+// CreateSubAccount 新建子账号
+func CreateSubAccount(ctx context.Context, account gorm_model.YounggeeSubAccount) error {
+	db := GetWriteDB(ctx)
+	err := db.Create(&account).Error
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
+// UpdateSubAccount 更新子账号
+func UpdateSubAccount(ctx context.Context, account gorm_model.YounggeeSubAccount) error {
+	db := GetWriteDB(ctx)
+	whereCondition := gorm_model.YounggeeSubAccount{SubAccountId: account.SubAccountId}
+	err := db.Model(&gorm_model.YounggeeSubAccount{}).Where(whereCondition).Updates(account).Error
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
+// DeleteSubAccount 删除子账号
+func DeleteSubAccount(ctx context.Context, account gorm_model.YounggeeSubAccount) error {
+	db := GetWriteDB(ctx)
+	whereCondition := gorm_model.YounggeeSubAccount{SubAccountId: account.SubAccountId}
+	err := db.Where(whereCondition).Delete(&gorm_model.YounggeeSubAccount{}).Error
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
+// FindSubAccountByPhone 根据手机号码查询子账号
+func FindSubAccountByPhone(ctx context.Context, phone string) (*gorm_model.YounggeeSubAccount, error) {
+	db := GetReadDB(ctx)
+	var total int64
+	var subAccount *gorm_model.YounggeeSubAccount
+	whereCondition := gorm_model.YounggeeSubAccount{PhoneNumber: phone}
+	err := db.Model(gorm_model.YounggeeSubAccount{}).Where(whereCondition).Find(&subAccount).Count(&total).Error
+	if err != nil {
+		return nil, err
+	}
+	fmt.Println(total)
+	if total == 0 {
+		return nil, err
+	}
+	return subAccount, nil
+}

+ 23 - 4
handler/addNewSubAccount.go

@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"github.com/gin-gonic/gin"
 	"youngee_b_api/model/http_model"
+	"youngee_b_api/service"
 )
 
 func WrapAddNewSubAccountHandler(ctx *gin.Context) {
@@ -13,14 +14,14 @@ func WrapAddNewSubAccountHandler(ctx *gin.Context) {
 
 func newAddNewSubAccountHandler(ctx *gin.Context) *AddNewSubAccountHandler {
 	return &AddNewSubAccountHandler{
-		req:  http_model.NewRegisterRequest(),
-		resp: http_model.NewRegisterResponse(),
+		req:  http_model.NewAddSubAccountRequest(),
+		resp: http_model.NewAddSubAccountResponse(),
 		ctx:  ctx,
 	}
 }
 
 type AddNewSubAccountHandler struct {
-	req  *http_model.RegisterRequest
+	req  *http_model.AddNewSubAccountRequest
 	resp *http_model.CommonResponse
 	ctx  *gin.Context
 }
@@ -35,7 +36,25 @@ func (h *AddNewSubAccountHandler) getResponse() interface{} {
 	return h.resp
 }
 func (h *AddNewSubAccountHandler) run() {
-	fmt.Println("AddNewSubAccountHandler Running")
+	// fmt.Println("AddNewSubAccountHandler Running")
+	newSubAccount := http_model.AddNewSubAccountRequest{}
+	newSubAccount = *h.req
+	tag, err := service.LoginAuth.SubAccountAuthCode(h.ctx, newSubAccount.PhoneNumber, newSubAccount.Code)
+	if err != nil {
+		fmt.Println(err)
+	}
+	if tag == "1" {
+		err := service.SubAccount.CreateSubAccount(h.ctx, newSubAccount)
+		if err != nil {
+			fmt.Println(err)
+			h.resp.Message = "创建失败"
+		} else {
+			h.resp.Message = "成功创建子账号"
+		}
+	} else {
+		h.resp.Message = tag
+	}
+	return
 }
 
 func (h *AddNewSubAccountHandler) checkParam() error {

+ 2 - 1
model/gorm_model/subaccount.go → model/gorm_model/sub_account.go

@@ -2,10 +2,11 @@ package gorm_model
 
 type YounggeeSubAccount struct {
 	SubAccountId   int    `gorm:"column:sub_account_id;primary_key;AUTO_INCREMENT"` // 子账号ID
-	PhoneNumber    int    `gorm:"column:phone_number"`                              // 手机号
+	PhoneNumber    string `gorm:"column:phone_number"`                              // 手机号
 	SubAccountName string `gorm:"column:sub_account_name"`                          // 子账号名称
 	JobId          int    `gorm:"column:job_id"`                                    // 岗位ID
 	EnterpriseId   string `gorm:"column:enterprise_id"`                             // 所属商家账号ID
+	AccountStatus  int    `gorm:"column:account_status"`                            // 账号状态,1为正常,2为停用
 }
 
 func (m *YounggeeSubAccount) TableName() string {

+ 2 - 3
model/gorm_model/user.go

@@ -1,4 +1,3 @@
-// Code generated by sql2gorm. DO NOT EDIT.
 package gorm_model
 
 import (
@@ -6,7 +5,7 @@ import (
 )
 
 type YounggeeUser struct {
-	ID            int64       `gorm:"column:id;primary_key;AUTO_INCREMENT"` // 用户表id
+	ID            int64     `gorm:"column:id;primary_key;AUTO_INCREMENT"` // 用户表id
 	User          string    `gorm:"column:user"`                          // 账号
 	Username      string    `gorm:"column:username"`                      // 后台用户名
 	Password      string    `gorm:"column:password"`                      // 用户密码
@@ -18,9 +17,9 @@ type YounggeeUser struct {
 	UserState     string    `gorm:"column:user_state;default:1;NOT NULL"` // 0,禁用,1,正常
 	CreatedAt     time.Time `gorm:"column:created_at"`                    // 创建时间
 	UpdatedAt     time.Time `gorm:"column:updated_at"`                    // 更新时间
+	AuthStatus    int       `gorm:"column:auth_status"`                   // 认证状态
 }
 
 func (m *YounggeeUser) TableName() string {
 	return "younggee_user"
 }
-

+ 25 - 0
model/http_model/addNewSubAccount.go

@@ -0,0 +1,25 @@
+package http_model
+
+type AddNewSubAccountRequest struct {
+	EnterpriseId   string `json:"enterprise_id"`    // 子账号属于的企业id
+	PhoneNumber    string `json:"phone_number"`     // 手机号
+	SubAccountName string `json:"sub_account_name"` // 子账号名称
+	JobId          int    `json:"job_id"`           // 岗位ID
+	Code           string `json:"code"`             // 验证码
+}
+
+type AddNewSubAccountData struct {
+	UserID       int64  `json:"user_id"`          // 用户id
+	EnterpriseId string `json:"enterprise_id"`    // 企业id
+	SubAccountID string `json:"sub_account_name"` // 子账号id
+}
+
+func NewAddSubAccountRequest() *AddNewSubAccountRequest {
+	return new(AddNewSubAccountRequest)
+}
+
+func NewAddSubAccountResponse() *CommonResponse {
+	resp := new(CommonResponse)
+	resp.Data = new(AddNewSubAccountData)
+	return resp
+}

+ 54 - 0
service/login_auth.go

@@ -53,6 +53,7 @@ func (l *loginAuth) AuthCode(ctx context.Context, phone string, code string) (st
 	user, err := db.GetUserByPhone(ctx, phone)
 	fmt.Println("login_auth", user, err)
 	if err != nil {
+		// 数据库操作错误
 		return "", err
 	} else if user == nil {
 		// 账号不存在,则注册账号
@@ -193,3 +194,56 @@ func (l *loginAuth) encryptPassword(password string) string {
 func (l *loginAuth) getRedisKey(key string) string {
 	return fmt.Sprintf("%s%s", consts.SessionRedisPrefix, key)
 }
+
+func (l *loginAuth) SubAccountAuthCode(ctx context.Context, phone string, code string) (string, error) {
+	user, err := db.FindSubAccountByPhone(ctx, phone)
+	phoneNumber := phone
+	fmt.Println("login_auth", user, err)
+	if err != nil {
+		// 数据库错误
+		return "数据库错误", err
+	} else if user == nil {
+		// 账号不存在,则判断此手机号码是否被商家主账号注册
+		user, err := db.GetUserByPhone(ctx, phoneNumber)
+		if err != nil {
+			// 数据库操作错误
+			return "", err
+		} else if user == nil {
+			// 没有被商家主账户注册,则可以注册
+			vcode, err := l.getSessionCode(ctx, phoneNumber)
+			if err != nil {
+				return "session err", err
+			}
+			fmt.Printf("缓存的验证码 vcode: %v,实际填入的 code:%v", vcode, code)
+			if vcode != code {
+				// 验证码错误
+				logrus.Debugf("[AuthCode] auth fail,phone:%+v", phone)
+				return "验证码有误", errors.New("auth fail")
+			}
+			return "1", err
+		} else if string(user.Role) != consts.BRole {
+			if user.AuthStatus == 1 {
+				// 被商家主账户注册,未认证,则可以注册
+				vcode, err := l.getSessionCode(ctx, phoneNumber)
+				if err != nil {
+					return "session err", err
+				}
+				fmt.Printf("缓存的验证码 vcode: %v,实际填入的 code:%v", vcode, code)
+				if vcode != code {
+					// 验证码错误
+					logrus.Debugf("[AuthCode] auth fail,phone:%+v", phone)
+					return "验证码有误", errors.New("auth fail")
+				}
+				return "1", err
+			} else {
+				return "主账号存在", errors.New("auth fail")
+			}
+		}
+	} else if user != nil {
+		// 子账号存在,则无法注册
+		logrus.Debugf("[AuthCode] auth fail,phone:%+v", phone)
+		return "子账号存在", errors.New("auth fail")
+	}
+	return "", nil
+
+}

+ 51 - 0
service/sub_account.go

@@ -0,0 +1,51 @@
+package service
+
+import (
+	"context"
+	"youngee_b_api/db"
+	"youngee_b_api/model/gorm_model"
+	"youngee_b_api/model/http_model"
+)
+
+var SubAccount *subaccount
+
+type subaccount struct {
+}
+
+// CreateSubAccount 新增子账号
+func (*subaccount) CreateSubAccount(ctx context.Context, request http_model.AddNewSubAccountRequest) error {
+	var newSubAccount = gorm_model.YounggeeSubAccount{
+		PhoneNumber:    request.PhoneNumber,
+		SubAccountName: request.SubAccountName,
+		JobId:          request.JobId,
+		EnterpriseId:   request.EnterpriseId,
+		AccountStatus:  1,
+	}
+	err := db.CreateSubAccount(ctx, newSubAccount)
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
+// UpdateSubAccount 修改子账号
+func (*subaccount) UpdateSubAccount(ctx context.Context, request http_model.UpdateJobRequest) error {
+	var newSubAccount = gorm_model.YounggeeSubAccount{}
+	err := db.UpdateSubAccount(ctx, newSubAccount)
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
+// DeleteSubAccount 删除子账号
+func (*subaccount) DeleteSubAccount(ctx context.Context, request http_model.DeleteJobRequest) error {
+	var newSubAccount = gorm_model.YounggeeSubAccount{
+		JobId: request.JobId,
+	}
+	err := db.DeleteSubAccount(ctx, newSubAccount)
+	if err != nil {
+		return err
+	}
+	return nil
+}