dividends_order.go 7.5 KB
package service

import (
	"strconv"

	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/dividendsOrder/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/dividendsOrder/dto"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/dividendsOrder/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_cooperation"
)

// 分红订单实体对象
type DividendsOrderService struct {
}

// 创建分红订单实体对象
func (dividendsOrderService *DividendsOrderService) CreateDividendsOrder(createDividendsOrderCommand *command.CreateDividendsOrderCommand) (interface{}, error) {
	// if err := createDividendsOrderCommand.ValidateCommand(); err != nil {
	// 	return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	// }
	orderGoods := []allied_creation_cooperation.DividendsOrderGoodAdd{}
	for _, v := range createDividendsOrderCommand.OrderGoods {
		good := allied_creation_cooperation.DividendsOrderGoodAdd{
			CooperationContractNumber: v.CooperationContractNumber,
			DividendsOrderNumber:      v.DividendsOrderNumber,
			OrderGoodAmount:           0,
			OrderGoodExpense:          v.OrderGoodExpense,
			OrderGoodName:             v.OrderGoodName,
			OrderGoodPrice:            v.OrderGoodPrice,
			OrderGoodQuantity:         v.OrderGoodQuantity,
		}
		orderGoodAmount := v.OrderGoodPrice*float64(v.OrderGoodQuantity) - v.OrderGoodExpense
		good.OrderGoodAmount = orderGoodAmount
		orderGoods = append(orderGoods, good)
	}
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(createDividendsOrderCommand.Operator)
	result, err := creationCooperationGateway.DividendsOrderAdd(allied_creation_cooperation.ReqDividendsOrderAdd{
		CompanyId:                 int(createDividendsOrderCommand.Operator.CompanyId),
		UserId:                    int(createDividendsOrderCommand.Operator.UserId),
		CustomerName:              createDividendsOrderCommand.CustomerName,
		DividendsOriginalOrderNum: createDividendsOrderCommand.DividendsOriginalOrderNum,
		OperatorUid:               int(createDividendsOrderCommand.Operator.UserId),
		OrderTime:                 strconv.Itoa(int(createDividendsOrderCommand.OrderTime)),
		Remarks:                   createDividendsOrderCommand.Remarks,
		RegionName:                createDividendsOrderCommand.RegionName,
		OrderGoods:                orderGoods,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}

	return dto.ToDividendsOrder(&result.DividendsOrder), nil
}

// 返回分红订单实体对象
func (dividendsOrderService *DividendsOrderService) GetDividendsOrder(getDividendsOrderQuery *query.GetDividendsOrderQuery) (interface{}, error) {
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(getDividendsOrderQuery.Operator)
	result, err := creationCooperationGateway.DividendsOrderGet(allied_creation_cooperation.ReqDividendsOrderGet{
		DividendsOrderId:     getDividendsOrderQuery.DividendsOrderId,
		DividendsOrderNumber: getDividendsOrderQuery.DividendsOrderNumber,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return dto.ToDividendsOrder(&result.DividendsOrder), nil
}

// 移除分红订单实体对象
func (dividendsOrderService *DividendsOrderService) RemoveDividendsOrder(removeDividendsOrderCommand *command.RemoveDividendsOrderCommand) (interface{}, error) {
	// if err := removeDividendsOrderCommand.ValidateCommand(); err != nil {
	// 	return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	// }

	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(removeDividendsOrderCommand.Operator)
	_, err := creationCooperationGateway.DividendsOrderBatchRemove(allied_creation_cooperation.ReqDividendsOrderBatchRemove{
		DividendsOrderIds: removeDividendsOrderCommand.DividendsOrderId,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return removeDividendsOrderCommand, nil
}

// 查询分红订单方法
func (dividendsOrderService *DividendsOrderService) SearchDividendsOrder(searchDividendsOrderQuery *query.SearchDividendsOrderQuery) (int, interface{}, error) {
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(searchDividendsOrderQuery.Operator)
	result, err := creationCooperationGateway.DividendsOrderSearch(allied_creation_cooperation.ReqDividendsOrderSearch{
		PageNumber:           searchDividendsOrderQuery.PageNumber,
		PageSize:             searchDividendsOrderQuery.PageSize,
		CustomerName:         searchDividendsOrderQuery.CustomerName,
		DividendsOrderNumber: searchDividendsOrderQuery.DividendsOrderNumber,
	})
	if err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	dataList := []dto.DividendsOrder{}
	for i := range result.Grid.List {
		item := dto.ToDividendsOrder(&result.Grid.List[i])
		dataList = append(dataList, *item)
	}
	return result.Grid.Total, dataList, nil
}

// 更新分红订单实体对象
func (dividendsOrderService *DividendsOrderService) UpdateDividendsOrder(updateDividendsOrderCommand *command.UpdateDividendsOrderCommand) (interface{}, error) {
	// if err := updateDividendsOrderCommand.ValidateCommand(); err != nil {
	// 	return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	// }
	orderGoods := []allied_creation_cooperation.DividendsOrderGoodAdd{}
	for _, v := range updateDividendsOrderCommand.OrderGoods {
		good := allied_creation_cooperation.DividendsOrderGoodAdd{
			OrderGoodId:               int64(v.OrderGoodId),
			CooperationContractNumber: v.CooperationContractNumber,
			DividendsOrderNumber:      v.DividendsOrderNumber,
			OrderGoodExpense:          v.OrderGoodExpense,
			OrderGoodAmount:           0,
			OrderGoodName:             v.OrderGoodName,
			OrderGoodPrice:            v.OrderGoodPrice,
			OrderGoodQuantity:         v.OrderGoodQuantity,
		}
		orderGoodAmount := v.OrderGoodPrice*float64(v.OrderGoodQuantity) - v.OrderGoodExpense
		good.OrderGoodAmount = orderGoodAmount
		orderGoods = append(orderGoods, good)
	}
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(updateDividendsOrderCommand.Operator)
	_, err := creationCooperationGateway.DividendsOrderUpdate(allied_creation_cooperation.ReqDividendsOrderUpdate{
		DividendsOrderId:          updateDividendsOrderCommand.DividendsOrderID,
		CompanyId:                 int(updateDividendsOrderCommand.Operator.CompanyId),
		CustomerName:              updateDividendsOrderCommand.CustomerName,
		DividendsOrderAmount:      updateDividendsOrderCommand.DividendsOrderAmount,
		DividendsOriginalOrderNum: updateDividendsOrderCommand.DividendsOriginalOrderNum,
		OperatorUid:               int(updateDividendsOrderCommand.Operator.UserId),
		OrderTime:                 strconv.Itoa(int(updateDividendsOrderCommand.OrderTime)),
		Remarks:                   updateDividendsOrderCommand.Remarks,
		RegionName:                updateDividendsOrderCommand.RegionName,
		OrderGoods:                orderGoods,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return updateDividendsOrderCommand, nil
}

func NewDividendsOrderService(options map[string]interface{}) *DividendsOrderService {
	newDividendsOrderService := &DividendsOrderService{}
	return newDividendsOrderService
}