80 lines
2.8 KiB
Go
80 lines
2.8 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) {
|
|
var user models.UserAccount
|
|
var relation models.UserServiceRelation
|
|
err := common.ActiveRecords(impl.DBService.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 {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return models.UserAccount{}, models.UserServiceRelation{}, false
|
|
}
|
|
if err := common.ActiveRecords(impl.DBService).
|
|
Where("user_account_id = ? AND gas_basic_id = ?", user.ID, gasID).First(&relation).Error; err != nil {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return models.UserAccount{}, models.UserServiceRelation{}, false
|
|
}
|
|
return user, relation, true
|
|
}
|