refactor platform logic modules
This commit is contained in:
68
backend/api/internal/logic/platform/fin/fin.go
Normal file
68
backend/api/internal/logic/platform/fin/fin.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package fin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/infra"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func FinSettlementHandlers() (gin.HandlerFunc, gin.HandlerFunc, gin.HandlerFunc, gin.HandlerFunc) {
|
||||
fields := []string{"settlement_no", "subject_type", "subject_id", "period_start", "period_end"}
|
||||
return func(ctx *gin.Context) { common.ListResource(ctx, &models.FinSettlement{}) },
|
||||
func(ctx *gin.Context) {
|
||||
if err := rewriteSettlementSubject(ctx); err != nil {
|
||||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
_, create, _, _ := common.ResourceHandlers(&models.FinSettlement{}, fields, fields)
|
||||
create(ctx)
|
||||
},
|
||||
func(ctx *gin.Context) { common.GetResource(ctx, &models.FinSettlement{}) },
|
||||
func(ctx *gin.Context) {
|
||||
if err := rewriteSettlementSubject(ctx); err != nil {
|
||||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
_, _, _, update := common.ResourceHandlers(&models.FinSettlement{}, fields, fields)
|
||||
update(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func rewriteSettlementSubject(ctx *gin.Context) error {
|
||||
var input map[string]any
|
||||
if err := ctx.ShouldBindJSON(&input); err != nil {
|
||||
return err
|
||||
}
|
||||
subjectType, _ := input["subject_type"].(string)
|
||||
identity, _ := input["subject_identity"].(string)
|
||||
var model any
|
||||
switch subjectType {
|
||||
case "gas", "gas_basic":
|
||||
model = &models.GasBasic{}
|
||||
case "delivery", "delivery_basic":
|
||||
model = &models.DeliveryBasic{}
|
||||
case "staff", "staff_account":
|
||||
model = &models.StaffAccount{}
|
||||
default:
|
||||
return errors.New("invalid settlement subject")
|
||||
}
|
||||
id, err := common.ResolveIdentityID(model, identity, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
delete(input, "subject_identity")
|
||||
input["subject_id"] = id
|
||||
encoded, err := json.Marshal(input)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx.Request.Body = io.NopCloser(bytes.NewReader(encoded))
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user