diff --git a/domain/model/groupPower_m/family.go b/domain/model/groupPower_m/family.go index 9871b0e497736cddb045d836bf1a79dbb046e4e3..c8c1580274bcb0b8f373f551b9aaa604e26078d0 100644 --- a/domain/model/groupPower_m/family.go +++ b/domain/model/groupPower_m/family.go @@ -7,6 +7,8 @@ import ( "hilo-group/_const/enum/groupPower_e" "hilo-group/common" "hilo-group/common/utime" + "hilo-group/myerr" + "hilo-group/myerr/bizerr" "time" ) @@ -112,3 +114,46 @@ func UpdateFamilyAdmin(model *domain.Model, userId, familyId uint64, role groupP } return nil } + +func UpdateFamily(model *domain.Model, familyId uint64, name, nameplate, declaration, icon string) error { + if familyId == 0 || (name == "" && nameplate == "" && declaration == "" && icon == "") { + return nil + } + db := model.Db.Model(GroupPower{}) + updateMap := make(map[string]interface{}) + if name != "" { + updateMap["name"] = name + } + if nameplate != "" { + updateMap["nameplate"] = nameplate + db = db.Where("not exists (select id from group_power where nameplate = ?)", nameplate) + } + if declaration != "" { + updateMap["declaration"] = declaration + } + if icon != "" { + updateMap["icon"] = icon + } + result := db.Updates(updateMap).Limit(1) + if result.Error != nil { + return myerr.WrapErr(result.Error) + } + if result.RowsAffected <= 0 { + return myerr.WrapErr(bizerr.GroupPowerHaveChangeInfo) + } + + return nil +} + +func IsExistsNameplate(model *domain.Model, nameplate string) bool { + var count int64 + err := model.Db.Model(GroupPower{}).Where("nameplate = ?", nameplate).Count(&count).Error + if err != nil { + model.Log.Errorf("IsExistsNameplate err:%v, nameplate:%s", err, nameplate) + return false + } + if count > 0 { + return true + } + return false +} diff --git a/myerr/bizerr/bizCode.go b/myerr/bizerr/bizCode.go index 2a6ca805d8a4d035ac40de34558ba2bd0a35a08d..c1303c9f35326f083f0df0e3fd7efc6a1f3384ff 100644 --- a/myerr/bizerr/bizCode.go +++ b/myerr/bizerr/bizCode.go @@ -86,6 +86,9 @@ var ( GroupPowerHaveNoApply = myerr.NewBusinessCode(15013, "User have no apply", myerr.BusinessData{}) // 没有找到申请 GroupPowerHaveAlreadyApply = myerr.NewBusinessCode(15014, "Already applied", myerr.BusinessData{}) // 已经申请过 GroupPowerHaveAlreadyChange = myerr.NewBusinessCode(15015, "Already changed", myerr.BusinessData{}) // 已经改变了 + GroupPowerHaveChangeInfo = myerr.NewBusinessCode(15016, "Setting failed", myerr.BusinessData{}) // 修改失败 + GroupPowerHaveTooLong = myerr.NewBusinessCode(15017, "too long", myerr.BusinessData{}) // 长度太长 + GroupPowerCannotRepeated = myerr.NewBusinessCode(15018, "Cannot be repeated", 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 80de63207315dc35498501b951b491b2412ed6c3..4219f7d08892445dac083520f7ae37aba463b761 100644 --- a/route/group_power_r/group_power.go +++ b/route/group_power_r/group_power.go @@ -1256,3 +1256,77 @@ func GroupPowerSetAdmin(c *gin.Context) (*mycontext.MyContext, error) { resp.ResponseOk(c, nil) return myContext, nil } + +// @Tags 家族 +// @Summary 设置家族信息 +// @Param id formData int true "家族id" +// @Param icon formData string false "家族图片" +// @Param name formData string false "家族名称" +// @Param nameplate formData string false "家族铭牌" +// @Param declaration formData string false "家族宣言" +// @Success 200 +// @Router /v1/groupPower/info/set [post] +func GroupPowerSetInfo(c *gin.Context) (*mycontext.MyContext, error) { + myContext := mycontext.CreateMyContext(c.Keys) + userId, err := req.GetUserId(c) + if err != nil { + return myContext, err + } + type param struct { + Id uint64 `json:"id"` + Name string `json:"name"` + Nameplate string `json:"nameplate"` // 铭牌 + Declaration string `json:"declaration"` // 宣言 + Icon string `json:"icon"` // 头像 + } + para := new(param) + if err := c.ShouldBind(¶); err != nil { + return myContext, err + } + if para.Id <= 0 { + return myContext, bizerr.InvalidParameter + } + model := domain.CreateModelContext(myContext) + + // 判断是否加入了家族 + gpU := groupPower_m.GroupPowerUser{UserId: userId} + myGroupPUser, err := gpU.GetGroupPowerUser(model) + if err != nil { + return myContext, err + } + // 操作者是否加入了家族,是否有操作权限 + if myGroupPUser == nil { + return myContext, bizerr.GroupPowerHaveNoJoin + } + if myGroupPUser.Role != groupPower_e.GroupPowerUserRoleMgr { + return myContext, bizerr.GroupPowerHaveNoPower + } + // 检查铭牌是否能够修改 + if para.Nameplate != "" { + // 等级检查 + gp := &groupPower_m.GroupPower{Entity: mysql.Entity{ID: myGroupPUser.GroupPowerId}} + groupPInfo, err := gp.Get(model) + if err != nil { + return myContext, err + } + if groupPInfo.Grade < 1 { + return myContext, bizerr.GroupPowerHaveNoPower + } + // 检查铭牌长度和唯一性 + if len(para.Nameplate) > 10 { + return myContext, bizerr.GroupPowerHaveTooLong + } + if groupPower_m.IsExistsNameplate(model, para.Nameplate) { + return myContext, bizerr.GroupPowerCannotRepeated + } + } + + // 修改家族信息 + err = groupPower_m.UpdateFamily(model, userId, para.Name, para.Nameplate, para.Declaration, para.Icon) + if err != nil { + return myContext, myerr.WrapErr(err) + } + + resp.ResponseOk(c, nil) + return myContext, nil +}