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

import (
	"git.hilo.cn/hilo-common/domain"
hujiebin's avatar
hujiebin committed
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 45 46 47 48 49 50 51 52 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
	"gorm.io/gorm"
	"hilo-group/common"
	"hilo-group/myerr"
	"hilo-group/myerr/bizerr"
	"time"
)

type GroupPowerApplyJoin struct {
	Id           uint64    `json:"id"`
	UserId       uint64    `json:"user_id"`
	GroupPowerId uint64    `json:"group_power_id"`
	CreatedTime  time.Time `json:"created_time"`
	UpdatedTime  time.Time `json:"updated_time"`
	IsAccept     int8      `json:"is_accept"`
	MgrId        uint64    `json:"mgr_id"`
}

func InsertGroupPowerApplyJoin(model *domain.Model, userId, familyId uint64) error {
	sql := "insert into group_power_apply_join(user_id,group_power_id,is_accept) " +
		"select ?, ?, ? where not exists (select id from group_power_apply_join where user_id=? and group_power_id=? and is_accept=0)"
	result := model.Db.Exec(sql, userId, familyId, 0, userId, familyId)
	if result.Error != nil {
		return myerr.WrapErr(result.Error)
	}
	if result.RowsAffected <= 0 {
		return myerr.WrapErr(bizerr.GroupPowerHaveAlreadyApply)
	}
	return nil
}

func GetGroupPowerApplyJoin(model *domain.Model, userId, familyId uint64, isAccept int) (*GroupPowerApplyJoin, error) {
	res := new(GroupPowerApplyJoin)
	db := model.Db.Where(GroupPowerApplyJoin{UserId: userId, GroupPowerId: familyId})
	if isAccept != -1 {
		db = db.Where("is_accept=?", isAccept)
	}
	err := db.First(&res).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		} else {
			return nil, myerr.WrapErr(err)
		}
	}
	return res, nil
}

func OptGroupPowerApplyJoinById(model *domain.Model, id, mgrId uint64, optType int) error {
	err := model.Db.Exec("update group_power_apply_join set is_accept = ?, mgr_id = ? where id=? and is_accept = 0", optType, mgrId, 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)
	}
	return nil
}

func OptGroupPowerApplyList(model *domain.Model, familyId uint64, pageSize, pageIndex int) ([]*GroupPowerApplyJoin, int, bool, error) {
	rows := make([]*GroupPowerApplyJoin, 0)
	db := model.Db.Model(GroupPowerApplyJoin{}).Where("group_power_id = ?", familyId).Order("is_accept, 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
}
84 85 86 87 88 89 90 91

func DelGroupPowerApplyJoinNoDeal(model *domain.Model, userId uint64) error {
	err := model.Db.Exec("delete from group_power_apply_join where user_id=? and is_accept = 0", userId).Error
	if err != nil {
		return myerr.WrapErr(err)
	}
	return nil
}
hujiebin's avatar
hujiebin committed
92 93 94 95 96 97 98 99 100 101 102 103 104

// 获取家族申请人数
// param: isAccept 0:未审核 1:通过 2:拒绝
func CountGroupPowerApply(model *domain.Model, groupPowerId mysql.ID, isAccept int) (int64, error) {
	var cnt int64
	if err := model.DB().Model(GroupPowerApplyJoin{}).
		Where("group_power_id = ?", groupPowerId).
		Where("is_accept = ?", isAccept).Count(&cnt).Error; err != nil {
		model.Log.Errorf("CountGroupPowerApply fail:%v", err)
		return cnt, err
	}
	return cnt, nil
}