统一合同气阀候选与绑定入口

This commit is contained in:
czl231
2026-08-31 22:04:22 +08:00
parent 9a23ab2f5a
commit 0f2475098f
23 changed files with 352 additions and 47 deletions

View File

@@ -1,5 +1,5 @@
// 功能描述:实现配送点范围内的合同、合同气瓶和配送订单接口。
// 版本v1.4.1
// 版本v1.5.0
package delivery
import (
@@ -22,6 +22,8 @@ import (
"gorm.io/gorm/clause"
)
var errContractUserOutsidePoint = errors.New("签约用户已不属于当前配送点,只允许查看或终止合同")
func rewriteJSON(ctx *gin.Context, mutate func(map[string]any) bool) bool {
body, err := io.ReadAll(ctx.Request.Body)
if err != nil {
@@ -49,6 +51,16 @@ func scopedContract(ctx *gin.Context, identity string, pointID uint64) (models.G
return contract, true
}
// contractUserActiveAtPoint 校验合同用户仍属于合同气站及当前配送点。
func contractUserActiveAtPoint(databaseService *gorm.DB, contract models.GasorderContract, pointID uint64) bool {
var count int64
err := databaseService.Model(&models.UserServiceRelation{}).
Where("user_account_id = ? AND gas_basic_id = ? AND delivery_basic_id = ? AND status <> ?",
contract.UserAccountID, contract.GasBasicID, pointID, common.StatusArchived).
Count(&count).Error
return err == nil && count > 0
}
// deliveryContractListQuery 构造当前配送点合同列表;订单候选模式额外限定可履约状态。
func deliveryContractListQuery(database *gorm.DB, gasID, pointID uint64, candidate string, now time.Time) *gorm.DB {
query := platformgasorder.ContractPartyDisplayQuery(database).
@@ -280,8 +292,15 @@ func BindContractProduct(ctx *gin.Context) {
}
if !rewriteJSON(ctx, func(values map[string]any) bool {
identity, _ := values["gasorder_contract_identity"].(string)
_, valid := scopedContract(ctx, identity, point.ID)
return valid
contract, valid := scopedContract(ctx, identity, point.ID)
if !valid {
return false
}
if !contractUserActiveAtPoint(db(), contract, point.ID) {
infra.Response.Error(ctx, errContractUserOutsidePoint)
return false
}
return true
}) {
return
}
@@ -329,28 +348,54 @@ func GetContractRevision(ctx *gin.Context) {
respondRecord(ctx, query, &item)
}
// deliveryContractProductCandidateQuery 按合同用户及当前配送点服务关系限定候选气阀。
func deliveryContractProductCandidateQuery(databaseService *gorm.DB, contract models.GasorderContract, pointID uint64) *gorm.DB {
query := common.ActiveRecords(databaseService.Model(&models.ProductInfo{}))
return platformgasorder.ContractProductCandidateQuery(query, contract).
Where(`EXISTS (
SELECT 1 FROM user_service_relation AS candidate_relation
WHERE candidate_relation.user_account_id = product_info.user_account_id
AND candidate_relation.gas_basic_id = ?
AND candidate_relation.delivery_basic_id = ?
AND candidate_relation.status <> ?
AND candidate_relation.deleted_at IS NULL
)`, contract.GasBasicID, pointID, common.StatusArchived)
}
// ListProductCandidate 仅返回属于指定合同用户且仍在当前配送点服务范围内的候选气阀。
func ListProductCandidate(ctx *gin.Context) {
point, _, ok := currentScope(ctx)
if !ok {
return
}
query := common.ActiveRecords(db().Model(&models.ProductInfo{})).
Joins("JOIN user_service_relation ON user_service_relation.user_account_id = product_info.user_account_id AND user_service_relation.status <> ?",
common.StatusArchived).Where("user_service_relation.delivery_basic_id = ?", point.ID)
query := common.ActiveRecords(db().Model(&models.ProductInfo{})).Where("1 = 0")
if contractIdentity := strings.TrimSpace(ctx.Query("contract_identity")); contractIdentity != "" {
contract, valid := scopedContract(ctx, contractIdentity, point.ID)
if !valid {
return
}
query = deliveryContractProductCandidateQuery(db(), contract, point.ID)
}
listScoped(ctx, &models.ProductInfo{}, query, "product_info.created_at desc")
}
// GetProductCandidate 返回指定合同用户范围内的单条候选气阀。
func GetProductCandidate(ctx *gin.Context) {
point, _, ok := currentScope(ctx)
if !ok {
return
}
var item models.ProductInfo
query := common.ActiveRecords(db().Model(&models.ProductInfo{})).
Select("product_info.*").
Joins("JOIN user_service_relation ON user_service_relation.user_account_id = product_info.user_account_id AND user_service_relation.status <> ?",
common.StatusArchived).
Where("product_info.identity = ? AND user_service_relation.delivery_basic_id = ?", ctx.Param("identity"), point.ID)
contractIdentity := strings.TrimSpace(ctx.Query("contract_identity"))
query := common.ActiveRecords(db().Model(&models.ProductInfo{})).Where("1 = 0")
if contractIdentity != "" {
contract, valid := scopedContract(ctx, contractIdentity, point.ID)
if !valid {
return
}
query = deliveryContractProductCandidateQuery(db(), contract, point.ID)
}
query = query.Select("product_info.*").Where("product_info.identity = ?", ctx.Param("identity"))
respondRecord(ctx, query, &item)
}

View File

@@ -1,5 +1,5 @@
// 功能描述:验证配送点创建订单的合同、地址和合同气瓶候选查询范围。
// 版本v1.0.0。
// 版本v1.1.0。
package delivery
import (
@@ -7,6 +7,7 @@ import (
"testing"
"time"
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
"github.com/DATA-DOG/go-sqlmock"
"gorm.io/driver/postgres"
"gorm.io/gorm"
@@ -76,3 +77,19 @@ func TestDeliveryOrderContractProductCandidates(t *testing.T) {
"gasorder_contract_product.product_code",
)
}
// TestDeliveryContractProductBindingCandidates 验证配送点候选同时受合同用户和当前配送点服务关系约束。
func TestDeliveryContractProductBindingCandidates(t *testing.T) {
database := candidateTestDatabase(t)
statement := database.ToSQL(func(tx *gorm.DB) *gorm.DB {
contract := models.GasorderContract{Entity: models.Entity{ID: 27}, UserAccountID: 31, GasBasicID: 7, DeliveryBasicID: 9}
return deliveryContractProductCandidateQuery(tx, contract, 9).Find(&[]models.ProductInfo{})
})
assertSQLFragments(t, statement,
"product_info.user_account_id = 31",
"candidate_binding.gasorder_contract_id = 27",
"candidate_relation.gas_basic_id = 7",
"candidate_relation.delivery_basic_id = 9",
"candidate_relation.user_account_id = product_info.user_account_id",
)
}

View File

@@ -1,5 +1,5 @@
// 功能描述:实现气站范围内的配送合同、合同气瓶和燃气配送订单接口。
// 版本v1.2.0
// 版本v1.3.0
package gas
import (
@@ -233,29 +233,52 @@ func GetGasorderContractProduct(ctx *gin.Context) {
respondScopedRecord(ctx, query, &models.GasorderContractProduct{})
}
// ListContractProductCandidate 仅返回当前气站服务用户名下可用于合同绑定的气瓶
// gasContractProductCandidateQuery 按合同用户和当前气站服务关系限定可绑定智能气阀
func gasContractProductCandidateQuery(databaseService *gorm.DB, contract models.GasorderContract, gasBasicID uint64) *gorm.DB {
query := common.ActiveRecords(databaseService.Model(&models.ProductInfo{}))
return platformgasorder.ContractProductCandidateQuery(query, contract).
Where(`EXISTS (
SELECT 1 FROM user_service_relation AS candidate_relation
WHERE candidate_relation.user_account_id = product_info.user_account_id
AND candidate_relation.gas_basic_id = ?
AND candidate_relation.status <> ?
AND candidate_relation.deleted_at IS NULL
)`, gasBasicID, common.StatusArchived)
}
// ListContractProductCandidate 仅返回属于指定合同用户且仍在当前气站服务范围内的可绑定气阀。
func ListContractProductCandidate(ctx *gin.Context) {
station, ok := currentGas(ctx)
if !ok {
return
}
query := common.ActiveRecords(impl.DBService.Model(&models.ProductInfo{})).
Joins("JOIN user_service_relation ON user_service_relation.user_account_id = product_info.user_account_id AND user_service_relation.status <> ?",
common.StatusArchived).
Where("user_service_relation.gas_basic_id = ?", station.ID)
query := common.ActiveRecords(impl.DBService.Model(&models.ProductInfo{})).Where("1 = 0")
if contractIdentity := strings.TrimSpace(ctx.Query("contract_identity")); contractIdentity != "" {
contract, valid := scopedContract(ctx, contractIdentity, station.ID)
if !valid {
return
}
query = gasContractProductCandidateQuery(impl.DBService, contract, station.ID)
}
listScoped(ctx, &models.ProductInfo{}, query, "product_info.created_at desc")
}
// GetContractProductCandidate 返回当前气站服务用户范围内的单条候选气
// GetContractProductCandidate 返回指定合同用户范围内的单条候选气
func GetContractProductCandidate(ctx *gin.Context) {
station, ok := currentGas(ctx)
if !ok {
return
}
query := common.ActiveRecords(impl.DBService.Model(&models.ProductInfo{})).
Select("product_info.*").
Joins("JOIN user_service_relation ON user_service_relation.user_account_id = product_info.user_account_id AND user_service_relation.status <> ?", common.StatusArchived).
Where("product_info.identity = ? AND user_service_relation.gas_basic_id = ?", ctx.Param("identity"), station.ID)
contractIdentity := strings.TrimSpace(ctx.Query("contract_identity"))
query := common.ActiveRecords(impl.DBService.Model(&models.ProductInfo{})).Where("1 = 0")
if contractIdentity != "" {
contract, valid := scopedContract(ctx, contractIdentity, station.ID)
if !valid {
return
}
query = gasContractProductCandidateQuery(impl.DBService, contract, station.ID)
}
query = query.Select("product_info.*").Where("product_info.identity = ?", ctx.Param("identity"))
respondScopedRecord(ctx, query, &models.ProductInfo{})
}

View File

@@ -1,11 +1,12 @@
// 功能描述:验证气站合同与配送订单展示查询的对象权限范围。
// 版本v1.0.0
// 版本v1.1.0
package gas
import (
"strings"
"testing"
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
"github.com/DATA-DOG/go-sqlmock"
"gorm.io/driver/postgres"
"gorm.io/gorm"
@@ -35,3 +36,28 @@ func TestScopedOrderDisplayQuery(t *testing.T) {
}
}
}
// TestGasContractProductCandidates 验证气站候选按指定合同用户过滤,而非返回本站全部用户设备。
func TestGasContractProductCandidates(t *testing.T) {
connection, _, err := sqlmock.New()
if err != nil {
t.Fatalf("创建 SQL 模拟失败:%v", err)
}
t.Cleanup(func() { _ = connection.Close() })
databaseService, err := gorm.Open(postgres.New(postgres.Config{Conn: connection}), &gorm.Config{DryRun: true})
if err != nil {
t.Fatalf("创建 GORM 数据库失败:%v", err)
}
statement := gasContractProductCandidateQuery(databaseService, models.GasorderContract{Entity: models.Entity{ID: 17}, UserAccountID: 23}, 42).
Find(&[]models.ProductInfo{}).Statement.SQL.String()
for _, fragment := range []string{
"product_info.user_account_id =",
"candidate_binding.gasorder_contract_id =",
"candidate_relation.gas_basic_id =",
"candidate_relation.user_account_id = product_info.user_account_id",
} {
if !strings.Contains(statement, fragment) {
t.Fatalf("气站合同气阀候选 SQL 缺少 %q%s", fragment, statement)
}
}
}

View File

@@ -1,5 +1,5 @@
// 功能描述:为配送合同提供关联主体名称和当前服务关系状态,不新增数据库字段。
// 版本v1.1.0
// 版本v1.2.0
package gasorder
import (
@@ -15,6 +15,7 @@ type ContractPartyDisplay struct {
GasBasicDisplayName string `gorm:"column:gas_basic_display_name" json:"gas_basic_display_name"`
DeliveryDisplayName string `gorm:"column:delivery_basic_display_name" json:"delivery_basic_display_name"`
UserServiceActive bool `gorm:"column:user_service_active" json:"user_service_active"`
UserDeliveryActive bool `gorm:"column:user_delivery_service_active" json:"user_delivery_service_active"`
}
// ContractPartyDisplayQuery 仅通过合同已保存的关联主键读取名称,不开放平台主体全集。
@@ -33,7 +34,18 @@ func ContractPartyDisplayQuery(databaseService *gorm.DB) *gorm.DB {
AND contract_relation.gas_basic_id = gasorder_contract.gas_basic_id
AND contract_relation.status <> ?
AND contract_relation.deleted_at IS NULL
) AS user_service_active`, common.StatusArchived).
) AS user_service_active,
CASE
WHEN gasorder_contract.delivery_basic_id = 0 THEN FALSE
ELSE EXISTS (
SELECT 1 FROM user_service_relation AS delivery_relation
WHERE delivery_relation.user_account_id = gasorder_contract.user_account_id
AND delivery_relation.gas_basic_id = gasorder_contract.gas_basic_id
AND delivery_relation.delivery_basic_id = gasorder_contract.delivery_basic_id
AND delivery_relation.status <> ?
AND delivery_relation.deleted_at IS NULL
)
END AS user_delivery_service_active`, common.StatusArchived, common.StatusArchived).
Joins("LEFT JOIN user_account AS contract_user ON contract_user.id = gasorder_contract.user_account_id").
Joins("LEFT JOIN gas_basic AS contract_gas ON contract_gas.id = gasorder_contract.gas_basic_id").
Joins("LEFT JOIN delivery_basic AS contract_delivery ON contract_delivery.id = gasorder_contract.delivery_basic_id")

View File

@@ -0,0 +1,22 @@
// 功能描述:提供跨管理端一致的合同可绑定智能气阀候选查询。
// 版本v1.0.0。
package gasorder
import (
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common"
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
"gorm.io/gorm"
)
// ContractProductCandidateQuery 仅返回属于合同用户、可用、未报废且未重复绑定的智能气阀。
func ContractProductCandidateQuery(query *gorm.DB, contract models.GasorderContract) *gorm.DB {
return query.
Where("product_info.status = ? AND product_info.product_status <> ? AND product_info.user_account_id = ?",
common.StatusEnable, common.StatusScrapped, contract.UserAccountID).
Where(`NOT EXISTS (
SELECT 1 FROM gasorder_contract_product AS candidate_binding
WHERE candidate_binding.product_info_id = product_info.id
AND candidate_binding.gasorder_contract_id = ?
AND candidate_binding.unbound_at IS NULL
)`, contract.ID)
}

View File

@@ -1,5 +1,5 @@
// 功能描述:验证平台配送合同、订单创建和调度状态规则。
// 版本v1.3.0
// 版本v1.4.0
package gasorder
import (
@@ -100,6 +100,8 @@ func TestContractPartyDisplayQuery(t *testing.T) {
"delivery_basic_display_name",
"contract_relation.user_account_id = gasorder_contract.user_account_id",
"contract_relation.gas_basic_id = gasorder_contract.gas_basic_id",
"delivery_relation.delivery_basic_id = gasorder_contract.delivery_basic_id",
"user_delivery_service_active",
"gasorder_contract.gas_basic_id = 42",
} {
if !strings.Contains(statement, fragment) {
@@ -108,6 +110,35 @@ func TestContractPartyDisplayQuery(t *testing.T) {
}
}
// TestContractProductCandidateQuery 验证三端共享候选严格按合同用户和设备可用性筛选。
func TestContractProductCandidateQuery(t *testing.T) {
sqlDatabase, _, err := sqlmock.New()
if err != nil {
t.Fatalf("创建模拟数据库失败:%v", err)
}
defer sqlDatabase.Close()
database, err := gorm.Open(postgres.New(postgres.Config{Conn: sqlDatabase}), &gorm.Config{})
if err != nil {
t.Fatalf("创建 GORM 数据库失败:%v", err)
}
statement := database.ToSQL(func(tx *gorm.DB) *gorm.DB {
query := common.ActiveRecords(tx.Model(&models.ProductInfo{}))
return ContractProductCandidateQuery(query, models.GasorderContract{Entity: models.Entity{ID: 17}, UserAccountID: 23}).
Find(&[]models.ProductInfo{})
})
for _, fragment := range []string{
"product_info.status = 1",
"product_info.product_status <> 27",
"product_info.user_account_id = 23",
"candidate_binding.gasorder_contract_id = 17",
"candidate_binding.unbound_at IS NULL",
} {
if !strings.Contains(statement, fragment) {
t.Fatalf("合同气阀候选 SQL 缺少 %q%s", fragment, statement)
}
}
}
// TestFilterGasorderContractProductCandidates 验证订单候选仅包含当前合同未解绑气瓶并按业务字段排序。
func TestFilterGasorderContractProductCandidates(t *testing.T) {
sqlDatabase, _, err := sqlmock.New()

View File

@@ -11,6 +11,7 @@ import (
"git.apinb.com/bsm-sdk/core/middleware"
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common"
platformgasorder "git.apinb.com/heqiapp/platforms/backend/api/internal/logic/platform/gasorder"
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
@@ -47,9 +48,7 @@ func listProductInfo(ctx *gin.Context) {
return
}
common.ListPageFiltered[models.ProductInfo](ctx, func(query *gorm.DB) *gorm.DB {
return query.
Where("status = ? AND product_status <> ? AND user_account_id = ?", common.StatusEnable, common.StatusScrapped, contract.UserAccountID).
Where("NOT EXISTS (SELECT 1 FROM gasorder_contract_product WHERE gasorder_contract_product.product_info_id = product_info.id AND gasorder_contract_product.gasorder_contract_id = ? AND gasorder_contract_product.unbound_at IS NULL)", contract.ID)
return platformgasorder.ContractProductCandidateQuery(query, contract)
})
}