All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 5m37s
- 添加 Device 和 IotCard 模型的 SeriesID 字段 - 实现 DeviceService 和 IotCardService 的套餐系列绑定逻辑 - 添加 DeviceStore 和 IotCardStore 的数据库操作方法 - 更新 API 接口和路由支持套餐系列绑定 - 创建数据库迁移脚本(000027_add_series_binding_fields) - 添加完整的单元测试和集成测试 - 更新 OpenAPI 文档 - 归档 OpenSpec 变更文档 Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
126 lines
3.2 KiB
Go
126 lines
3.2 KiB
Go
package admin
|
|
|
|
import (
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
|
iotCardService "github.com/break/junhong_cmp_fiber/internal/service/iot_card"
|
|
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
|
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
|
"github.com/break/junhong_cmp_fiber/pkg/middleware"
|
|
"github.com/break/junhong_cmp_fiber/pkg/response"
|
|
)
|
|
|
|
type IotCardHandler struct {
|
|
service *iotCardService.Service
|
|
}
|
|
|
|
func NewIotCardHandler(service *iotCardService.Service) *IotCardHandler {
|
|
return &IotCardHandler{service: service}
|
|
}
|
|
|
|
func (h *IotCardHandler) ListStandalone(c *fiber.Ctx) error {
|
|
var req dto.ListStandaloneIotCardRequest
|
|
if err := c.QueryParser(&req); err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
|
}
|
|
|
|
result, err := h.service.ListStandalone(c.UserContext(), &req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.SuccessWithPagination(c, result.List, result.Total, result.Page, result.PageSize)
|
|
}
|
|
|
|
func (h *IotCardHandler) GetByICCID(c *fiber.Ctx) error {
|
|
iccid := c.Params("iccid")
|
|
if iccid == "" {
|
|
return errors.New(errors.CodeInvalidParam, "ICCID不能为空")
|
|
}
|
|
|
|
result, err := h.service.GetByICCID(c.UserContext(), iccid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Success(c, result)
|
|
}
|
|
|
|
func (h *IotCardHandler) AllocateCards(c *fiber.Ctx) error {
|
|
var req dto.AllocateStandaloneCardsRequest
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
|
}
|
|
|
|
ctx := c.UserContext()
|
|
operatorID := middleware.GetUserIDFromContext(ctx)
|
|
userType := middleware.GetUserTypeFromContext(ctx)
|
|
|
|
var operatorShopID *uint
|
|
if userType == constants.UserTypeAgent {
|
|
shopID := middleware.GetShopIDFromContext(ctx)
|
|
if shopID > 0 {
|
|
operatorShopID = &shopID
|
|
}
|
|
}
|
|
|
|
result, err := h.service.AllocateCards(ctx, &req, operatorID, operatorShopID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Success(c, result)
|
|
}
|
|
|
|
func (h *IotCardHandler) RecallCards(c *fiber.Ctx) error {
|
|
var req dto.RecallStandaloneCardsRequest
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
|
}
|
|
|
|
ctx := c.UserContext()
|
|
operatorID := middleware.GetUserIDFromContext(ctx)
|
|
userType := middleware.GetUserTypeFromContext(ctx)
|
|
|
|
var operatorShopID *uint
|
|
if userType == constants.UserTypeAgent {
|
|
shopID := middleware.GetShopIDFromContext(ctx)
|
|
if shopID > 0 {
|
|
operatorShopID = &shopID
|
|
}
|
|
}
|
|
|
|
result, err := h.service.RecallCards(ctx, &req, operatorID, operatorShopID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Success(c, result)
|
|
}
|
|
|
|
func (h *IotCardHandler) BatchSetSeriesBinding(c *fiber.Ctx) error {
|
|
var req dto.BatchSetCardSeriesBindngRequest
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
|
}
|
|
|
|
ctx := c.UserContext()
|
|
userType := middleware.GetUserTypeFromContext(ctx)
|
|
|
|
var operatorShopID *uint
|
|
if userType == constants.UserTypeAgent {
|
|
shopID := middleware.GetShopIDFromContext(ctx)
|
|
if shopID > 0 {
|
|
operatorShopID = &shopID
|
|
}
|
|
}
|
|
|
|
result, err := h.service.BatchSetSeriesBinding(ctx, &req, operatorShopID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Success(c, result)
|
|
}
|