reward_standard.go
7.0 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package service
import (
"time"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/rewardStandard/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/rewardStandard/dto"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/rewardStandard/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
)
type RewardStandardService struct{}
func NewProductService(options map[string]interface{}) *RewardStandardService {
newService := &RewardStandardService{}
return newService
}
//SaveRewardStandard 保存奖惩标准数据
func (srv RewardStandardService) SaveRewardStandard(operateInfo domain.OperateInfo, param *command.SaveRewardStandardCommand) (*command.SaveRewardStandardCommand, error) {
if err := param.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
rewardStandardRepo, _ := factory.CreateRewardStandardRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
_, dataList, err := rewardStandardRepo.Find(map[string]interface{}{
"limit": 1,
"workshopId": param.WorkshopId,
"lineId": param.LineId,
"sectionId": param.SectionId,
"targetType": param.TargetType,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if len(dataList) > 0 {
if dataList[0].Id != param.Id {
return nil, application.ThrowError(application.BUSINESS_ERROR, "当前指标已存在")
}
}
var rewardStandardData *domain.RewardStandard
if param.Id > 0 {
rewardStandardData, err = rewardStandardRepo.FindOne(map[string]interface{}{
"id": param.Id,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, "指标数据不存在"+err.Error())
}
if rewardStandardData.CompanyId != operateInfo.CompanyId {
return nil, application.ThrowError(application.BUSINESS_ERROR, "指标数据填写错误")
}
} else {
rewardStandardData = &domain.RewardStandard{
CreatedAt: time.Now(),
}
}
workShopRepo, _ := factory.CreateWorkshopRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
//获取车间数据
workShopData, err := workShopRepo.FindOne(map[string]interface{}{
"workshopId": param.WorkshopId,
})
if err != nil {
return nil, application.ThrowError(application.ARG_ERROR, "车间数据不能存在"+err.Error())
}
if workShopData.CompanyId != operateInfo.CompanyId {
return nil, application.ThrowError(application.ARG_ERROR, "车间数据填写错误")
}
productLineData, err := workShopData.FindLine(param.LineId)
if err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
productSection, err := productLineData.FindSection(param.SectionId)
if err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
rewardStandardData.UpdatedAt = time.Now()
rewardStandardData.ProductSection = *productSection
rewardStandardData.ProductLine = productLineData.SimpleProductLine()
rewardStandardData.Workshop = workShopData.SimpleWorkshop()
rewardStandardData.Remark = param.Remark
err = rewardStandardData.UpdateTarge(
param.TargetType,
param.TargeVal1,
param.TargeVal2,
param.TargeVal3,
param.TargeVal4)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
_, err = rewardStandardRepo.Save(rewardStandardData)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
//GetRewardStandard 根据id获取奖惩标准数据
func (srv RewardStandardService) GetRewardStandard(param *query.GetRewardStandard) (*dto.RewardStandardInfo, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
rewardStandardRepo, _ := factory.CreateRewardStandardRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
rewardStandardData, err := rewardStandardRepo.FindOne(map[string]interface{}{
"id": param.Id,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, "指标数据不存在"+err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
result := dto.RewardStandardInfo{
Id: rewardStandardData.Id,
WorkshopId: rewardStandardData.Workshop.WorkshopId,
WorkshopName: rewardStandardData.Workshop.WorkshopName,
LineId: rewardStandardData.ProductLine.LineId,
LineName: rewardStandardData.ProductLine.LineName,
SectionId: rewardStandardData.ProductSection.SectionId,
SectionName: rewardStandardData.ProductSection.SectionName,
Remark: rewardStandardData.Remark,
TargetType: rewardStandardData.TargetType,
TargeVal1: rewardStandardData.TargeVal1,
TargeVal2: rewardStandardData.TargeVal2,
TargeVal3: rewardStandardData.TargeVal3,
TargeVal4: rewardStandardData.TargeVal4,
}
return &result, nil
}
//DeleteRewardStandard 根据id删除奖惩标准
func (srv RewardStandardService) DeleteRewardStandard(param *query.GetRewardStandard) (*domain.RewardStandard, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
rewardStandardRepo, _ := factory.CreateRewardStandardRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
rewardStandardData, err := rewardStandardRepo.FindOne(map[string]interface{}{
"id": param.Id,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, "指标数据不存在"+err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return rewardStandardData, nil
}