package model

import (
	"strings"

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

type EventAbortion struct {
	Id                  int64                          `json:"id"`
	PastureId           int64                          `json:"pastureId"`
	CowId               int64                          `json:"cowId"`
	Lact                int32                          `json:"lact"`
	CowType             pasturePb.CowType_Kind         `json:"cowType"`
	DayAge              int32                          `json:"dayAge"`
	PregnantAge         int32                          `json:"pregnantAge"`
	AbortionAt          int64                          `json:"abortionAt"`
	IsAfterbirth        pasturePb.IsShow_Kind          `json:"isAfterbirth"`
	Photos              string                         `json:"photos"`
	AbortionReasons     pasturePb.AbortionReasons_Kind `json:"abortionReasons"`
	AbortionReasonsName string                         `json:"abortionReasonsName"`
	Remarks             string                         `json:"remarks"`
	IsAppend            pasturePb.IsShow_Kind          `json:"isAppend"`
	OperationId         int64                          `json:"operationId"`
	OperationName       string                         `json:"operationName"`
	CreatedAt           int64                          `json:"createdAt"`
	UpdatedAt           int64                          `json:"updatedAt"`
}

func (e *EventAbortion) TableName() string {
	return "event_abortion"
}

func NewEventAbortion(pastureId int64, cow *Cow, req *pasturePb.EventAbortionRequest, isAppend pasturePb.IsShow_Kind) *EventAbortion {
	return &EventAbortion{
		PastureId:           pastureId,
		CowId:               cow.Id,
		Lact:                cow.Lact,
		CowType:             cow.CowType,
		PregnantAge:         cow.GetDaysPregnant(),
		DayAge:              cow.DayAge,
		AbortionAt:          int64(req.AbortionAt),
		IsAfterbirth:        req.IsAfterbirth,
		Photos:              strings.Join(req.Photos, ","),
		AbortionReasons:     req.AbortionReasons,
		AbortionReasonsName: req.AbortionReasonsName,
		Remarks:             req.Remarks,
		IsAppend:            isAppend,
		OperationId:         int64(req.OperationId),
		OperationName:       req.OperationName,
	}
}

type AbortionSlice []*EventAbortion

func (a AbortionSlice) ToPB() []*pasturePb.EventAbortionRequest {
	res := make([]*pasturePb.EventAbortionRequest, len(a))
	for i, v := range a {
		res[i] = &pasturePb.EventAbortionRequest{
			Id:                  int32(v.Id),
			CowId:               int32(v.CowId),
			DayAge:              v.DayAge,
			Lact:                v.Lact,
			AbortionAt:          int32(v.AbortionAt),
			IsAfterbirth:        v.IsAfterbirth,
			Photos:              strings.Split(v.Photos, ","),
			AbortionReasons:     v.AbortionReasons,
			AbortionReasonsName: v.AbortionReasonsName,
			PregnancyAge:        v.PregnantAge,
			Remarks:             v.Remarks,
			OperationId:         int32(v.OperationId),
			OperationName:       v.OperationName,
			CreatedAt:           int32(v.CreatedAt),
			UpdatedAt:           int32(v.UpdatedAt),
		}
	}
	return res
}