up
This commit is contained in:
@@ -902,14 +902,73 @@ func ApiWarehouse(r *gin.RouterGroup) {
|
||||
}
|
||||
}
|
||||
|
||||
// 为每个物品计算面包屑
|
||||
// 收集所有物品ID
|
||||
itemIDs := make([]uint, 0, len(items))
|
||||
for _, item := range items {
|
||||
itemIDs = append(itemIDs, item.ID)
|
||||
}
|
||||
|
||||
// 批量查询工单绑定数量
|
||||
workOrderCounts := make(map[uint]int)
|
||||
if len(itemIDs) > 0 {
|
||||
var woBinds []TabWarehouseItemWorkOrderBind
|
||||
models.DB.Where("item_id IN ?", itemIDs).Find(&woBinds)
|
||||
for _, bind := range woBinds {
|
||||
workOrderCounts[bind.ItemID]++
|
||||
}
|
||||
}
|
||||
|
||||
// 批量查询客户关联
|
||||
type CustomerInfo struct {
|
||||
ID uint `json:"id"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
itemCustomers := make(map[uint][]CustomerInfo)
|
||||
if len(itemIDs) > 0 {
|
||||
var customerBinds []TabWarehouseItemCustomerBind
|
||||
models.DB.Where("item_id IN ?", itemIDs).Find(&customerBinds)
|
||||
customerIDs := make([]uint, 0)
|
||||
for _, bind := range customerBinds {
|
||||
customerIDs = append(customerIDs, bind.CustomerID)
|
||||
}
|
||||
// 查询客户信息
|
||||
customerMap := make(map[uint]TabCustomer)
|
||||
if len(customerIDs) > 0 {
|
||||
var customers []TabCustomer
|
||||
models.DB.Where("id IN ?", customerIDs).Find(&customers)
|
||||
for _, c := range customers {
|
||||
customerMap[c.ID] = c
|
||||
}
|
||||
}
|
||||
// 构建物品ID到客户列表的映射
|
||||
for _, bind := range customerBinds {
|
||||
if c, ok := customerMap[bind.CustomerID]; ok {
|
||||
itemCustomers[bind.ItemID] = append(itemCustomers[bind.ItemID], CustomerInfo{
|
||||
ID: c.ID,
|
||||
FirstName: c.FirstName,
|
||||
LastName: c.LastName,
|
||||
Title: c.Title,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 为每个物品计算面包屑并添加额外信息
|
||||
type ItemWithBreadcrumb struct {
|
||||
TabWarehouseItem
|
||||
ContainerBreadcrumb string `json:"ContainerBreadcrumb"`
|
||||
ContainerBreadcrumb string `json:"ContainerBreadcrumb"`
|
||||
WorkOrderCount int `json:"WorkOrderCount"`
|
||||
Customers []CustomerInfo `json:"Customers"`
|
||||
}
|
||||
itemsWithBreadcrumb := make([]ItemWithBreadcrumb, len(items))
|
||||
for i, item := range items {
|
||||
itemsWithBreadcrumb[i] = ItemWithBreadcrumb{TabWarehouseItem: item}
|
||||
itemsWithBreadcrumb[i] = ItemWithBreadcrumb{
|
||||
TabWarehouseItem: item,
|
||||
WorkOrderCount: workOrderCounts[item.ID],
|
||||
Customers: itemCustomers[item.ID],
|
||||
}
|
||||
if item.ContainerID != nil {
|
||||
itemsWithBreadcrumb[i].ContainerBreadcrumb = buildContainerBreadcrumb(*item.ContainerID, containerMap)
|
||||
}
|
||||
|
||||
@@ -330,9 +330,58 @@ func ApiWorkOrder(r *gin.RouterGroup) {
|
||||
Limit(from.Entries).
|
||||
Find(&orders)
|
||||
|
||||
// 定义返回结构
|
||||
type CustomerInfo struct {
|
||||
ID uint `json:"id"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
PrimaryPhone string `json:"primary_phone"`
|
||||
}
|
||||
type OrderWithCustomers struct {
|
||||
TabWorkOrder
|
||||
Customers []CustomerInfo `json:"customers"`
|
||||
}
|
||||
|
||||
var ordersWithCustomers []OrderWithCustomers
|
||||
for _, order := range orders {
|
||||
orderItem := OrderWithCustomers{
|
||||
TabWorkOrder: order,
|
||||
Customers: []CustomerInfo{},
|
||||
}
|
||||
|
||||
// 查询关联客户
|
||||
var customerBinds []TabWorkOrderCustomerBind
|
||||
models.DB.Where("work_order_id = ?", order.ID).Find(&customerBinds)
|
||||
if len(customerBinds) > 0 {
|
||||
var customerIDs []uint
|
||||
for _, b := range customerBinds {
|
||||
customerIDs = append(customerIDs, b.CustomerID)
|
||||
}
|
||||
var customers []TabCustomer
|
||||
models.DB.Where("id IN ?", customerIDs).Find(&customers)
|
||||
for _, c := range customers {
|
||||
customerInfo := CustomerInfo{
|
||||
ID: c.ID,
|
||||
FirstName: c.FirstName,
|
||||
LastName: c.LastName,
|
||||
PrimaryPhone: "",
|
||||
}
|
||||
// 获取主电话
|
||||
var phone TabCustomerPhone
|
||||
if err := models.DB.Where("customer_id = ? AND is_primary = ?", c.ID, true).First(&phone).Error; err == nil {
|
||||
customerInfo.PrimaryPhone = phone.Phone
|
||||
} else if err := models.DB.Where("customer_id = ?", c.ID).First(&phone).Error; err == nil {
|
||||
customerInfo.PrimaryPhone = phone.Phone
|
||||
}
|
||||
orderItem.Customers = append(orderItem.Customers, customerInfo)
|
||||
}
|
||||
}
|
||||
ordersWithCustomers = append(ordersWithCustomers, orderItem)
|
||||
}
|
||||
|
||||
ReturnJson(ctx, "apiOK", gin.H{
|
||||
"all_count": count,
|
||||
"all_orders": orders,
|
||||
"all_orders": ordersWithCustomers,
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user