作者 tangxvhui

更新

package command
// 添加部门
type AddDepartmentCommand struct {
Id int64 `json:"id" ` // 组织id
CompanyId int64 `json:"company_id"` // 公司编号
... ...
package command
type BatchDeleteCommand struct {
Ids []int64 `json:"ids"`
}
... ...
package command
type EditDepartmentCommand struct {
Id int64 `json:"id" ` // 组织id
CompanyId int64 `json:"company_id"` // 公司编号
Level int `json:"level"` // 组织名称
Name string `json:"name"` // 组织名称
ParentId int64 `json:"parent_id"` // 组织父级id
ChargeUserIds []int64 `json:"charge"` // 主管uids
Path string `json:"path"` // 组织路径
ChangeDepartmentLists []struct {
Id int64 `json:"id"` // 组织id
Level int `json:"level"` // 组织名称
Path string `json:"path"` // 组织路径
} `json:"change_department_list"`
}
... ...
package command
//导入部门数据
type ImportDepartmentCommand struct {
Id int64 `json:"id"` // 组织id
CompanyId int64 `json:"company_id"` // 公司编号
Level int `json:"level"` // 组织级别
Name string `json:"name"` // 组织名称
ParentId int64 `json:"parent_id"` // 组织父级id
Path string `json:"path"` // 组织路径
}
... ...
package department
import (
"time"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/department/command"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
type SyncDataDepartmentService struct{}
//AddDepartment
//从BusinessAdmins 接收消息 添加部门
//module="department" action="add"
func (srv SyncDataDepartmentService) AddDepartment() error {
func (srv SyncDataDepartmentService) addDepartment(param command.AddDepartmentCommand) 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()
newDepartment := domain.Department{
Id: param.Id,
CompanyId: param.CompanyId,
Level: param.Level,
Name: param.Name,
ParentId: param.ParentId,
ChargeUserIds: param.ChargeUserIds,
Path: param.Path,
CreateAt: nowTime,
UpdateAt: nowTime,
DeleteAt: nil,
}
departmentRepo := factory.CreateDepartmentRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
_, err = departmentRepo.Insert(&newDepartment)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil
}
//EditDepartment
//从BusinessAdmins 接收消息 编辑部门
//module="department" action="edit"
func (srv SyncDataDepartmentService) EditDepartment() error {
func (srv SyncDataDepartmentService) editDepartment(param command.EditDepartmentCommand) 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()
}()
departmentIds := []int64{param.Id}
for _, v := range param.ChangeDepartmentLists {
departmentIds = append(departmentIds, v.Id)
}
departmentRepo := factory.CreateDepartmentRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
_, departmentList, err := departmentRepo.Find(map[string]interface{}{
"ids": departmentIds,
})
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
for i := range departmentList {
if departmentList[i].Id == param.Id {
departmentList[i].CompanyId = param.CompanyId
departmentList[i].Name = param.Name
departmentList[i].Name = param.Path
departmentList[i].ChargeUserIds = param.ChargeUserIds
departmentList[i].Level = param.Level
departmentList[i].ParentId = param.ParentId
continue
}
for _, vv := range param.ChangeDepartmentLists {
if vv.Id == departmentList[i].Id {
departmentList[i].Path = vv.Path
departmentList[i].Level = vv.Level
break
}
}
}
for i := range departmentList {
_, err := departmentRepo.Update(departmentList[i])
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil
}
//batchDelete
//从BusinessAdmins 接收消息 删除部门
//module="department" action="batchDelete"
func (srv SyncDataDepartmentService) batchDelete() error {
func (srv SyncDataDepartmentService) batchDeleteDepartment(param command.BatchDeleteCommand) error {
if len(param.Ids) == 0 {
return nil
}
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()
}()
departmentRepo := factory.CreateDepartmentRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
err = departmentRepo.Remove(param.Ids)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil
}
//importDepartment
//从BusinessAdmins 接收消息 导入部门数据
//module="department" action="import"
func (srv SyncDataDepartmentService) importDepartment() error {
func (srv SyncDataDepartmentService) importDepartment(param []command.ImportDepartmentCommand) 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()
}()
departmentRepo := factory.CreateDepartmentRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
nowTime := time.Now()
for i := range param {
newDepartment := domain.Department{
Id: param[i].Id,
CompanyId: param[i].CompanyId,
Level: param[i].Level,
Name: param[i].Name,
ParentId: param[i].ParentId,
ChargeUserIds: []int64{},
Path: param[i].Path,
CreateAt: nowTime,
UpdateAt: nowTime,
DeleteAt: nil,
}
_, err = departmentRepo.Insert(&newDepartment)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil
}
... ...
... ... @@ -27,3 +27,11 @@ func CreateUserRepository(options map[string]interface{}) domain.UserRepository
}
return repository.NewUserRepository(transactionContext)
}
func CreateDepartmentRepository(options map[string]interface{}) domain.DepartmentRepository {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewDepartmentRepository(transactionContext)
}
... ...
package command
type SaveUserCommand struct {
Id int64 ` json:"id"` // 用户Id
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禁用)
UserDepartments []struct {
DepartmentId int `json:"department_id" `
} `json:"user_departments"` //用户的组织ids
}
... ...
... ... @@ -25,6 +25,10 @@ func (srv SyncDataUserService) AddUser(param command.SaveUserCommand) error {
defer func() {
_ = transactionContext.RollbackTransaction()
}()
var departmentIds []int
for _, v := range param.UserDepartments {
departmentIds = append(departmentIds, v.DepartmentId)
}
nowTime := time.Now()
newUser := domain.User{
Id: param.Id,
... ... @@ -32,6 +36,7 @@ func (srv SyncDataUserService) AddUser(param command.SaveUserCommand) error {
AvatarUrl: param.Avatar,
CompanyId: param.CompanyId,
AdminType: param.AdminType,
DepartmentId: departmentIds,
Name: param.Name,
Status: param.Status,
UpdateAt: nowTime,
... ... @@ -43,10 +48,10 @@ func (srv SyncDataUserService) AddUser(param command.SaveUserCommand) error {
})
_, err = userRepo.Insert(&newUser)
if err != nil {
return err
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return err
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil
}
... ... @@ -73,7 +78,7 @@ func (srv SyncDataUserService) UpdateUser(param command.SaveUserCommand) error {
"id": param.Id,
})
if err != nil {
return err
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var (
newUser *domain.User
... ... @@ -97,16 +102,16 @@ func (srv SyncDataUserService) UpdateUser(param command.SaveUserCommand) error {
if len(userList) > 0 {
_, err = userRepo.Update(newUser)
if err != nil {
return err
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
} else {
_, err = userRepo.Insert(newUser)
if err != nil {
return err
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return err
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
}
... ... @@ -134,10 +139,10 @@ func (srv SyncDataUserService) batchDelete(param command.BatchDeleteCommand) err
err = userRepo.Remove(param.Uids)
if err != nil {
return err
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return err
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
}
... ... @@ -167,17 +172,17 @@ func (srv SyncDataUserService) batchForbid(param command.BatchForbidCommand) err
"limit": len(param.Uids),
})
if err != nil {
return err
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
for i := range userList {
userList[i].Status = param.Status
_, err = userRepo.Update(userList[i])
if err != nil {
return err
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return err
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
}
... ... @@ -210,7 +215,7 @@ func (srv SyncDataUserService) importUser(param command.ImportUserCommand) error
"ids": editUserIds,
})
if err != nil {
return err
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
nowTime := time.Now()
for i := range editUserList {
... ... @@ -248,11 +253,11 @@ func (srv SyncDataUserService) importUser(param command.ImportUserCommand) error
}
_, err := userRepo.Insert(&tempUser)
if err != nil {
return err
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return err
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
... ...
... ... @@ -17,7 +17,7 @@ type Department struct {
type DepartmentRepository interface {
Insert(param *Department) (*Department, error)
Update(param *Department) (*Department, error)
Remove(param *Department) (*Department, error)
Remove(ids []int64) error
FindOne(queryOptions map[string]interface{}) (*Department, error)
Find(queryOptions map[string]interface{}) (int, []*Department, error)
}
... ...
... ... @@ -10,6 +10,7 @@ type User struct {
AdminType int // 1普通员工 2 主管理员
Name string // 用户姓名
Status int // 用户状态(1正常 2禁用)
DepartmentId []int // 用户归属的部门
UpdateAt time.Time // 更新时间
DeleteAt *time.Time
CreateAt time.Time
... ...
... ... @@ -11,6 +11,7 @@ type User struct {
AdminType int // 1普通员工 2 主管理员
Name string // 用户姓名
Status int // 用户状态(1正常 2禁用)
DepartmentId []int // 用户归属的部门
UpdateAt time.Time // 更新时间
CreateAt time.Time // 创建时间
DeleteAt *time.Time // 删除时间
... ...
... ... @@ -15,8 +15,8 @@ type DepartmentRepository struct {
var _ domain.DepartmentRepository = (*DepartmentRepository)(nil)
func NewDepartmentRepository(tx *pgTransaction.TransactionContext) *CompanyRepository {
return &CompanyRepository{
func NewDepartmentRepository(tx *pgTransaction.TransactionContext) *DepartmentRepository {
return &DepartmentRepository{
transactionContext: tx,
}
}
... ... @@ -54,7 +54,7 @@ func (repo *DepartmentRepository) Update(u *domain.Department) (*domain.Departme
Path: u.Path,
CreateAt: u.CreateAt,
UpdateAt: u.UpdateAt,
DeleteAt: nil,
DeleteAt: u.DeleteAt,
}
tx := repo.transactionContext.PgTx
_, err := tx.Model(&departmentModel).WherePK().Update()
... ... @@ -64,11 +64,15 @@ func (repo *DepartmentRepository) Update(u *domain.Department) (*domain.Departme
return u, nil
}
func (repo *DepartmentRepository) Remove(u *domain.Department) (*domain.Department, error) {
func (repo *DepartmentRepository) Remove(ids []int64) error {
nowTime := time.Now()
u.DeleteAt = &nowTime
_, err := repo.Update(u)
return u, err
tx := repo.transactionContext.PgTx
uModel := models.Department{}
_, err := tx.Model(&uModel).
Set("delete_at", nowTime).
Where("id in (?)", pg.In(ids)).
Update()
return err
}
func (repo *DepartmentRepository) FindOne(queryOptions map[string]interface{}) (*domain.Department, error) {
... ... @@ -97,6 +101,9 @@ func (repo *DepartmentRepository) Find(queryOptions map[string]interface{}) (int
if v, ok := queryOptions["id"]; ok {
query.Where("id=?", v)
}
if v, ok := queryOptions["ids"]; ok {
query.Where("id in(?)", pg.In(v))
}
if v, ok := queryOptions["limit"]; ok {
query.Limit(v.(int))
}
... ...