作者 tangxvhui

Merge branch 'dev' into test

# Conflicts:
#	pkg/application/order/command/update_order.go
#	pkg/application/order/service/order.go
#	pkg/domain/order.go
#	pkg/infrastructure/repository/pg_order_repository.go
#	pkg/port/beego/controllers/order_controller.go
... ... @@ -20,19 +20,3 @@ func CreatePartnerInfoDao(options map[string]interface{}) (*dao.PartnerInfoDao,
}
return dao.NewPartnerInfoDao(transactionContext)
}
func CreateOrderDao(options map[string]interface{}) (*dao.OrderDao, error) {
var transactionContext *transaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*transaction.TransactionContext)
}
return dao.NewOrderDao(transactionContext)
}
func CreateOrderPaymentDao(options map[string]interface{}) (*dao.OrderPayment, error) {
var transactionContext *transaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*transaction.TransactionContext)
}
return dao.NewOrderPayment(transactionContext)
}
... ...
... ... @@ -33,24 +33,6 @@ func CreateAdminPermissionRepository(options map[string]interface{}) (domain.Adm
return repository.NewAdminPermissionRepository(transactionContext)
}
//CreateOrderPaymentRepository 分红单信息
func CreateOrderPaymentRepository(options map[string]interface{}) (domain.OrderPaymentRepository, error) {
var transactionContext *transaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*transaction.TransactionContext)
}
return repository.NewOrderPaymentRepository(transactionContext)
}
//CreateOrderRepository 订单信息
func CreateOrderRepository(options map[string]interface{}) (domain.OrderRepository, error) {
var transactionContext *transaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*transaction.TransactionContext)
}
return repository.NewOrderRepository(transactionContext)
}
//CreateOrderBaseRepository 订单信息
func CreateOrderBaseRepository(options map[string]interface{}) (domain.OrderBaseRepository, error) {
var transactionContext *transaction.TransactionContext
... ...
package command
import (
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
type CreateOrderCommand struct {
//订单区域
OrderRegion string
//订单编号
OrderCode string `json:"orderCode"`
//订单名称
OrderName string `json:"oderName"`
//数量
OrderCount int `json:"orderCount"`
//实际订单数量
OrderActualCount int `json:"orderActualCount"`
//订单金额
OrderAmount float64 `json:"orderAmount"`
//订单实际金额
OrderActualAmount float64 `json:"orderActualAmount"`
//订单状态
OrderStatus int `json:"orderStatus"`
//买家
BuyerName string `json:"buyerName"`
//买家电话
BuyerPhone string `json:"buyerPhone"`
//地址
BuyerAddress string `json:"address"`
//合伙人数据
PartnerId int64 `json:"partId"`
//合伙人分红百分比
PartnerBonusPercent float64 `json:"partnerBonusPercent"`
//业务员分红百分比
SalesmanBonusPercent float64 `json:"salesmanBonusPercent"`
//订单类型
OrderType int `json:"orderType"`
}
func (command CreateOrderCommand) ValidateCommand() error {
if len(command.BuyerName) == 0 || len(command.BuyerPhone) == 0 {
return lib.ThrowError(lib.ARG_ERROR, "买家信息必填")
}
if len(command.BuyerAddress) == 0 {
return lib.ThrowError(lib.ARG_ERROR, "买家地址必填")
}
if len(command.OrderCode) == 0 {
return lib.ThrowError(lib.ARG_ERROR, "订单编号必填")
}
if len(command.OrderRegion) == 0 {
return lib.ThrowError(lib.ARG_ERROR, "订单区域必填")
}
if !(command.OrderType == domain.OrderReal ||
command.OrderType == domain.OrderIntention) {
return lib.ThrowError(lib.ARG_ERROR, "订单类型错误")
}
if !(command.OrderStatus == domain.OrderStatusDeliverSome ||
command.OrderStatus == domain.OrderStatusDeliverAll ||
command.OrderStatus == domain.OrderStatusReserve) {
return lib.ThrowError(lib.ARG_ERROR, "订单状态设置错误")
}
return nil
}
package command
import (
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
type UpdateOrderCommand struct {
//id
Id int64 `json:"id"`
//订单编号
OrderCode string `json:"orderCode"`
//订单名称
OrderName string `json:"oderName"`
//数量
OrderCount int `json:"orderCount"`
//订单金额
OrderAmount float64 `json:"orderAmount"`
//买家
BuyerPhone string `json:"buyerPhone"`
//地址
BuyerAddress string `json:"address"`
//订单区域
OrderRegion string `json:"orderRegion"`
//合伙人分红百分比
PartnerBonusPercent float64 `json:"partnerBonusPercent"`
//业务员分红百分比
SalesmanBonusPercent float64 `json:"salesmanBonusPercent"`
//订单状态
OrderStatus int `json:"orderStatus"`
//订单类型
OrderType int `json:"orderType"`
}
func (command UpdateOrderCommand) ValidateCommand() error {
if command.Id == 0 {
return lib.ThrowError(lib.ARG_ERROR, "订单id错误")
}
if len(command.BuyerPhone) == 0 {
return lib.ThrowError(lib.ARG_ERROR, "买家信息必填")
}
if len(command.BuyerAddress) == 0 {
return lib.ThrowError(lib.ARG_ERROR, "买家地址必填")
}
if len(command.OrderCode) == 0 {
return lib.ThrowError(lib.ARG_ERROR, "订单编号必填")
}
if len(command.OrderRegion) == 0 {
return lib.ThrowError(lib.ARG_ERROR, "订单区域必填")
}
if !(command.OrderType == domain.OrderReal ||
command.OrderType == domain.OrderIntention) {
return lib.ThrowError(lib.ARG_ERROR, "订单类型错误")
}
if !(command.OrderStatus == domain.OrderStatusDeliverSome ||
command.OrderStatus == domain.OrderStatusDeliverAll ||
command.OrderStatus == domain.OrderStatusReserve) {
return lib.ThrowError(lib.ARG_ERROR, "订单状态设置错误")
}
return nil
}
package command
import (
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
type UpdateOrderRealCommand struct {
//id
Id int64 `json:"id"`
//订单编号
OrderCode string `json:"orderCode"`
//订单名称
OrderName string `json:"oderName"`
//数量
OrderActualCount int `json:"orderActualCount"`
//订单金额
OrderActualAmount float64 `json:"orderActualAmount"`
//买家
BuyerPhone string `json:"buyerPhone"`
//地址
BuyerAddress string `json:"address"`
//订单区域
OrderRegion string `json:"orderRegion"`
//合伙人分红百分比
PartnerBonusPercent float64 `json:"partnerBonusPercent"`
//业务员分红百分比
SalesmanBonusPercent float64 `json:"salesmanBonusPercent"`
//状态
OrderStatus int `json:"orderStatus"`
Reason string `json:"reason"`
}
func (command UpdateOrderRealCommand) ValidateCommand() error {
if command.Id == 0 {
return lib.ThrowError(lib.ARG_ERROR, "订单id错误")
}
if len(command.BuyerPhone) == 0 {
return lib.ThrowError(lib.ARG_ERROR, "买家信息必填")
}
if len(command.BuyerAddress) == 0 {
return lib.ThrowError(lib.ARG_ERROR, "买家地址必填")
}
if len(command.OrderCode) == 0 {
return lib.ThrowError(lib.ARG_ERROR, "订单编号必填")
}
if len(command.OrderRegion) == 0 {
return lib.ThrowError(lib.ARG_ERROR, "订单区域必填")
}
if !(command.OrderStatus == domain.OrderStatusDeliverSome ||
command.OrderStatus == domain.OrderStatusDeliverAll) {
return lib.ThrowError(lib.ARG_ERROR, "订单状态设置错误")
}
return nil
}
package query
//GetOrderQuery 获取订单详情
type GetOrderQuery struct {
OrderId int64 `json:"orderId"`
}
package query
//ListOrderQuery 获取订单列表
type ListOrderQuery struct {
//合伙人id
PartnerId int64 `json:"partnerId" `
//订单编号
OrderCode string `json:"order_code"`
// 查询偏离量
Offset int `json:"offset" `
// 查询限制
Limit int `json:"limit"`
//订单类型
OrderType int `json:"orderType"`
}
package service
import (
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order/command"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order/query"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
//OrderService 管理员相关服务
type OrderService struct {
}
func NewOrderService(option map[string]interface{}) *OrderService {
newAdminUserService := new(OrderService)
return newAdminUserService
}
func (service OrderService) PageListOrder(listOrderQuery query.ListOrderQuery) ([]domain.Order, int, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, 0, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
var (
orderRepository domain.OrderRepository
orders []domain.Order
cnt int
)
if value, err := factory.CreateOrderRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
} else {
orderRepository = value
}
query := domain.OrderFindQuery{
PartnerId: listOrderQuery.PartnerId,
OrderCode: listOrderQuery.OrderCode,
Offset: listOrderQuery.Offset,
Limit: listOrderQuery.Limit,
OrderType: listOrderQuery.OrderType,
}
orders, err = orderRepository.Find(query)
if err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
cnt, err = orderRepository.CountAll(query)
if err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var PartnerInfoRepository domain.PartnerInfoRepository
if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
for i := range orders {
var partnerData *domain.PartnerInfo
partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: orders[i].PartnerInfo.Id})
if err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
orders[i].PartnerInfo = partnerData.Partner
}
return orders, cnt, nil
}
func (service OrderService) GetOrder(getOrderQuery query.GetOrderQuery) (*domain.Order, error) {
//实际业务
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
var (
orderRepository domain.OrderRepository
order *domain.Order
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
if value, err := factory.CreateOrderRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
} else {
orderRepository = value
}
order, err = orderRepository.FindOne(domain.OrderFindOneQuery{
OrderId: getOrderQuery.OrderId,
})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var PartnerInfoRepository domain.PartnerInfoRepository
if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var partnerData *domain.PartnerInfo
partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: order.PartnerInfo.Id})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
order.PartnerInfo = partnerData.Partner
err = transactionContext.CommitTransaction()
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return order, nil
}
//CreateOrder 创建意向单
func (service OrderService) CreateOrder(command command.CreateOrderCommand) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = command.ValidateCommand(); err != nil {
return lib.ThrowError(lib.ARG_ERROR, err.Error())
}
if err = transactionContext.StartTransaction(); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
orderDao, _ := factory.CreateOrderDao(map[string]interface{}{"transactionContext": transactionContext})
ok, err := orderDao.OrderCodeIsExist(command.OrderCode, 0)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if ok {
return lib.ThrowError(lib.BUSINESS_ERROR, "订单编号已存在")
}
var PartnerInfoRepository domain.PartnerInfoRepository
if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var partnerData *domain.PartnerInfo
partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: command.PartnerId})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var orderRepository domain.OrderRepository
if orderRepository, err = factory.CreateOrderRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
newOrder := domain.Order{
//订单类型
OrderType: command.OrderType,
//订单编号
OrderCode: command.OrderCode,
//订单名称
OrderName: command.OrderName,
//订单状态
OrderStatus: command.OrderStatus,
//数量
OrderCount: command.OrderCount,
//实际数量
OrderActualCount: command.OrderActualCount,
//订单金额
OrderAmount: command.OrderAmount,
//实际订单金额
OrderActualAmount: command.OrderActualAmount,
//订单已支付分红金额(货款)
OrderPaymentAmount: 0,
//订单区域信息
OrderRegionInfo: domain.RegionInfo{
RegionName: command.OrderRegion,
},
//买家
Buyer: domain.Buyer{
BuyerName: command.BuyerName,
ShippingAddress: command.BuyerAddress,
ContactInfo: command.BuyerPhone,
},
PartnerInfo: partnerData.Partner,
//合伙人分红百分比
PartnerBonusPercent: command.PartnerBonusPercent,
//业务员分红百分比
SalesmanBonusPercent: command.SalesmanBonusPercent,
}
err = orderRepository.Save(newOrder)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
}
//UpdateOrderPurpose 更新意向单
func (service OrderService) UpdateOrderPurpose(command command.UpdateOrderCommand) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = command.ValidateCommand(); err != nil {
return lib.ThrowError(lib.ARG_ERROR, err.Error())
}
if err = transactionContext.StartTransaction(); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
orderDao, _ := factory.CreateOrderDao(map[string]interface{}{"transactionContext": transactionContext})
ok, err := orderDao.OrderCodeIsExist(command.OrderCode, command.Id)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if ok {
return lib.ThrowError(lib.BUSINESS_ERROR, "订单编号已存在")
}
var (
orderRepository domain.OrderRepository
orderData *domain.Order
)
if orderRepository, err = factory.CreateOrderRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
orderData, err = orderRepository.FindOne(domain.OrderFindOneQuery{
OrderId: command.Id,
})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if orderData.OrderType != domain.OrderIntention {
return lib.ThrowError(lib.BUSINESS_ERROR, "订单不是意向单")
}
updateMap := map[string]interface{}{
//订单编号
"orderCode": command.OrderCode,
"oderName": command.OrderName,
"orderCount": command.OrderCount,
"orderAmount": command.OrderAmount,
"orderActualCount": command.OrderCount,
"orderActualAmount": command.OrderAmount,
"buyer": domain.Buyer{
BuyerName: orderData.Buyer.BuyerName,
ContactInfo: command.BuyerPhone,
ShippingAddress: command.BuyerAddress,
},
"orderRegion": domain.RegionInfo{
RegionName: command.OrderRegion,
},
"partnerBonusPercent": command.PartnerBonusPercent,
"salesmanBonusPercent": command.SalesmanBonusPercent,
"orderStatus": command.OrderStatus,
"orderType": command.OrderType,
}
err = orderData.Update(updateMap)
if err != nil {
return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
}
err = orderRepository.Save(*orderData)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
}
//RemoveOrder 删除意向单
func (service OrderService) RemoveOrder(id int64) error {
//实际业务
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
if err = transactionContext.StartTransaction(); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
orderRepository domain.OrderRepository
order *domain.Order
)
if value, err := factory.CreateOrderRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
} else {
orderRepository = value
}
order, err = orderRepository.FindOne(domain.OrderFindOneQuery{
OrderId: id,
})
if err != nil {
return lib.ThrowError(lib.RES_NO_FIND_ERROR, err.Error())
}
if order.OrderType != domain.OrderIntention {
return lib.ThrowError(lib.BUSINESS_ERROR, "订单不是意向单")
}
err = orderRepository.Remove(order.Id)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
}
//UpdateOrderReal 更新为实发单
func (service OrderService) UpdateOrderReal(command command.UpdateOrderRealCommand) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = command.ValidateCommand(); err != nil {
return lib.ThrowError(lib.ARG_ERROR, err.Error())
}
if err = transactionContext.StartTransaction(); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
orderDao, _ := factory.CreateOrderDao(map[string]interface{}{"transactionContext": transactionContext})
ok, err := orderDao.OrderCodeIsExist(command.OrderCode, command.Id)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if ok {
return lib.ThrowError(lib.BUSINESS_ERROR, "订单编号已存在")
}
var (
orderRepository domain.OrderRepository
orderData *domain.Order
)
if orderRepository, err = factory.CreateOrderRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
orderData, err = orderRepository.FindOne(domain.OrderFindOneQuery{
OrderId: command.Id,
})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = orderData.Update(map[string]interface{}{
//订单编号
"orderCode": command.OrderCode,
"oderName": command.OrderName,
"orderActualCount": command.OrderActualCount,
"orderActualAmount": command.OrderActualAmount,
"buyer": domain.Buyer{
BuyerName: orderData.Buyer.BuyerName,
ContactInfo: command.BuyerPhone,
ShippingAddress: command.BuyerAddress,
},
"orderRegion": domain.RegionInfo{
RegionName: command.OrderRegion,
},
"partnerBonusPercent": command.PartnerBonusPercent,
"salesmanBonusPercent": command.SalesmanBonusPercent,
"orderStatus": command.OrderStatus,
"orderType": domain.OrderReal,
"reason": command.Reason,
})
if err != nil {
return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
}
err = orderRepository.Save(*orderData)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
}
package command
type CreateOrderPaymentCommand struct {
//订单编号
OrderId int64 `json:"orderId"`
DivdendPaymentItem []DivdendPyamentItem `json:"dividendPayment"`
TotalPaymentAmount float64 `json:"payment_amount"`
}
type DivdendPyamentItem struct {
// 货款
PaymentForGoods float64 `json:"paymentForGoods,omitempty"`
// 支付状态
StateOfPayment int `json:"stateOfPayment,omitempty"`
//支付批次
PaymentSn int `json:"paymentSn,omitempty"`
//支付编号
PaymentId int `json:"id,omitempty"`
}
func (command CreateOrderPaymentCommand) ValidateCommand() error {
return nil
}
package query
type ListDividendOrdersQuery struct {
// 合伙人类别
PartnerId int `json:"partner"`
SearchText string `json:"searchText"`
PageSize int `json:"pageSize"`
PageNumber int `json:"pageNumber"`
}
func (q *ListDividendOrdersQuery) ValidateQuery() error {
return nil
}
package query
type GetOrderPaymentQuery struct {
// 合伙人类别
OrderId int64 `json:"orderId"`
}
func (q *GetOrderPaymentQuery) ValidateQuery() error {
return nil
}
package service
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/command"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/query"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/utils"
"time"
//"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/query"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
// 客户价值服务
type OrderPaymentService struct {
}
func NewOrderPaymentService(options map[string]interface{}) *OrderPaymentService {
newOrderPaymentService := &OrderPaymentService{}
return newOrderPaymentService
}
// 创建订单支付数据
func (OrderPaymentService *OrderPaymentService) CreateOrderPayment(command *command.CreateOrderPaymentCommand) (data interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
OrderDao, _ = factory.CreateOrderDao(map[string]interface{}{"transactionContext": transactionContext})
OrderPaymentDao, _ = factory.CreateOrderPaymentDao(map[string]interface{}{"transactionContext": transactionContext})
)
if err = command.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
}
defer func() {
if err == nil {
err = transactionContext.CommitTransaction()
}
if err != nil {
transactionContext.RollbackTransaction()
}
}()
//检查订单是否存在
var OrderPaymentRepository domain.OrderPaymentRepository
if OrderPaymentRepository, err = factory.CreateOrderPaymentRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
orderBase, e := OrderDao.GetOrderBaseInfo(command.OrderId)
if e != nil {
err = e
return
}
var excludeIdList []int
var bonusStatus int = domain.BonusPaid //分红状态
excludeIdList = append(excludeIdList, 0)
for i := range command.DivdendPaymentItem {
paymentItem := command.DivdendPaymentItem[i]
dm := &domain.OrderPayment{
OrderId: command.OrderId,
CreateAt: time.Now(),
UpdateAt: time.Now(),
}
if bonusStatus == domain.BonusPaid && paymentItem.StateOfPayment == domain.BonusWaitPay {
bonusStatus = domain.BonusWaitPay
}
if paymentItem.PaymentId > 0 {
//检查货款 已存在 / 未存在
if findDm, e := OrderPaymentRepository.FindOne(domain.OrderPaymentFindOneQuery{OrderId: command.OrderId, PaymentId: paymentItem.PaymentId}); e == nil {
//状态更金额一样的时候 不做更新
if findDm.BonusStatus == paymentItem.StateOfPayment && findDm.PaymentAmount == paymentItem.PaymentForGoods {
excludeIdList = append(excludeIdList, paymentItem.PaymentId)
continue
}
dm = findDm
}
}
dm.PartnerId = orderBase["PartnerId"].(int64)
bonusPercent := orderBase["PartnerBonusPercent"].(float64)
dm.BonusAmount = utils.Decimal(paymentItem.PaymentForGoods * (bonusPercent / 100.0))
dm.PaymentAmount = paymentItem.PaymentForGoods
dm.BonusStatus = paymentItem.StateOfPayment
dm.UpdateAt = time.Now()
if data, err = OrderPaymentRepository.Save(dm); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
excludeIdList = append(excludeIdList, int(dm.Id))
}
if err = OrderPaymentDao.Remove(command.OrderId, domain.BonusWaitPay, excludeIdList); err != nil {
return
}
if err = OrderDao.Update(map[string]interface{}{"id": command.OrderId, "orderPaymentAmount": command.TotalPaymentAmount, "bonusStatus": bonusStatus}); err != nil {
return
}
return
}
// 返回订单支付列表
func (OrderPaymentService *OrderPaymentService) ListOrderPayment(listOrderPaymentQuery *query.GetOrderPaymentQuery) ([]*domain.OrderPayment, error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
OrderDao, _ = factory.CreateOrderDao(map[string]interface{}{"transactionContext": transactionContext})
OrderPayments []*domain.OrderPayment
err error
)
if err = listOrderPaymentQuery.ValidateQuery(); err != nil {
return nil, lib.ThrowError(lib.ARG_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var OrderPaymentRepository domain.OrderPaymentRepository
if OrderPaymentRepository, err = factory.CreateOrderPaymentRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
queryOption := domain.OrderPaymentQuery{
OrderId: listOrderPaymentQuery.OrderId,
}
_, e := OrderDao.GetOrderBaseInfo(listOrderPaymentQuery.OrderId)
if e != nil {
return nil, e
}
if OrderPayments, err = OrderPaymentRepository.Find(queryOption); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if err = transactionContext.CommitTransaction(); err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
return OrderPayments, nil
}
// 返回分红管理列表
func (OrderPaymentService *OrderPaymentService) ListDividendOrders(listOrderPaymentQuery *query.ListDividendOrdersQuery) (int, interface{}, error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
//OrderPayments []*domain.OrderPayment
count int
err error
OrderDao, _ = factory.CreateOrderDao(map[string]interface{}{"transactionContext": transactionContext})
orders []*models.Order
)
if err = listOrderPaymentQuery.ValidateQuery(); err != nil {
return 0, nil, lib.ThrowError(lib.ARG_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return 0, nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
if count, orders, err = OrderDao.GetDividendOrders(map[string]interface{}{
"orderCode": listOrderPaymentQuery.SearchText,
"partnerId": listOrderPaymentQuery.PartnerId,
"orderType": 1,
"offset": (listOrderPaymentQuery.PageNumber - 1) * listOrderPaymentQuery.PageSize,
"limit": listOrderPaymentQuery.PageSize,
}); err != nil {
return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
type DividendOrderItem struct {
OrderId string `json:"id"` //订单编号
OrderNumber string `json:"orderNumber"` //订单号
OrderState int `json:"orderState"` //订单状态
StateOfPayment int `json:"stateOfPayment"` //支付状态
CreateTime string `json:"createTime"` //订单创建时间
PartnerName string `json:"partnerName"` //合伙人姓名
DividendProportion float64 `json:"dividendProportion"` //分红比例
DividendsReceivable float64 `json:"dividendsReceivable"` //应收分红
DividendSpending float64 `json:"dividendSpending"` //分红支出
ReceiveDividends float64 `json:"receiveDividends"` //实收分红
CommissionProportion float64 `json:"commissionProportion"` //业务员抽成比例
}
var list = make([]DividendOrderItem, 0)
for i := range orders {
order := orders[i]
item := DividendOrderItem{
OrderId: fmt.Sprintf("%v", order.Id),
OrderNumber: order.OrderCode,
OrderState: order.OrderStatus,
StateOfPayment: order.BonusStatus,
CreateTime: order.CreateAt.Local().Format("2006-01-02 15:04:05"),
PartnerName: order.PartnerInfo.PartnerName,
DividendProportion: order.PartnerBonusPercent,
DividendsReceivable: utils.Decimal(order.OrderAmount * (order.PartnerBonusPercent / 100.0)),
DividendSpending: 0,
ReceiveDividends: utils.Decimal(order.OrderActualAmount * (order.PartnerBonusPercent / 100.0)),
CommissionProportion: order.SalesmanBonusPercent,
}
if order.OrderActualAmount < order.OrderAmount {
item.DividendSpending = utils.Decimal((order.OrderAmount - order.OrderActualAmount) * (order.PartnerBonusPercent / 100.0))
}
list = append(list, item)
}
if err = transactionContext.CommitTransaction(); err != nil {
return 0, nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
return count, list, nil
}
package domain
import (
"time"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/utils"
)
const (
OrderStatusReserve = iota + 1 //预定中
OrderStatusDeliverSome //部分发货
OrderStatusDeliverAll //全部发货
)
type Order struct {
Id int64 `json:"id"`
//订单类型
OrderType int `json:"orderType"`
//订单编号
OrderCode string `json:"orderCode"`
//订单名称
OrderName string `json:"oderName"`
//订单状态
OrderStatus int `json:"orderStatus"`
//数量
OrderCount int `json:"orderCount"`
//实际数量
OrderActualCount int `json:"orderActualCount"`
//订单金额
OrderAmount float64 `json:"orderAmount"`
//实际订单金额
OrderActualAmount float64 `json:"orderActualAmount"`
//订单已支付分红金额(货款)
OrderPaymentAmount float64 `json:"orderPaymentAmount"`
//订单区域信息
OrderRegionInfo RegionInfo `json:"orderRegionInfo"`
//买家
Buyer Buyer `json:"buyer"`
//合伙人数据
PartnerInfo Partner `json:"partnerInfo"`
//合伙人分红百分比
PartnerBonusPercent float64 `json:"partnerBonusPercent"`
//业务员分红百分比
SalesmanBonusPercent float64 `json:"salesmanBonusPercent"`
//最后查看得时间
LastViewTime time.Time `json:"lastViewTime"`
//更新时间
UpdateAt time.Time `json:"updateAt"`
CreateAt time.Time `json:"createAt"`
//理由
Reason string
}
//TODO
func (order *Order) Update(data map[string]interface{}) error {
if v, ok := data["orderType"]; ok {
order.OrderType = v.(int)
}
if v, ok := data["orderCode"]; ok {
order.OrderCode = v.(string)
}
if v, ok := data["oderName"]; ok {
order.OrderName = v.(string)
}
if v, ok := data["orderStatus"]; ok {
order.OrderStatus = v.(int)
}
if v, ok := data["orderCount"]; ok {
order.OrderCount = v.(int)
}
if v, ok := data["orderActualCount"]; ok {
order.OrderActualCount = v.(int)
}
if v, ok := data["orderAmount"]; ok {
order.OrderAmount = v.(float64)
}
if v, ok := data["orderActualAmount"]; ok {
order.OrderActualAmount = v.(float64)
}
if v, ok := data["orderPaymentAmount"]; ok {
order.OrderPaymentAmount = v.(float64)
}
if v, ok := data["orderRegion"]; ok {
order.OrderRegionInfo = v.(RegionInfo)
}
if v, ok := data["buyer"]; ok {
order.Buyer = v.(Buyer)
}
if v, ok := data["partnerInfo"]; ok {
order.PartnerInfo = v.(Partner)
}
if v, ok := data["partnerBonusPercent"]; ok {
order.PartnerBonusPercent = v.(float64)
}
if v, ok := data["salesmanBonusPercent"]; ok {
order.SalesmanBonusPercent = v.(float64)
}
if v, ok := data["reason"]; ok {
order.Reason = v.(string)
}
return nil
}
//订单累计分红
func (m *Order) OrderTotalBonus() float64 {
return utils.Decimal(m.OrderActualAmount * (m.PartnerBonusPercent / 100.0))
}
//订单已收分红
func (m *Order) OrderBonusReceive() float64 {
return utils.Decimal(m.OrderPaymentAmount * (m.PartnerBonusPercent / 100.0))
}
//订单未收分红
func (m *Order) OrderBonusWait() float64 {
bonusWait := m.OrderTotalBonus() - m.OrderBonusReceive()
if bonusWait < 0 {
return 0
}
return bonusWait
}
//分红支出
func (m *Order) OrderBonusOutstanding() float64 {
if m.OrderAmount <= m.OrderActualAmount {
return 0
}
return utils.Decimal((m.OrderAmount - m.OrderActualAmount) * (m.PartnerBonusPercent / 100.0))
}
//订单被取消金额
func (m *Order) OrderAmountCancel() float64 {
if m.OrderAmount <= m.OrderActualAmount {
return 0
}
return utils.Decimal((m.OrderAmount - m.OrderActualAmount))
}
type OrderFindOneQuery struct {
OrderId int64
}
type OrderFindQuery struct {
PartnerId int64
OrderCode string
DeliveryCode string
Offset int
Limit int
OrderType int
}
type OrderRepository interface {
Save(order Order) error
FindOne(qureyOptions OrderFindOneQuery) (*Order, error)
Find(queryOptions OrderFindQuery) ([]Order, error)
CountAll(queryOption OrderFindQuery) (int, error)
Remove(id int64) error
}
package domain
import "time"
const (
BonusWaitPay = iota + 1 //等待支付分红
BonusPaid //已经支付分红
)
type OrderPayment struct {
//编号
Id int64 `json:"id"`
//订单编号
OrderId int64 `json:"orderId"`
//合伙人编号
PartnerId int64 `json:"partnerId"`
//支付货款
PaymentAmount float64 `json:"paymentAmount"`
//分红金额
BonusAmount float64 `json:"bonusAmount"`
//分红状态 1.等待支付分红 2.已支付分红
BonusStatus int `json:"bonusStatus"`
//创建时间
CreateAt time.Time `json:"createAt"`
//更新时间
UpdateAt time.Time `json:"updateAt"`
//扩展
PartnerBonusPercent float64 `json:"-"`
}
func (m *OrderPayment) Identify() interface{} {
if m.Id == 0 {
return nil
}
return m.Id
}
func (m *OrderPayment) Update(data map[string]interface{}) error {
if m.BonusStatus != BonusWaitPay {
return nil
}
if paymentAmount, ok := data["paymentAmount"]; ok && paymentAmount != 0 {
m.PaymentAmount = paymentAmount.(float64)
}
if bonusAmount, ok := data["bonusAmount"]; ok && bonusAmount != 0 {
m.BonusAmount = bonusAmount.(float64)
}
if bonusStatus, ok := data["bonusStatus"]; ok && bonusStatus != 0 {
m.BonusStatus = bonusStatus.(int)
}
m.UpdateAt = time.Now()
return nil
}
type OrderPaymentFindOneQuery struct {
Id int64
OrderId int64
PaymentId int
}
type OrderPaymentQuery struct {
Offset int
Limit int
OrderId int64
//PartnerCategory []int //合伙人类型
//RegionInfo *RegionInfo //区域
//PartnerName string //合伙人姓名
}
type OrderPaymentRepository interface {
Save(dm *OrderPayment) (*OrderPayment, error)
FindOne(queryOptions OrderPaymentFindOneQuery) (*OrderPayment, error)
Find(queryOptions OrderPaymentQuery) ([]*OrderPayment, error)
CountAll(queryOptions OrderPaymentQuery) (int, error)
}
package dao
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
type OrderDao struct {
transactionContext *transaction.TransactionContext
}
func (dao *OrderDao) Update(options map[string]interface{}) (err error) {
tx := dao.transactionContext.PgTx
order := new(models.Order)
q := tx.Model(order)
if v, ok := options["orderPaymentAmount"]; ok {
q.Set("order_payment_amount = ?", v)
}
if v, ok := options["bonusStatus"]; ok {
q.Set("bonus_status = ?", v)
}
if v, ok := options["id"]; ok {
q.Where("id = ?", v)
}
_, err = q.Update()
return
}
func (dao *OrderDao) OrderCodeIsExist(code string, notId int64) (bool, error) {
tx := dao.transactionContext.PgDd
ok, err := tx.Model(new(models.Order)).
Where("order_code=?", code).
Where("id<>?", notId).
Exists()
return ok, err
}
func (dao *OrderDao) GetOrderBaseInfo(id int64) (data map[string]interface{}, err error) {
tx := dao.transactionContext.PgTx
order := new(models.Order)
data = make(map[string]interface{})
q := tx.Model(order)
q.Column("partner_id", "partner_bonus_percent", "order_payment_amount", "buyer")
q.Where("id = ?", id)
err = q.Select()
if err == nil {
data["PartnerId"] = order.PartnerId
data["PartnerBonusPercent"] = order.PartnerBonusPercent
data["OrderPaymentAmount"] = order.OrderPaymentAmount
data["Buyer"] = order.Buyer
}
return
}
func (dao *OrderDao) GetDividendOrders(options map[string]interface{}) (count int, orders []*models.Order, err error) {
tx := dao.transactionContext.PgTx
//Order:=new(models.Order)
q := tx.Model(&orders)
q.ColumnExpr(`"order".*`, `partner_info.partner_name`)
q.Relation("PartnerInfo")
if v, ok := options["orderCode"]; ok && len(v.(string)) > 0 {
q.Where(`"order".order_code like ?`, fmt.Sprintf("%%%v%%", v))
}
if v, ok := options["orderType"]; ok {
q.Where(`"order".order_type=?`, v)
}
if v, ok := options["partnerId"]; ok && v.(int) > 0 {
q.Where(`"order".partner_id =?`, v)
}
if v, ok := options["offset"]; ok {
q.Offset(v.(int))
}
if v, ok := options["limit"]; ok {
q.Limit(v.(int))
}
q.Order(`order.id DESC`)
count, err = q.SelectAndCount()
return
}
func NewOrderDao(transactionContext *transaction.TransactionContext) (*OrderDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &OrderDao{
transactionContext: transactionContext,
}, nil
}
}
package dao
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
type OrderPayment struct {
transactionContext *transaction.TransactionContext
}
//删除数据
func (o *OrderPayment) Remove(orderId int64, status int, idList []int) error {
if len(idList) == 0 {
return nil
}
tx := o.transactionContext.PgTx
m := new(models.OrderPayment)
q := tx.Model(m).Where("order_id=?", orderId).
WhereIn("id not in(?)", idList)
_, err := q.Delete()
return err
}
func NewOrderPayment(transactionContext *transaction.TransactionContext) (*OrderPayment, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &OrderPayment{
transactionContext: transactionContext,
}, nil
}
}
package repository
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
type OrderPaymentRepository struct {
transactionContext *transaction.TransactionContext
}
func (repository *OrderPaymentRepository) Save(dm *domain.OrderPayment) (*domain.OrderPayment, error) {
var (
err error
tx = repository.transactionContext.PgTx
)
m := &models.OrderPayment{
Id: dm.Id,
OrderId: dm.OrderId,
PartnerId: dm.PartnerId,
PaymentAmount: dm.PaymentAmount,
BonusAmount: dm.BonusAmount,
BonusStatus: dm.BonusStatus,
CreateAt: dm.CreateAt,
UpdateAt: dm.UpdateAt,
}
if m.Id == 0 {
err = tx.Insert(m)
dm.Id = m.Id
if err != nil {
return nil, err
}
} else {
err = tx.Update(m)
if err != nil {
return nil, err
}
}
return dm, nil
}
func (repository *OrderPaymentRepository) Remove(OrderPayment *domain.OrderPayment) (*domain.OrderPayment, error) {
var (
tx = repository.transactionContext.PgTx
OrderPaymentModel = &models.OrderPayment{Id: OrderPayment.Identify().(int64)}
)
if _, err := tx.Model(OrderPaymentModel).Where("id = ?", OrderPayment.Id).Delete(); err != nil {
return OrderPayment, err
}
return OrderPayment, nil
}
func (repository *OrderPaymentRepository) FindOne(queryOptions domain.OrderPaymentFindOneQuery) (*domain.OrderPayment, error) {
tx := repository.transactionContext.PgTx
OrderPaymentModel := new(models.OrderPayment)
query := tx.Model(OrderPaymentModel)
if queryOptions.OrderId > 0 {
query.Where("order_payment.order_id = ?", queryOptions.OrderId)
}
if queryOptions.PaymentId > 0 {
query.Where("order_payment.id = ?", queryOptions.PaymentId)
}
if err := query.First(); err != nil {
return nil, err
}
if OrderPaymentModel.Id == 0 {
return nil, nil
}
return repository.transformPgModelToDomainModel(OrderPaymentModel)
}
func (repository *OrderPaymentRepository) Find(queryOptions domain.OrderPaymentQuery) ([]*domain.OrderPayment, error) {
tx := repository.transactionContext.PgTx
var OrderPaymentModels []*models.OrderPayment
query := tx.Model(&OrderPaymentModels)
query.Where("order_id=?", queryOptions.OrderId)
query.Order("id ASC")
var (
err error
rsp = make([]*domain.OrderPayment, 0)
)
err = query.Select()
if err != nil {
return rsp, err
}
for i := range OrderPaymentModels {
dm, err := repository.transformPgModelToDomainModel(OrderPaymentModels[i])
if err != nil {
return rsp, err
}
rsp = append(rsp, dm)
}
return rsp, nil
}
func (repository OrderPaymentRepository) CountAll(queryOption domain.OrderPaymentQuery) (int, error) {
db := repository.transactionContext.PgDd
partnerModels := models.PartnerInfo{}
query := db.Model(&partnerModels)
//if len(queryOption.PartnerName) > 0 {
// query = query.Where("partner_name like ?", "%"+queryOption.PartnerName+"%")
//}
//if queryOption.RegionInfo != nil {
// query = query.Where("region_info::jsonb->>'regionName' like ?", "%"+queryOption.RegionInfo.RegionName+"%")
//}
//if len(queryOption.PartnerCategory) > 0 {
// query = query.WhereIn("partner_category in(?)", queryOption.PartnerCategory)
//}
cnt, err := query.Count()
return cnt, err
}
func (repository *OrderPaymentRepository) transformPgModelToDomainModel(dm *models.OrderPayment) (*domain.OrderPayment, error) {
m := &domain.OrderPayment{
Id: dm.Id,
OrderId: dm.OrderId,
PartnerId: dm.PartnerId,
PaymentAmount: dm.PaymentAmount,
BonusAmount: dm.BonusAmount,
BonusStatus: dm.BonusStatus,
CreateAt: dm.CreateAt,
UpdateAt: dm.UpdateAt,
}
return m, nil
}
func NewOrderPaymentRepository(transactionContext *transaction.TransactionContext) (*OrderPaymentRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &OrderPaymentRepository{transactionContext: transactionContext}, nil
}
package repository
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
type OrderRepository struct {
transactionContext *transaction.TransactionContext
}
var (
_ domain.OrderRepository = (*OrderRepository)(nil)
)
func NewOrderRepository(transactionContext *transaction.TransactionContext) (*OrderRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &OrderRepository{transactionContext: transactionContext}, nil
}
func (reponsitory OrderRepository) transformPgModelToDomainModel(orderModel *models.Order) (domain.Order, error) {
result := domain.Order{
Id: orderModel.Id,
OrderType: orderModel.OrderType,
OrderCode: orderModel.OrderCode,
OrderName: orderModel.OrderName,
OrderStatus: orderModel.OrderStatus,
OrderCount: orderModel.OrderCount,
OrderActualCount: orderModel.OrderActualCount,
OrderAmount: orderModel.OrderAmount,
OrderActualAmount: orderModel.OrderActualAmount,
OrderPaymentAmount: orderModel.OrderPaymentAmount,
OrderRegionInfo: orderModel.OrderRegionInfo,
Buyer: orderModel.Buyer,
PartnerInfo: domain.Partner{
Id: orderModel.PartnerId,
},
PartnerBonusPercent: orderModel.PartnerBonusPercent,
SalesmanBonusPercent: orderModel.SalesmanBonusPercent,
LastViewTime: orderModel.LastViewTime,
UpdateAt: orderModel.UpdateAt,
CreateAt: orderModel.CreateAt,
Reason: orderModel.Reason,
}
return result, nil
}
func (repository OrderRepository) Save(orderInfo domain.Order) error {
var (
err error
tx = repository.transactionContext.PgTx
)
m := &models.Order{
Id: orderInfo.Id,
OrderType: orderInfo.OrderType,
OrderCode: orderInfo.OrderCode,
OrderName: orderInfo.OrderName,
OrderStatus: orderInfo.OrderStatus,
OrderCount: orderInfo.OrderCount,
OrderActualCount: orderInfo.OrderActualCount,
OrderAmount: orderInfo.OrderAmount,
OrderActualAmount: orderInfo.OrderActualAmount,
OrderPaymentAmount: orderInfo.OrderPaymentAmount,
OrderRegionInfo: orderInfo.OrderRegionInfo,
Buyer: orderInfo.Buyer,
PartnerId: orderInfo.PartnerInfo.Id,
PartnerBonusPercent: orderInfo.PartnerBonusPercent,
SalesmanBonusPercent: orderInfo.SalesmanBonusPercent,
BonusStatus: 1,
Reason: orderInfo.Reason,
}
if m.Id == 0 {
err = tx.Insert(m)
orderInfo.Id = m.Id
} else {
_, err = tx.Model(m).WherePK().
Column("order_type", "order_code", "order_name", "order_status", "order_count",
"order_actual_count", "order_amount", "order_actual_amount", "order_payment_amount",
"order_region_info", "buyer", "partner_id", "partner_bonus_percent", "salesman_bonus_percent",
"update_at", "reason").
Update()
}
return err
}
func (repository OrderRepository) Find(queryOption domain.OrderFindQuery) ([]domain.Order, error) {
db := repository.transactionContext.PgDd
orderModels := []models.Order{}
query := db.Model(&orderModels)
if queryOption.PartnerId > 0 {
query = query.Where("partner_id=?", queryOption.PartnerId)
}
if len(queryOption.OrderCode) > 0 {
query = query.Where("order_code like ?", "%"+queryOption.OrderCode+"%")
}
if queryOption.OrderType > 0 {
query = query.Where("order_type=?", queryOption.OrderType)
}
if queryOption.Offset > -1 {
query = query.Offset(queryOption.Offset)
}
if queryOption.Limit > 0 {
query = query.Limit(queryOption.Limit)
} else {
query = query.Limit(20)
}
var (
err error
ordersReturn = make([]domain.Order, 0)
)
query = query.Order("order.id DESC")
err = query.Select()
if err != nil {
return ordersReturn, err
}
for i := range orderModels {
domainOrder, err := repository.transformPgModelToDomainModel(&orderModels[i])
if err != nil {
return ordersReturn, err
}
ordersReturn = append(ordersReturn, domainOrder)
}
return ordersReturn, nil
}
func (repository OrderRepository) CountAll(queryOption domain.OrderFindQuery) (int, error) {
db := repository.transactionContext.PgDd
orderModels := []models.Order{}
query := db.Model(&orderModels)
if queryOption.PartnerId > 0 {
query = query.Where("partner_id=?", queryOption.PartnerId)
}
if len(queryOption.OrderCode) > 0 {
query = query.Where("order_code like ?", "%"+queryOption.OrderCode+"%")
}
if queryOption.OrderType > 0 {
query = query.Where("order_type=?", queryOption.OrderType)
}
var (
err error
)
cnt, err := query.Count()
if err != nil {
return cnt, err
}
return cnt, nil
}
func (repository OrderRepository) FindOne(qureyOptions domain.OrderFindOneQuery) (*domain.Order, error) {
var (
err error
tx = repository.transactionContext.PgDd
)
m := new(models.Order)
err = tx.Model(m).
Where("id=?", qureyOptions.OrderId).
First()
if err != nil {
return nil, err
}
result, err := repository.transformPgModelToDomainModel(m)
return &result, err
}
func (repository OrderRepository) Remove(id int64) error {
var (
err error
tx = repository.transactionContext.PgTx
)
m := &models.Order{
Id: id,
}
_, err = tx.Model(m).WherePK().Delete()
return err
}
package controllers
import (
"errors"
"fmt"
"github.com/astaxie/beego/logs"
OrderPaymentCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/command"
OrderPaymentQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/query"
OrderPaymentSvr "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order_payment/service"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/utils"
OrderQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order/query"
OrderSvr "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order/service"
PartnerInfoQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/partnerInfo/query"
PartnerInfoSvr "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/partnerInfo/service"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"strconv"
)
type DividendsController struct {
BaseController
}
////Prepare 重写 BaseController 的Prepare方法
func (c *DividendsController) Prepare() {
c.BaseController.Prepare()
if ok := c.ValidJWTToken(); !ok {
return
}
if ok := c.ValidAdminPermission(domain.PERMISSION_DIVIDEND); !ok {
return
}
}
//Edit 编辑分红支付
func (c *DividendsController) Edit() {
//用与适配前端定义的数据结构
type DividendPaymentItem struct {
PaymentForGoods float64 `json:"paymentForGoods"`
StateOfPayment int `json:"stateOfPayment"`
Id int `json:"id"`
}
type Parameter struct {
Id string `json:"id"` //订单编号
DividendPayment []DividendPaymentItem `json:"dividendPayment"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
if len(param.Id) == 0 {
c.ResponseError(errors.New("id is empty"))
return
}
orderId, _ := strconv.ParseInt(param.Id, 10, 64)
cmd := OrderPaymentCmd.CreateOrderPaymentCommand{
OrderId: orderId,
DivdendPaymentItem: make([]OrderPaymentCmd.DivdendPyamentItem, 0),
}
//编辑
for i := range param.DividendPayment {
item := param.DividendPayment[i]
paymentItem := OrderPaymentCmd.DivdendPyamentItem{}
paymentItem.PaymentForGoods = item.PaymentForGoods
paymentItem.StateOfPayment = item.StateOfPayment
paymentItem.PaymentSn = i + 1
paymentItem.PaymentId = item.Id
if paymentItem.StateOfPayment == domain.BonusPaid {
cmd.TotalPaymentAmount += paymentItem.PaymentForGoods
}
cmd.DivdendPaymentItem = append(cmd.DivdendPaymentItem, paymentItem)
}
serve := OrderPaymentSvr.NewOrderPaymentService(nil)
_, err = serve.CreateOrderPayment(&cmd)
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(nil)
return
}
//Edit 分红支付详情
func (c *DividendsController) Detail() {
type Parameter struct {
Id string `json:"id"` //订单编号
}
var (
param Parameter
err error
order *domain.Order
partner *domain.PartnerInfo
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
cmd := OrderPaymentQuery.GetOrderPaymentQuery{}
cmd.OrderId, _ = strconv.ParseInt(param.Id, 10, 64)
var (
OrderPaymentSvr = OrderPaymentSvr.NewOrderPaymentService(nil)
OrderSvr = OrderSvr.NewOrderService(nil)
PartnerInfoSvr = PartnerInfoSvr.NewPartnerInfoService(nil)
)
var data []*domain.OrderPayment
data, err = OrderPaymentSvr.ListOrderPayment(&cmd)
if err != nil {
c.ResponseError(err)
return
}
if order, err = OrderSvr.GetOrder(OrderQuery.GetOrderQuery{OrderId: cmd.OrderId}); err != nil {
c.ResponseError(err)
return
}
if partner, err = PartnerInfoSvr.GetPartnerInfo(PartnerInfoQuery.GetPartnerInfoQuery{Id: order.PartnerInfo.Id}); err != nil {
c.ResponseError(err)
return
}
type DividendPayment struct {
PaymentForGoods float64 `json:"paymentForGoods"`
UpdateTime string `json:"updateTime"`
StateOfPayment int `json:"stateOfPayment"`
Dividend float64 `json:"dividend"`
DividendProportion float64 `json:"dividendProportion"`
Id int64 `json:"id"`
}
type Order struct {
OrderNumber string `json:"orderNumber"` //订单号
OrderState int `json:"orderState"` //订单状态
CreateTime string `json:"createTime"` //订单创建时间
OrderName string `json:"orderName"` //订单名称
OrderNum string `json:"orderNum"` //订单数量
OrderAmount string `json:"orderAmount"` //订单金额
Id string `json:"id"` //订单编号
}
type Partner struct {
PartnerName string `json:"partnerName"` //合伙人姓名
DividendProportion float64 `json:"dividendProportion"` //分红比例
DividendsReceivable float64 `json:"dividendsReceivable"` //应收分红
DividendSpending float64 `json:"dividendSpending"` //分红支出
ReceiveDividends float64 `json:"receiveDividends"` //实收分红
NotReceivedDividends float64 `json:"notReceivedDividends"` //未收分红
RceivedDividends float64 `json:"receivedDividends"` //已收分红
}
type Commission struct {
SalesmanName string `json:"salesmanName"` //业务员名称
CommissionProportion float64 `json:"commissionProportion"` //业务员抽成比例
ExpectedCommission float64 `json:"expectedCommission"` //业务员预计抽成
}
type Response struct {
DividendPayment []DividendPayment `json:"dividendPayment"`
Order Order `json:"order"`
Partner Partner `json:"partner"`
Commission Commission `json:"commission"`
}
rsp := Response{DividendPayment: make([]DividendPayment, 0)}
for i := range data {
item := data[i]
payment := DividendPayment{
PaymentForGoods: item.PaymentAmount,
UpdateTime: item.UpdateAt.Local().Format("2006-01-02 15:04:05"),
StateOfPayment: item.BonusStatus,
Dividend: utils.Decimal(item.PaymentAmount * (order.PartnerBonusPercent / 100.0)),
DividendProportion: order.PartnerBonusPercent,
Id: item.Id,
}
rsp.DividendPayment = append(rsp.DividendPayment, payment)
}
rsp.Order = Order{
OrderNumber: order.OrderCode,
OrderState: order.OrderStatus,
CreateTime: order.CreateAt.Local().Format("2006-01-02 15:04:05"),
OrderName: order.OrderName,
OrderNum: fmt.Sprintf("%v", order.OrderActualCount),
OrderAmount: fmt.Sprintf("%v", order.OrderActualAmount),
Id: fmt.Sprintf("%v", order.Id),
}
rsp.Partner = Partner{
PartnerName: partner.Partner.PartnerName,
DividendProportion: order.PartnerBonusPercent,
DividendsReceivable: order.OrderTotalBonus() + order.OrderBonusOutstanding(), //应收分红 = 实收分红+分红支出
DividendSpending: order.OrderBonusOutstanding(), //分红支出
ReceiveDividends: order.OrderTotalBonus(), //实收分红 = 实际金额 * 比例
NotReceivedDividends: order.OrderBonusWait(), //未收分红
RceivedDividends: order.OrderBonusReceive(), //已收分红
}
rsp.Commission = Commission{
CommissionProportion: order.SalesmanBonusPercent,
ExpectedCommission: utils.Decimal(order.OrderActualAmount * (order.SalesmanBonusPercent / 100)),
}
if len(partner.Salesman) > 0 {
rsp.Commission.SalesmanName = partner.Salesman[0].Name
}
c.ResponseData(rsp)
return
}
//分红管理
func (c *DividendsController) List() {
var (
param OrderPaymentQuery.ListDividendOrdersQuery
err error
count int
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
if param.PageSize == 0 {
param.PageSize = 20
}
if param.PageNumber == 0 {
param.PageNumber = 1
}
serve := OrderPaymentSvr.NewOrderPaymentService(nil)
var data interface{}
count, data, err = serve.ListDividendOrders(&param)
if err != nil {
c.ResponseError(err)
return
}
c.ResponsePageList(data, count, param.PageNumber)
return
}
package controllers
import (
"errors"
"strconv"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
"github.com/astaxie/beego/logs"
orderCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order/command"
orderQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order/query"
orderService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order/service"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
)
type OrderController struct {
BaseController
}
////Prepare 重写 BaseController 的Prepare方法
func (c *OrderController) Prepare() {
c.BaseController.Prepare()
if ok := c.ValidJWTToken(); !ok {
return
}
if ok := c.ValidAdminPermission(domain.PERMISSION_ORDER); !ok {
return
}
}
type orderDetail struct {
//买家姓名
Buyer string `json:"buyer"`
//买家联系方式
BuyerPhone string `json:"buyerPhone"`
//收货地址
Address string `json:"address"`
//对应合伙人 id
Partner int64 `json:"partner"`
//合伙人抽成比例
PartnerRatio float64 `json:"partnerRatio"`
//业务员抽成比例
SalesmanRatio float64 `json:"salesmanRatio"`
//订单号
OrderId string `json:"orderId"`
//订单名称
OrderName string `json:"orderName"`
//订单数量
OrderNum int `json:"orderNum"`
//订单金额
OrderPrice float64 `json:"orderPrice"`
//订单区域
OrderDist string `json:"orderDist"`
//id
Id int64 `json:"id"`
//订单状态
OrderStatue int `json:"orderStatue"`
//理由
Reason string `json:"reason"`
}
//UpdateOrderPurpose 更新意向订单
func (c *OrderController) UpdateOrderPurpose() {
//用与适配前端定义的数据结构
var (
param orderDetail
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
if param.Id == 0 {
err = c.addOrderPurpose(param)
} else {
err = c.editOrderPurpose(param)
}
if err != nil {
c.ResponseError(err)
}
c.ResponseData(nil)
return
}
func (c *OrderController) addOrderPurpose(param orderDetail) error {
orderSrv := orderService.NewOrderService(nil)
Createcmd := orderCmd.CreateOrderCommand{
//订单区域
OrderRegion: param.OrderDist,
//订单编号
OrderCode: param.OrderId,
//订单名称
OrderName: param.OrderName,
//数量
OrderCount: param.OrderNum,
OrderActualCount: param.OrderNum,
//订单金额
OrderAmount: param.OrderPrice,
OrderActualAmount: param.OrderPrice,
//买家
BuyerName: param.Buyer,
//买家电话
BuyerPhone: param.BuyerPhone,
//地址
BuyerAddress: param.Address,
//合伙人数据
PartnerId: param.Partner,
//合伙人分红百分比
PartnerBonusPercent: param.PartnerRatio,
//业务员分红百分比
SalesmanBonusPercent: param.SalesmanRatio,
//订单类型
OrderType: domain.OrderIntention,
OrderStatus: domain.OrderStatusReserve,
}
err := orderSrv.CreateOrder(Createcmd)
return err
}
func (c *OrderController) editOrderPurpose(param orderDetail) error {
updateCmd := orderCmd.UpdateOrderCommand{
Id: param.Id,
//订单区域
OrderRegion: param.OrderDist,
//订单编号
OrderCode: param.OrderId,
//订单名称
OrderName: param.OrderName,
//数量
OrderCount: param.OrderNum,
//订单金额
OrderAmount: param.OrderPrice,
//买家电话
BuyerPhone: param.BuyerPhone,
//地址
BuyerAddress: param.Address,
//合伙人分红百分比
PartnerBonusPercent: param.PartnerRatio,
//业务员分红百分比
SalesmanBonusPercent: param.SalesmanRatio,
OrderStatus: domain.OrderStatusReserve,
OrderType: domain.OrderIntention,
}
orderSrv := orderService.NewOrderService(nil)
err := orderSrv.UpdateOrderPurpose(updateCmd)
return err
}
//GetOrderPurpose 获取意向订单
func (c *OrderController) GetOrderPurpose() {
type Parameter struct {
Id string `json:"id"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
orderid, _ := strconv.ParseInt(param.Id, 10, 64)
orderSrv := orderService.NewOrderService(nil)
orderinfo, err := orderSrv.GetOrder(orderQuery.GetOrderQuery{
OrderId: orderid,
})
if err != nil {
c.ResponseError(err)
return
}
rsp := map[string]interface{}{
"createTime": orderinfo.CreateAt.Local().Format("2006-01-02 15:04:05"),
"updateTime": orderinfo.UpdateAt.Local().Format("2006-01-02 15:04:05"),
"buyer": orderinfo.Buyer.BuyerName,
"buyerPhone": orderinfo.Buyer.ContactInfo,
"address": orderinfo.Buyer.ShippingAddress,
"id": orderinfo.Id,
"partner": orderinfo.PartnerInfo.Id,
"partnerRatio": orderinfo.PartnerBonusPercent,
"salesmanRatio": orderinfo.SalesmanBonusPercent,
"orderId": orderinfo.OrderCode,
"orderName": orderinfo.OrderName,
"orderNum": orderinfo.OrderCount,
"orderPrice": orderinfo.OrderAmount,
"orderDist": orderinfo.OrderRegionInfo.RegionName,
"orderStatue": orderinfo.OrderStatus,
}
c.ResponseData(rsp)
}
//PageListOrderPurpose 分页获取意向订单列表
func (c *OrderController) PageListOrderPurpose() {
type Parameter struct {
SearchText string `json:"searchText"`
Partner int64 `json:"partner"`
PageSize int `json:"pageSize"`
PageNumber int `json:"pageNumber"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
if param.PageNumber == 0 {
param.PageNumber = 1
}
if param.PageSize == 0 {
param.PageSize = 20
}
orderSrv := orderService.NewOrderService(nil)
orderinfos, cnt, err := orderSrv.PageListOrder(orderQuery.ListOrderQuery{
PartnerId: param.Partner,
OrderCode: param.SearchText,
OrderType: domain.OrderIntention,
Limit: param.PageSize,
Offset: (param.PageNumber - 1) * param.PageSize,
})
if err != nil {
c.ResponseError(err)
return
}
rsp := []map[string]interface{}{}
for i := range orderinfos {
orderinfo := orderinfos[i]
m := map[string]interface{}{
"createTime": orderinfo.CreateAt.Local().Format("2006-01-02 15:04:05"),
"updateTime": orderinfo.UpdateAt.Local().Format("2006-01-02 15:04:05"),
"buyer": orderinfo.Buyer.BuyerName,
"id": orderinfo.Id,
"orderId": orderinfo.OrderCode,
"partner": orderinfo.PartnerInfo.PartnerName,
"partnerRatio": orderinfo.PartnerBonusPercent,
"orderName": orderinfo.OrderName,
"orderNum": orderinfo.OrderCount,
"orderPrice": orderinfo.OrderAmount,
"orderDist": orderinfo.OrderRegionInfo.RegionName,
"orderStatue": orderinfo.OrderStatus,
}
rsp = append(rsp, m)
}
c.ResponsePageList(rsp, cnt, param.PageNumber)
}
//RemoveOrderPurpose 删除意向订单
func (c *OrderController) RemoveOrderPurpose() {
type Parameter struct {
Id int64 `json:"id"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
orderSrv := orderService.NewOrderService(nil)
err = orderSrv.RemoveOrder(param.Id)
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(nil)
}
//PageListOrderReal 分页获取实发订单列表
func (c *OrderController) PageListOrderReal() {
type Parameter struct {
SearchText string `json:"searchText"`
Partner int64 `json:"partner"`
PageSize int `json:"pageSize"`
PageNumber int `json:"pageNumber"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
if param.PageNumber == 0 {
param.PageNumber = 1
}
if param.PageSize == 0 {
param.PageSize = 20
}
orderSrv := orderService.NewOrderService(nil)
orderinfos, cnt, err := orderSrv.PageListOrder(orderQuery.ListOrderQuery{
PartnerId: param.Partner,
OrderCode: param.SearchText,
OrderType: domain.OrderReal,
Limit: param.PageSize,
Offset: (param.PageNumber - 1) * param.PageSize,
})
if err != nil {
c.ResponseError(err)
return
}
rsp := []map[string]interface{}{}
for i := range orderinfos {
orderinfo := orderinfos[i]
m := map[string]interface{}{
"createTime": orderinfo.CreateAt.Local().Format("2006-01-02 15:04:05"),
"updateTime": orderinfo.UpdateAt.Local().Format("2006-01-02 15:04:05"),
"buyer": orderinfo.Buyer.BuyerName,
"id": orderinfo.Id,
"orderId": orderinfo.OrderCode,
"partner": orderinfo.PartnerInfo.PartnerName,
"partnerRatio": orderinfo.PartnerBonusPercent,
"orderName": orderinfo.OrderName,
"orderNum": orderinfo.OrderCount,
"orderPrice": orderinfo.OrderAmount,
"orderActualNum": orderinfo.OrderActualCount,
"orderActualPrice": orderinfo.OrderActualAmount,
"orderDist": orderinfo.OrderRegionInfo.RegionName,
"orderStatue": orderinfo.OrderStatus,
}
rsp = append(rsp, m)
}
c.ResponsePageList(rsp, cnt, param.PageNumber)
}
//UpdateOrderPurpose 意向订单转实发单
func (c *OrderController) OrderPurposeToReal() {
//用与适配前端定义的数据结构
var (
param orderDetail
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
orderSrv := orderService.NewOrderService(nil)
if param.Id > 0 {
err = orderSrv.UpdateOrderPurpose(orderCmd.UpdateOrderCommand{
Id: param.Id,
OrderCode: param.OrderId,
OrderName: param.OrderName,
OrderCount: param.OrderNum,
OrderAmount: param.OrderPrice,
BuyerPhone: param.BuyerPhone,
BuyerAddress: param.Address,
OrderRegion: param.OrderDist,
PartnerBonusPercent: param.PartnerRatio,
SalesmanBonusPercent: param.SalesmanRatio,
OrderStatus: param.OrderStatue,
OrderType: domain.OrderReal,
})
} else {
err = c.addOrderReal(param)
}
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(nil)
return
}
//GetOrderReal 获取实发单详情
func (c *OrderController) GetOrderReal() {
type Parameter struct {
Id string `json:"id"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
orderid, _ := strconv.ParseInt(param.Id, 10, 64)
orderSrv := orderService.NewOrderService(nil)
orderinfo, err := orderSrv.GetOrder(orderQuery.GetOrderQuery{
OrderId: orderid,
})
if err != nil {
c.ResponseError(err)
return
}
rsp := map[string]interface{}{
"createTime": orderinfo.CreateAt.Local().Format("2006-01-02 15:04:05"),
"updateTime": orderinfo.UpdateAt.Local().Format("2006-01-02 15:04:05"),
"buyer": orderinfo.Buyer.BuyerName,
"buyerPhone": orderinfo.Buyer.ContactInfo,
"address": orderinfo.Buyer.ShippingAddress,
"id": orderinfo.Id,
"partner": orderinfo.PartnerInfo.Id,
"partnerRatio": orderinfo.PartnerBonusPercent,
"salesmanRatio": orderinfo.SalesmanBonusPercent,
"orderId": orderinfo.OrderCode,
"orderName": orderinfo.OrderName,
"orderNum": orderinfo.OrderActualCount,
"orderPrice": orderinfo.OrderActualAmount,
"orderDist": orderinfo.OrderRegionInfo.RegionName,
"orderStatue": orderinfo.OrderStatus,
"reason": orderinfo.Reason,
}
c.ResponseData(rsp)
}
//UpdateOrderReal 更新实发订单数据
func (c *OrderController) UpdateOrderReal() {
//用与适配前端定义的数据结构
var (
param orderDetail
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
if param.Id == 0 {
err = c.addOrderReal(param)
} else {
err = c.editOrderReal(param)
}
if err != nil {
c.ResponseError(err)
}
c.ResponseData(nil)
return
}
//addOrderReal 添加实发订单
func (c *OrderController) addOrderReal(param orderDetail) error {
orderSrv := orderService.NewOrderService(nil)
if !(param.OrderStatue == domain.OrderStatusDeliverAll ||
param.OrderStatue == domain.OrderStatusDeliverSome) {
return lib.ThrowError(lib.ARG_ERROR, "订单状态错误")
}
Createcmd := orderCmd.CreateOrderCommand{
//订单区域
OrderRegion: param.OrderDist,
//订单编号
OrderCode: param.OrderId,
//订单名称
OrderName: param.OrderName,
//数量
OrderCount: param.OrderNum,
OrderActualCount: param.OrderNum,
//订单金额
OrderAmount: param.OrderPrice,
OrderActualAmount: param.OrderPrice,
//买家
BuyerName: param.Buyer,
//买家电话
BuyerPhone: param.BuyerPhone,
//地址
BuyerAddress: param.Address,
//合伙人数据
PartnerId: param.Partner,
//合伙人分红百分比
PartnerBonusPercent: param.PartnerRatio,
//业务员分红百分比
SalesmanBonusPercent: param.SalesmanRatio,
//订单类型
OrderType: domain.OrderReal,
//状态
OrderStatus: param.OrderStatue,
}
err := orderSrv.CreateOrder(Createcmd)
return err
}
//editOrderReal 更新实发订单
func (c *OrderController) editOrderReal(param orderDetail) error {
updateCmd := orderCmd.UpdateOrderRealCommand{
Id: param.Id,
//订单区域
OrderRegion: param.OrderDist,
//订单编号
OrderCode: param.OrderId,
//订单名称
OrderName: param.OrderName,
//数量
OrderActualCount: param.OrderNum,
//订单金额
OrderActualAmount: param.OrderPrice,
//买家电话
BuyerPhone: param.BuyerPhone,
//地址
BuyerAddress: param.Address,
//合伙人分红百分比
PartnerBonusPercent: param.PartnerRatio,
//业务员分红百分比
SalesmanBonusPercent: param.SalesmanRatio,
//状态
OrderStatus: param.OrderStatue,
//理由
Reason: param.Reason,
}
orderSrv := orderService.NewOrderService(nil)
err := orderSrv.UpdateOrderReal(updateCmd)
return err
}