作者 陈志颖

feat:增加分红退货单导入功能

@@ -261,8 +261,18 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -261,8 +261,18 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
261 dividendsOrderDao = value 261 dividendsOrderDao = value
262 } 262 }
263 263
  264 + // 分红订单仓储初始化
  265 + var dividendsOrderRepository domain.DividendsOrderRepository
  266 + if value, err1 := factory.CreateDividendsOrderRepository(map[string]interface{}{
  267 + "transactionContext": transactionContext,
  268 + }); err1 != nil {
  269 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err1.Error())
  270 + } else {
  271 + dividendsOrderRepository = value
  272 + }
  273 +
264 var dividendsOrderImportFailed []*domain.DividendsOrder 274 var dividendsOrderImportFailed []*domain.DividendsOrder
265 - var dividendsOrderImportSuccesfully []*domain.DividendsOrder 275 + var dividendsOrderImportSuccessfully []*domain.DividendsOrder
266 276
267 for _, dividendsOrder := range importDividendsOrderCommand.DividendsOrderData { 277 for _, dividendsOrder := range importDividendsOrderCommand.DividendsOrderData {
268 // 生成分红订单号 278 // 生成分红订单号
@@ -272,13 +282,13 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -272,13 +282,13 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
272 } 282 }
273 283
274 // 校验分红订单编号是否唯一 284 // 校验分红订单编号是否唯一
275 - numberAvailable, err := dividendsOrderDao.CheckDividendsOrderNumberAvailable(map[string]interface{}{ 285 + numberAvailable, err3 := dividendsOrderDao.CheckDividendsOrderNumberAvailable(map[string]interface{}{
276 "companyId": importDividendsOrderCommand.CompanyId, 286 "companyId": importDividendsOrderCommand.CompanyId,
277 "orgId": importDividendsOrderCommand.OrgId, 287 "orgId": importDividendsOrderCommand.OrgId,
278 "dividendsOrderNumber": dividendsOrderNumber, 288 "dividendsOrderNumber": dividendsOrderNumber,
279 }) 289 })
280 - if err != nil {  
281 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 290 + if err3 != nil {
  291 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err3.Error())
282 } 292 }
283 if !numberAvailable { 293 if !numberAvailable {
284 return nil, application.ThrowError(application.TRANSACTION_ERROR, "新增分红订单异常") 294 return nil, application.ThrowError(application.TRANSACTION_ERROR, "新增分红订单异常")
@@ -332,26 +342,18 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD @@ -332,26 +342,18 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
332 Operator: operator, 342 Operator: operator,
333 } 343 }
334 344
335 - // 分红订单仓储初始化  
336 - var dividendsOrderRepository domain.DividendsOrderRepository  
337 - if value, err1 := factory.CreateDividendsOrderRepository(map[string]interface{}{  
338 - "transactionContext": transactionContext,  
339 - }); err1 != nil {  
340 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err1.Error())  
341 - } else {  
342 - dividendsOrderRepository = value  
343 - }  
344 if dividendsOrderSaved, err2 := dividendsOrderRepository.Save(newDividendsOrder); err2 != nil { 345 if dividendsOrderSaved, err2 := dividendsOrderRepository.Save(newDividendsOrder); err2 != nil {
345 dividendsOrderImportFailed = append(dividendsOrderImportFailed, newDividendsOrder) 346 dividendsOrderImportFailed = append(dividendsOrderImportFailed, newDividendsOrder)
346 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err2.Error())  
347 } else { 347 } else {
348 - dividendsOrderImportSuccesfully = append(dividendsOrderImportSuccesfully, dividendsOrderSaved)  
349 - return dividendsOrderSaved, nil 348 + dividendsOrderImportSuccessfully = append(dividendsOrderImportSuccessfully, dividendsOrderSaved)
350 } 349 }
351 } 350 }
  351 + if len(dividendsOrderImportFailed) == 0 {
352 if err3 := transactionContext.CommitTransaction(); err3 != nil { 352 if err3 := transactionContext.CommitTransaction(); err3 != nil {
353 return nil, application.ThrowError(application.TRANSACTION_ERROR, err3.Error()) 353 return nil, application.ThrowError(application.TRANSACTION_ERROR, err3.Error())
354 } 354 }
  355 + return dividendsOrderImportSuccessfully, nil
  356 + }
355 return nil, nil 357 return nil, nil
356 } 358 }
357 359
@@ -207,7 +207,7 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) GetDividends @@ -207,7 +207,7 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) GetDividends
207 } 207 }
208 } 208 }
209 209
210 -// ImportDividendsReturnedOrder TODO 导入分红退货单 210 +// ImportDividendsReturnedOrder 导入分红退货单
211 func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDividendsReturnedOrder(importDividendsReturnedOrderCommand *command.ImportDividendsReturnedOrderCommand) (interface{}, error) { 211 func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDividendsReturnedOrder(importDividendsReturnedOrderCommand *command.ImportDividendsReturnedOrderCommand) (interface{}, error) {
212 if err := importDividendsReturnedOrderCommand.ValidateCommand(); err != nil { 212 if err := importDividendsReturnedOrderCommand.ValidateCommand(); err != nil {
213 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 213 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
@@ -222,9 +222,141 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide @@ -222,9 +222,141 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
222 defer func() { 222 defer func() {
223 _ = transactionContext.RollbackTransaction() 223 _ = transactionContext.RollbackTransaction()
224 }() 224 }()
  225 +
  226 + // 公司REST服务初始化
  227 + var companyService service.CompanyService
  228 + if value, err := factory.CreateCompanyService(map[string]interface{}{}); err != nil {
  229 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  230 + } else {
  231 + companyService = value
  232 + }
  233 +
  234 + // 获取公司信息
  235 + var company *domain.Company
  236 + if data, err := companyService.CompanyFrom(importDividendsReturnedOrderCommand.CompanyId); err != nil {
  237 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  238 + } else {
  239 + company = data
  240 + }
  241 +
  242 + // 组织机构REST服务初始化
  243 + var organizationService service.OrgService
  244 + if value, err := factory.CreateOrganizationService(map[string]interface{}{}); err != nil {
  245 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  246 + } else {
  247 + organizationService = value
  248 + }
  249 +
  250 + // 获取组织机构信息
  251 + var organization *domain.Org
  252 + if data, err := organizationService.OrgFrom(importDividendsReturnedOrderCommand.CompanyId, importDividendsReturnedOrderCommand.OrgId); err != nil {
  253 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  254 + } else {
  255 + organization = data
  256 + }
  257 +
  258 + // 用户REST服务初始化
  259 + var userService service.UserService
  260 + if value, err := factory.CreateUserService(map[string]interface{}{}); err != nil {
  261 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  262 + } else {
  263 + userService = value
  264 + }
  265 +
  266 + // 获取操作人
  267 + var operator *domain.User
  268 + if data, err := userService.OperatorFrom(importDividendsReturnedOrderCommand.CompanyId, importDividendsReturnedOrderCommand.OrgId, importDividendsReturnedOrderCommand.UserId); err != nil {
  269 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  270 + } else {
  271 + operator = data
  272 + }
  273 +
  274 + // 分红退货单DAO初始化
  275 + var dividendsReturnedOrderDao *dao.DividendsReturnedOrderDao
  276 + if value, err := factory.CreateDividendsReturnedOrderDao(map[string]interface{}{
  277 + "transactionContext": transactionContext,
  278 + }); err != nil {
  279 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  280 + } else {
  281 + dividendsReturnedOrderDao = value
  282 + }
  283 +
  284 + // 分红退货单仓储初始化
  285 + var dividendsReturnedOrderRepository domain.DividendsReturnedOrderRepository
  286 + if value, err := factory.CreateDividendsReturnedOrderRepository(map[string]interface{}{
  287 + "transactionContext": transactionContext,
  288 + }); err != nil {
  289 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  290 + } else {
  291 + dividendsReturnedOrderRepository = value
  292 + }
  293 +
  294 + var dividendsReturnedOrderImportFailed []*domain.DividendsReturnedOrder
  295 + var dividendsReturnedOrderImportSuccessfully []*domain.DividendsReturnedOrder
  296 +
  297 + for _, dividendsReturnedOrder := range importDividendsReturnedOrderCommand.DividendsReturnedOrderData {
  298 + // 生成分红订单号
  299 + dividendsReturnedOrderNumber, err := dividendsReturnedOrderDao.GenerateDividendsReturnedOrderNumber()
  300 + if err != nil {
  301 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  302 + }
  303 +
  304 + // 获取分红退货单产品
  305 + var orderGoods []*domain.OrderGood
  306 + for _, orderGood := range dividendsReturnedOrder.OrderGoods {
  307 + orderGoods = append(orderGoods, &domain.OrderGood{
  308 + OrderGoodId: 0,
  309 + OrderGoodAmount: orderGood.OrderGoodAmount,
  310 + OrderGoodName: orderGood.OrderGoodName,
  311 + OrderGoodPrice: orderGood.OrderGoodPrice,
  312 + OrderGoodQuantity: orderGood.OrderGoodQuantity,
  313 + DividendsOrderNumber: "",
  314 + DividendsReturnedOrderNumber: dividendsReturnedOrderNumber,
  315 + CooperationContractNumber: orderGood.CooperationContractNumber,
  316 + OrderGoodExpense: 0,
  317 + OrgId: importDividendsReturnedOrderCommand.OrgId,
  318 + CompanyId: importDividendsReturnedOrderCommand.CompanyId,
  319 + CreatedAt: time.Now(),
  320 + DeletedAt: time.Time{},
  321 + UpdatedAt: time.Time{},
  322 + })
  323 + }
  324 +
  325 + newDividendsReturnedOrder := &domain.DividendsReturnedOrder{
  326 + DividendsReturnedOrderNumber: dividendsReturnedOrderNumber,
  327 + DividendsReturnedOrderRefund: dividendsReturnedOrder.DividendsReturnedOrderRefund,
  328 + OriginalOrderNum: dividendsReturnedOrder.OriginalOrderNum,
  329 + DividendsOrderNumber: "",
  330 + DividendsReturnedCustomerName: dividendsReturnedOrder.DividendsReturnedCustomerName,
  331 + DividendsReturnedDate: dividendsReturnedOrder.DividendsReturnedDate,
  332 + Region: &domain.RegionInfo{
  333 + RegionNumber: "",
  334 + RegionName: dividendsReturnedOrder.RegionName,
  335 + },
  336 + Goods: orderGoods,
  337 + Remarks: dividendsReturnedOrder.Remarks,
  338 + DividendStatus: domain.TO_BE_DIVIDENDED,
  339 + DividendTime: time.Time{},
  340 + Org: organization,
  341 + Company: company,
  342 + CreatedAt: time.Now(),
  343 + UpdatedAt: time.Time{},
  344 + Operator: operator,
  345 + OperateTime: time.Now(),
  346 + }
  347 +
  348 + if dividendsReturnedOrderSaved, err := dividendsReturnedOrderRepository.Save(newDividendsReturnedOrder); err != nil {
  349 + dividendsReturnedOrderImportFailed = append(dividendsReturnedOrderImportFailed, newDividendsReturnedOrder)
  350 + } else {
  351 + dividendsReturnedOrderImportSuccessfully = append(dividendsReturnedOrderImportSuccessfully, dividendsReturnedOrderSaved)
  352 + }
  353 + }
  354 + if len(dividendsReturnedOrderImportFailed) == 0 {
225 if err := transactionContext.CommitTransaction(); err != nil { 355 if err := transactionContext.CommitTransaction(); err != nil {
226 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 356 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
227 } 357 }
  358 + return dividendsReturnedOrderImportSuccessfully, nil
  359 + }
228 return nil, nil 360 return nil, nil
229 } 361 }
230 362