refactor: 对齐后端样例工程规范
This commit is contained in:
60
backend/api/internal/models/org_gas_station.go
Normal file
60
backend/api/internal/models/org_gas_station.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// OrgGasStation 对应 org_gas_station,表示可燃气体站经营主体。
|
||||
type OrgGasStation struct {
|
||||
Entity
|
||||
StationCode string `gorm:"column:station_code;type:varchar(32);uniqueIndex;not null" json:"station_code"` // 气站编码
|
||||
Name string `gorm:"column:name;type:varchar(128);not null" json:"name"` // 气站名称
|
||||
Principal string `gorm:"column:principal;type:varchar(64);not null" json:"principal"` // 负责人
|
||||
ServiceArea string `gorm:"column:service_area;type:varchar(128);not null" json:"service_area"` // 服务区域
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.AppendMigrate(&OrgGasStation{})
|
||||
}
|
||||
|
||||
// TableName 返回与模型、文件名一致的单数数据表名。
|
||||
func (table *OrgGasStation) TableName() string { return "org_gas_station" }
|
||||
|
||||
// CreateOrgGasStation 创建待审核气站。
|
||||
func CreateOrgGasStation(data *OrgGasStation) error {
|
||||
if err := impl.DBService.Create(data).Error; err != nil {
|
||||
return errcode.ErrDB
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListOrgGasStation 按创建时间倒序查询气站。
|
||||
func ListOrgGasStation(page, size int) ([]OrgGasStation, int64, error) {
|
||||
var list []OrgGasStation
|
||||
var total int64
|
||||
databaseQuery := impl.DBService.Model(&OrgGasStation{})
|
||||
if err := databaseQuery.Count(&total).Error; err != nil {
|
||||
return nil, 0, errcode.ErrDB
|
||||
}
|
||||
if err := databaseQuery.Order("created_at desc").Offset((page - 1) * size).Limit(size).Find(&list).Error; err != nil {
|
||||
return nil, 0, errcode.ErrDB
|
||||
}
|
||||
return list, total, nil
|
||||
}
|
||||
|
||||
// GetOrgGasStationByIdentity 查询单个气站。
|
||||
func GetOrgGasStationByIdentity(identity string) (*OrgGasStation, error) {
|
||||
var data OrgGasStation
|
||||
if err := impl.DBService.Where("identity = ?", identity).First(&data).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return &data, nil
|
||||
}
|
||||
Reference in New Issue
Block a user