sync_data_service.go
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package service
import (
"encoding/json"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/position/command"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
type SyncDataPositionService struct{}
func (service SyncDataPositionService) FromBusinessAdmin(param *domain.MessageBody) error {
var err error
switch param.Action {
//新增-编辑职位
case "add", "edit":
createPositionCommand := &command.SavePositionCommand{}
err = json.Unmarshal(param.Data, createPositionCommand)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
err = service.CreateOrUpdatePosition(createPositionCommand)
//批量删除职位
case "batchDelete":
batchDeletePositionCommand := &command.BatchDeletePositionCommand{}
err = json.Unmarshal(param.Data, batchDeletePositionCommand)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
err = service.BatchDeletePosition(batchDeletePositionCommand)
}
return err
}
// CreateOrUpdatePosition 新增职位
func (service SyncDataPositionService) CreateOrUpdatePosition(positionCommand *command.SavePositionCommand) 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()
}()
positionRepository := factory.CreatePositionRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
position := &domain.Position{
Id: positionCommand.Id,
CompanyId: positionCommand.CompanyId,
Name: positionCommand.Name,
ParentId: positionCommand.ParentId,
Path: positionCommand.Path,
Level: positionCommand.Level,
}
pdm, _ := positionRepository.FindOne(map[string]interface{}{"id": position.Id})
if pdm != nil && pdm.Id > 0 {
_, err = positionRepository.Update(position)
} else {
_, err = positionRepository.Insert(position)
}
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
}
// BatchDeletePosition 批量删除职位
func (service SyncDataPositionService) BatchDeletePosition(batchDeletePositionCommand *command.BatchDeletePositionCommand) 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()
}()
positionRepository := factory.CreatePositionRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
err = positionRepository.Remove(batchDeletePositionCommand.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
}