Files
platforms/backend/api/internal/logic/gas/scope.go

114 lines
4.7 KiB
Go

package gas
import (
"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"
)
func currentGas(ctx *gin.Context) (models.GasBasic, bool) {
_, station, ok := CurrentGasAccount(ctx)
return station, ok
}
func respondList(ctx *gin.Context, list any, total int64) {
response, err := common.PublicResourceResponse(list)
if err != nil {
infra.Response.Error(ctx, err)
return
}
infra.Response.Success(ctx, gin.H{"total": total, "list": response})
}
func respondScopedRecord(ctx *gin.Context, query *gorm.DB, model any) {
if err := query.First(model).Error; err != nil {
common.RespondRecordError(ctx, err)
return
}
response, err := common.PublicResourceResponse(model)
if err != nil {
infra.Response.Error(ctx, err)
return
}
infra.Response.Success(ctx, response)
}
func requireDelivery(ctx *gin.Context, identity string, gasID uint64) (models.DeliveryBasic, bool) {
var delivery models.DeliveryBasic
if err := common.ActiveRecords(impl.DBService).
Where("identity = ? AND gas_basic_id = ?", identity, gasID).First(&delivery).Error; err != nil {
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
return models.DeliveryBasic{}, false
}
return delivery, true
}
func requireStaff(ctx *gin.Context, identity string, gasID uint64) (models.StaffAccount, bool) {
var staff models.StaffAccount
if err := common.ActiveRecords(impl.DBService).
Where("identity = ? AND gas_basic_id = ?", identity, gasID).First(&staff).Error; err != nil {
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
return models.StaffAccount{}, false
}
return staff, true
}
func requireUser(ctx *gin.Context, identity string, gasID uint64) (models.UserAccount, models.UserServiceRelation, bool) {
user, relation, err := findCurrentGasUser(impl.DBService, identity, gasID)
if err != nil {
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
return models.UserAccount{}, models.UserServiceRelation{}, false
}
return user, relation, true
}
// findCurrentGasUser 查询当前气站有效服务关系内的用户,不直接写入 HTTP 响应,供只读合同兜底复用。
func findCurrentGasUser(databaseService *gorm.DB, identity string, gasID uint64) (models.UserAccount, models.UserServiceRelation, error) {
var user models.UserAccount
var relation models.UserServiceRelation
err := common.ActiveRecords(databaseService.Model(&models.UserAccount{})).
Select("user_account.*").
Joins("JOIN user_service_relation ON user_service_relation.user_account_id = user_account.id AND user_service_relation.status <> ?",
common.StatusArchived).
Where("user_account.identity = ? AND user_service_relation.gas_basic_id = ?", identity, gasID).
First(&user).Error
if err != nil {
return models.UserAccount{}, models.UserServiceRelation{}, err
}
if err := common.ActiveRecords(databaseService).
Where("user_account_id = ? AND gas_basic_id = ?", user.ID, gasID).First(&relation).Error; err != nil {
return models.UserAccount{}, models.UserServiceRelation{}, err
}
return user, relation, nil
}
// contractScopedUserRecord 带出合同保存的默认配送点,供无服务关系用户只读展示本站合同上下文。
type contractScopedUserRecord struct {
models.UserAccount
ContractDeliveryBasicID uint64 `gorm:"column:contract_delivery_basic_id"`
}
// findContractScopedUser 仅查询当前气站合同中实际引用的用户,禁止借详情接口枚举全局用户。
func findContractScopedUser(databaseService *gorm.DB, identity string, gasID uint64, contractIdentity string) (contractScopedUserRecord, error) {
var record contractScopedUserRecord
err := contractScopedUserQuery(databaseService, identity, gasID, contractIdentity).
First(&record).Error
return record, err
}
// contractScopedUserQuery 统一合同用户只读详情与头像的气站范围查询条件。
func contractScopedUserQuery(databaseService *gorm.DB, identity string, gasID uint64, contractIdentity string) *gorm.DB {
query := common.ActiveRecords(databaseService.Model(&models.UserAccount{})).
Select("user_account.*, gasorder_contract.delivery_basic_id AS contract_delivery_basic_id").
Joins("JOIN gasorder_contract ON gasorder_contract.user_account_id = user_account.id AND gasorder_contract.status <> ?", common.StatusArchived).
Where("user_account.identity = ? AND gasorder_contract.gas_basic_id = ?", identity, gasID)
if contractIdentity != "" {
query = query.Where("gasorder_contract.identity = ?", contractIdentity)
}
return query.Order("gasorder_contract.created_at DESC")
}