|
|
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
|
|
|
} |
...
|
...
|
|