mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-25 15:55:44 +00:00
Feat[BE]: add multilpple type of chickin growing and laying, make convertion product when chickin approved, add projectflockkandangid on projectflock api
This commit is contained in:
+153
-160
@@ -8,8 +8,9 @@ import (
|
||||
areaDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/dto"
|
||||
fcrDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/fcrs/dto"
|
||||
flockDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/dto"
|
||||
kandangDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/dto"
|
||||
locationDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/dto"
|
||||
productDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/products/dto"
|
||||
warehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/dto"
|
||||
chickinDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/dto"
|
||||
projectFlockDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/dto"
|
||||
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
|
||||
@@ -27,7 +28,7 @@ type ProjectFlockCustomDTO struct {
|
||||
Category string `json:"category"`
|
||||
Fcr *fcrDTO.FcrBaseDTO `json:"fcr,omitempty"`
|
||||
Location *locationDTO.LocationBaseDTO `json:"location,omitempty"`
|
||||
Kandangs []kandangDTO.KandangBaseDTO `json:"kandangs,omitempty"`
|
||||
Kandangs []KandangCustomDTO `json:"kandangs,omitempty"`
|
||||
CreatedUser *userDTO.UserBaseDTO `json:"created_user,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
@@ -39,22 +40,11 @@ type KandangCustomDTO struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
type ProductBaseDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type WarehouseBaseDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type ProductWarehouseDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Quantity float64 `json:"quantity"`
|
||||
Product *ProductBaseDTO `json:"product,omitempty"`
|
||||
Warehouse *WarehouseBaseDTO `json:"warehouse,omitempty"`
|
||||
Id uint `json:"id"`
|
||||
Quantity float64 `json:"quantity"`
|
||||
Product *productDTO.ProductBaseDTO `json:"product,omitempty"`
|
||||
Warehouse *warehouseDTO.WarehouseBaseDTO `json:"warehouse,omitempty"`
|
||||
}
|
||||
|
||||
type AvailableQtyDTO struct {
|
||||
@@ -95,74 +85,38 @@ func toProjectFlockCustomDTO(pf *projectFlockDTO.ProjectFlockListDTO) *ProjectFl
|
||||
Category: pf.Category,
|
||||
Fcr: pf.Fcr,
|
||||
Location: pf.Location,
|
||||
Kandangs: pf.Kandangs,
|
||||
Kandangs: buildKandangCustomDTOs(pf.Kandangs),
|
||||
CreatedUser: pf.CreatedUser,
|
||||
CreatedAt: pf.CreatedAt,
|
||||
UpdatedAt: pf.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func buildAvailableQtys(chickins []entity.ProjectChickin) []AvailableQtyDTO {
|
||||
if len(chickins) == 0 {
|
||||
return nil
|
||||
func ToProjectFlockKandangListDTOWithAvailableQty(e entity.ProjectFlockKandang, availableQtysRaw []map[string]interface{}) ProjectFlockKandangListDTO {
|
||||
var projectFlockSummary *projectFlockDTO.ProjectFlockListDTO
|
||||
if e.ProjectFlock.Id != 0 {
|
||||
mapped := projectFlockDTO.ToProjectFlockListDTO(e.ProjectFlock)
|
||||
projectFlockSummary = &mapped
|
||||
}
|
||||
|
||||
availableQtyMap := make(map[uint]AvailableQtyDTO)
|
||||
for _, ch := range chickins {
|
||||
if ch.ProductWarehouse == nil || ch.ProductWarehouse.Quantity <= 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if _, exists := availableQtyMap[ch.ProductWarehouseId]; exists {
|
||||
continue
|
||||
}
|
||||
|
||||
pwDTO := &ProductWarehouseDTO{
|
||||
Id: ch.ProductWarehouse.Id,
|
||||
Quantity: ch.ProductWarehouse.Quantity,
|
||||
}
|
||||
|
||||
if ch.ProductWarehouse.Product.Id != 0 {
|
||||
pwDTO.Product = &ProductBaseDTO{
|
||||
Id: ch.ProductWarehouse.Product.Id,
|
||||
Name: ch.ProductWarehouse.Product.Name,
|
||||
}
|
||||
}
|
||||
|
||||
if ch.ProductWarehouse.Warehouse.Id != 0 {
|
||||
pwDTO.Warehouse = &WarehouseBaseDTO{
|
||||
Id: ch.ProductWarehouse.Warehouse.Id,
|
||||
Name: ch.ProductWarehouse.Warehouse.Name,
|
||||
Type: ch.ProductWarehouse.Warehouse.Type,
|
||||
}
|
||||
}
|
||||
|
||||
availableQtyMap[ch.ProductWarehouseId] = AvailableQtyDTO{
|
||||
ProductWarehouse: pwDTO,
|
||||
}
|
||||
return ProjectFlockKandangListDTO{
|
||||
ProjectFlockKandangBaseDTO: ToProjectFlockKandangBaseDTO(e),
|
||||
ProjectFlock: toProjectFlockCustomDTO(projectFlockSummary),
|
||||
Kandang: buildKandangFromEntity(e.Kandang),
|
||||
Chickins: buildChickins(e.Chickins),
|
||||
AvailableQtys: buildAvailableQtysFromRaw(availableQtysRaw),
|
||||
CreatedAt: e.CreatedAt,
|
||||
CreatedUser: buildCreatedUser(e.ProjectFlock),
|
||||
Approval: defaultProjectFlockKandangLatestApproval(e),
|
||||
}
|
||||
|
||||
if len(availableQtyMap) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
result := make([]AvailableQtyDTO, 0, len(availableQtyMap))
|
||||
for _, v := range availableQtyMap {
|
||||
result = append(result, v)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func buildChickins(chickins []entity.ProjectChickin) []chickinDTO.ChickinBaseDTO {
|
||||
if len(chickins) == 0 {
|
||||
return nil
|
||||
func toKandangCustomDTO(k projectFlockDTO.KandangWithProjectFlockIdDTO) KandangCustomDTO {
|
||||
return KandangCustomDTO{
|
||||
Id: k.Id,
|
||||
Name: k.Name,
|
||||
Status: k.Status,
|
||||
}
|
||||
|
||||
result := make([]chickinDTO.ChickinBaseDTO, len(chickins))
|
||||
for i, ch := range chickins {
|
||||
result[i] = chickinDTO.ToChickinBaseDTO(ch)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func defaultProjectFlockKandangLatestApproval(e entity.ProjectFlockKandang) *approvalDTO.ApprovalBaseDTO {
|
||||
@@ -180,32 +134,14 @@ func ToProjectFlockKandangListDTO(e entity.ProjectFlockKandang) ProjectFlockKand
|
||||
projectFlockSummary = &mapped
|
||||
}
|
||||
|
||||
var kandangSummary *KandangCustomDTO
|
||||
if e.Kandang.Id != 0 {
|
||||
kandangSummary = &KandangCustomDTO{
|
||||
Id: e.Kandang.Id,
|
||||
Name: e.Kandang.Name,
|
||||
Status: e.Kandang.Status,
|
||||
}
|
||||
}
|
||||
|
||||
var createdUser *userDTO.UserBaseDTO
|
||||
if e.ProjectFlock.CreatedUser.Id != 0 {
|
||||
mapped := userDTO.ToUserBaseDTO(e.ProjectFlock.CreatedUser)
|
||||
createdUser = &mapped
|
||||
} else if e.ProjectFlock.CreatedBy != 0 {
|
||||
mapped := userDTO.UserBaseDTO{Id: e.ProjectFlock.CreatedBy, IdUser: int64(e.ProjectFlock.CreatedBy)}
|
||||
createdUser = &mapped
|
||||
}
|
||||
|
||||
return ProjectFlockKandangListDTO{
|
||||
ProjectFlockKandangBaseDTO: ToProjectFlockKandangBaseDTO(e),
|
||||
ProjectFlock: toProjectFlockCustomDTO(projectFlockSummary),
|
||||
Kandang: kandangSummary,
|
||||
Kandang: buildKandangFromEntity(e.Kandang),
|
||||
Chickins: buildChickins(e.Chickins),
|
||||
AvailableQtys: buildAvailableQtys(e.Chickins),
|
||||
CreatedAt: e.CreatedAt,
|
||||
CreatedUser: createdUser,
|
||||
CreatedUser: buildCreatedUser(e.ProjectFlock),
|
||||
Approval: defaultProjectFlockKandangLatestApproval(e),
|
||||
}
|
||||
}
|
||||
@@ -224,69 +160,6 @@ func ToProjectFlockKandangDetailDTO(e entity.ProjectFlockKandang) ProjectFlockKa
|
||||
}
|
||||
}
|
||||
|
||||
func ToProjectFlockKandangListDTOWithAvailableQty(e entity.ProjectFlockKandang, availableQtysRaw []map[string]interface{}) ProjectFlockKandangListDTO {
|
||||
var projectFlockSummary *projectFlockDTO.ProjectFlockListDTO
|
||||
if e.ProjectFlock.Id != 0 {
|
||||
mapped := projectFlockDTO.ToProjectFlockListDTO(e.ProjectFlock)
|
||||
projectFlockSummary = &mapped
|
||||
}
|
||||
|
||||
var kandangSummary *KandangCustomDTO
|
||||
if e.Kandang.Id != 0 {
|
||||
kandangSummary = &KandangCustomDTO{
|
||||
Id: e.Kandang.Id,
|
||||
Name: e.Kandang.Name,
|
||||
Status: e.Kandang.Status,
|
||||
}
|
||||
}
|
||||
|
||||
var createdUser *userDTO.UserBaseDTO
|
||||
if e.ProjectFlock.CreatedUser.Id != 0 {
|
||||
mapped := userDTO.ToUserBaseDTO(e.ProjectFlock.CreatedUser)
|
||||
createdUser = &mapped
|
||||
} else if e.ProjectFlock.CreatedBy != 0 {
|
||||
mapped := userDTO.UserBaseDTO{Id: e.ProjectFlock.CreatedBy, IdUser: int64(e.ProjectFlock.CreatedBy)}
|
||||
createdUser = &mapped
|
||||
}
|
||||
|
||||
// convert available qtys from raw map data
|
||||
var availableQtys []AvailableQtyDTO
|
||||
if len(availableQtysRaw) > 0 {
|
||||
availableQtys = buildAvailableQtysFromRaw(availableQtysRaw)
|
||||
}
|
||||
|
||||
return ProjectFlockKandangListDTO{
|
||||
ProjectFlockKandangBaseDTO: ToProjectFlockKandangBaseDTO(e),
|
||||
ProjectFlock: toProjectFlockCustomDTO(projectFlockSummary),
|
||||
Kandang: kandangSummary,
|
||||
Chickins: buildChickins(e.Chickins),
|
||||
AvailableQtys: availableQtys,
|
||||
CreatedAt: e.CreatedAt,
|
||||
CreatedUser: createdUser,
|
||||
Approval: defaultProjectFlockKandangLatestApproval(e),
|
||||
}
|
||||
}
|
||||
|
||||
func buildAvailableQtysFromRaw(availableQtysRaw []map[string]interface{}) []AvailableQtyDTO {
|
||||
if len(availableQtysRaw) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
result := make([]AvailableQtyDTO, len(availableQtysRaw))
|
||||
for i, v := range availableQtysRaw {
|
||||
pwData, ok := v["product_warehouse"].(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
pwDTO := buildProductWarehouseFromMap(pwData)
|
||||
result[i] = AvailableQtyDTO{
|
||||
ProductWarehouse: pwDTO,
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func buildProductWarehouseFromMap(pwData map[string]interface{}) *ProductWarehouseDTO {
|
||||
if pwData == nil {
|
||||
return nil
|
||||
@@ -315,12 +188,12 @@ func buildProductWarehouseFromMap(pwData map[string]interface{}) *ProductWarehou
|
||||
return dto
|
||||
}
|
||||
|
||||
func buildProductFromMap(pData map[string]interface{}) *ProductBaseDTO {
|
||||
func buildProductFromMap(pData map[string]interface{}) *productDTO.ProductBaseDTO {
|
||||
if pData == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
product := &ProductBaseDTO{}
|
||||
product := &productDTO.ProductBaseDTO{}
|
||||
if id, ok := pData["id"].(float64); ok {
|
||||
product.Id = uint(id)
|
||||
} else if id, ok := pData["id"].(uint); ok {
|
||||
@@ -332,12 +205,12 @@ func buildProductFromMap(pData map[string]interface{}) *ProductBaseDTO {
|
||||
return product
|
||||
}
|
||||
|
||||
func buildWarehouseFromMap(wData map[string]interface{}) *WarehouseBaseDTO {
|
||||
func buildWarehouseFromMap(wData map[string]interface{}) *warehouseDTO.WarehouseBaseDTO {
|
||||
if wData == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
warehouse := &WarehouseBaseDTO{}
|
||||
warehouse := &warehouseDTO.WarehouseBaseDTO{}
|
||||
if id, ok := wData["id"].(float64); ok {
|
||||
warehouse.Id = uint(id)
|
||||
} else if id, ok := wData["id"].(uint); ok {
|
||||
@@ -351,3 +224,123 @@ func buildWarehouseFromMap(wData map[string]interface{}) *WarehouseBaseDTO {
|
||||
}
|
||||
return warehouse
|
||||
}
|
||||
|
||||
func buildCreatedUser(pf entity.ProjectFlock) *userDTO.UserBaseDTO {
|
||||
if pf.CreatedUser.Id != 0 {
|
||||
mapped := userDTO.ToUserBaseDTO(pf.CreatedUser)
|
||||
return &mapped
|
||||
} else if pf.CreatedBy != 0 {
|
||||
return &userDTO.UserBaseDTO{
|
||||
Id: pf.CreatedBy,
|
||||
IdUser: int64(pf.CreatedBy),
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildKandangCustomDTOs(kandangs []projectFlockDTO.KandangWithProjectFlockIdDTO) []KandangCustomDTO {
|
||||
if len(kandangs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
result := make([]KandangCustomDTO, len(kandangs))
|
||||
for i, k := range kandangs {
|
||||
result[i] = toKandangCustomDTO(k)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func buildKandangFromEntity(kandang entity.Kandang) *KandangCustomDTO {
|
||||
if kandang.Id == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &KandangCustomDTO{
|
||||
Id: kandang.Id,
|
||||
Name: kandang.Name,
|
||||
Status: kandang.Status,
|
||||
}
|
||||
}
|
||||
|
||||
func buildChickins(chickins []entity.ProjectChickin) []chickinDTO.ChickinBaseDTO {
|
||||
if len(chickins) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
result := make([]chickinDTO.ChickinBaseDTO, len(chickins))
|
||||
for i, ch := range chickins {
|
||||
result[i] = chickinDTO.ToChickinBaseDTO(ch)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func buildAvailableQtys(chickins []entity.ProjectChickin) []AvailableQtyDTO {
|
||||
if len(chickins) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
availableQtyMap := make(map[uint]AvailableQtyDTO)
|
||||
for _, ch := range chickins {
|
||||
if ch.ProductWarehouse == nil || ch.ProductWarehouse.Quantity <= 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if _, exists := availableQtyMap[ch.ProductWarehouseId]; exists {
|
||||
continue
|
||||
}
|
||||
|
||||
pwDTO := &ProductWarehouseDTO{
|
||||
Id: ch.ProductWarehouse.Id,
|
||||
Quantity: ch.ProductWarehouse.Quantity,
|
||||
}
|
||||
|
||||
if ch.ProductWarehouse.Product.Id != 0 {
|
||||
pwDTO.Product = &productDTO.ProductBaseDTO{
|
||||
Id: ch.ProductWarehouse.Product.Id,
|
||||
Name: ch.ProductWarehouse.Product.Name,
|
||||
}
|
||||
}
|
||||
|
||||
if ch.ProductWarehouse.Warehouse.Id != 0 {
|
||||
pwDTO.Warehouse = &warehouseDTO.WarehouseBaseDTO{
|
||||
Id: ch.ProductWarehouse.Warehouse.Id,
|
||||
Name: ch.ProductWarehouse.Warehouse.Name,
|
||||
Type: ch.ProductWarehouse.Warehouse.Type,
|
||||
}
|
||||
}
|
||||
|
||||
availableQtyMap[ch.ProductWarehouseId] = AvailableQtyDTO{
|
||||
ProductWarehouse: pwDTO,
|
||||
}
|
||||
}
|
||||
|
||||
if len(availableQtyMap) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
result := make([]AvailableQtyDTO, 0, len(availableQtyMap))
|
||||
for _, v := range availableQtyMap {
|
||||
result = append(result, v)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func buildAvailableQtysFromRaw(availableQtysRaw []map[string]interface{}) []AvailableQtyDTO {
|
||||
if len(availableQtysRaw) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
result := make([]AvailableQtyDTO, len(availableQtysRaw))
|
||||
for i, v := range availableQtysRaw {
|
||||
pwData, ok := v["product_warehouse"].(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
pwDTO := buildProductWarehouseFromMap(pwData)
|
||||
result[i] = AvailableQtyDTO{
|
||||
ProductWarehouse: pwDTO,
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user