97 lines
3.5 KiB
Go
97 lines
3.5 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"
|
|
)
|
|
|
|
// ListGasBasic 查询可燃气体站分页列表。
|
|
func ListGasBasic(ctx *gin.Context) { common.ListPage[models.GasBasic](ctx) }
|
|
|
|
// GetGasBasic 查询一个可燃气体站。
|
|
func GetGasBasic(ctx *gin.Context) {
|
|
var station models.GasBasic
|
|
if err := common.ActiveRecords(impl.DBService).Where("identity = ?", ctx.Param("identity")).First(&station).Error; err != nil {
|
|
common.RespondRecordError(ctx, err)
|
|
return
|
|
}
|
|
response, err := gasBasicDetailResponse(station, common.HasPreciseLocationScope(ctx))
|
|
if err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
infra.Response.Success(ctx, response)
|
|
}
|
|
|
|
// gasBasicDetailResponse 仅向具备精确定位范围的后台账号返回气站地址和坐标。
|
|
func gasBasicDetailResponse(station models.GasBasic, retainPreciseLocation bool) (any, error) {
|
|
response, err := common.PublicResourceResponse(station)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !retainPreciseLocation {
|
|
common.ProtectPublicFields(response, false, false, false)
|
|
}
|
|
return response, nil
|
|
}
|
|
|
|
// CreateGasBasic 创建可燃气体站档案。
|
|
func CreateGasBasic(ctx *gin.Context) {
|
|
var request models.GasBasic
|
|
if err := ctx.ShouldBindJSON(&request); err != nil || request.Code == "" || request.Name == "" {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
request.Entity = common.NewEntity(common.StatusEnable)
|
|
if err := impl.DBService.Create(&request).Error; err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
common.RespondCreatedResource(ctx, request)
|
|
}
|
|
|
|
// UpdateGasBasic 更新可燃气体站基础资料。
|
|
func UpdateGasBasic(ctx *gin.Context) {
|
|
var request struct {
|
|
Name string `json:"name" binding:"required,max=128"`
|
|
CreditCode string `json:"credit_code" binding:"max=64"`
|
|
Principal string `json:"principal" binding:"max=64"`
|
|
Address string `json:"address" binding:"max=255"`
|
|
Longitude string `json:"longitude" binding:"max=32"`
|
|
Latitude string `json:"latitude" binding:"max=32"`
|
|
}
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
common.UpdateAllowedByIdentity(ctx, &models.GasBasic{}, gin.H{"name": request.Name, "credit_code": request.CreditCode, "principal": request.Principal, "address": request.Address, "longitude": request.Longitude, "latitude": request.Latitude}, []string{"name", "credit_code", "principal", "address", "longitude", "latitude"})
|
|
}
|
|
|
|
// UpdateGasBasicStatus 仅允许已审核气站在启用和停用之间切换。
|
|
func UpdateGasBasicStatus(ctx *gin.Context) {
|
|
var request struct {
|
|
Status int `json:"status" binding:"required"`
|
|
}
|
|
if err := ctx.ShouldBindJSON(&request); err != nil ||
|
|
(request.Status != common.StatusEnable && request.Status != common.StatusDisable) {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
result := impl.DBService.Model(&models.GasBasic{}).
|
|
Where("identity = ? AND status IN ?", ctx.Param("identity"), []int{common.StatusDraft, common.StatusEnable, common.StatusDisable}).
|
|
Update("status", request.Status)
|
|
if result.Error != nil {
|
|
infra.Response.Error(ctx, result.Error)
|
|
return
|
|
}
|
|
if result.RowsAffected != 1 {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
infra.Response.Success(ctx, gin.H{"updated": true})
|
|
}
|