package models

import (
	"context"
	"time"

	"github.com/go-pg/pg/v10"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
)

type Order struct {
	tableName struct{} `pg:"order"`
	//id
	Id int64 `pg:",pk"`
	//订单类型 1:实际订单 2:意向订单
	OrderType int `pg:",notnull,default:1"`
	//订单编号
	OrderCode string `pg:",unique,notnull`
	//订单名称
	OrderName string
	//订单状态
	OrderStatus int `pg:",notnull,default:1"`
	//订单数量
	OrderCount int
	//实际订单数量
	OrderActualCount int
	//订单金额
	OrderAmount float64
	//订单实际金额
	OrderActualAmount float64
	//订单已支付金额(货款)
	OrderPaymentAmount float64 `pg:",use_zero"`
	//最后分红状态
	BonusStatus int `pg:",default:1"`
	//订单区域信息
	OrderRegionInfo domain.RegionInfo

	Buyer domain.Buyer
	//合伙人编号
	PartnerId   int64
	PartnerInfo PartnerInfo `pg:"fk:partner_id"`
	//合伙人分红百分比
	PartnerBonusPercent float64
	//业务员分红百分比
	SalesmanBonusPercent float64

	//创建时间
	CreateAt time.Time
	//更新时间
	UpdateAt time.Time

	//上一次查看时间 已读情况
	LastViewTime time.Time
	//理由
	Reason string
}

var _ pg.BeforeUpdateHook = (*Order)(nil)

func (or *Order) BeforeUpdate(ctx context.Context) (context.Context, error) {
	or.UpdateAt = time.Now()
	return ctx, nil
}

var _ pg.BeforeInsertHook = (*Order)(nil)

func (or *Order) BeforeInsert(ctx context.Context) (context.Context, error) {
	or.CreateAt = time.Now()
	or.UpdateAt = time.Now()
	return ctx, nil
}