pg_partner_auth_services.go 2.8 KB
package domain_service

import (
	"fmt"
	"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"
	"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 partnerInfo, err = PartnerInfoService.FindOne(map[string]interface{}{"account": oldPhone}); err != nil || (partnerInfo.Account != oldPhone) {
		err = errUserNotFound //账号不存在
		return
	}
	//验证新手机是否已经存在
	if _, e := PartnerInfoService.FindOne(map[string]interface{}{"account": newPhone}); e == nil {
		err = fmt.Errorf("当前手机号已存在,请重新输入") //账号已存在
		return
	}
	if err = PartnerInfoDao.Update(map[string]interface{}{
		"oldAccount": oldPhone,
		"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{}{"account": phone}); err != nil {
		err = errUserNotFound //账号不存在
		return
	}
	if !strings.EqualFold(partnerInfo.Password, oldPwd) {
		err = fmt.Errorf("旧密码不正确") //账号不存在
		return
	}
	if err = PartnerInfoDao.Update(map[string]interface{}{
		"oldAccount": phone,
		"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 _, err = PartnerInfoService.FindOne(map[string]interface{}{"account": phone}); err != nil {
		err = errUserNotFound //账号不存在
		return
	}
	if err = PartnerInfoDao.Update(map[string]interface{}{
		"oldAccount": phone,
		"Password":   password,
	}); err != nil {
		return
	}
	return
}