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

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.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)
}
}
}