作者 陈志颖

refactor:优化取消分红预算服务,整合领域服务,增加校验

@@ -177,6 +177,17 @@ func (dividendsEstimateService *DividendsEstimateService) CancelDividendsEstimat @@ -177,6 +177,17 @@ func (dividendsEstimateService *DividendsEstimateService) CancelDividendsEstimat
177 defer func() { 177 defer func() {
178 _ = transactionContext.RollbackTransaction() 178 _ = transactionContext.RollbackTransaction()
179 }() 179 }()
  180 +
  181 + // 取消分红预算领域服务初始化
  182 + var cancelDividendsEstimateService service.CancelDividendsEstimate
  183 + if value, err := factory.CreateCancelDividendsEstimateService(map[string]interface{}{
  184 + "transactionContext": transactionContext,
  185 + }); err != nil {
  186 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  187 + } else {
  188 + cancelDividendsEstimateService = value
  189 + }
  190 +
180 // 分红预算仓储初始化 191 // 分红预算仓储初始化
181 var dividendsEstimateRepository domain.DividendsEstimateRepository 192 var dividendsEstimateRepository domain.DividendsEstimateRepository
182 if value, err := factory.CreateDividendsEstimateRepository(map[string]interface{}{ 193 if value, err := factory.CreateDividendsEstimateRepository(map[string]interface{}{
@@ -186,6 +197,8 @@ func (dividendsEstimateService *DividendsEstimateService) CancelDividendsEstimat @@ -186,6 +197,8 @@ func (dividendsEstimateService *DividendsEstimateService) CancelDividendsEstimat
186 } else { 197 } else {
187 dividendsEstimateRepository = value 198 dividendsEstimateRepository = value
188 } 199 }
  200 +
  201 + // 获取当前被取消的分红预算单
189 dividendsEstimate, err := dividendsEstimateRepository.FindOne(map[string]interface{}{"dividendsEstimateId": cancelDividendsEstimateCommand.DividendsEstimateId}) 202 dividendsEstimate, err := dividendsEstimateRepository.FindOne(map[string]interface{}{"dividendsEstimateId": cancelDividendsEstimateCommand.DividendsEstimateId})
190 if err != nil { 203 if err != nil {
191 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 204 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
@@ -193,20 +206,42 @@ func (dividendsEstimateService *DividendsEstimateService) CancelDividendsEstimat @@ -193,20 +206,42 @@ func (dividendsEstimateService *DividendsEstimateService) CancelDividendsEstimat
193 if dividendsEstimate == nil { 206 if dividendsEstimate == nil {
194 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(cancelDividendsEstimateCommand.DividendsEstimateId, 10))) 207 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(cancelDividendsEstimateCommand.DividendsEstimateId, 10)))
195 } 208 }
196 - // TODO 校验是否能够取消  
197 209
198 - // 设置取消状态  
199 - cancelDividendsEstimateCommand.IsCanceled = true  
200 - if err := dividendsEstimate.Update(tool_funs.SimpleStructToMap(cancelDividendsEstimateCommand)); err != nil {  
201 - return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) 210 + // 校验是否能够取消
  211 + if dividendsEstimate.DividendsType == domain.ORDER_DIVIDENDS || dividendsEstimate.DividendsType == domain.RETURN_WRITE_OFF { // 订单分红、退货冲销
  212 + if count, dividendsEstimatesRelative, err2 := dividendsEstimateRepository.Find(map[string]interface{}{"orderOrReturnedOrderNum": dividendsEstimate.OrderOrReturnedOrderNum}); err2 != nil {
  213 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err2.Error())
  214 + } else {
  215 + if count > 0 {
  216 + for _, dividendsEstimateRelative := range dividendsEstimatesRelative {
  217 + if dividendsEstimateRelative.DividendsAccountStatus == domain.ACCOUNTED {
  218 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已有相关分红预算单已结算,无法取消")
  219 + }
  220 + }
  221 + }
  222 + }
  223 + } else if dividendsEstimate.DividendsType == domain.MONEY_INCENTIVES { // 金额激励分红
  224 + if count, dividendsEstimatesRelative, err3 := dividendsEstimateRepository.Find(map[string]interface{}{"cooperationContractNumber": dividendsEstimate.CooperationContractNumber}); err3 != nil {
  225 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err3.Error())
  226 + } else {
  227 + if count > 0 {
  228 + for _, dividendsEstimateRelative := range dividendsEstimatesRelative {
  229 + if dividendsEstimateRelative.DividendsAccountStatus == domain.ACCOUNTED {
  230 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已有相关分红预算单已结算,无法取消")
  231 + }
  232 + }
  233 + }
  234 + }
202 } 235 }
203 - if dividendsEstimate, err := dividendsEstimateRepository.Save(dividendsEstimate); err != nil { 236 +
  237 + // 取消分红预算
  238 + if dividendsEstimatesCanceled, err := cancelDividendsEstimateService.CancelEstimate([]int64{cancelDividendsEstimateCommand.DividendsEstimateId}, cancelDividendsEstimateCommand.UserId); err != nil {
204 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 239 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
205 } else { 240 } else {
206 if err := transactionContext.CommitTransaction(); err != nil { 241 if err := transactionContext.CommitTransaction(); err != nil {
207 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 242 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
208 } 243 }
209 - return dividendsEstimate, nil 244 + return dividendsEstimatesCanceled[1], nil
210 } 245 }
211 } 246 }
212 247
@@ -225,6 +260,17 @@ func (dividendsEstimateService *DividendsEstimateService) BatchCancelDividendsEs @@ -225,6 +260,17 @@ func (dividendsEstimateService *DividendsEstimateService) BatchCancelDividendsEs
225 defer func() { 260 defer func() {
226 _ = transactionContext.RollbackTransaction() 261 _ = transactionContext.RollbackTransaction()
227 }() 262 }()
  263 +
  264 + // 取消分红预算领域服务初始化
  265 + var cancelDividendsEstimateService service.CancelDividendsEstimate
  266 + if value, err := factory.CreateCancelDividendsEstimateService(map[string]interface{}{
  267 + "transactionContext": transactionContext,
  268 + }); err != nil {
  269 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  270 + } else {
  271 + cancelDividendsEstimateService = value
  272 + }
  273 +
228 // 分红预算仓储初始化 274 // 分红预算仓储初始化
229 var dividendsEstimateRepository domain.DividendsEstimateRepository 275 var dividendsEstimateRepository domain.DividendsEstimateRepository
230 if value, err := factory.CreateDividendsEstimateRepository(map[string]interface{}{ 276 if value, err := factory.CreateDividendsEstimateRepository(map[string]interface{}{
@@ -234,24 +280,58 @@ func (dividendsEstimateService *DividendsEstimateService) BatchCancelDividendsEs @@ -234,24 +280,58 @@ func (dividendsEstimateService *DividendsEstimateService) BatchCancelDividendsEs
234 } else { 280 } else {
235 dividendsEstimateRepository = value 281 dividendsEstimateRepository = value
236 } 282 }
237 - dividendsEstimateIds, _ := utils.SliceAtoi(batchCancelEstimateCommand.DividendsEstimateIds) 283 +
  284 + // 分红预算ID类型转换
  285 + dividendsEstimateIds, err := utils.SliceAtoi(batchCancelEstimateCommand.DividendsEstimateIds)
  286 + if err != nil {
  287 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "分红预算单参数转换错误")
  288 + }
  289 +
  290 + // 获取分红预算
238 if count, dividendsEstimates, err := dividendsEstimateRepository.Find(map[string]interface{}{ 291 if count, dividendsEstimates, err := dividendsEstimateRepository.Find(map[string]interface{}{
239 "dividendsEstimateIds": dividendsEstimateIds, 292 "dividendsEstimateIds": dividendsEstimateIds,
240 }); err != nil { 293 }); err != nil {
241 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 294 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
242 } else { 295 } else {
243 if count > 0 { 296 if count > 0 {
244 - for i, _ := range dividendsEstimates {  
245 - dividendsEstimates[i].IsCanceled = true  
246 - }  
247 - dividendsEstimatesCanceled, err := dividendsEstimateRepository.UpdateMany(dividendsEstimates)  
248 - if err != nil {  
249 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 297 + // 校验分红预算是否可以取消,已有相关分红预算单已结算,无法取消
  298 + for _, dividendsEstimate := range dividendsEstimates {
  299 + // 校验是否能够取消
  300 + if dividendsEstimate.DividendsType == domain.ORDER_DIVIDENDS || dividendsEstimate.DividendsType == domain.RETURN_WRITE_OFF { // 订单分红、退货冲销
  301 + if count, dividendsEstimatesRelative, err2 := dividendsEstimateRepository.Find(map[string]interface{}{"orderOrReturnedOrderNum": dividendsEstimate.OrderOrReturnedOrderNum}); err2 != nil {
  302 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err2.Error())
  303 + } else {
  304 + if count > 0 {
  305 + for _, dividendsEstimateRelative := range dividendsEstimatesRelative {
  306 + if dividendsEstimateRelative.DividendsAccountStatus == domain.ACCOUNTED {
  307 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已有相关分红预算单已结算,无法取消")
  308 + }
  309 + }
  310 + }
  311 + }
  312 + } else if dividendsEstimate.DividendsType == domain.MONEY_INCENTIVES { // 金额激励分红
  313 + if count, dividendsEstimatesRelative, err3 := dividendsEstimateRepository.Find(map[string]interface{}{"cooperationContractNumber": dividendsEstimate.CooperationContractNumber}); err3 != nil {
  314 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err3.Error())
  315 + } else {
  316 + if count > 0 {
  317 + for _, dividendsEstimateRelative := range dividendsEstimatesRelative {
  318 + if dividendsEstimateRelative.DividendsAccountStatus == domain.ACCOUNTED {
  319 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已有相关分红预算单已结算,无法取消")
  320 + }
  321 + }
  322 + }
  323 + }
  324 + }
250 } 325 }
251 - if err := transactionContext.CommitTransaction(); err != nil {  
252 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 326 + // 批量取消分红预算单
  327 + if dividendsEstimatesCanceled, err := cancelDividendsEstimateService.CancelEstimate(dividendsEstimateIds, batchCancelEstimateCommand.UserId); err != nil {
  328 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  329 + } else {
  330 + if err := transactionContext.CommitTransaction(); err != nil {
  331 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  332 + }
  333 + return dividendsEstimatesCanceled, nil
253 } 334 }
254 - return dividendsEstimatesCanceled, nil  
255 } else { 335 } else {
256 return map[string]interface{}{}, nil 336 return map[string]interface{}{}, nil
257 } 337 }
@@ -92,9 +92,9 @@ func (domainService *CancelDividendsEstimateService) CancelEstimate(dividendsEst @@ -92,9 +92,9 @@ func (domainService *CancelDividendsEstimateService) CancelEstimate(dividendsEst
92 // 订单/退货单号数组去重 92 // 订单/退货单号数组去重
93 orderOrReturnOrderNumsWithoutDup := utils.RemoveDuplication(orderOrReturnOrderNums) 93 orderOrReturnOrderNumsWithoutDup := utils.RemoveDuplication(orderOrReturnOrderNums)
94 for _, orderNullNum := range orderOrReturnOrderNumsWithoutDup { 94 for _, orderNullNum := range orderOrReturnOrderNumsWithoutDup {
95 - if orderNullNum[0:2] == "RE" { // 分红退后单 95 + if orderNullNum[:2] == "RE" { // 分红退后单
96 returnedOrderNums = append(returnedOrderNums, orderNullNum) 96 returnedOrderNums = append(returnedOrderNums, orderNullNum)
97 - } else if orderNullNum[0:2] == "FH" { // 分红订单 97 + } else if orderNullNum[:2] == "SL" { // 分红订单
98 orderNums = append(orderNums, orderNullNum) 98 orderNums = append(orderNums, orderNullNum)
99 } 99 }
100 } 100 }
@@ -258,6 +258,9 @@ func (repository *DividendsEstimateRepository) FindOne(queryOptions map[string]i @@ -258,6 +258,9 @@ func (repository *DividendsEstimateRepository) FindOne(queryOptions map[string]i
258 dividendsEstimateModel := new(models.DividendsEstimate) 258 dividendsEstimateModel := new(models.DividendsEstimate)
259 query := sqlbuilder.BuildQuery(tx.Model(dividendsEstimateModel), queryOptions) 259 query := sqlbuilder.BuildQuery(tx.Model(dividendsEstimateModel), queryOptions)
260 query.SetWhereByQueryOption("dividends_estimate.dividends_estimate_id = ?", "dividendsEstimateId") 260 query.SetWhereByQueryOption("dividends_estimate.dividends_estimate_id = ?", "dividendsEstimateId")
  261 + if orderOrReturnedOrderNum, ok := queryOptions["orderOrReturnedOrderNum"]; ok && orderOrReturnedOrderNum != "" {
  262 + query.Where("order_or_returned_order_num = ?", orderOrReturnedOrderNum)
  263 + }
261 if err := query.First(); err != nil { 264 if err := query.First(); err != nil {
262 if err.Error() == "pg: no rows in result set" { 265 if err.Error() == "pg: no rows in result set" {
263 return nil, fmt.Errorf("没有此资源") 266 return nil, fmt.Errorf("没有此资源")
@@ -286,6 +289,9 @@ func (repository *DividendsEstimateRepository) Find(queryOptions map[string]inte @@ -286,6 +289,9 @@ func (repository *DividendsEstimateRepository) Find(queryOptions map[string]inte
286 if dividendsEstimateIds, ok := queryOptions["dividendsEstimateIds"]; ok && len(dividendsEstimateIds.([]int64)) > 0 { 289 if dividendsEstimateIds, ok := queryOptions["dividendsEstimateIds"]; ok && len(dividendsEstimateIds.([]int64)) > 0 {
287 query.Where("dividends_estimate_id IN (?)", pg.In(dividendsEstimateIds)) 290 query.Where("dividends_estimate_id IN (?)", pg.In(dividendsEstimateIds))
288 } 291 }
  292 + if orderOrReturnedOrderNum, ok := queryOptions["orderOrReturnedOrderNum"]; ok && orderOrReturnedOrderNum != "" {
  293 + query.Where("order_or_returned_order_num = ?", orderOrReturnedOrderNum)
  294 + }
289 if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 { 295 if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
290 query.Where("company->>'companyId' = '?'", companyId) 296 query.Where("company->>'companyId' = '?'", companyId)
291 } 297 }