作者 yangfu

add companyCreatedEvent/companyAdminAssignedEvent

... ... @@ -22,4 +22,5 @@ _testmain.go
*.exe
*.test
.log
.idea
\ No newline at end of file
.idea
.tmp
\ No newline at end of file
... ...
{"/Users/steve/go/src/partner01/pkg/port/beego":1617694648836700729}
\ No newline at end of file
不能预览此文件类型
... ... @@ -3,16 +3,21 @@ package service
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
coreDomain "github.com/linmadan/egglib-go/core/domain"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/company/command"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/company/query"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/event/subscriber"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain/event"
"time"
)
// 公司服务
type CompanyService struct {
coreDomain.BaseEventPublisher
}
// 公司状态转换 禁用、启用
... ... @@ -86,7 +91,7 @@ func (companyService *CompanyService) CreateCompany(createCompanyCommand *comman
Admin: &domain.CompanyAdmin{
UserAccount: createCompanyCommand.UserAccount, // 用户手机号
UserName: createCompanyCommand.UserName, // 用户名称
}, // 公司主管理员
}, // 公司主管理员
},
CreateAt: time.Now(),
UpdateAt: time.Now(),
... ... @@ -103,7 +108,10 @@ func (companyService *CompanyService) CreateCompany(createCompanyCommand *comman
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
// todo 新增公司管理员
companyService.Subscribe(&subscriber.UsersServiceSubscriber{TransactionContext: transactionContext.(*pgTransaction.TransactionContext)})
companyService.Publish(&event.CompanyCreated{
Company: company,
})
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
... ... @@ -178,7 +186,7 @@ func (companyService *CompanyService) ListCompany(listCompanyQuery *query.ListCo
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return map[string]interface{}{
"count": count,
"count": count,
"companies": companies,
}, nil
}
... ... @@ -268,7 +276,11 @@ func (companyService *CompanyService) SetCompanyAdmin(setCompanyAdminCommand *co
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
// todo 更改公司管理员
companyService.Subscribe(&subscriber.UsersServiceSubscriber{TransactionContext: transactionContext.(*pgTransaction.TransactionContext)})
companyService.Publish(&event.CompanyAdminAssigned{
CompanyId: company.CompanyId,
Admin: company.CompanyInfo.Admin,
})
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
... ...
package subscriber
import (
"github.com/linmadan/egglib-go/core/domain"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/factory"
domainx "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain/event"
"time"
)
type UsersServiceSubscriber struct {
TransactionContext *pgTransaction.TransactionContext
}
func (subscriber *UsersServiceSubscriber) HandleEvent(domainEvent domain.DomainEvent) error {
userCreateService, _ := factory.CreateUserCreateService(map[string]interface{}{
"transactionContext": subscriber.TransactionContext,
})
switch domainEvent.EventType() {
case event.CompanyCreatedEvent:
companyCreatedEvent := domainEvent.(*event.CompanyCreated)
admin := companyCreatedEvent.Company.CompanyInfo.Admin
if _, err := userCreateService.CreateUser(companyCreatedEvent.OperatorId, &domainx.User{
UserType: domainx.Manager,
CompanyId: companyCreatedEvent.Company.CompanyId,
UserInfo: &domainx.UserInfo{
IsPrincipal: true,
UserAccount: admin.UserAccount,
UserName: admin.UserName,
},
AdminType: domainx.EnterpriseAdministrator,
PartnerInfo: &domainx.PartnerInfo{},
Status: domainx.StatusEnable,
CreateAt: time.Now(),
}); err != nil {
return err
}
case event.CompanyAdminAssignedEvent:
companyAdminAssigned := domainEvent.(*event.CompanyAdminAssigned)
admin := companyAdminAssigned.Admin
if _, err := userCreateService.CreateUser(companyAdminAssigned.OperatorId, &domainx.User{
UserType: domainx.Manager,
CompanyId: companyAdminAssigned.CompanyId,
UserInfo: &domainx.UserInfo{
IsPrincipal: true,
UserAccount: admin.UserAccount,
UserName: admin.UserName,
},
AdminType: domainx.EnterpriseAdministrator,
PartnerInfo: &domainx.PartnerInfo{},
Status: domainx.StatusEnable,
CreateAt: time.Now(),
}); err != nil {
return err
}
case event.UsersCreatedEvent:
}
return nil
}
func (subscriber *UsersServiceSubscriber) SubscribedToEventTypes() []string {
return []string{
event.CompanyAdminAssignedEvent,
event.CompanyCreatedEvent,
}
}
... ...
... ... @@ -37,3 +37,19 @@ func CreateReturnGoodsService(options map[string]interface{}) (service.ReturnGoo
}
return domainService.NewReturnGoodsService(transactionContext)
}
func CreateUserCreateService(options map[string]interface{}) (service.UserCreateService, error) {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pgTransaction.TransactionContext)
}
return domainService.NewUserCreateService(transactionContext)
}
func CreatePhoneAuthService(options map[string]interface{}) (service.PhoneAuthService, error) {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pgTransaction.TransactionContext)
}
return domainService.NewPhoneAuthenticationService(transactionContext)
}
... ...
... ... @@ -4,7 +4,7 @@ import "os"
const SERVICE_NAME = "partner01"
var LOG_LEVEL = "debug"
var LOG_LEVEL = "debug" //"debug"
func init() {
if os.Getenv("LOG_LEVEL") != "" {
... ...
package event
import (
coreDomain "github.com/linmadan/egglib-go/core/domain"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
)
const CompanyAdminAssignedEvent = "Company-Admin-Assigned"
type CompanyAdminAssigned struct {
coreDomain.BaseEvent
OperatorId int64
CompanyId int64
Admin *domain.CompanyAdmin
}
func (event *CompanyAdminAssigned) EventType() string {
return CompanyAdminAssignedEvent
}
... ...
package event
import (
coreDomain "github.com/linmadan/egglib-go/core/domain"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
)
const CompanyCreatedEvent = "Company-Created-Event"
type CompanyCreated struct {
coreDomain.BaseEvent
OperatorId int64
Company *domain.Company
}
func (event *CompanyCreated) EventType() string {
return CompanyCreatedEvent
}
... ...
package event
import (
coreDomain "github.com/linmadan/egglib-go/core/domain"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
)
const UsersCreatedEvent = "Users-Created-Event"
type UsersCreated struct {
coreDomain.BaseEvent
OperatorId int64
User *domain.User
}
func (event *UsersCreated) EventType() string {
return UsersCreatedEvent
}
... ...
package service
import (
coreDomain "github.com/linmadan/egglib-go/core/domain"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
)
type UserCreateService interface {
coreDomain.DomainEventPublisher
CreateUser(operatorId int64, user *domain.User) (*domain.User, error)
}
... ...
package service
import (
coreDomain "github.com/linmadan/egglib-go/core/domain"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
)
type PhoneAuthService interface {
coreDomain.DomainEventPublisher
PhoneAuth(userId int64, phone, passwd string) (*domain.UserAuth, error)
}
... ...
... ... @@ -36,23 +36,26 @@ func (userAuth *UserAuth) Identify() interface{} {
}
func (userAuth *UserAuth) Update(data map[string]interface{}) error {
if userAuthId, ok := data["userAuthId"]; ok {
userAuth.UserAuthId = userAuthId.(int64)
}
if users, ok := data["users"]; ok {
userAuth.Users = users.([]int64)
}
if phone, ok := data["phone"]; ok {
userAuth.PhoneAuth.Phone = phone.(string)
}
if password, ok := data["password"]; ok {
if password, ok := data["password"]; ok && len(password.(string)) > 0 {
userAuth.PhoneAuth.Password = password.(string)
}
if createAt, ok := data["createAt"]; ok {
userAuth.CreateAt = createAt.(time.Time)
}
if updateAt, ok := data["updateAt"]; ok {
userAuth.UpdateAt = updateAt.(time.Time)
if uid, ok := data["userId"]; ok {
var userExisted bool = false
userId := uid.(int64)
for i := range userAuth.Users {
if userAuth.Users[i] == userId {
userExisted = true
}
}
if !userExisted {
userAuth.Users = append(userAuth.Users, userId)
}
}
return nil
}
... ...
package domainService
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/linmadan/egglib-go/core/application"
coreDomain "github.com/linmadan/egglib-go/core/domain"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/repository"
"time"
)
const defaultPassword = "7c4a8d09ca3762af61e59520943dc26494f8941b" //sha1 mmm123456
type PhoneAuthenticationService struct {
coreDomain.BaseEventPublisher
transactionContext *pgTransaction.TransactionContext
}
func (service *PhoneAuthenticationService) PhoneAuth(userId int64, phone, passwd string) (*domain.UserAuth, error) {
var userAuth *domain.UserAuth
var err error
UserAuthRepository, _ := repository.NewUserAuthRepository(service.transactionContext)
userAuth, err = UserAuthRepository.FindOne(map[string]interface{}{"phone": phone})
if userAuth != nil {
userAuth.Update(map[string]interface{}{"password": passwd, "userId": userId})
}
if err == pg.ErrNoRows {
if passwd == "" {
passwd = defaultPassword
}
userAuth = &domain.UserAuth{
Users: []int64{userId},
PhoneAuth: &domain.PhoneAuth{
Phone: phone,
Password: passwd,
},
CredentialAuths: []*domain.CredentialAuth{},
CreateAt: time.Now(),
UpdateAt: time.Now(),
}
err = nil
}
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
return UserAuthRepository.Save(userAuth)
}
func NewPhoneAuthenticationService(transactionContext *pgTransaction.TransactionContext) (*PhoneAuthenticationService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PhoneAuthenticationService{
transactionContext: transactionContext,
}, nil
}
}
... ...
package domainService
import (
"fmt"
coreDomain "github.com/linmadan/egglib-go/core/domain"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/repository"
)
type UserCreateService struct {
coreDomain.BaseEventPublisher
transactionContext *pgTransaction.TransactionContext
}
func (service *UserCreateService) CreateUser(operatorId int64, user *domain.User) (*domain.User, error) {
// 检查操作人权限
var err error
UserRepository, _ := repository.NewUserRepository(service.transactionContext)
//operator,err := UserRepository.FindOne(map[string]interface{}{"userId":operatorId})
//if err!=nil{
// return nil,err
//}
//if operator.AdminType>=domain.NormalUser{
// return nil, fmt.Errorf("权限不足")
//}
if user.UserType&domain.Partner > 0 {
if user.PartnerInfo == nil {
return nil, fmt.Errorf("合伙人信息不能为空")
}
}
user, err = UserRepository.Save(user)
if err != nil {
return nil, err
}
PhoneAuthenticationService, _ := NewPhoneAuthenticationService(service.transactionContext)
if _, err = PhoneAuthenticationService.PhoneAuth(user.UserId, user.UserInfo.UserAccount, ""); err != nil {
return nil, err
}
return user, err
}
func NewUserCreateService(transactionContext *pgTransaction.TransactionContext) (*UserCreateService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &UserCreateService{
transactionContext: transactionContext,
}, nil
}
}
... ...
package pg
import (
"context"
"fmt"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/constant"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/log"
"github.com/linmadan/egglib-go/persistent/pg/hooks"
)
... ... @@ -43,3 +45,18 @@ func init() {
}
}
}
type SqlGeneratePrintHook struct{}
func (hook SqlGeneratePrintHook) BeforeQuery(c context.Context, q *pg.QueryEvent) (context.Context, error) {
return c, nil
}
func (hook SqlGeneratePrintHook) AfterQuery(c context.Context, q *pg.QueryEvent) error {
sqlStr, err := q.FormattedQuery()
if err != nil {
return err
}
log.Logger.Debug(string(sqlStr))
return nil
}
... ...
... ... @@ -98,6 +98,9 @@ func (repository *UserAuthRepository) FindOne(queryOptions map[string]interface{
userAuthModel := new(models.UserAuth)
query := sqlbuilder.BuildQuery(tx.Model(userAuthModel), queryOptions)
query.SetWhereByQueryOption("user_auth.user_auth_id = ?", "userAuthId")
if v, ok := queryOptions["userId"]; ok {
query.Where(fmt.Sprintf(`user_auth.users @>'{%v}'`, v))
}
if v, ok := queryOptions["phone"]; ok {
query.Where(fmt.Sprintf(`user_auth.phone_auth @>'{"phone":"%v"}'`, v))
}
... ... @@ -106,7 +109,7 @@ func (repository *UserAuthRepository) FindOne(queryOptions map[string]interface{
}
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
return nil, pg.ErrNoRows
} else {
return nil, err
}
... ...
... ... @@ -16,7 +16,7 @@ func init() {
web.BConfig.RunMode = "dev"
web.BConfig.Listen.HTTPPort = 8080
web.BConfig.Listen.EnableAdmin = false
web.BConfig.WebConfig.CommentRouterPath = "/pkg/port/beego"
web.BConfig.WebConfig.CommentRouterPath = "/pkg/port/beego/routers"
if os.Getenv("RUN_MODE") != "" {
web.BConfig.RunMode = os.Getenv("RUN_MODE")
}
... ...
... ... @@ -37,6 +37,8 @@ var _ = Describe("创建", func() {
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM companies WHERE true")
_, err = pG.DB.Exec("DELETE FROM users WHERE true")
_, err = pG.DB.Exec("DELETE FROM user_auth WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
... ... @@ -18,19 +18,19 @@ var _ = Describe("设置公司管理员", func() {
var newCompany *domain.Company
BeforeEach(func() {
newCompany = &domain.Company{
CompanyId:GinkgoRandomSeed(),
CompanyId: GinkgoRandomSeed(),
CompanyInfo: &domain.CompanyInfo{
Name: "公司名称", // 公司名称
Name: "公司名称", // 公司名称
Abbreviation: "公司简称", // 公司简称
Logo: "", // 公司logo
Phone: "", // 公司联系电话
Status: 1, // 状态 1正常 2禁用
Remarks: "备注", // 备注
Enable: 1, // 是否有效【1:有效】【2:无效】
Logo: "", // 公司logo
Phone: "", // 公司联系电话
Status: 1, // 状态 1正常 2禁用
Remarks: "备注", // 备注
Enable: 1, // 是否有效【1:有效】【2:无效】
Admin: &domain.CompanyAdmin{
UserAccount: fmt.Sprint(GinkgoRandomSeed()), // 用户手机号
UserName: "用户名称", // 用户名称
}, // 公司主管理员
UserName: "用户名称", // 用户名称
}, // 公司主管理员
},
CreateAt: time.Now(),
UpdateAt: time.Now(),
... ... @@ -67,6 +67,8 @@ var _ = Describe("设置公司管理员", func() {
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM companies WHERE true")
_, err = pG.DB.Exec("DELETE FROM users WHERE true")
_, err = pG.DB.Exec("DELETE FROM user_auth WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...