作者 陈志颖

feat:分红退货单增加批量删除

  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type BatchRemoveDividendsReturnedOrderCommand struct {
  12 + // 分红退货单ID列表
  13 + DividendsReturnedOrderIds []string `cname:"分红退货单ID列表" json:"dividendsReturnedOrderIds,omitempty"`
  14 + // 公司ID,通过集成REST上下文获取
  15 + CompanyId int64 `cname:"公司ID" json:"companyId,string" valid:"Required"`
  16 + // 组织机构ID
  17 + OrgId int64 `cname:"组织机构ID" json:"orgId,string" valid:"Required"`
  18 + // 用户ID,通过集成REST上下文获取,可翻译成发起人、承接人、推荐人、业务员
  19 + UserId int64 `cname:"用户ID" json:"userId,string" valid:"Required"`
  20 + // 用户基础数据id
  21 + UserBaseId int64 `cname:"用户基础数据ID" json:"userBaseId,string" valid:"Required"`
  22 +}
  23 +
  24 +func (batchRemoveDividendsReturnedOrderCommand *BatchRemoveDividendsReturnedOrderCommand) Valid(validation *validation.Validation) {
  25 + //validation.SetError("CustomValid", "未实现的自定义认证")
  26 +}
  27 +
  28 +func (batchRemoveDividendsReturnedOrderCommand *BatchRemoveDividendsReturnedOrderCommand) ValidateCommand() error {
  29 + valid := validation.Validation{}
  30 + b, err := valid.Valid(batchRemoveDividendsReturnedOrderCommand)
  31 + if err != nil {
  32 + return err
  33 + }
  34 + if !b {
  35 + elem := reflect.TypeOf(batchRemoveDividendsReturnedOrderCommand).Elem()
  36 + for _, validErr := range valid.Errors {
  37 + field, isExist := elem.FieldByName(validErr.Field)
  38 + if isExist {
  39 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  40 + } else {
  41 + return fmt.Errorf(validErr.Message)
  42 + }
  43 + }
  44 + }
  45 + return nil
  46 +}
@@ -10,6 +10,7 @@ import ( @@ -10,6 +10,7 @@ import (
10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service" 11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service"
12 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao" 12 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
  13 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
13 "time" 14 "time"
14 ) 15 )
15 16
@@ -306,6 +307,51 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) RemoveDivide @@ -306,6 +307,51 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) RemoveDivide
306 } 307 }
307 } 308 }
308 309
  310 +// BatchRemoveDividendsReturnedOrder 批量移除分红退货单实体对象
  311 +func (dividendsReturnedOrderService *DividendsReturnedOrderService) BatchRemoveDividendsReturnedOrder(batchRemoveDividendsOrderCommand *command.BatchRemoveDividendsReturnedOrderCommand) (interface{}, error) {
  312 + if err := batchRemoveDividendsOrderCommand.ValidateCommand(); err != nil {
  313 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  314 + }
  315 + transactionContext, err := factory.CreateTransactionContext(nil)
  316 + if err != nil {
  317 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  318 + }
  319 + if err := transactionContext.StartTransaction(); err != nil {
  320 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  321 + }
  322 + defer func() {
  323 + _ = transactionContext.RollbackTransaction()
  324 + }()
  325 + var dividendsReturnedOrderRepository domain.DividendsReturnedOrderRepository
  326 + if value, err := factory.CreateDividendsReturnedOrderRepository(map[string]interface{}{
  327 + "transactionContext": transactionContext,
  328 + }); err != nil {
  329 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  330 + } else {
  331 + dividendsReturnedOrderRepository = value
  332 + }
  333 +
  334 + dividendsReturnedOrderIds, _ := utils.SliceAtoi(batchRemoveDividendsOrderCommand.DividendsReturnedOrderIds)
  335 + if count, dividendsReturnedOrders, err := dividendsReturnedOrderRepository.Find(map[string]interface{}{
  336 + "dividendsReturnedOrderIds": dividendsReturnedOrderIds,
  337 + }); err != nil {
  338 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  339 + } else {
  340 + if count > 0 {
  341 + dividendsOrdersRemoved, err := dividendsReturnedOrderRepository.BatchRemove(dividendsReturnedOrders)
  342 + if err != nil {
  343 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  344 + }
  345 + if err := transactionContext.CommitTransaction(); err != nil {
  346 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  347 + }
  348 + return dividendsOrdersRemoved, nil
  349 + } else {
  350 + return map[string]interface{}{}, nil
  351 + }
  352 + }
  353 +}
  354 +
309 // SearchDividendsReturnedOrder 查找分红退货单 355 // SearchDividendsReturnedOrder 查找分红退货单
310 func (dividendsReturnedOrderService *DividendsReturnedOrderService) SearchDividendsReturnedOrder(searchDividendsReturnedOrderQuery *query.SearchDividendsReturnedOrderQuery) (interface{}, error) { 356 func (dividendsReturnedOrderService *DividendsReturnedOrderService) SearchDividendsReturnedOrder(searchDividendsReturnedOrderQuery *query.SearchDividendsReturnedOrderQuery) (interface{}, error) {
311 if err := searchDividendsReturnedOrderQuery.ValidateQuery(); err != nil { 357 if err := searchDividendsReturnedOrderQuery.ValidateQuery(); err != nil {
@@ -47,6 +47,7 @@ type DividendsReturnedOrder struct { @@ -47,6 +47,7 @@ type DividendsReturnedOrder struct {
47 type DividendsReturnedOrderRepository interface { 47 type DividendsReturnedOrderRepository interface {
48 Save(dividendsReturnedOrder *DividendsReturnedOrder) (*DividendsReturnedOrder, error) 48 Save(dividendsReturnedOrder *DividendsReturnedOrder) (*DividendsReturnedOrder, error)
49 Remove(dividendsReturnedOrder *DividendsReturnedOrder) (*DividendsReturnedOrder, error) 49 Remove(dividendsReturnedOrder *DividendsReturnedOrder) (*DividendsReturnedOrder, error)
  50 + BatchRemove(dividendsReturnedOrders []*DividendsReturnedOrder) ([]*DividendsReturnedOrder, error)
50 FindOne(queryOptions map[string]interface{}) (*DividendsReturnedOrder, error) 51 FindOne(queryOptions map[string]interface{}) (*DividendsReturnedOrder, error)
51 Find(queryOptions map[string]interface{}) (int64, []*DividendsReturnedOrder, error) 52 Find(queryOptions map[string]interface{}) (int64, []*DividendsReturnedOrder, error)
52 } 53 }
@@ -331,6 +331,9 @@ func (repository *DividendsOrderRepository) Find(queryOptions map[string]interfa @@ -331,6 +331,9 @@ func (repository *DividendsOrderRepository) Find(queryOptions map[string]interfa
331 if customerName, ok := queryOptions["customerName"]; ok && customerName != "" { 331 if customerName, ok := queryOptions["customerName"]; ok && customerName != "" {
332 query.Where("customer_name like ?", fmt.Sprintf("%%%s%%", customerName)) 332 query.Where("customer_name like ?", fmt.Sprintf("%%%s%%", customerName))
333 } 333 }
  334 + if dividendsOrderIds, ok := queryOptions["dividendsOrderIds"]; ok && len(dividendsOrderIds.([]int64)) > 0 {
  335 + query.Where("dividends_order_id IN (?)", pg.In(dividendsOrderIds.([]int64)))
  336 + }
334 offsetLimitFlag := true 337 offsetLimitFlag := true
335 if offsetLimit, ok := queryOptions["offsetLimit"]; ok { 338 if offsetLimit, ok := queryOptions["offsetLimit"]; ok {
336 offsetLimitFlag = offsetLimit.(bool) 339 offsetLimitFlag = offsetLimit.(bool)
@@ -96,7 +96,7 @@ func (repository *DividendsReturnedOrderRepository) Save(dividendsReturnedOrder @@ -96,7 +96,7 @@ func (repository *DividendsReturnedOrderRepository) Save(dividendsReturnedOrder
96 dividendsReturnedOrder.Org, 96 dividendsReturnedOrder.Org,
97 dividendsReturnedOrder.Company, 97 dividendsReturnedOrder.Company,
98 dividendsReturnedOrder.CreatedAt, 98 dividendsReturnedOrder.CreatedAt,
99 - dividendsReturnedOrder.DeletedAt, 99 + nil,
100 dividendsReturnedOrder.UpdatedAt, 100 dividendsReturnedOrder.UpdatedAt,
101 dividendsReturnedOrder.Operator, 101 dividendsReturnedOrder.Operator,
102 dividendsReturnedOrder.OperateTime, 102 dividendsReturnedOrder.OperateTime,
@@ -162,7 +162,7 @@ func (repository *DividendsReturnedOrderRepository) Save(dividendsReturnedOrder @@ -162,7 +162,7 @@ func (repository *DividendsReturnedOrderRepository) Save(dividendsReturnedOrder
162 dividendsReturnedOrder.Org, 162 dividendsReturnedOrder.Org,
163 dividendsReturnedOrder.Company, 163 dividendsReturnedOrder.Company,
164 dividendsReturnedOrder.CreatedAt, 164 dividendsReturnedOrder.CreatedAt,
165 - dividendsReturnedOrder.DeletedAt, 165 + nil,
166 dividendsReturnedOrder.UpdatedAt, 166 dividendsReturnedOrder.UpdatedAt,
167 dividendsReturnedOrder.Operator, 167 dividendsReturnedOrder.Operator,
168 dividendsReturnedOrder.OperateTime, 168 dividendsReturnedOrder.OperateTime,
@@ -273,6 +273,28 @@ func (repository *DividendsReturnedOrderRepository) Remove(dividendsReturnedOrde @@ -273,6 +273,28 @@ func (repository *DividendsReturnedOrderRepository) Remove(dividendsReturnedOrde
273 return dividendsReturnedOrder, nil 273 return dividendsReturnedOrder, nil
274 } 274 }
275 275
  276 +func (repository *DividendsReturnedOrderRepository) BatchRemove(dividendsReturnedOrders []*domain.DividendsReturnedOrder) ([]*domain.DividendsReturnedOrder, error) {
  277 + tx := repository.transactionContext.PgTx
  278 + var dividendsReturnedOrderModels []*models.DividendsReturnedOrder
  279 + for _, dividendsReturnedOrder := range dividendsReturnedOrders {
  280 + dividendsReturnedOrderModels = append(dividendsReturnedOrderModels, &models.DividendsReturnedOrder{
  281 + DividendsReturnedOrderId: dividendsReturnedOrder.Identify().(int64),
  282 + })
  283 + }
  284 + if _, err := tx.Model(&dividendsReturnedOrderModels).WherePK().Delete(); err != nil {
  285 + return dividendsReturnedOrders, err
  286 + } else {
  287 + for _, dividendsReturnedOrder := range dividendsReturnedOrders {
  288 + // 删除退货单产品
  289 + var orderGoodModels []*models.OrderGood
  290 + if _, err := tx.Model(&orderGoodModels).Where("dividends_returned_order_number = ?", dividendsReturnedOrder.DividendsReturnedOrderNumber).Delete(); err != nil {
  291 + return nil, err
  292 + }
  293 + }
  294 + }
  295 + return dividendsReturnedOrders, nil
  296 +}
  297 +
276 func (repository *DividendsReturnedOrderRepository) FindOne(queryOptions map[string]interface{}) (*domain.DividendsReturnedOrder, error) { 298 func (repository *DividendsReturnedOrderRepository) FindOne(queryOptions map[string]interface{}) (*domain.DividendsReturnedOrder, error) {
277 tx := repository.transactionContext.PgTx 299 tx := repository.transactionContext.PgTx
278 dividendsReturnedOrderModel := new(models.DividendsReturnedOrder) 300 dividendsReturnedOrderModel := new(models.DividendsReturnedOrder)
@@ -304,6 +326,9 @@ func (repository *DividendsReturnedOrderRepository) Find(queryOptions map[string @@ -304,6 +326,9 @@ func (repository *DividendsReturnedOrderRepository) Find(queryOptions map[string
304 var dividendsReturnedOrderModels []*models.DividendsReturnedOrder 326 var dividendsReturnedOrderModels []*models.DividendsReturnedOrder
305 dividendsReturnedOrders := make([]*domain.DividendsReturnedOrder, 0) 327 dividendsReturnedOrders := make([]*domain.DividendsReturnedOrder, 0)
306 query := sqlbuilder.BuildQuery(tx.Model(&dividendsReturnedOrderModels), queryOptions) 328 query := sqlbuilder.BuildQuery(tx.Model(&dividendsReturnedOrderModels), queryOptions)
  329 + if dividendsReturnedOrderIds, ok := queryOptions["dividendsReturnedOrderIds"]; ok && len(dividendsReturnedOrderIds.([]int64)) > 0 {
  330 + query.Where("dividends_returned_order_id IN (?)", pg.In(dividendsReturnedOrderIds.([]int64)))
  331 + }
307 offsetLimitFlag := true 332 offsetLimitFlag := true
308 if offsetLimit, ok := queryOptions["offsetLimit"]; ok { 333 if offsetLimit, ok := queryOptions["offsetLimit"]; ok {
309 offsetLimitFlag = offsetLimit.(bool) 334 offsetLimitFlag = offsetLimit.(bool)
@@ -10,6 +10,7 @@ func init() { @@ -10,6 +10,7 @@ func init() {
10 web.Router("/dividends-orders/:dividendsOrderId", &controllers.DividendsOrderController{}, "Put:UpdateDividendsOrder") 10 web.Router("/dividends-orders/:dividendsOrderId", &controllers.DividendsOrderController{}, "Put:UpdateDividendsOrder")
11 web.Router("/dividends-orders/:dividendsOrderId", &controllers.DividendsOrderController{}, "Get:GetDividendsOrder") 11 web.Router("/dividends-orders/:dividendsOrderId", &controllers.DividendsOrderController{}, "Get:GetDividendsOrder")
12 web.Router("/dividends-orders/:dividendsOrderId", &controllers.DividendsOrderController{}, "Delete:RemoveDividendsOrder") 12 web.Router("/dividends-orders/:dividendsOrderId", &controllers.DividendsOrderController{}, "Delete:RemoveDividendsOrder")
  13 + web.Router("/dividends-orders/batch-remove", &controllers.DividendsOrderController{}, "Post:BatchRemoveDividendsOrder")
13 web.Router("/dividends-orders/search", &controllers.DividendsOrderController{}, "Post:SearchDividendsOrder") 14 web.Router("/dividends-orders/search", &controllers.DividendsOrderController{}, "Post:SearchDividendsOrder")
14 web.Router("/dividends-orders/search-order-number", &controllers.DividendsOrderController{}, "Post:SearchDividendsOrderNumber") 15 web.Router("/dividends-orders/search-order-number", &controllers.DividendsOrderController{}, "Post:SearchDividendsOrderNumber")
15 web.Router("/dividends-orders/", &controllers.DividendsOrderController{}, "Get:ListDividendsOrders") 16 web.Router("/dividends-orders/", &controllers.DividendsOrderController{}, "Get:ListDividendsOrders")