dividends_order.go 7.5 KB
package service

import (
	"time"

	"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.DividendsOrderGoods{}
	for _, v := range createDividendsOrderCommand.OrderGoods {
		good := allied_creation_cooperation.DividendsOrderGoods{
			CooperationContractNumber: v.CooperationContractNumber,
			DividendsOrderNumber:      v.DividendsOrderNumber,
			OrderGoodAmount:           v.OrderGoodAmount,
			OrderGoodExpense:          v.OrderGoodExpense,
			OrderGoodName:             v.OrderGoodName,
			OrderGoodPrice:            v.OrderGoodPrice,
			OrderGoodQuantity:         v.OrderGoodQuantity,
		}
		orderGoods = append(orderGoods, good)
	}
	t := time.Unix(createDividendsOrderCommand.OrderTime, 0)
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(createDividendsOrderCommand.Operator)
	result, err := creationCooperationGateway.DividendsOrderAdd(allied_creation_cooperation.ReqDividendsOrderAdd{
		CompanyId:                 int(createDividendsOrderCommand.Operator.CompanyId),
		CustomerName:              createDividendsOrderCommand.CustomerName,
		DividendsOriginalOrderNum: createDividendsOrderCommand.DividendsOriginalOrderNum,
		OperatorUid:               int(createDividendsOrderCommand.Operator.UserId),
		OrderTime:                 t,
		Remarks:                   createDividendsOrderCommand.Remarks,
		RegionName:                createDividendsOrderCommand.RegionName,
		OrderGoods:                orderGoods,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	data := struct {
		DividendsOrderId int
		command.CreateDividendsOrderCommand
	}{
		DividendsOrderId:            result.DividendsOrderID,
		CreateDividendsOrderCommand: *createDividendsOrderCommand,
	}
	return data, nil
}

// 返回分红订单实体对象
func (dividendsOrderService *DividendsOrderService) GetDividendsOrder(getDividendsOrderQuery *query.GetDividendsOrderQuery) (interface{}, error) {
	if err := getDividendsOrderQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(getDividendsOrderQuery.Operator)
	result, err := creationCooperationGateway.DividendsOrderGet(allied_creation_cooperation.ReqDividendsOrderGet{
		DividendsOrderId: getDividendsOrderQuery.DividendsOrderId,
	})
	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) {
	if err := searchDividendsOrderQuery.ValidateQuery(); err != nil {
		return 0, nil, application.ThrowError(application.ARG_ERROR, err.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())
	}
	var dataList []dto.DividendsOrder
	for i := range result.Gride.List {
		item := dto.ToDividendsOrder(&result.Gride.List[i])
		dataList = append(dataList, *item)
	}
	return result.Gride.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.DividendsOrderGoods{}
	for _, v := range updateDividendsOrderCommand.OrderGoods {
		good := allied_creation_cooperation.DividendsOrderGoods{
			CooperationContractNumber: v.CooperationContractNumber,
			DividendsOrderNumber:      v.DividendsOrderNumber,
			OrderGoodAmount:           v.OrderGoodAmount,
			OrderGoodExpense:          v.OrderGoodExpense,
			OrderGoodName:             v.OrderGoodName,
			OrderGoodPrice:            v.OrderGoodPrice,
			OrderGoodQuantity:         v.OrderGoodQuantity,
		}
		orderGoods = append(orderGoods, good)
	}
	t := time.Unix(updateDividendsOrderCommand.OrderTime, 0)
	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:                 t,
		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
}