package model

import (
	"kpt-pasture/util"
	"time"

	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
)

type EventWeaning struct {
	Id            int64                 `json:"id"`
	PastureId     int64                 `json:"pastureId"`
	CowId         int64                 `json:"cowId"`
	PlanDay       int64                 `json:"planDay"`
	EndDay        int64                 `json:"endDay"`
	RealityDay    int64                 `json:"realityDay"`
	Status        pasturePb.IsShow_Kind `json:"status"`
	BeforePenId   int32                 `json:"beforePenId"`
	AfterPenId    int32                 `json:"afterPenId"`
	Remarks       string                `json:"remarks"`
	OperationId   int32                 `json:"operationId"`
	OperationName string                `json:"operationName"`
	MessageId     int32                 `json:"messageId"`
	MessageName   string                `json:"messageName"`
	CreatedAt     int64                 `json:"createdAt"`
	UpdatedAt     int64                 `json:"updatedAt"`
}

func (e *EventWeaning) TableName() string {
	return "event_weaning"
}

func (e *EventWeaning) EventUpdate(weaningAt int64, remarks string, afterPenId int32, operationUser, currentUser *SystemUser) {
	e.Status = pasturePb.IsShow_Ok
	e.RealityDay = weaningAt
	e.OperationId = int32(operationUser.Id)
	e.OperationName = operationUser.Name
	e.MessageId = int32(currentUser.Id)
	e.MessageName = currentUser.Name
	e.Remarks = remarks
	e.AfterPenId = afterPenId
}

func NewEventWeaning(pastureId, cowId int64, penId int32) *EventWeaning {
	return &EventWeaning{
		PastureId:   pastureId,
		CowId:       cowId,
		PlanDay:     util.TimeParseLocalUnix(time.Now().Format(LayoutDate2)),
		EndDay:      util.TimeParseLocalEndUnix(time.Now().Format(LayoutDate2)),
		Status:      pasturePb.IsShow_No,
		BeforePenId: penId,
	}
}

func NewEventWeaningList(pastureId int64, cowList []*Cow) []*EventWeaning {
	var weaningList = make([]*EventWeaning, 0)
	for _, cow := range cowList {
		weaningList = append(weaningList, NewEventWeaning(pastureId, cow.Id, cow.PenId))
	}
	return weaningList
}