pg_admin_user_dao.go 1.7 KB
package dao

import (
	"fmt"

	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)

type AdminUserDao struct {
	transactionContext *transaction.TransactionContext
}

func NewAdminUserDao(transactionContext *transaction.TransactionContext) (*AdminUserDao, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &AdminUserDao{
			transactionContext: transactionContext,
		}, nil
	}
}

//UpdatePassword ....
func (dao *AdminUserDao) UpdatePassword(id int64, pwd string) error {
	tx := dao.transactionContext.PgDd
	m := &models.AdminUser{}
	err := tx.Model(m).Where("id=?", id).First()
	if err != nil {
		return err
	}
	_, err = tx.Model(m).Where("id=?", id).
		Set("password=?", pwd).
		Update()
	return err
}

//UpdatePassword ....
func (dao *AdminUserDao) UpdateIsUsable(id int64, isUsable bool) error {
	tx := dao.transactionContext.PgDd
	m := &models.AdminUser{}
	err := tx.Model(m).Where("id=?", id).First()
	if err != nil {
		return err
	}
	_, err = tx.Model(m).Where("id=?", id).
		Set("is_usable=?", isUsable).
		Update()
	return err
}

//AdminUserAccountExist ...
func (dao *AdminUserDao) AdminUserAccountExist(account string) (bool, error) {
	tx := dao.transactionContext.PgDd
	m := &models.AdminUser{}
	ok, err := tx.Model(m).
		Where("account=?", account).
		Exists()

	return ok, err
}

func (dao *AdminUserDao) AdminUserIsDefault(id int64) (bool, error) {
	tx := dao.transactionContext.PgDd
	m := &models.AdminUser{}
	err := tx.Model(m).
		Where("id=?", id).
		Column("is_default").
		First()

	return m.IsDefault, err
}