package backend

import (
	"PaymentCenter/app/http/entities"
	"PaymentCenter/app/models/orderlogmodel"
	"PaymentCenter/app/models/ordersmodel"
	"PaymentCenter/app/utils"
	"github.com/pkg/errors"

	"time"
)

type OrderListRequest struct {
	Id              int64  `json:"id" form:"id"`
	MerchantId      int64  `json:"merchant_id" form:"merchant_id"`
	PayId           int64  `json:"pay_id" form:"pay_id"`
	AppId           int64  `json:"app_id" form:"app_id"`
	MerchantOrderId string `json:"merchant_order_id" form:"merchant_order_id"`
	Status          int    `json:"status" form:"status"`
	OrderType       int    `json:"order_type" form:"order_type"`
	StartTime       string `json:"start_time" form:"start_time"`
	EndTime         string `json:"end_time" form:"end_time"`
	entities.PageRequest
}
type OrderList struct {
	Id              int64     `json:"id"`
	MerchantId      int64     `json:"merchant_id"`
	PayId           int64     `json:"pay_id"`
	AppId           int64     `json:"app_id"`
	MerchantOrderId string    `json:"merchant_order_id"`
	Status          int       `json:"status"`
	OrderType       int       `json:"order_type"`
	StartTime       time.Time `json:"start_time"`
	EndTime         time.Time `json:"end_time"`
	entities.PageRequest
}

func (o *OrderListRequest) ValidateRequest() (r OrderList, err error) {
	if o.StartTime != "" {
		r.StartTime, err = utils.StrToTimeShanghai(o.StartTime)
		if err != nil {
			err = errors.Wrap(err, "时间格式错误")
			return
		}
	}
	if o.EndTime != "" {
		r.EndTime, err = utils.StrToTimeShanghai(o.EndTime)
		if err != nil {
			err = errors.Wrap(err, "时间格式错误")
			return
		}
	}

	r.Id = o.Id
	r.MerchantId = o.MerchantId
	r.PayId = o.PayId
	r.AppId = o.AppId
	r.MerchantOrderId = o.MerchantOrderId
	r.Status = o.Status
	r.OrderType = o.OrderType
	r.PageRequest = o.PageRequest
	r.SetDefault()
	return
}

type OrdersResponse struct {
	Id               int64  `json:"id"`
	MerchantId       int64  `json:"merchant_id"`
	PayId            int64  `json:"pay_id"`
	AppId            int64  `json:"app_id"`
	MerchantOrderId  string `json:"merchant_order_id"`
	Status           int    `json:"status"`
	OrderType        int    `json:"order_type"`
	Amount           int    `json:"amount"`
	IpAddress        string `json:"ip_address"`
	MerchantRequest  string `json:"merchant_request"`
	MerchantResponse string `json:"merchant_response"`
	OrderResponse    string `json:"order_response"`
	ExtJson          string `json:"ext_json"`
	CreateTime       string `json:"create_time"`
	UpdateTime       string `json:"update_time"`
	MerchantName     string `json:"merchant_name"`
	PayName          string `json:"pay_name"`
	AppName          string `json:"app_name"`
}

func (o *OrdersResponse) ResponseFromDb(db ordersmodel.OrdersBackendList) {
	o.Id = db.Id
	o.MerchantId = db.MerchantId
	o.PayId = db.PayId
	o.AppId = db.AppId
	o.MerchantOrderId = db.MerchantOrderId
	o.Status = db.Status
	o.OrderType = db.OrderType
	o.Amount = db.Amount
	o.IpAddress = db.IpAddress
	o.MerchantRequest = db.MerchantRequest
	o.MerchantResponse = db.MerchantResponse
	o.OrderResponse = db.OrderResponse
	o.ExtJson = db.ExtJson
	o.CreateTime = db.CreateTime.Format("2006-01-02 15:04:05")
	o.UpdateTime = db.UpdateTime.Format("2006-01-02 15:04:05")
	o.MerchantName = db.MerchantName
	o.PayName = db.PayName
	o.AppName = db.AppName
}

type OrderLogsListRequest struct {
	OrderId int64 `json:"order_id" form:"order_id"`
	entities.PageRequest
}

type OrderLogResponse struct {
	Id               int64  `json:"id"`
	OrderId          int64  `json:"order_id"`
	PayCallback      string `json:"pay_callback"`
	Status           int    `json:"status"`
	MerchantParam    string `json:"merchant_param"`
	MerchantCallback string `json:"merchant_callback"`
	CreateTime       string `json:"create_time"`
}

func (o *OrderLogResponse) ResponseFromDb(db orderlogmodel.OrderLog) {
	o.Id = db.Id
	o.OrderId = db.OrderId
	o.PayCallback = db.PayCallback
	o.Status = db.Status
	o.MerchantParam = db.MerchantParam
	o.MerchantCallback = db.MerchantCallback
	o.CreateTime = db.CreateTime.Format("2006-01-02 15:04:05")
}