family.go 5.49 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4
package groupPower_m

import (
	"git.hilo.cn/hilo-common/domain"
5
	"git.hilo.cn/hilo-common/resource/mysql"
hujiebin's avatar
hujiebin committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
	"gorm.io/gorm"
	"hilo-group/_const/enum/groupPower_e"
	"hilo-group/common"
	"hilo-group/common/utime"
	"hilo-group/myerr"
	"hilo-group/myerr/bizerr"
	"time"
)

//type GroupPowerGrade struct {
//	GroupPowerId uint64    `json:"group_power_id"`
//	Exp          int64     `json:"exp"`
//	Grade        int32     `json:"grade"`
//	ExpireAt     time.Time `json:"expire_at"`
//}

type GroupPowerQuitLog struct {
	Id           uint64    `json:"id"`
	UserId       uint64    `json:"user_id"`
	MgrId        uint64    `json:"mgr_id"`
	CreatedTime  time.Time `json:"created_time"`
	GroupPowerId uint64    `json:"group_power_id"`
}

func (this *GroupPower) Get(model *domain.Model) (*GroupPower, error) {
	group := new(GroupPower)
	err := model.Db.Where(this).First(&group).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		}
		return nil, err
	}
	grade := new(GroupPowerGrade)
	err = model.Db.Model(&GroupPowerGrade{}).Where("group_power_id=? and expire_at > ?", this.ID, time.Now().Format(utime.Layout)).First(&grade).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	if grade.GroupPowerId > 0 {
45
		group.Grade = grade.Grade
hujiebin's avatar
hujiebin committed
46 47 48 49 50 51
	}
	return group, nil
}

func (gpu *GroupPowerUser) GetBy(model *domain.Model, pageSize, pageIndex int) ([]*GroupPowerUser, int64, int, bool, error) {
	rows := make([]*GroupPowerUser, 0)
chenweijian's avatar
chenweijian committed
52
	db := model.Db.Model(GroupPowerUser{}).Where(gpu).Order("field(`role`, 2, 3, 1),`id`")
hujiebin's avatar
hujiebin committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	var count int64
	err := db.Count(&count).Error
	if err != nil {
		return nil, 0, 0, false, err
	}
	err = db.Limit(pageSize).Offset(pageIndex).Find(&rows).Error
	if err != nil {
		return nil, 0, 0, false, err
	}
	nextPageIndex, hasNextPage := common.PageNext(count, pageIndex, pageSize)
	return rows, count, nextPageIndex, hasNextPage, 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
}

func QuitFamily(model *domain.Model, userId, mgrId, familyId uint64) error {
	err := model.Db.Exec("delete from group_power_user where group_power_id = ? and user_id = ?", familyId, userId).Error
	if err != nil {
		return err
	}
	// log
	log := &GroupPowerQuitLog{UserId: userId, MgrId: mgrId, GroupPowerId: familyId, CreatedTime: time.Now()}
	return model.Db.Create(log).Error
}

func GroupPowerQuitList(model *domain.Model, familyId uint64, pageSize, pageIndex int) ([]*GroupPowerQuitLog, int, bool, error) {
	rows := make([]*GroupPowerQuitLog, 0)
	db := model.Db.Model(GroupPowerQuitLog{}).Where("group_power_id = ?", familyId).Order("created_time desc")
	var count int64
	err := db.Count(&count).Error
	if err != nil {
		return nil, 0, false, err
	}
	err = db.Limit(pageSize).Offset(pageIndex).Find(&rows).Error
	if err != nil {
		return nil, 0, false, err
	}
	nextIdx, hasNext := common.PageNext(count, pageIndex, pageSize)
	return rows, nextIdx, hasNext, nil
}

func UpdateFamilyAdmin(model *domain.Model, userId, familyId uint64, role groupPower_e.GroupPowerUserRole) error {
	err := model.Db.Exec("update group_power_user set role = ? where group_power_id = ? and user_id = ?", role, familyId, userId).Error
	if err != nil {
		return err
	}
	return nil
}

func UpdateFamily(model *domain.Model, familyId uint64, name, nameplate, declaration, icon string) error {
	if familyId == 0 || (name == "" && nameplate == "" && declaration == "" && icon == "") {
		return nil
	}
120 121 122 123
	var groupPower GroupPower
	if err := model.DB().Model(GroupPower{}).Where("id = ?", familyId).First(&groupPower).Error; err != nil {
		return err
	}
124 125
	// 修改铭牌
	if nameplate != "" {
126
		sql := "UPDATE `group_power` a left join (SELECT ? `id` FROM group_power WHERE nameplate = ?) b on a.id = b.id SET a.`nameplate`=?,modify_nameplate = 1 where a.id=? and b.id is null;"
127 128 129 130 131 132 133 134 135
		result := model.Db.Exec(sql, familyId, nameplate, nameplate, familyId)
		if result.Error != nil {
			return myerr.WrapErr(result.Error)
		}
		if result.RowsAffected <= 0 {
			return myerr.WrapErr(bizerr.GroupPowerHaveChangeInfo)
		}
		return nil
	}
hujiebin's avatar
hujiebin committed
136 137 138 139
	db := model.Db.Model(GroupPower{})
	updateMap := make(map[string]interface{})
	if name != "" {
		updateMap["name"] = name
140 141 142 143 144 145 146 147
		if groupPower.ModifyNameplate != mysql.YES {
			// 同步修改铭牌
			n := len([]rune(name))
			if n > 6 {
				n = 6
			}
			updateMap["nameplate"] = string([]rune(name[0:n]))
		}
hujiebin's avatar
hujiebin committed
148 149 150 151 152 153 154
	}
	if declaration != "" {
		updateMap["declaration"] = declaration
	}
	if icon != "" {
		updateMap["icon"] = icon
	}
155
	result := db.Where("group_power.id = ?", familyId).Updates(updateMap).Limit(1)
hujiebin's avatar
hujiebin committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
	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
}
178 179 180 181 182 183 184 185 186

func GetMemberNum(model *domain.Model, familyId uint64) (int64, error) {
	var count int64
	err := model.Db.Model(GroupPowerUser{}).Where("group_power_id=?", familyId).Count(&count).Error
	if err != nil {
		return 0, err
	}
	return count, nil
}