Parcourir la source

subAccountBugs

Xingyu Xian il y a 3 mois
Parent
commit
6c9a0dfcaa

+ 2 - 3
db/job.go

@@ -38,10 +38,10 @@ func DeleteJob(ctx context.Context, job gorm_model.YounggeeJob) error {
 }
 
 // FindJobByEnterpriseId 按商家ID查找岗位信息
-func FindJobByEnterpriseId(ctx context.Context, job gorm_model.YounggeeJob) ([]*gorm_model.YounggeeJob, error) {
+func FindJobByEnterpriseId(ctx context.Context, enterpriseId string) ([]*gorm_model.YounggeeJob, error) {
 	db := GetReadDB(ctx)
 	var Jobs []*gorm_model.YounggeeJob
-	whereCondition := gorm_model.YounggeeJob{EnterpriseId: job.EnterpriseId}
+	whereCondition := gorm_model.YounggeeJob{EnterpriseId: enterpriseId}
 	err := db.Model(gorm_model.YounggeeJob{}).Where(whereCondition).Find(&Jobs).Error
 	if err != nil {
 		return nil, err
@@ -72,5 +72,4 @@ func GetRewardStrategyBySelectionId(ctx context.Context, SelectionId string) ([]
 	}
 	return RewardStrategys, nil
 }
-
 */

+ 16 - 0
db/sub_account.go

@@ -54,3 +54,19 @@ func FindSubAccountByPhone(ctx context.Context, phone string) (*gorm_model.Young
 	}
 	return subAccount, nil
 }
+
+// FindSubAccountByEnterpriseId 根据商家ID查找包含的所有子账号信息
+func FindSubAccountByEnterpriseId(ctx context.Context, enterpriseId string) ([]*gorm_model.YounggeeSubAccount, int64, error) {
+	db := GetReadDB(ctx)
+	var total int64
+	var subAccount []*gorm_model.YounggeeSubAccount
+	whereCondition := gorm_model.YounggeeSubAccount{EnterpriseId: enterpriseId, SubAccountType: 1}
+	err := db.Model(gorm_model.YounggeeSubAccount{}).Where(whereCondition).Find(&subAccount).Count(&total).Error
+	if err != nil {
+		return nil, 0, err
+	}
+	if total == 0 {
+		return nil, 0, err
+	}
+	return subAccount, total, nil
+}

+ 58 - 0
handler/find_all_job.go

@@ -0,0 +1,58 @@
+package handler
+
+import (
+	"github.com/gin-gonic/gin"
+	"github.com/sirupsen/logrus"
+	log "github.com/sirupsen/logrus"
+	"youngee_b_api/consts"
+	"youngee_b_api/model/http_model"
+	"youngee_b_api/service"
+	"youngee_b_api/util"
+)
+
+func WrapFindAllJobHandler(ctx *gin.Context) {
+	handler := newFindAllJobHandler(ctx)
+	baseRun(handler)
+}
+
+func newFindAllJobHandler(ctx *gin.Context) *FindAllJobHandler {
+	return &FindAllJobHandler{
+		req:  http_model.NewFindAllJobRequest(),
+		resp: http_model.NewFindAllJobResponse(),
+		ctx:  ctx,
+	}
+}
+
+type FindAllJobHandler struct {
+	req  *http_model.FindAllJobRequest
+	resp *http_model.CommonResponse
+	ctx  *gin.Context
+}
+
+func (h *FindAllJobHandler) getRequest() interface{} {
+	return h.req
+}
+func (h *FindAllJobHandler) getContext() *gin.Context {
+	return h.ctx
+}
+func (h *FindAllJobHandler) getResponse() interface{} {
+	return h.resp
+}
+func (h *FindAllJobHandler) run() {
+	data, err := service.Job.FindJobByEnterpriseId(h.ctx, *h.req)
+	if err != nil {
+		logrus.Errorf("[FindSubAccountByEnterpriseId] call SetSession err:%+v\n", err)
+		util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, err.Error())
+		log.Info("FindSubAccountByEnterpriseId fail,req:%+v", h.req)
+		h.resp.Message = err.Error()
+		h.resp.Status = 40000
+		return
+	}
+	h.resp.Data = data
+	h.resp.Status = 20000
+	h.resp.Message = "ok"
+}
+
+func (h *FindAllJobHandler) checkParam() error {
+	return nil
+}

+ 58 - 0
handler/find_all_sub_account.go

@@ -0,0 +1,58 @@
+package handler
+
+import (
+	"github.com/gin-gonic/gin"
+	"github.com/sirupsen/logrus"
+	log "github.com/sirupsen/logrus"
+	"youngee_b_api/consts"
+	"youngee_b_api/model/http_model"
+	"youngee_b_api/service"
+	"youngee_b_api/util"
+)
+
+func WrapFindAllSubAccountHandler(ctx *gin.Context) {
+	handler := newFindAllSubAccountHandler(ctx)
+	baseRun(handler)
+}
+
+func newFindAllSubAccountHandler(ctx *gin.Context) *FindAllSubAccountHandler {
+	return &FindAllSubAccountHandler{
+		req:  http_model.NewFindAllSubAccountRequest(),
+		resp: http_model.NewFindAllSubAccountResponse(),
+		ctx:  ctx,
+	}
+}
+
+type FindAllSubAccountHandler struct {
+	req  *http_model.FindAllSubAccountRequest
+	resp *http_model.CommonResponse
+	ctx  *gin.Context
+}
+
+func (h *FindAllSubAccountHandler) getRequest() interface{} {
+	return h.req
+}
+func (h *FindAllSubAccountHandler) getContext() *gin.Context {
+	return h.ctx
+}
+func (h *FindAllSubAccountHandler) getResponse() interface{} {
+	return h.resp
+}
+func (h *FindAllSubAccountHandler) run() {
+	data, err := service.SubAccount.FindSubAccountByEnterpriseId(h.ctx, *h.req)
+	if err != nil {
+		logrus.Errorf("[FindSubAccountByEnterpriseId] call SetSession err:%+v\n", err)
+		util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, err.Error())
+		log.Info("FindSubAccountByEnterpriseId fail,req:%+v", h.req)
+		h.resp.Message = err.Error()
+		h.resp.Status = 40000
+		return
+	}
+	h.resp.Data = data
+	h.resp.Status = 20000
+	h.resp.Message = "ok"
+}
+
+func (h *FindAllSubAccountHandler) checkParam() error {
+	return nil
+}

+ 3 - 4
handler/get_user_info.go

@@ -1,14 +1,13 @@
 package handler
 
 import (
+	"github.com/gin-gonic/gin"
+	"github.com/sirupsen/logrus"
+	log "github.com/sirupsen/logrus"
 	"youngee_b_api/consts"
 	"youngee_b_api/model/http_model"
 	"youngee_b_api/service"
 	"youngee_b_api/util"
-
-	"github.com/gin-gonic/gin"
-	"github.com/sirupsen/logrus"
-	log "github.com/sirupsen/logrus"
 )
 
 func WrapGetUserInfoHandler(ctx *gin.Context) {

+ 25 - 0
model/http_model/find_all_job.go

@@ -0,0 +1,25 @@
+package http_model
+
+import "youngee_b_api/model/gorm_model"
+
+type FindAllJobRequest struct {
+	EnterpriseId string `json:"enterprise_id"` // 子账号属于的企业id
+}
+
+type FindAllJobInfo struct {
+}
+
+type FindAllJobData struct {
+	JobName []*gorm_model.YounggeeJob `json:"sub_account_info"` // 岗位信息
+	Total   int64                     `json:"total"`            // 总数
+}
+
+func NewFindAllJobRequest() *FindAllJobRequest {
+	return new(FindAllJobRequest)
+}
+
+func NewFindAllJobResponse() *CommonResponse {
+	resp := new(CommonResponse)
+	resp.Data = new(FindAllJobData)
+	return resp
+}

+ 31 - 0
model/http_model/find_all_sub_account.go

@@ -0,0 +1,31 @@
+package http_model
+
+type FindAllSubAccountRequest struct {
+	EnterpriseId string `json:"enterprise_id"` // 子账号属于的企业id
+}
+
+type FindAllSubAccountInfo struct {
+	SubAccountId   int    `json:"sub_account_id"`   // 子账号ID
+	PhoneNumber    string `json:"phone_number"`     // 手机号
+	SubAccountName string `json:"sub_account_name"` // 子账号名称
+	JobId          int    `json:"job_id"`           // 岗位ID
+	JobName        string `json:"job_name"`         // 岗位名称
+	EnterpriseId   string `json:"enterprise_id"`    // 所属商家账号ID
+	AccountStatus  int    `json:"account_status"`   // 账号状态,1为正常,2为停用
+	UserId         int    `json:"user_id"`          // 用户表中ID
+}
+
+type FindAllSubAccountData struct {
+	SubAccountInfo []*FindAllSubAccountInfo `json:"sub_account_info"` // 子账号信息列表
+	Total          int64                    `json:"total"`            // 总数
+}
+
+func NewFindAllSubAccountRequest() *FindAllSubAccountRequest {
+	return new(FindAllSubAccountRequest)
+}
+
+func NewFindAllSubAccountResponse() *CommonResponse {
+	resp := new(CommonResponse)
+	resp.Data = new(FindAllSubAccountData)
+	return resp
+}

+ 10 - 8
route/init.go

@@ -15,14 +15,16 @@ func InitRoute(r *gin.Engine) {
 	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
 	business := r.Group("/youngee")
 	{
-		business.POST("/register", handler.WrapRegisterHandler)                 // 商家主账号注册
-		business.POST("/addNewSubAccount", handler.WrapAddNewSubAccountHandler) // 商家子账号注册
-		business.POST("/addNewJob", handler.WrapaddNewJobHandler)               // 商家新增岗位
-		business.POST("/updateJob", handler.WrapupdateJobHandler)               // 商家修改岗位
-		business.POST("/deleteJob", handler.WrapdeleteJobHandler)               // 商家删除岗位
-		business.POST("/sendCode", handler.WrapSendCodeHandler)                 // 发送登录验证码
-		business.POST("/login", handler.WrapCodeLoginHandler)                   // 商家登录
-		business.POST("/getUserInfo", handler.WrapGetUserInfoHandler)           // 商家新增岗位
+		business.POST("/register", handler.WrapRegisterHandler)                   // 商家主账号注册
+		business.POST("/addNewSubAccount", handler.WrapAddNewSubAccountHandler)   // 商家子账号注册
+		business.POST("/findAllSubAccount", handler.WrapFindAllSubAccountHandler) // 查找商家全部所属子账号
+		business.POST("/findAllJob", handler.WrapFindAllJobHandler)               // 查找商家全部所属岗位
+		business.POST("/addNewJob", handler.WrapaddNewJobHandler)                 // 商家新增岗位
+		business.POST("/updateJob", handler.WrapupdateJobHandler)                 // 商家修改岗位
+		business.POST("/deleteJob", handler.WrapdeleteJobHandler)                 // 商家删除岗位
+		business.POST("/sendCode", handler.WrapSendCodeHandler)                   // 发送登录验证码
+		business.POST("/login", handler.WrapCodeLoginHandler)                     // 商家登录
+		business.POST("/getUserInfo", handler.WrapGetUserInfoHandler)             // 商家用户信息
 		business.GET("/test/ping", func(c *gin.Context) {
 			resp := http_model.CommonResponse{
 				Status:  20000,

+ 14 - 0
service/job.go

@@ -60,3 +60,17 @@ func (*job) DeleteJob(ctx context.Context, request http_model.DeleteJobRequest)
 	}
 	return nil
 }
+
+// FindJobByEnterpriseId 根据商家ID查找岗位
+func (*job) FindJobByEnterpriseId(ctx context.Context, request http_model.FindAllJobRequest) (*http_model.FindAllJobData, error) {
+	var jobNameData *http_model.FindAllJobData
+	jobNameData = &http_model.FindAllJobData{}
+	jobInfo, jobErr := db.FindJobByEnterpriseId(ctx, request.EnterpriseId)
+	if jobErr != nil {
+		return nil, jobErr
+	}
+	if jobInfo != nil {
+		jobNameData.JobName = jobInfo
+	}
+	return nil, nil
+}

+ 11 - 9
service/login_auth.go

@@ -281,19 +281,21 @@ func (l *loginAuth) getRedisKey(key string) string {
 }
 
 func (l *loginAuth) SubAccountAuthCode(ctx context.Context, phone string, code string) (string, error) {
-	user, err := db.FindSubAccountByPhone(ctx, phone)
+	bSubAccountUser, err := db.FindSubAccountByPhone(ctx, phone)
 	phoneNumber := phone
-	fmt.Println("login_auth", user, err)
+	fmt.Println("login_auth", bSubAccountUser, err)
 	if err != nil {
 		// 数据库错误
 		return "数据库错误", err
-	} else if user == nil {
+	}
+	if bSubAccountUser == nil {
 		// 账号不存在,则判断此手机号码是否被商家主账号注册
-		user, err := db.GetUserByPhone(ctx, phoneNumber)
+		bUser, err := db.GetUserByPhone(ctx, phoneNumber)
 		if err != nil {
 			// 数据库操作错误
 			return "", err
-		} else if user == nil {
+		}
+		if bUser == nil {
 			// 没有被商家主账户注册,则可以注册
 			vcode, err := l.getSessionCode(ctx, phoneNumber)
 			if err != nil {
@@ -306,8 +308,8 @@ func (l *loginAuth) SubAccountAuthCode(ctx context.Context, phone string, code s
 				return "验证码有误", errors.New("auth fail")
 			}
 			return "1", err
-		} else if string(user.Role) == consts.BRole {
-			if user.AuthStatus == 1 {
+		} else if string(bUser.Role) == consts.BRole {
+			if bUser.AuthStatus == 1 {
 				// 被商家主账户注册,未认证,则可以注册
 				vcode, err := l.getSessionCode(ctx, phoneNumber)
 				if err != nil {
@@ -324,10 +326,10 @@ func (l *loginAuth) SubAccountAuthCode(ctx context.Context, phone string, code s
 				return "主账号存在", errors.New("auth fail")
 			}
 		}
-	} else if user != nil {
+	} else {
 		// 子账号存在,则无法注册
 		logrus.Debugf("[AuthCode] auth fail,phone:%+v", phone)
-		return "子账号存在", errors.New("auth fail")
+		return "子账号存在", errors.New("subAccount exist")
 	}
 	return "", nil
 

+ 39 - 1
service/sub_account.go

@@ -51,7 +51,7 @@ func (*subaccount) CreateSubAccount(ctx context.Context, request http_model.AddN
 	return nil
 }
 
-// UpdateSubAccount 修改子账号
+// UpdateSubAccount 修改子账号信息
 func (*subaccount) UpdateSubAccount(ctx context.Context, request http_model.UpdateJobRequest) error {
 	var newSubAccount = gorm_model.YounggeeSubAccount{}
 	err := db.UpdateSubAccount(ctx, newSubAccount)
@@ -72,3 +72,41 @@ func (*subaccount) DeleteSubAccount(ctx context.Context, request http_model.Dele
 	}
 	return nil
 }
+
+// FindSubAccountByEnterpriseId 根据商家ID查找包含的所有子账号信息
+func (*subaccount) FindSubAccountByEnterpriseId(ctx context.Context, request http_model.FindAllSubAccountRequest) (*http_model.FindAllSubAccountData, error) {
+	var subAccountResp *http_model.FindAllSubAccountData
+	subAccountResp = &http_model.FindAllSubAccountData{}
+
+	// 1. 取出子账号基本信息
+	newSubAccount, total, subaccountErr := db.FindSubAccountByEnterpriseId(ctx, request.EnterpriseId)
+	if subaccountErr != nil {
+		return nil, subaccountErr
+	}
+	if newSubAccount != nil {
+		for _, s := range newSubAccount {
+			var subAccountInfo *http_model.FindAllSubAccountInfo
+			subAccountInfo = &http_model.FindAllSubAccountInfo{}
+			// fmt.Println("s:", s.SubAccountId)
+			subAccountInfo.SubAccountId = s.SubAccountId
+			subAccountInfo.SubAccountName = s.SubAccountName
+			subAccountInfo.JobId = s.JobId
+			subAccountInfo.UserId = s.UserId
+			subAccountInfo.PhoneNumber = s.PhoneNumber
+			subAccountInfo.EnterpriseId = s.EnterpriseId
+			subAccountInfo.AccountStatus = s.AccountStatus
+
+			// 2. 岗位信息
+			jobInfo, jobErr := db.FindJobByJobId(ctx, s.JobId)
+			if jobErr != nil {
+				return nil, jobErr
+			}
+			if jobInfo != nil {
+				subAccountInfo.JobName = jobInfo.JobName
+			}
+			subAccountResp.SubAccountInfo = append(subAccountResp.SubAccountInfo, subAccountInfo)
+		}
+	}
+	subAccountResp.Total = total
+	return subAccountResp, nil
+}