作者 陈志颖

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

package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type BatchRemoveDividendsReturnedOrderCommand struct {
// 分红退货单ID列表
DividendsReturnedOrderIds []string `cname:"分红退货单ID列表" json:"dividendsReturnedOrderIds,omitempty"`
// 公司ID,通过集成REST上下文获取
CompanyId int64 `cname:"公司ID" json:"companyId,string" valid:"Required"`
// 组织机构ID
OrgId int64 `cname:"组织机构ID" json:"orgId,string" valid:"Required"`
// 用户ID,通过集成REST上下文获取,可翻译成发起人、承接人、推荐人、业务员
UserId int64 `cname:"用户ID" json:"userId,string" valid:"Required"`
// 用户基础数据id
UserBaseId int64 `cname:"用户基础数据ID" json:"userBaseId,string" valid:"Required"`
}
func (batchRemoveDividendsReturnedOrderCommand *BatchRemoveDividendsReturnedOrderCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (batchRemoveDividendsReturnedOrderCommand *BatchRemoveDividendsReturnedOrderCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(batchRemoveDividendsReturnedOrderCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(batchRemoveDividendsReturnedOrderCommand).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
... ... @@ -10,6 +10,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
"time"
)
... ... @@ -306,6 +307,51 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) RemoveDivide
}
}
// BatchRemoveDividendsReturnedOrder 批量移除分红退货单实体对象
func (dividendsReturnedOrderService *DividendsReturnedOrderService) BatchRemoveDividendsReturnedOrder(batchRemoveDividendsOrderCommand *command.BatchRemoveDividendsReturnedOrderCommand) (interface{}, error) {
if err := batchRemoveDividendsOrderCommand.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()
}()
var dividendsReturnedOrderRepository domain.DividendsReturnedOrderRepository
if value, err := factory.CreateDividendsReturnedOrderRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
dividendsReturnedOrderRepository = value
}
dividendsReturnedOrderIds, _ := utils.SliceAtoi(batchRemoveDividendsOrderCommand.DividendsReturnedOrderIds)
if count, dividendsReturnedOrders, err := dividendsReturnedOrderRepository.Find(map[string]interface{}{
"dividendsReturnedOrderIds": dividendsReturnedOrderIds,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if count > 0 {
dividendsOrdersRemoved, err := dividendsReturnedOrderRepository.BatchRemove(dividendsReturnedOrders)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return dividendsOrdersRemoved, nil
} else {
return map[string]interface{}{}, nil
}
}
}
// SearchDividendsReturnedOrder 查找分红退货单
func (dividendsReturnedOrderService *DividendsReturnedOrderService) SearchDividendsReturnedOrder(searchDividendsReturnedOrderQuery *query.SearchDividendsReturnedOrderQuery) (interface{}, error) {
if err := searchDividendsReturnedOrderQuery.ValidateQuery(); err != nil {
... ...
... ... @@ -47,6 +47,7 @@ type DividendsReturnedOrder struct {
type DividendsReturnedOrderRepository interface {
Save(dividendsReturnedOrder *DividendsReturnedOrder) (*DividendsReturnedOrder, error)
Remove(dividendsReturnedOrder *DividendsReturnedOrder) (*DividendsReturnedOrder, error)
BatchRemove(dividendsReturnedOrders []*DividendsReturnedOrder) ([]*DividendsReturnedOrder, error)
FindOne(queryOptions map[string]interface{}) (*DividendsReturnedOrder, error)
Find(queryOptions map[string]interface{}) (int64, []*DividendsReturnedOrder, error)
}
... ...
... ... @@ -331,6 +331,9 @@ func (repository *DividendsOrderRepository) Find(queryOptions map[string]interfa
if customerName, ok := queryOptions["customerName"]; ok && customerName != "" {
query.Where("customer_name like ?", fmt.Sprintf("%%%s%%", customerName))
}
if dividendsOrderIds, ok := queryOptions["dividendsOrderIds"]; ok && len(dividendsOrderIds.([]int64)) > 0 {
query.Where("dividends_order_id IN (?)", pg.In(dividendsOrderIds.([]int64)))
}
offsetLimitFlag := true
if offsetLimit, ok := queryOptions["offsetLimit"]; ok {
offsetLimitFlag = offsetLimit.(bool)
... ...
... ... @@ -96,7 +96,7 @@ func (repository *DividendsReturnedOrderRepository) Save(dividendsReturnedOrder
dividendsReturnedOrder.Org,
dividendsReturnedOrder.Company,
dividendsReturnedOrder.CreatedAt,
dividendsReturnedOrder.DeletedAt,
nil,
dividendsReturnedOrder.UpdatedAt,
dividendsReturnedOrder.Operator,
dividendsReturnedOrder.OperateTime,
... ... @@ -162,7 +162,7 @@ func (repository *DividendsReturnedOrderRepository) Save(dividendsReturnedOrder
dividendsReturnedOrder.Org,
dividendsReturnedOrder.Company,
dividendsReturnedOrder.CreatedAt,
dividendsReturnedOrder.DeletedAt,
nil,
dividendsReturnedOrder.UpdatedAt,
dividendsReturnedOrder.Operator,
dividendsReturnedOrder.OperateTime,
... ... @@ -273,6 +273,28 @@ func (repository *DividendsReturnedOrderRepository) Remove(dividendsReturnedOrde
return dividendsReturnedOrder, nil
}
func (repository *DividendsReturnedOrderRepository) BatchRemove(dividendsReturnedOrders []*domain.DividendsReturnedOrder) ([]*domain.DividendsReturnedOrder, error) {
tx := repository.transactionContext.PgTx
var dividendsReturnedOrderModels []*models.DividendsReturnedOrder
for _, dividendsReturnedOrder := range dividendsReturnedOrders {
dividendsReturnedOrderModels = append(dividendsReturnedOrderModels, &models.DividendsReturnedOrder{
DividendsReturnedOrderId: dividendsReturnedOrder.Identify().(int64),
})
}
if _, err := tx.Model(&dividendsReturnedOrderModels).WherePK().Delete(); err != nil {
return dividendsReturnedOrders, err
} else {
for _, dividendsReturnedOrder := range dividendsReturnedOrders {
// 删除退货单产品
var orderGoodModels []*models.OrderGood
if _, err := tx.Model(&orderGoodModels).Where("dividends_returned_order_number = ?", dividendsReturnedOrder.DividendsReturnedOrderNumber).Delete(); err != nil {
return nil, err
}
}
}
return dividendsReturnedOrders, nil
}
func (repository *DividendsReturnedOrderRepository) FindOne(queryOptions map[string]interface{}) (*domain.DividendsReturnedOrder, error) {
tx := repository.transactionContext.PgTx
dividendsReturnedOrderModel := new(models.DividendsReturnedOrder)
... ... @@ -304,6 +326,9 @@ func (repository *DividendsReturnedOrderRepository) Find(queryOptions map[string
var dividendsReturnedOrderModels []*models.DividendsReturnedOrder
dividendsReturnedOrders := make([]*domain.DividendsReturnedOrder, 0)
query := sqlbuilder.BuildQuery(tx.Model(&dividendsReturnedOrderModels), queryOptions)
if dividendsReturnedOrderIds, ok := queryOptions["dividendsReturnedOrderIds"]; ok && len(dividendsReturnedOrderIds.([]int64)) > 0 {
query.Where("dividends_returned_order_id IN (?)", pg.In(dividendsReturnedOrderIds.([]int64)))
}
offsetLimitFlag := true
if offsetLimit, ok := queryOptions["offsetLimit"]; ok {
offsetLimitFlag = offsetLimit.(bool)
... ...
... ... @@ -10,6 +10,7 @@ func init() {
web.Router("/dividends-orders/:dividendsOrderId", &controllers.DividendsOrderController{}, "Put:UpdateDividendsOrder")
web.Router("/dividends-orders/:dividendsOrderId", &controllers.DividendsOrderController{}, "Get:GetDividendsOrder")
web.Router("/dividends-orders/:dividendsOrderId", &controllers.DividendsOrderController{}, "Delete:RemoveDividendsOrder")
web.Router("/dividends-orders/batch-remove", &controllers.DividendsOrderController{}, "Post:BatchRemoveDividendsOrder")
web.Router("/dividends-orders/search", &controllers.DividendsOrderController{}, "Post:SearchDividendsOrder")
web.Router("/dividends-orders/search-order-number", &controllers.DividendsOrderController{}, "Post:SearchDividendsOrderNumber")
web.Router("/dividends-orders/", &controllers.DividendsOrderController{}, "Get:ListDividendsOrders")
... ...