feat: implement gas and delivery admin systems
This commit is contained in:
136
backend/api/internal/logic/gas/delivery.go
Normal file
136
backend/api/internal/logic/gas/delivery.go
Normal file
@@ -0,0 +1,136 @@
|
||||
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 ListDelivery(ctx *gin.Context) {
|
||||
station, ok := currentGas(ctx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
page, size := common.PageSize(ctx)
|
||||
var list []models.DeliveryBasic
|
||||
var total int64
|
||||
query := common.ApplyKeywordFilter(ctx,
|
||||
common.ActiveRecords(impl.DBService.Model(&models.DeliveryBasic{})).Where("gas_basic_id = ?", station.ID),
|
||||
&models.DeliveryBasic{})
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
if err := query.Order("created_at desc").Offset((page - 1) * size).Limit(size).Find(&list).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
respondList(ctx, list, total)
|
||||
}
|
||||
|
||||
func GetDelivery(ctx *gin.Context) {
|
||||
station, ok := currentGas(ctx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
respondScopedRecord(ctx, common.ActiveRecords(impl.DBService).
|
||||
Where("identity = ? AND gas_basic_id = ?", ctx.Param("identity"), station.ID), &models.DeliveryBasic{})
|
||||
}
|
||||
|
||||
func CreateDelivery(ctx *gin.Context) {
|
||||
station, ok := currentGas(ctx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var request struct {
|
||||
DeliveryCode string `json:"delivery_code" binding:"required,max=32"`
|
||||
Name string `json:"name" binding:"required,max=128"`
|
||||
Principal string `json:"principal" binding:"max=64"`
|
||||
Address string `json:"address" binding:"max=255"`
|
||||
}
|
||||
if err := ctx.ShouldBindJSON(&request); err != nil {
|
||||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
delivery := models.DeliveryBasic{
|
||||
Entity: common.NewEntity(common.StatusEnable), DeliveryCode: request.DeliveryCode, GasBasicID: station.ID,
|
||||
Name: request.Name, Principal: request.Principal, Address: request.Address,
|
||||
}
|
||||
if err := impl.DBService.Create(&delivery).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
common.RespondCreatedResource(ctx, delivery)
|
||||
}
|
||||
|
||||
func UpdateDelivery(ctx *gin.Context) {
|
||||
station, ok := currentGas(ctx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if _, ok := requireDelivery(ctx, ctx.Param("identity"), station.ID); !ok {
|
||||
return
|
||||
}
|
||||
var request struct {
|
||||
Name string `json:"name" binding:"required,max=128"`
|
||||
Principal string `json:"principal" binding:"max=64"`
|
||||
Address string `json:"address" binding:"max=255"`
|
||||
}
|
||||
if err := ctx.ShouldBindJSON(&request); err != nil {
|
||||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
common.UpdateAllowedByIdentity(ctx, &models.DeliveryBasic{},
|
||||
gin.H{"name": request.Name, "principal": request.Principal, "address": request.Address},
|
||||
[]string{"name", "principal", "address"})
|
||||
}
|
||||
|
||||
func UpdateDeliveryStatus(ctx *gin.Context) {
|
||||
station, ok := currentGas(ctx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if _, ok := requireDelivery(ctx, ctx.Param("identity"), station.ID); !ok {
|
||||
return
|
||||
}
|
||||
common.UpdateRecordStatus(ctx, &models.DeliveryBasic{})
|
||||
}
|
||||
|
||||
func ArchiveDelivery(ctx *gin.Context) {
|
||||
station, ok := currentGas(ctx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
delivery, ok := requireDelivery(ctx, ctx.Param("identity"), station.ID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var blocking int64
|
||||
if err := impl.DBService.Model(&models.GasorderBasic{}).
|
||||
Where("delivery_basic_id = ? AND order_status NOT IN ?", delivery.ID, []int{common.StatusCompleted, common.StatusCancelled}).
|
||||
Count(&blocking).Error; err != nil || blocking > 0 {
|
||||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
if err := impl.DBService.Model(&models.UserServiceRelation{}).
|
||||
Where("delivery_basic_id = ? AND status <> ?", delivery.ID, common.StatusArchived).Count(&blocking).Error; err != nil || blocking > 0 {
|
||||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
if err := impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Model(&delivery).Update("status", common.StatusArchived).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Model(&models.DeliveryAccount{}).
|
||||
Where("delivery_basic_id = ? AND status <> ?", delivery.ID, common.StatusArchived).
|
||||
Update("status", common.StatusArchived).Error
|
||||
}); err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(ctx, gin.H{"archived": true})
|
||||
}
|
||||
Reference in New Issue
Block a user