feat: improve gas station management workflow
This commit is contained in:
@@ -1,16 +1,38 @@
|
||||
package delivery
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// ListDeliveryBasic 查询配送点分页列表。
|
||||
func ListDeliveryBasic(ctx *gin.Context) { common.ListPage[models.DeliveryBasic](ctx) }
|
||||
func ListDeliveryBasic(ctx *gin.Context) {
|
||||
gasIdentities := strings.Split(strings.TrimSpace(ctx.Query("gas_basic_identities")), ",")
|
||||
if len(gasIdentities) == 1 && gasIdentities[0] == "" {
|
||||
gasIdentities = nil
|
||||
}
|
||||
if len(gasIdentities) > 100 {
|
||||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
common.ListPageFiltered[models.DeliveryBasic](ctx, func(query *gorm.DB) *gorm.DB {
|
||||
if len(gasIdentities) == 0 {
|
||||
return query
|
||||
}
|
||||
return query.Where(
|
||||
"gas_basic_id IN (SELECT id FROM gas_basic WHERE identity IN ? AND status <> ?)",
|
||||
gasIdentities,
|
||||
common.StatusArchived,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
// GetDeliveryBasic 查询一个配送点。
|
||||
func GetDeliveryBasic(ctx *gin.Context) { common.GetByIdentity[models.DeliveryBasic](ctx) }
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
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 查询可燃气体站分页列表。
|
||||
@@ -46,3 +52,78 @@ func UpdateGasBasic(ctx *gin.Context) {
|
||||
}
|
||||
common.UpdateAllowedByIdentity(ctx, &models.GasBasic{}, gin.H{"name": request.Name, "credit_code": request.CreditCode, "principal": request.Principal, "address": request.Address, "longitude": request.Longitude, "latitude": request.Latitude}, []string{"name", "credit_code", "principal", "address", "longitude", "latitude"})
|
||||
}
|
||||
|
||||
// UpdateGasBasicStatus 仅允许已审核气站在启用和停用之间切换。
|
||||
func UpdateGasBasicStatus(ctx *gin.Context) {
|
||||
var request struct {
|
||||
Status int `json:"status" binding:"required"`
|
||||
}
|
||||
if err := ctx.ShouldBindJSON(&request); err != nil ||
|
||||
(request.Status != common.StatusEnable && request.Status != common.StatusDisable) {
|
||||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
result := impl.DBService.Model(&models.GasBasic{}).
|
||||
Where("identity = ? AND status IN ?", ctx.Param("identity"), []int{common.StatusEnable, common.StatusDisable}).
|
||||
Update("status", request.Status)
|
||||
if result.Error != nil {
|
||||
infra.Response.Error(ctx, result.Error)
|
||||
return
|
||||
}
|
||||
if result.RowsAffected != 1 {
|
||||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
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})
|
||||
}
|
||||
|
||||
@@ -46,6 +46,9 @@ func platformMenuAllowsRequest(menus []platformbase.Menu, requestPath, method st
|
||||
(resource == "delivery_basic" || resource == "staff_account") {
|
||||
return true
|
||||
}
|
||||
if method == "GET" && menu.Identity == "gas_basic" && resource == "delivery_basic" {
|
||||
return true
|
||||
}
|
||||
if method == "GET" && relative == "wallet_basic" &&
|
||||
(menu.Identity == "gas_basic" || menu.Identity == "delivery_basic" ||
|
||||
menu.Identity == "staff" || menu.Identity == "user_account" ||
|
||||
|
||||
@@ -58,6 +58,16 @@ func TestOwnerMenusCanReadEmbeddedWallets(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGasManagementCanReadDeliveryPointCounts(t *testing.T) {
|
||||
menus := []platformbase.Menu{{Identity: "gas_basic"}}
|
||||
if !platformMenuAllowsRequest(menus, "/heqi/platform/v1/delivery_basic", "GET") {
|
||||
t.Fatal("gas management could not read its delivery point counts")
|
||||
}
|
||||
if platformMenuAllowsRequest(menus, "/heqi/platform/v1/delivery_basic", "POST") {
|
||||
t.Fatal("gas management unexpectedly received delivery point creation access")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocationScopeValuesAreExplicit(t *testing.T) {
|
||||
if !validLocationScope("standard") || !validLocationScope("precise") {
|
||||
t.Fatal("supported location scopes were rejected")
|
||||
|
||||
Reference in New Issue
Block a user