diff --git a/domain/model/groupPower_m/family.go b/domain/model/groupPower_m/family.go index d274e3661530b6d5f8a8ad21c00c25513f09b957..97d416ce8b616632c5f84efd78f0d5fa4b2788e5 100644 --- a/domain/model/groupPower_m/family.go +++ b/domain/model/groupPower_m/family.go @@ -45,3 +45,19 @@ func (gpu *GroupPowerUser) GetBy(model *domain.Model, pageSize, pageIndex int) ( } return rows, nil } + +func (gpu *GroupPowerUser) GetGroupPowerUser(model *domain.Model) (*GroupPowerUser, error) { + rows := make([]*GroupPowerUser, 0) + err := model.Db.Where(gpu).Find(&rows).Error + if err != nil { + return nil, err + } + if len(rows) == 0 { + return nil, nil + } + return rows[0], nil +} + +func (gpu *GroupPowerUser) Create(db *gorm.DB) error { + return db.Create(gpu).Error +} diff --git a/domain/model/groupPower_m/group_power_apply.go b/domain/model/groupPower_m/group_power_apply.go index 8247eb7e7963f5e947ec96f70bb611e3e71e8af9..175752635ae27701983e168e15d0880a087eaabc 100644 --- a/domain/model/groupPower_m/group_power_apply.go +++ b/domain/model/groupPower_m/group_power_apply.go @@ -1,7 +1,7 @@ package groupPower_m import ( - "git.hilo.cn/hilo-common/resource/mysql" + "git.hilo.cn/hilo-common/domain" "gorm.io/gorm" "hilo-group/myerr" "time" @@ -16,20 +16,20 @@ type GroupPowerApplyJoin struct { IsAccept int8 `json:"is_accept"` } -func InsertGroupPowerApplyJoin(userId, familyId uint64) error { +func InsertGroupPowerApplyJoin(model *domain.Model, userId, familyId uint64) error { nowTime := time.Now() sql := "insert into group_power_apply_join(user_id,group_power_id,is_accept,created_time,updated_time) " + "value(?,?,?,?,?) on duplicate key update is_accept=?,created_time=?,updated_time=?" - err := mysql.Db.Exec(sql, userId, familyId, 0, nowTime, nowTime, 0, nowTime, nowTime).Error + err := model.Db.Exec(sql, userId, familyId, 0, nowTime, nowTime, 0, nowTime, nowTime).Error if err != nil { return myerr.WrapErr(err) } return nil } -func GetGroupPowerApplyJoin(userId, familyId uint64) (*GroupPowerApplyJoin, error) { +func GetGroupPowerApplyJoin(model *domain.Model, userId, familyId uint64) (*GroupPowerApplyJoin, error) { res := new(GroupPowerApplyJoin) - err := mysql.Db.Where(GroupPowerApplyJoin{UserId: userId, GroupPowerId: familyId}).Where("is_accept=0").First(&res).Error + err := model.Db.Where(GroupPowerApplyJoin{UserId: userId, GroupPowerId: familyId}).Where("is_accept=0").First(&res).Error if err != nil { if err == gorm.ErrRecordNotFound { return nil, nil @@ -40,8 +40,16 @@ func GetGroupPowerApplyJoin(userId, familyId uint64) (*GroupPowerApplyJoin, erro return res, nil } -func AcceptGroupPowerApplyJoin(userId, familyId uint64) error { - err := mysql.Db.Exec("update group_power_apply_join set is_accept = 1 where user_id=? and group_power_id=?", userId, familyId).Error +func AcceptGroupPowerApplyJoinById(model *domain.Model, id uint64) error { + err := model.Db.Exec("update group_power_apply_join set is_accept = 1 where id=?", id).Error + if err != nil { + return myerr.WrapErr(err) + } + return nil +} + +func AcceptGroupPowerApplyJoin(model *domain.Model, userId, familyId uint64) error { + err := model.Db.Exec("update group_power_apply_join set is_accept = 1 where user_id=? and group_power_id=?", userId, familyId).Error if err != nil { return myerr.WrapErr(err) } diff --git a/myerr/bizerr/bizCode.go b/myerr/bizerr/bizCode.go index df81d337e88c5a7f21bc136540505eb74899e062..664624a0aed30423b4ec0a08ef156ef62b5f4ea4 100644 --- a/myerr/bizerr/bizCode.go +++ b/myerr/bizerr/bizCode.go @@ -81,6 +81,9 @@ var ( GroupPowerDealerCanNotSole = myerr.NewBusinessCode(15008, "This user already has a family agent and cannot sell diamonds", myerr.BusinessData{}) // 代理转账失败-此用户已有家族代理,不能出售钻石 GroupPowerDealerCanNotBuy = myerr.NewBusinessCode(15009, "Buy diamonds from your family agent", myerr.BusinessData{}) // 请向本家族代理购买钻石 GroupPowerDealerNeedJoin = myerr.NewBusinessCode(15010, "Please join the agent's family to purchase", myerr.BusinessData{}) // 请加入代理的家族后进行购买 + GroupPowerHaveNoJoin = myerr.NewBusinessCode(15011, "You have no joined power", myerr.BusinessData{}) // 还未加入国家势力 + GroupPowerHaveNoPower = myerr.NewBusinessCode(15012, "You have no power", myerr.BusinessData{}) // 没有权限 + GroupPowerHaveNoApply = myerr.NewBusinessCode(15013, "User have no apply", myerr.BusinessData{}) // 没有找到申请 TaskHasAward = myerr.NewBusinessCode(19001, "task has award", myerr.BusinessData{}) diff --git a/route/group_power_r/group_power.go b/route/group_power_r/group_power.go index 1373cda1b5e2c71ac83b31560208e2102a638f12..04a3ab997bac8a8d1a80f07dc70d8ddf708f01d6 100644 --- a/route/group_power_r/group_power.go +++ b/route/group_power_r/group_power.go @@ -852,7 +852,7 @@ func GroupPowerMembers(c *gin.Context) (*mycontext.MyContext, error) { // @Summary 申请加入家族 // @Param id formData int true "家族id" // @Success 200 -// @Router /v1/groupPower/applyJoin [get] +// @Router /v1/groupPower/applyJoin [post] func GroupPowerApplyJoin(c *gin.Context) (*mycontext.MyContext, error) { myContext := mycontext.CreateMyContext(c.Keys) userId, err := req.GetUserId(c) @@ -885,7 +885,66 @@ func GroupPowerApplyJoin(c *gin.Context) (*mycontext.MyContext, error) { } // 插入申请表 - err = groupPower_m.InsertGroupPowerApplyJoin(userId, gpInfo.ID) + err = groupPower_m.InsertGroupPowerApplyJoin(model, userId, gpInfo.ID) + if err != nil { + return myContext, err + } + + resp.ResponseOk(c, nil) + return myContext, nil +} + +// @Tags 家族 +// @Summary 批准加入家族 +// @Param userExtId query string false "用户extId" +// @Success 200 +// @Router /v1/groupPower/passApply [post] +func GroupPowerApplyPass(c *gin.Context) (*mycontext.MyContext, error) { + myContext := mycontext.CreateMyContext(c.Keys) + userId, err := req.GetUserId(c) + if err != nil { + return myContext, err + } + model := domain.CreateModelContext(myContext) + + // 判断是否加入了家族 + gpU := groupPower_m.GroupPowerUser{UserId: userId} + myGroupPUser, err := gpU.GetGroupPowerUser(model) + if err != nil { + return myContext, err + } + // 操作者是否加入了家族,是否有操作权限 + if myGroupPUser == nil || myGroupPUser.ID == 0 { + return myContext, bizerr.GroupPowerHaveNoJoin + } + if myGroupPUser.Role == 0 || myGroupPUser.Role == groupPower_e.GroupPowerUserRoleUser { + return myContext, bizerr.GroupPowerHaveNoPower + } + // 查找申请记录 + apply, err := groupPower_m.GetGroupPowerApplyJoin(model, userId, myGroupPUser.GroupPowerId) + if err != nil { + return myContext, err + } + if apply == nil { + return myContext, bizerr.GroupPowerHaveNoApply + } + + err = model.Transaction(func(model *domain.Model) error { + txModel := domain.CreateModel(model.CtxAndDb) + // 插入家族成员表 + gpU := groupPower_m.GroupPowerUser{GroupPowerId: apply.GroupPowerId, UserId: apply.UserId, Role: groupPower_e.GroupPowerUserRoleUser} + err := gpU.Create(txModel.Db) + if err != nil { + txModel.Log.Errorf("GroupPowerApplyPass err:%v, info:%v", err, gpU) + return err + } + // 更改申请表状态 + err = groupPower_m.AcceptGroupPowerApplyJoinById(model, apply.Id) + if err != nil { + return err + } + return nil + }) if err != nil { return myContext, err } diff --git a/route/router.go b/route/router.go index 5e57d9d08218df28f6233f3a0ad66b3e7212849a..7a586d67c566ce46ab478f1715e22213c4e396fd 100644 --- a/route/router.go +++ b/route/router.go @@ -130,6 +130,7 @@ func InitRouter() *gin.Engine { groupPower.GET("/rooms", wrapper(group_power_r.GroupPowerRooms)) groupPower.GET("/members", wrapper(group_power_r.GroupPowerMembers)) groupPower.POST("/applyJoin", wrapper(group_power_r.GroupPowerApplyJoin)) + groupPower.POST("/passApply", wrapper(group_power_r.GroupPowerApplyPass)) } charge := v1.Group("/charge")