作者 tangxvhui

更新

... ... @@ -2,7 +2,7 @@ package command
//AddCompanyCommand 数据来源 企业平台 推送的消息
//新建公司
type AddCompanyCommand struct {
type SaveCompanyCommand struct {
//新添加的公司
Comapany struct {
Id int64 `json:"id" `
... ... @@ -10,7 +10,7 @@ type AddCompanyCommand struct {
Name string `json:"name"` // 公司名称
Status int `json:"status"` // 公司状态,1正常 2禁用
} `json:"company"`
//新添加公司时, 新建的公司主管理员
//新添加公司时,第一個用户就是主管理员
User struct {
Id int64 ` json:"id"` // 用户Id
Phone string `json:"phone"` // 用户账号
... ...
package company
import (
"time"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/company/command"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
type CompanyServices struct {
}
//从BusinessAdmins 接收消息,变更公司数据
//
func (c CompanyServices) BusinessAdminCompany() error {
return nil
}
func (c CompanyServices) AddCompany() error {
return nil
}
//addCompany
//从BusinessAdmins 接收消息 添加公司
func (c CompanyServices) addCompany(param *command.SaveCompanyCommand) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
nowTime := time.Now()
newCompany := domain.Company{
Id: param.Comapany.Id,
Logo: param.Comapany.Logo,
Name: param.Comapany.Name,
Status: param.Comapany.Status,
UpdateAt: nowTime,
CreateAt: nowTime,
DeleteAt: nil,
}
func (c CompanyServices) EditCompany() error {
newUser := domain.User{
Id: param.User.Id,
Account: param.User.Phone,
AvatarUrl: param.User.Avatar,
CompanyId: param.User.CompanyId,
AdminType: param.User.AdminType,
Name: param.User.Name,
Status: param.User.Status,
UpdateAt: nowTime,
DeleteAt: nil,
CreateAt: nowTime,
}
companyRepo := factory.CreateCompanyRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
userRepo := factory.CreateUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
_, err = companyRepo.Insert(&newCompany)
if err != nil {
return err
}
_, err = userRepo.Insert(&newUser)
if err != nil {
return err
}
if err := transactionContext.CommitTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil
}
func (c CompanyServices) ChangeAdmin() error {
return nil
}
//editCompany
//从BusinessAdmins 接收消息 更新公司
func (c CompanyServices) editCompany(param *command.SaveCompanyCommand) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
companyRepo := factory.CreateCompanyRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
userRepo := factory.CreateUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
_, companyList, err := companyRepo.Find(map[string]interface{}{
"limit": 1,
"id": param.Comapany.Id,
})
if err != nil {
return err
}
_, userList, err := userRepo.Find(map[string]interface{}{
"limit": 1,
"id": param.User.Id,
})
if err != nil {
return err
}
var (
newCompany *domain.Company
newUser *domain.User
)
nowTime := time.Now()
if len(companyList) > 0 {
newCompany = companyList[0]
} else {
newCompany = &domain.Company{
CreateAt: nowTime,
}
}
if len(userList) > 0 {
newUser = userList[0]
} else {
newUser = &domain.User{
CreateAt: nowTime,
}
}
newCompany.Id = param.Comapany.Id
newCompany.Logo = param.Comapany.Logo
newCompany.Name = param.Comapany.Name
newCompany.Status = param.Comapany.Status
newCompany.UpdateAt = nowTime
func (c CompanyServices) SetCompanyCharge() error {
newUser.Id = param.User.Id
newUser.Account = param.User.Phone
newUser.AvatarUrl = param.User.Avatar
newUser.CompanyId = param.User.CompanyId
newUser.AdminType = param.User.AdminType
newUser.Name = param.User.Name
newUser.Status = param.User.Status
newUser.UpdateAt = nowTime
if len(companyList) > 0 {
_, err = companyRepo.Update(newCompany)
if err != nil {
return err
}
} else {
_, err = companyRepo.Insert(newCompany)
if err != nil {
return err
}
}
if len(userList) > 0 {
_, err = userRepo.Update(newUser)
if err != nil {
return err
}
} else {
_, err = userRepo.Insert(newUser)
if err != nil {
return err
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return err
}
return nil
}
... ...
package factory
import (
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
pgDB "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/repository"
)
func CreateTransactionContext(options map[string]interface{}) (application.TransactionContext, error) {
return pg.NewPGTransactionContext(pgDB.DB), nil
}
func CreateCompanyRepository(options map[string]interface{}) domain.CompanyRepository {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewCompanyRepository(transactionContext)
}
func CreateUserRepository(options map[string]interface{}) domain.UserRepository {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewUserRepository(transactionContext)
}
... ...
package command
//UpdateCompanyCommand 数据来源 企业平台 推送的消息
type UpdateCompanyCommand struct {
Id int64 `json:"id,string"` // 公司编号
Logo string `json:"logo"` // 公司logo
Name string `json:"name"` // 公司名称
Status int `json:"status"` // 公司状态,1正常 2禁用
// 公司主管理员
User struct {
Id int64 ` json:"id"` // 用户Id
Phone string `json:"phone"` // 用户账号
Avatar string `json:"avatar"` // 用户头像URL
CompanyId int64 `json:"company_id"` // 公司编号
AdminType int `json:"admin_type"` // 1普通员工 2 主管理员
Name string `json:"name"` // 用户姓名
Status int `json:"status"` // 用户状态(1正常 2禁用)
} `json:"user"`
type SaveUserCommand struct {
Id int64 ` json:"id"` // 用户Id
Phone string `json:"phone"` // 用户账号
Avatar string `json:"avatar"` // 用户头像URL
CompanyId int64 `json:"company_id"` // 公司编号
AdminType int `json:"admin_type"` // 1普通员工 2 主管理员
Name string `json:"name"` // 用户姓名
Status int `json:"status"` // 用户状态(1正常 2禁用)
}
... ...
package user
import (
"time"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/command"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
type UserService struct{}
func (srv UserService) CreateUser() {}
//AddUser
//从BusinessAdmins 接收消息 添加用户
func (srv UserService) addUser(param command.SaveUserCommand) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
nowTime := time.Now()
newUser := domain.User{
Id: param.Id,
Account: param.Phone,
AvatarUrl: param.Avatar,
CompanyId: param.CompanyId,
AdminType: param.AdminType,
Name: param.Name,
Status: param.Status,
UpdateAt: nowTime,
DeleteAt: nil,
CreateAt: nowTime,
}
userRepo := factory.CreateUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
_, err = userRepo.Insert(&newUser)
if err != nil {
return err
}
if err := transactionContext.CommitTransaction(); err != nil {
return err
}
return nil
}
//UpdateUser
//从BusinessAdmins 接收消息 更新用户
func (srv UserService) updateUser(param command.SaveUserCommand) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
userRepo := factory.CreateUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
_, userList, err := userRepo.Find(map[string]interface{}{
"limit": 1,
"id": param.Id,
})
if err != nil {
return err
}
var (
newUser *domain.User
)
nowTime := time.Now()
if len(userList) > 0 {
newUser = userList[0]
} else {
newUser = &domain.User{
CreateAt: nowTime,
}
}
newUser.Id = param.Id
newUser.Account = param.Phone
newUser.AvatarUrl = param.Avatar
newUser.CompanyId = param.CompanyId
newUser.AdminType = param.AdminType
newUser.Name = param.Name
newUser.Status = param.Status
newUser.UpdateAt = nowTime
if len(userList) > 0 {
_, err = userRepo.Update(newUser)
if err != nil {
return err
}
} else {
_, err = userRepo.Insert(newUser)
if err != nil {
return err
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return err
}
return nil
}
//changeAdmin
//从BusinessAdmins 接收消息 变更主管
func (srv UserService) changeAdmin(param *command.ChangeAdminCommand) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil
}
func (srv UserService) setCompanyCharge(param *command.ChangeAdminCommand) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
func (srv UserService) UpdateUser() {}
if err := transactionContext.CommitTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil
}
... ...
... ... @@ -3,17 +3,18 @@ package domain
import "time"
type Company struct {
Id int64 //公司编号
Logo string //公司logo
Name string //公司名称
Status int //公司状态,1正常 2禁用
UpdateAt time.Time //更新时间
CreatedAt time.Time //创建时间
DeleteAt *time.Time
Id int64 //公司编号
Logo string //公司logo
Name string //公司名称
Status int //公司状态,1正常 2禁用
UpdateAt time.Time //更新时间
CreateAt time.Time //创建时间
DeleteAt *time.Time
}
type CompanyRepository interface {
Save(company *Company) (*Company, error)
Insert(company *Company) (*Company, error)
Update(Company *Company) (*Company, error)
Remove(company *Company) (*Company, error)
FindOne(queryOptions map[string]interface{}) (*Company, error)
Find(queryOptions map[string]interface{}) (int, []*Company, error)
... ...
... ... @@ -3,22 +3,21 @@ package domain
import "time"
type User struct {
Id int64 // 用户Id
Account string // 用户账号
AvatarUrl string // 用户头像URL
CompanyId int64 // 公司编号
IsPrincipal bool // 是否公司负责人
AdminType int // 1普通员工 2 主管理员
Name string // 用户姓名
RoleId int64 // 用户角色id
Status int // 用户状态(1正常 2禁用)
UpdateAt time.Time // 更新时间
DeleteAt *time.Time
CreateAt time.Time
Id int64 // 用户Id
Account string // 用户账号
AvatarUrl string // 用户头像URL
CompanyId int64 // 公司编号
AdminType int // 1普通员工 2 主管理员
Name string // 用户姓名
Status int // 用户状态(1正常 2禁用)
UpdateAt time.Time // 更新时间
DeleteAt *time.Time
CreateAt time.Time
}
type UserRepository interface {
Save(user *User) (*User, error)
Insert(user *User) (*User, error)
Update(user *User) (*User, error)
Remove(user *User) (*User, error)
FindOne(queryOptions map[string]interface{}) (*User, error)
Find(queryOptions map[string]interface{}) (int, []*User, error)
... ...
... ... @@ -3,17 +3,15 @@ package models
import "time"
type User struct {
tableName struct{} `pg:"user"`
Id int64 `pg:"pk:id"` // 用户Id
Account string // 用户账号
AvatarUrl string // 用户头像URL
CompanyId int64 // 公司编号
IsPrincipal bool // 是否公司负责人
AdminType int // 1普通员工 2 主管理员
Name string // 用户姓名
RoleId int64 // 用户角色id
Status int // 用户状态(1正常 2禁用)
UpdateAt time.Time // 更新时间
CreateAt time.Time // 创建时间
DeleteAt *time.Time // 删除时间
tableName struct{} `pg:"user"`
Id int64 `pg:"pk:id"` // 用户Id
Account string // 用户账号
AvatarUrl string // 用户头像URL
CompanyId int64 // 公司编号
AdminType int // 1普通员工 2 主管理员
Name string // 用户姓名
Status int // 用户状态(1正常 2禁用)
UpdateAt time.Time // 更新时间
CreateAt time.Time // 创建时间
DeleteAt *time.Time // 删除时间
}
... ...
package repository
import (
"errors"
"time"
"github.com/go-pg/pg/v10"
... ... @@ -16,28 +15,45 @@ type CompanyRepository struct {
var _ domain.CompanyRepository = (*CompanyRepository)(nil)
func (repo *CompanyRepository) Save(u *domain.Company) (*domain.Company, error) {
func NewCompanyRepository(tx *pgTransaction.TransactionContext) *CompanyRepository {
return &CompanyRepository{
transactionContext: tx,
}
}
func (repo *CompanyRepository) Insert(u *domain.Company) (*domain.Company, error) {
companyModel := models.Company{
Id: u.Id,
Logo: u.Logo,
Name: u.Name,
Status: u.Status,
UpdateAt: u.UpdateAt,
CreateAt: u.CreatedAt,
CreateAt: u.CreateAt,
DeleteAt: u.DeleteAt,
}
tx := repo.transactionContext.PgTx
if companyModel.Id == 0 {
_, err := tx.Model(&companyModel).Insert()
if err != nil {
return nil, err
}
u.Id = companyModel.Id
} else {
_, err := tx.Model(&companyModel).WherePK().Update()
if err != nil {
return nil, err
}
_, err := tx.Model(&companyModel).Insert()
if err != nil {
return nil, err
}
u.Id = companyModel.Id
return u, nil
}
func (repo *CompanyRepository) Update(u *domain.Company) (*domain.Company, error) {
companyModel := models.Company{
Id: u.Id,
Logo: u.Logo,
Name: u.Name,
Status: u.Status,
UpdateAt: u.UpdateAt,
CreateAt: u.CreateAt,
DeleteAt: u.DeleteAt,
}
tx := repo.transactionContext.PgTx
_, err := tx.Model(&companyModel).WherePK().Update()
if err != nil {
return nil, err
}
return u, nil
}
... ... @@ -45,7 +61,7 @@ func (repo *CompanyRepository) Save(u *domain.Company) (*domain.Company, error)
func (repo *CompanyRepository) Remove(u *domain.Company) (*domain.Company, error) {
nowTime := time.Now()
u.DeleteAt = &nowTime
_, err := repo.Save(u)
_, err := repo.Update(u)
return u, err
}
... ... @@ -58,7 +74,7 @@ func (repo *CompanyRepository) FindOne(queryOptions map[string]interface{}) (*do
}
err := query.First()
if err == pg.ErrNoRows {
return nil, errors.New("company 不存在")
return nil, ErrNoRows
}
if err != nil {
return nil, err
... ... @@ -72,6 +88,9 @@ func (repo *CompanyRepository) Find(queryOptions map[string]interface{}) (int, [
companyModel := []models.Company{}
query := tx.Model(&companyModel).
Limit(20)
if v, ok := queryOptions["id"]; ok {
query.Where("id=?", v)
}
if v, ok := queryOptions["limit"]; ok {
query.Limit(v.(int))
}
... ... @@ -92,12 +111,12 @@ func (repo *CompanyRepository) Find(queryOptions map[string]interface{}) (int, [
func (repo *CompanyRepository) TransformToCompanyDomain(m *models.Company) *domain.Company {
return &domain.Company{
Id: m.Id,
Logo: m.Logo,
Name: m.Name,
Status: m.Status,
UpdateAt: m.UpdateAt,
CreatedAt: m.CreateAt,
DeleteAt: m.DeleteAt,
Id: m.Id,
Logo: m.Logo,
Name: m.Name,
Status: m.Status,
UpdateAt: m.UpdateAt,
CreateAt: m.CreateAt,
DeleteAt: m.DeleteAt,
}
}
... ...
package repository
import (
"errors"
"time"
"github.com/go-pg/pg/v10"
... ... @@ -16,33 +15,51 @@ type UserRepository struct {
var _ domain.UserRepository = (*UserRepository)(nil)
func (repo *UserRepository) Save(user *domain.User) (*domain.User, error) {
func NewUserRepository(tx *pgTransaction.TransactionContext) *UserRepository {
return &UserRepository{
transactionContext: tx,
}
}
func (repo *UserRepository) Insert(user *domain.User) (*domain.User, error) {
userModel := models.User{
Id: user.Id,
Account: user.Account,
AvatarUrl: user.AvatarUrl,
CompanyId: user.CompanyId,
IsPrincipal: user.IsPrincipal,
AdminType: user.AdminType,
Name: user.Name,
RoleId: user.RoleId,
Status: user.Status,
UpdateAt: user.UpdateAt,
CreateAt: user.CreateAt,
DeleteAt: user.DeleteAt,
Id: user.Id,
Account: user.Account,
AvatarUrl: user.AvatarUrl,
CompanyId: user.CompanyId,
AdminType: user.AdminType,
Name: user.Name,
Status: user.Status,
UpdateAt: user.UpdateAt,
CreateAt: user.CreateAt,
DeleteAt: user.DeleteAt,
}
tx := repo.transactionContext.PgTx
if userModel.Id == 0 {
_, err := tx.Model(&userModel).Insert()
if err != nil {
return nil, err
}
user.Id = userModel.Id
} else {
_, err := tx.Model(&userModel).WherePK().Update()
if err != nil {
return nil, err
}
_, err := tx.Model(&userModel).Insert()
if err != nil {
return nil, err
}
user.Id = userModel.Id
return user, nil
}
func (repo *UserRepository) Update(user *domain.User) (*domain.User, error) {
userModel := models.User{
Id: user.Id,
Account: user.Account,
AvatarUrl: user.AvatarUrl,
CompanyId: user.CompanyId,
AdminType: user.AdminType,
Name: user.Name,
Status: user.Status,
UpdateAt: user.UpdateAt,
CreateAt: user.CreateAt,
DeleteAt: user.DeleteAt,
}
tx := repo.transactionContext.PgTx
_, err := tx.Model(&userModel).WherePK().Update()
if err != nil {
return nil, err
}
return user, nil
}
... ... @@ -50,7 +67,7 @@ func (repo *UserRepository) Save(user *domain.User) (*domain.User, error) {
func (repo *UserRepository) Remove(user *domain.User) (*domain.User, error) {
nowTime := time.Now()
user.DeleteAt = &nowTime
_, err := repo.Save(user)
_, err := repo.Update(user)
return user, err
}
... ... @@ -63,7 +80,7 @@ func (repo *UserRepository) FindOne(queryOptions map[string]interface{}) (*domai
}
err := query.First()
if err == pg.ErrNoRows {
return nil, errors.New("user 不存在")
return nil, ErrNoRows
}
if err != nil {
return nil, err
... ... @@ -77,6 +94,9 @@ func (repo *UserRepository) Find(queryOptions map[string]interface{}) (int, []*d
userModel := []models.User{}
query := tx.Model(&userModel).
Limit(20)
if v, ok := queryOptions["id"]; ok {
query.Where("id=?", v)
}
if v, ok := queryOptions["limit"]; ok {
query.Limit(v.(int))
}
... ... @@ -97,17 +117,15 @@ func (repo *UserRepository) Find(queryOptions map[string]interface{}) (int, []*d
func (repo *UserRepository) TransformToUserDomain(user *models.User) *domain.User {
return &domain.User{
Id: user.Id,
Account: user.Account,
AvatarUrl: user.AvatarUrl,
CompanyId: user.CompanyId,
IsPrincipal: user.IsPrincipal,
AdminType: user.AdminType,
Name: user.Name,
RoleId: user.RoleId,
Status: user.Status,
UpdateAt: user.UpdateAt,
CreateAt: user.CreateAt,
DeleteAt: user.DeleteAt,
Id: user.Id,
Account: user.Account,
AvatarUrl: user.AvatarUrl,
CompanyId: user.CompanyId,
AdminType: user.AdminType,
Name: user.Name,
Status: user.Status,
UpdateAt: user.UpdateAt,
CreateAt: user.CreateAt,
DeleteAt: user.DeleteAt,
}
}
... ...
package repository
import "github.com/go-pg/pg/v10"
var ErrNoRows = pg.ErrNoRows
... ...