pg_partner_auth_services.go 2.9 KB
package domain_service

import (
	"github.com/tiptok/gocomm/xa/eda"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/dao"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/repository"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
	"strings"
)

type PgPartnerAuthService struct {
	eda.EventCenterPublisher
	transactionContext *transaction.TransactionContext
}

func NewPgPartnerAuthService(ctx *transaction.TransactionContext) *PgPartnerAuthService {
	return &PgPartnerAuthService{
		transactionContext: ctx,
	}
}

func (s *PgPartnerAuthService) ChangeUserPhone(userId int64, newPhone, oldPhone string) (err error) {
	var (
		PartnerInfoDao, _     = dao.NewPartnerInfoDao(s.transactionContext)
		PartnerInfoService, _ = repository.NewPartnerInfoRepository(s.transactionContext)
		partnerInfo           *domain.PartnerInfo
	)
	//验证新手机是否已经存在
	if _, e := PartnerInfoService.FindOne(map[string]interface{}{"account": newPhone}); e == nil {
		err = protocol.NewErrWithMessage(2029, err) //账号已存在
		return
	}
	if partnerInfo, err = PartnerInfoService.FindOne(map[string]interface{}{"id": userId}); err != nil || (partnerInfo.Account != oldPhone) {
		err = protocol.NewErrWithMessage(502, err) //账号不存在
		return
	}
	if err = PartnerInfoDao.Update(map[string]interface{}{
		"Id":      userId,
		"Account": newPhone,
	}); err != nil {
		return
	}
	return
}
func (s *PgPartnerAuthService) ChangeUserPassword(userId int64, newPwd, oldPwd, phone string) (err error) {
	var (
		partnerInfo           *domain.PartnerInfo
		PartnerInfoDao, _     = dao.NewPartnerInfoDao(s.transactionContext)
		PartnerInfoService, _ = repository.NewPartnerInfoRepository(s.transactionContext)
	)
	if partnerInfo, err = PartnerInfoService.FindOne(map[string]interface{}{"id": userId}); err != nil {
		err = protocol.NewErrWithMessage(502, err) //账号不存在
		return
	}
	if !strings.EqualFold(partnerInfo.Password, oldPwd) {
		//密码不一致
		err = protocol.NewErrWithMessage(2028, err) //账号不存在
		return
	}
	if err = PartnerInfoDao.Update(map[string]interface{}{
		"Id":       partnerInfo.Id,
		"Password": newPwd,
	}); err != nil {
		return
	}
	return
}
func (s *PgPartnerAuthService) ResetUserPassword(userId int64, phone, password string) (err error) {
	var (
		partnerInfo           *domain.PartnerInfo
		PartnerInfoDao, _     = dao.NewPartnerInfoDao(s.transactionContext)
		PartnerInfoService, _ = repository.NewPartnerInfoRepository(s.transactionContext)
	)
	if partnerInfo, err = PartnerInfoService.FindOne(map[string]interface{}{"id": userId}); err != nil {
		err = protocol.NewErrWithMessage(502, err) //账号不存在
		return
	}
	if err = PartnerInfoDao.Update(map[string]interface{}{
		"Id":       partnerInfo.Id,
		"Password": password,
	}); err != nil {
		return
	}
	return
}