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

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",
)
}