mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
245 lines
7.9 KiB
Go
245 lines
7.9 KiB
Go
package dto
|
|
|
|
import (
|
|
"time"
|
|
|
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
|
areaBaseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/dto"
|
|
fcrBaseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/fcrs/dto"
|
|
flockBaseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/dto"
|
|
kandangBaseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/dto"
|
|
locationBaseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/dto"
|
|
pfutils "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/utils"
|
|
userBaseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
|
|
)
|
|
|
|
// === DTO Structs (ordered) ===
|
|
|
|
type ChickinBaseDTO struct {
|
|
Id uint `json:"id"`
|
|
ProjectFlockKandangId uint `json:"project_flock_kandang_id"`
|
|
ChickInDate time.Time `json:"chick_in_date"`
|
|
ProductWarehouseId uint `json:"product_warehouse_id"`
|
|
UsageQty float64 `json:"usage_qty"`
|
|
PendingUsageQty float64 `json:"pending_usage_qty"`
|
|
Notes string `json:"notes"`
|
|
}
|
|
|
|
type ProjectFlockDTO struct {
|
|
Id uint `json:"id"`
|
|
Period int `json:"period"`
|
|
Category string `json:"category"`
|
|
Flock *flockBaseDTO.FlockBaseDTO `json:"flock"`
|
|
Area *areaBaseDTO.AreaBaseDTO `json:"area"`
|
|
Fcr *fcrBaseDTO.FcrBaseDTO `json:"fcr"`
|
|
Location *locationBaseDTO.LocationBaseDTO `json:"location"`
|
|
}
|
|
|
|
type ProjectFlockKandangDTO struct {
|
|
Id uint `json:"id"`
|
|
ProjectFlock *ProjectFlockDTO `json:"project_flock"`
|
|
Kandang *kandangBaseDTO.KandangBaseDTO `json:"kandang"`
|
|
}
|
|
|
|
// gunakan base DTO dari package users
|
|
|
|
type ChickinSimpleDTO struct {
|
|
Id uint `json:"id"`
|
|
ProjectFlockKandangId uint `json:"project_flock_kandang_id"`
|
|
ChickInDate time.Time `json:"chick_in_date"`
|
|
ProductWarehouseId uint `json:"product_warehouse_id"`
|
|
UsageQty float64 `json:"usage_qty"`
|
|
PendingUsageQty float64 `json:"pending_usage_qty"`
|
|
Notes string `json:"notes"`
|
|
CreatedBy uint `json:"created_by"`
|
|
}
|
|
|
|
type ChickinListDTO struct {
|
|
ChickinBaseDTO
|
|
CreatedUser *userBaseDTO.UserBaseDTO `json:"created_user"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
type ChickinDetailDTO struct {
|
|
Id uint `json:"id"`
|
|
ProjectFlockKandangId uint `json:"project_flock_kandang_id"`
|
|
ChickInDate time.Time `json:"chick_in_date"`
|
|
ProductWarehouseId uint `json:"product_warehouse_id"`
|
|
UsageQty float64 `json:"usage_qty"`
|
|
PendingUsageQty float64 `json:"pending_usage_qty"`
|
|
Notes string `json:"notes"`
|
|
CreatedBy uint `json:"created_by"`
|
|
CreatedUser *userBaseDTO.UserBaseDTO `json:"created_user"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
// === Mapper Functions (ordered) ===
|
|
|
|
func ToFlockDTO(e entity.Flock) flockBaseDTO.FlockBaseDTO {
|
|
return flockBaseDTO.ToFlockBaseDTO(e)
|
|
}
|
|
|
|
func ToKandangDTO(e entity.Kandang) kandangBaseDTO.KandangBaseDTO {
|
|
return kandangBaseDTO.ToKandangBaseDTO(e)
|
|
}
|
|
func ToAreaDTO(e entity.Area) areaBaseDTO.AreaBaseDTO {
|
|
return areaBaseDTO.ToAreaBaseDTO(e)
|
|
}
|
|
|
|
func ToFcrDTO(e entity.Fcr) fcrBaseDTO.FcrBaseDTO {
|
|
return fcrBaseDTO.ToFcrBaseDTO(e)
|
|
}
|
|
|
|
func ToLocationDTO(e entity.Location) locationBaseDTO.LocationBaseDTO {
|
|
return locationBaseDTO.ToLocationBaseDTO(e)
|
|
}
|
|
|
|
func ToUserBaseDTO(e entity.User) userBaseDTO.UserBaseDTO {
|
|
return userBaseDTO.ToUserBaseDTO(e)
|
|
}
|
|
|
|
func ToProjectFlockDTO(e entity.ProjectFlock) ProjectFlockDTO {
|
|
var flock *flockBaseDTO.FlockBaseDTO
|
|
if base := pfutils.DeriveBaseName(e.FlockName); base != "" {
|
|
summary := flockBaseDTO.FlockBaseDTO{Id: 0, Name: base}
|
|
flock = &summary
|
|
}
|
|
var area *areaBaseDTO.AreaBaseDTO
|
|
if e.Area.Id != 0 {
|
|
mapped := areaBaseDTO.ToAreaBaseDTO(e.Area)
|
|
area = &mapped
|
|
}
|
|
var fcr *fcrBaseDTO.FcrBaseDTO
|
|
if e.Fcr.Id != 0 {
|
|
mapped := fcrBaseDTO.ToFcrBaseDTO(e.Fcr)
|
|
fcr = &mapped
|
|
}
|
|
var location *locationBaseDTO.LocationBaseDTO
|
|
if e.Location.Id != 0 {
|
|
mapped := locationBaseDTO.ToLocationBaseDTO(e.Location)
|
|
location = &mapped
|
|
}
|
|
return ProjectFlockDTO{
|
|
Id: e.Id,
|
|
Period: e.Period,
|
|
Category: e.Category,
|
|
Flock: flock,
|
|
Area: area,
|
|
Fcr: fcr,
|
|
Location: location,
|
|
}
|
|
}
|
|
|
|
func ToProjectFlockKandangDTO(e entity.ProjectFlockKandang) ProjectFlockKandangDTO {
|
|
var pf *ProjectFlockDTO
|
|
if e.ProjectFlock.Id != 0 {
|
|
mapped := ToProjectFlockDTO(e.ProjectFlock)
|
|
pf = &mapped
|
|
}
|
|
var kandang *kandangBaseDTO.KandangBaseDTO
|
|
if e.Kandang.Id != 0 {
|
|
mapped := kandangBaseDTO.ToKandangBaseDTO(e.Kandang)
|
|
kandang = &mapped
|
|
}
|
|
return ProjectFlockKandangDTO{
|
|
Id: e.Id,
|
|
ProjectFlock: pf,
|
|
Kandang: kandang,
|
|
}
|
|
}
|
|
|
|
func ToChickinBaseDTO(e entity.ProjectChickin) ChickinBaseDTO {
|
|
var projectFlockKandangId uint
|
|
// Check if ProjectFlockKandang relation is loaded
|
|
if e.ProjectFlockKandang != nil && e.ProjectFlockKandang.Id != 0 {
|
|
projectFlockKandangId = e.ProjectFlockKandang.Id
|
|
} else if e.ProjectFlockKandangId != 0 {
|
|
// If relation is not loaded but ID is available, use the ID
|
|
projectFlockKandangId = e.ProjectFlockKandangId
|
|
}
|
|
return ChickinBaseDTO{
|
|
Id: e.Id,
|
|
ProjectFlockKandangId: projectFlockKandangId,
|
|
ChickInDate: e.ChickInDate,
|
|
ProductWarehouseId: e.ProductWarehouseId,
|
|
UsageQty: e.UsageQty,
|
|
PendingUsageQty: e.PendingUsageQty,
|
|
Notes: e.Notes,
|
|
}
|
|
}
|
|
|
|
func ToChickinSimpleDTO(e entity.ProjectChickin) ChickinSimpleDTO {
|
|
return ChickinSimpleDTO{
|
|
Id: e.Id,
|
|
ProjectFlockKandangId: e.ProjectFlockKandangId,
|
|
ChickInDate: e.ChickInDate,
|
|
ProductWarehouseId: e.ProductWarehouseId,
|
|
UsageQty: e.UsageQty,
|
|
PendingUsageQty: e.PendingUsageQty,
|
|
Notes: e.Notes,
|
|
CreatedBy: e.CreatedBy,
|
|
}
|
|
}
|
|
|
|
func ToChickinListDTO(e entity.ProjectChickin) ChickinListDTO {
|
|
var createdUser *userBaseDTO.UserBaseDTO
|
|
if e.CreatedUser != nil && e.CreatedUser.Id != 0 {
|
|
mapped := userBaseDTO.ToUserBaseDTO(*e.CreatedUser)
|
|
createdUser = &mapped
|
|
}
|
|
return ChickinListDTO{
|
|
ChickinBaseDTO: ToChickinBaseDTO(e),
|
|
CreatedUser: createdUser,
|
|
CreatedAt: e.CreatedAt,
|
|
UpdatedAt: e.UpdatedAt,
|
|
}
|
|
}
|
|
|
|
func ToChickinListDTOs(e []entity.ProjectChickin) []ChickinListDTO {
|
|
result := make([]ChickinListDTO, len(e))
|
|
for i, r := range e {
|
|
result[i] = ToChickinListDTO(r)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func ToChickinSimpleDTOs(e []entity.ProjectChickin) []ChickinSimpleDTO {
|
|
result := make([]ChickinSimpleDTO, len(e))
|
|
for i, r := range e {
|
|
result[i] = ToChickinSimpleDTO(r)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func ToChickinDetailDTO(e entity.ProjectChickin) ChickinDetailDTO {
|
|
var createdUser *userBaseDTO.UserBaseDTO
|
|
if e.CreatedUser != nil && e.CreatedUser.Id != 0 {
|
|
mapped := userBaseDTO.ToUserBaseDTO(*e.CreatedUser)
|
|
createdUser = &mapped
|
|
}
|
|
|
|
return ChickinDetailDTO{
|
|
Id: e.Id,
|
|
ProjectFlockKandangId: e.ProjectFlockKandangId,
|
|
ChickInDate: e.ChickInDate,
|
|
ProductWarehouseId: e.ProductWarehouseId,
|
|
UsageQty: e.UsageQty,
|
|
PendingUsageQty: e.PendingUsageQty,
|
|
Notes: e.Notes,
|
|
CreatedBy: e.CreatedBy,
|
|
CreatedUser: createdUser,
|
|
CreatedAt: e.CreatedAt,
|
|
UpdatedAt: e.UpdatedAt,
|
|
}
|
|
}
|
|
|
|
func ToChickinDetailDTOs(e []entity.ProjectChickin) []ChickinDetailDTO {
|
|
result := make([]ChickinDetailDTO, len(e))
|
|
for i, r := range e {
|
|
result[i] = ToChickinDetailDTO(r)
|
|
}
|
|
return result
|
|
}
|