feat: product.

This commit is contained in:
2026-05-01 07:46:48 +08:00
parent 79515007b7
commit 7b43ccf42f
10 changed files with 1070 additions and 1 deletions
+83
View File
@@ -1,6 +1,7 @@
package controllers
import (
"encoding/json"
"errors"
"hr_receiver/config"
"hr_receiver/models"
@@ -122,6 +123,10 @@ func (gc *GatewayAdminController) Create(c *gin.Context) {
writeError(c, http.StatusInternalServerError, "保存网关失败")
return
}
if err := gc.syncGatewayInventory(record); err != nil {
writeError(c, http.StatusInternalServerError, err.Error())
return
}
writeSuccess(c, http.StatusCreated, "创建成功", record)
}
@@ -176,6 +181,10 @@ func (gc *GatewayAdminController) Update(c *gin.Context) {
writeError(c, http.StatusInternalServerError, "更新网关失败")
return
}
if err := gc.syncGatewayInventory(record); err != nil {
writeError(c, http.StatusInternalServerError, err.Error())
return
}
writeSuccess(c, http.StatusOK, "更新成功", record)
}
@@ -198,6 +207,10 @@ func (gc *GatewayAdminController) Delete(c *gin.Context) {
writeError(c, http.StatusInternalServerError, "删除网关失败")
return
}
if err := gc.deleteGatewayInventory(record); err != nil {
writeError(c, http.StatusInternalServerError, err.Error())
return
}
writeSuccess(c, http.StatusOK, "删除成功", nil)
}
@@ -232,6 +245,76 @@ func (gc *GatewayAdminController) findGatewayProjectType(projectTypeCode string)
return projectType, nil
}
func (gc *GatewayAdminController) syncGatewayInventory(record models.Gateway) error {
mac := strings.ToUpper(strings.TrimSpace(record.MAC))
if mac == "" {
return nil
}
parameterValuesBytes, err := json.Marshal(map[string]interface{}{
"mac": mac,
"location": strings.TrimSpace(record.Location),
})
if err != nil {
return err
}
status := models.ProductInventoryStatusInStock
if record.IsSold {
status = models.ProductInventoryStatusSold
}
serialNumber := mac
regionID := record.RegionID
var inventory models.ProductInventory
err = gc.DB.Where("source_type = ? AND source_ref = ?", "gateway", mac).First(&inventory).Error
switch {
case err == nil:
inventory.ProductCode = "collection_gateway"
inventory.ProjectTypeCode = strings.TrimSpace(strings.ToLower(record.ProjectType))
inventory.SerialNumber = &serialNumber
inventory.AssetName = strings.TrimSpace(record.Name)
inventory.Status = status
inventory.RegionID = &regionID
inventory.StorageLocation = strings.TrimSpace(record.Location)
inventory.ParameterValues = string(parameterValuesBytes)
inventory.SourceType = "gateway"
inventory.SourceRef = mac
if record.IsSold {
inventory.SoldAt = record.SoldAt
} else {
inventory.SoldAt = nil
}
return gc.DB.Save(&inventory).Error
case errors.Is(err, gorm.ErrRecordNotFound):
inventory = models.ProductInventory{
ProductCode: "collection_gateway",
ProjectTypeCode: strings.TrimSpace(strings.ToLower(record.ProjectType)),
SerialNumber: &serialNumber,
AssetName: strings.TrimSpace(record.Name),
Status: status,
RegionID: &regionID,
StorageLocation: strings.TrimSpace(record.Location),
SoldAt: record.SoldAt,
ParameterValues: string(parameterValuesBytes),
SourceType: "gateway",
SourceRef: mac,
}
return gc.DB.Create(&inventory).Error
default:
return err
}
}
func (gc *GatewayAdminController) deleteGatewayInventory(record models.Gateway) error {
mac := strings.ToUpper(strings.TrimSpace(record.MAC))
if mac == "" {
return nil
}
return gc.DB.Where("source_type = ? AND source_ref = ?", "gateway", mac).Delete(&models.ProductInventory{}).Error
}
// 辅助方法:验证输入数据
func validateGatewayPayload(payload gatewayPayload) error {
mac := strings.TrimSpace(payload.MAC)