group_power_apply.go 1.75 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 7 8 9 10 11 12 13 14 15 16 17 18
	"gorm.io/gorm"
	"hilo-group/myerr"
	"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
19
func InsertGroupPowerApplyJoin(model *domain.Model, userId, familyId uint64) error {
chenweijian's avatar
chenweijian committed
20 21 22
	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=?"
chenweijian's avatar
chenweijian committed
23
	err := model.Db.Exec(sql, userId, familyId, 0, nowTime, nowTime, 0, nowTime, nowTime).Error
chenweijian's avatar
chenweijian committed
24 25 26 27 28 29
	if err != nil {
		return myerr.WrapErr(err)
	}
	return nil
}

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

chenweijian's avatar
chenweijian committed
43 44 45 46 47 48 49 50 51 52
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
chenweijian's avatar
chenweijian committed
53 54 55 56 57
	if err != nil {
		return myerr.WrapErr(err)
	}
	return nil
}