package model

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

type FrozenSemen struct {
	Id              int64                          `json:"id"`
	PastureId       int64                          `json:"pasture_id"`
	ParentId        int64                          `json:"parent_id"`
	Producer        string                         `json:"producer"`
	BullId          string                         `json:"bull_id"`
	KindId          pasturePb.CowKind_Kind         `json:"kind_id"`
	KindName        string                         `json:"kind_name"`
	FrozenSemenType pasturePb.FrozenSemenType_Kind `json:"frozen_semen_type"`
	Quantity        int32                          `json:"quantity"`
	Unit            pasturePb.Unit_Kind            `json:"unit"`
	Remarks         string                         `json:"remarks"`
	OperationId     int64                          `json:"operation_id"`
	CreatedAt       int64                          `json:"created_at"`
	UpdatedAt       int64                          `json:"updated_at"`
}

func NewFrozenSemen(pastureId int64, req *pasturePb.SearchFrozenSemenList, currentUser *SystemUser) *FrozenSemen {
	return &FrozenSemen{
		PastureId:       pastureId,
		Producer:        req.Producer,
		BullId:          req.BullId,
		KindId:          req.CowKind,
		KindName:        req.CowKindName,
		FrozenSemenType: req.FrozenSemenType,
		Quantity:        req.Quantity,
		Unit:            req.Unit,
		Remarks:         req.Remarks,
		OperationId:     currentUser.Id,
	}
}

func (f *FrozenSemen) TableName() string {
	return "frozen_semen"
}

func (f *FrozenSemen) EventQuantityUpdate(quantity int32) {
	f.Quantity -= quantity
}

func (f *FrozenSemen) UpdateData(req *pasturePb.SearchFrozenSemenList) {
	f.Quantity = req.Quantity
	f.KindId = req.CowKind
	f.KindName = req.CowKindName
	f.FrozenSemenType = req.FrozenSemenType
}

type FrozenSemenSlice []*FrozenSemen

func (e FrozenSemenSlice) ToPB(
	frozenSemenTypeMap map[pasturePb.FrozenSemenType_Kind]string,
	unitMap map[pasturePb.Unit_Kind]string) []*pasturePb.SearchFrozenSemenList {
	res := make([]*pasturePb.SearchFrozenSemenList, len(e))
	for i, v := range e {
		res[i] = &pasturePb.SearchFrozenSemenList{
			Id:                  int32(v.Id),
			ParentId:            int32(v.ParentId),
			Producer:            v.Producer,
			BullId:              v.BullId,
			CowKind:             v.KindId,
			CowKindName:         v.KindName,
			FrozenSemenType:     v.FrozenSemenType,
			FrozenSemenTypeName: frozenSemenTypeMap[v.FrozenSemenType],
			Quantity:            v.Quantity,
			Unit:                v.Unit,
			UnitName:            unitMap[v.Unit],
			Remarks:             v.Remarks,
			OperationId:         int32(v.OperationId),
			CreatedAt:           int32(v.CreatedAt),
			UpdatedAt:           int32(v.UpdatedAt),
		}
	}
	return res
}