group_power_apply.go 2.33 KB
Newer Older
chenweijian's avatar
chenweijian committed
1 2 3
package groupPower_m

import (
chenweijian's avatar
chenweijian committed
4
	"git.hilo.cn/hilo-common/domain"
chenweijian's avatar
chenweijian committed
5 6
	"gorm.io/gorm"
	"hilo-group/myerr"
chenweijian's avatar
chenweijian committed
7
	"hilo-group/myerr/bizerr"
chenweijian's avatar
chenweijian committed
8 9 10 11 12 13 14 15 16 17
	"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"`
chenweijian's avatar
chenweijian committed
18
	MgrId        uint64    `json:"mgr_id"`
chenweijian's avatar
chenweijian committed
19 20
}

chenweijian's avatar
chenweijian committed
21
func InsertGroupPowerApplyJoin(model *domain.Model, userId, familyId uint64) error {
chenweijian's avatar
chenweijian committed
22 23 24 25 26 27 28 29
	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)
chenweijian's avatar
chenweijian committed
30 31 32 33
	}
	return nil
}

chenweijian's avatar
chenweijian committed
34
func GetGroupPowerApplyJoin(model *domain.Model, userId, familyId uint64) (*GroupPowerApplyJoin, error) {
chenweijian's avatar
chenweijian committed
35
	res := new(GroupPowerApplyJoin)
chenweijian's avatar
chenweijian committed
36
	err := model.Db.Where(GroupPowerApplyJoin{UserId: userId, GroupPowerId: familyId}).Where("is_accept=0").First(&res).Error
chenweijian's avatar
chenweijian committed
37 38 39 40 41 42 43 44 45 46
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		} else {
			return nil, myerr.WrapErr(err)
		}
	}
	return res, nil
}

chenweijian's avatar
chenweijian committed
47 48
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=?", optType, mgrId, id).Error
chenweijian's avatar
chenweijian committed
49 50 51 52 53 54 55 56
	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
chenweijian's avatar
chenweijian committed
57 58 59 60 61
	if err != nil {
		return myerr.WrapErr(err)
	}
	return nil
}
chenweijian's avatar
chenweijian committed
62 63 64 65 66 67 68 69 70 71

func OptGroupPowerApplyList(model *domain.Model, familyId uint64, pageSize, pageIndex int) ([]*GroupPowerApplyJoin, error) {
	rows := make([]*GroupPowerApplyJoin, 0)
	err := model.Db.Model(GroupPowerApplyJoin{}).Where("group_power_id = ?", familyId).
		Order("is_accept, created_time desc").Limit(pageSize).Offset(pageIndex - 1).Find(&rows).Error
	if err != nil {
		return nil, err
	}
	return rows, nil
}