feat(platform): improve admin data workflows
This commit is contained in:
@@ -1,25 +1,43 @@
|
||||
package gas
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/infra"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// ListGasBasic 查询可燃气体站分页列表。
|
||||
func ListGasBasic(ctx *gin.Context) { common.ListPage[models.GasBasic](ctx) }
|
||||
|
||||
// GetGasBasic 查询一个可燃气体站。
|
||||
func GetGasBasic(ctx *gin.Context) { common.GetByIdentity[models.GasBasic](ctx) }
|
||||
func GetGasBasic(ctx *gin.Context) {
|
||||
var station models.GasBasic
|
||||
if err := common.ActiveRecords(impl.DBService).Where("identity = ?", ctx.Param("identity")).First(&station).Error; err != nil {
|
||||
common.RespondRecordError(ctx, err)
|
||||
return
|
||||
}
|
||||
response, err := gasBasicDetailResponse(station, common.HasPreciseLocationScope(ctx))
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(ctx, response)
|
||||
}
|
||||
|
||||
// gasBasicDetailResponse 仅向具备精确定位范围的后台账号返回气站地址和坐标。
|
||||
func gasBasicDetailResponse(station models.GasBasic, retainPreciseLocation bool) (any, error) {
|
||||
response, err := common.PublicResourceResponse(station)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !retainPreciseLocation {
|
||||
common.ProtectPublicFields(response, false, false, false)
|
||||
}
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// CreateGasBasic 创建可燃气体站档案。
|
||||
func CreateGasBasic(ctx *gin.Context) {
|
||||
@@ -28,7 +46,7 @@ func CreateGasBasic(ctx *gin.Context) {
|
||||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
request.Entity = common.NewEntity(common.StatusDraft)
|
||||
request.Entity = common.NewEntity(common.StatusEnable)
|
||||
if err := impl.DBService.Create(&request).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
@@ -64,7 +82,7 @@ func UpdateGasBasicStatus(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
result := impl.DBService.Model(&models.GasBasic{}).
|
||||
Where("identity = ? AND status IN ?", ctx.Param("identity"), []int{common.StatusEnable, common.StatusDisable}).
|
||||
Where("identity = ? AND status IN ?", ctx.Param("identity"), []int{common.StatusDraft, common.StatusEnable, common.StatusDisable}).
|
||||
Update("status", request.Status)
|
||||
if result.Error != nil {
|
||||
infra.Response.Error(ctx, result.Error)
|
||||
@@ -76,54 +94,3 @@ func UpdateGasBasicStatus(ctx *gin.Context) {
|
||||
}
|
||||
infra.Response.Success(ctx, gin.H{"updated": true})
|
||||
}
|
||||
|
||||
// ReviewGasBasic 审核待审核气站;不通过时必须填写理由。
|
||||
func ReviewGasBasic(ctx *gin.Context) {
|
||||
var request struct {
|
||||
Approved bool `json:"approved"`
|
||||
Reason string `json:"reason" binding:"max=2000"`
|
||||
}
|
||||
if err := ctx.ShouldBindJSON(&request); err != nil ||
|
||||
(!request.Approved && strings.TrimSpace(request.Reason) == "") {
|
||||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
|
||||
operatorIdentity, operatorName := common.PlatformOperator(ctx)
|
||||
targetStatus := common.StatusDisable
|
||||
reviewStatus := common.StatusRejected
|
||||
if request.Approved {
|
||||
targetStatus = common.StatusEnable
|
||||
reviewStatus = common.StatusApproved
|
||||
}
|
||||
now := time.Now()
|
||||
err := impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
var station models.GasBasic
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("identity = ? AND status <> ?", ctx.Param("identity"), common.StatusArchived).
|
||||
First(&station).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if station.Status != common.StatusDraft {
|
||||
return errors.New("gas station is not pending review")
|
||||
}
|
||||
if err := tx.Model(&station).Update("status", targetStatus).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
review := models.GasBasicReview{
|
||||
Entity: common.NewEntity(common.StatusEnable),
|
||||
GasBasicID: station.ID,
|
||||
ReviewStatus: reviewStatus,
|
||||
ReviewReason: strings.TrimSpace(request.Reason),
|
||||
ReviewerIdentity: operatorIdentity,
|
||||
ReviewerName: operatorName,
|
||||
ReviewedAt: now,
|
||||
}
|
||||
return tx.Create(&review).Error
|
||||
})
|
||||
if err != nil {
|
||||
common.RespondRecordError(ctx, err)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(ctx, gin.H{"updated": true, "status": targetStatus})
|
||||
}
|
||||
|
||||
40
backend/api/internal/logic/platform/gas/gas_test.go
Normal file
40
backend/api/internal/logic/platform/gas/gas_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package gas
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||||
)
|
||||
|
||||
func TestGasBasicDetailPreciseLocationProjection(t *testing.T) {
|
||||
station := models.GasBasic{
|
||||
Entity: models.Entity{ID: 1, Identity: "00000000-0000-7000-8000-000000000001"},
|
||||
Code: "GAS-001",
|
||||
Name: "示例气站",
|
||||
Address: "示例地址",
|
||||
Longitude: "121.5000",
|
||||
Latitude: "31.2000",
|
||||
}
|
||||
|
||||
precise, err := gasBasicDetailResponse(station, true)
|
||||
if err != nil {
|
||||
t.Fatalf("project precise detail: %v", err)
|
||||
}
|
||||
preciseMap := precise.(map[string]any)
|
||||
for key := range map[string]struct{}{"address": {}, "longitude": {}, "latitude": {}} {
|
||||
if preciseMap[key] == nil || preciseMap[key] == "" {
|
||||
t.Fatalf("precise detail missing %s: %#v", key, preciseMap)
|
||||
}
|
||||
}
|
||||
|
||||
restricted, err := gasBasicDetailResponse(station, false)
|
||||
if err != nil {
|
||||
t.Fatalf("project restricted detail: %v", err)
|
||||
}
|
||||
restrictedMap := restricted.(map[string]any)
|
||||
for _, key := range []string{"address", "longitude", "latitude"} {
|
||||
if _, exists := restrictedMap[key]; exists {
|
||||
t.Fatalf("restricted detail exposed %s: %#v", key, restrictedMap)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user