package controllers

import (
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/order"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
	"time"
)

type OrderController struct {
	BaseController
}

//OrderStatistics 订单统计
// @router /statistics [post]
func (this *OrderController) Statistics() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *protocol.OrderStatisticsRequest
	if err := this.JsonUnmarshal(&request); err != nil {
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	header := this.GetRequestHeader(this.Ctx)
	msg = protocol.NewReturnResponse(order.Statistics(header, request))
}

//OrderStatistics 订单统计
// @router /statistics [post]
func (this *OrderController) StatisticsV2() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *protocol.OrderStatisticsRequest
	if err := this.JsonUnmarshal(&request); err != nil {
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	header := this.GetRequestHeader(this.Ctx)
	if request.PartnerId == 0 {
		request.PartnerId = header.UserId
	}
	msg = protocol.NewReturnResponse(order.StatisticsV2(header, request))
}

//OrderDetail 订单详情
// @router /orderDetail [post]
func (this *OrderController) OrderDetail() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *protocol.OrderDetailRequest
	if err := this.JsonUnmarshal(&request); err != nil {
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	header := this.GetRequestHeader(this.Ctx)
	msg = protocol.NewReturnResponse(order.Detail(header, request))
}

//OrderList
func (this *OrderController) OrderList() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *protocol.OrderListRequest
	if err := this.JsonUnmarshal(&request); err != nil {
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	request.OrderTypes = domain.UserOrderTypes(domain.Career)
	header := this.GetRequestHeader(this.Ctx)
	if request.PartnerId == 0 {
		request.PartnerId = header.UserId
	}
	msg = protocol.NewReturnResponse(order.List(header, request))
}

//OrderList
func (this *OrderController) Intentions() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *protocol.OrderListRequest
	if err := this.JsonUnmarshal(&request); err != nil {
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	request.EndTime = time.Now().Unix() * 1000
	request.OrderType = domain.OrderIntention
	header := this.GetRequestHeader(this.Ctx)
	msg = protocol.NewReturnResponse(order.List(header, request))
}