123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package admin
- import (
- "context"
- v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
- "github.com/go-nunu/nunu-layout-advanced/api/v1/admin"
- "github.com/go-nunu/nunu-layout-advanced/internal/model"
- "github.com/go-nunu/nunu-layout-advanced/internal/repository"
- "math"
- "strings"
- )
- type GatewayIpAdminRepository interface {
- GetGatewayIpAdmin(ctx context.Context, id int64) (*model.Gatewayip, error)
- GetGatewayGroupIpList(ctx context.Context,req admin.SearchGatewayIpParams) (*v1.PaginatedResponse[model.Gatewayip], error)
- AddGatewayIp(ctx context.Context,req model.Gatewayip) error
- EditGatewayIp(ctx context.Context,req model.Gatewayip) error
- DeleteGatewayIp(ctx context.Context,id int64) error
- DeleteGatewayIps(ctx context.Context, ids []int64) error
- }
- func NewGatewayIpAdminRepository(
- repository *repository.Repository,
- ) GatewayIpAdminRepository {
- return &gatewayIpAdminRepository{
- Repository: repository,
- }
- }
- type gatewayIpAdminRepository struct {
- *repository.Repository
- }
- func (r *gatewayIpAdminRepository) GetGatewayIpAdmin(ctx context.Context, id int64) (*model.Gatewayip, error) {
- var req model.Gatewayip
- return &req, r.DB(ctx).Model(&model.Gatewayip{}).Where("id = ?", id).First(&req).Error
- }
- func (r *gatewayIpAdminRepository) GetGatewayGroupIpList(ctx context.Context,req admin.SearchGatewayIpParams) (*v1.PaginatedResponse[model.Gatewayip], error) {
- var res []model.Gatewayip
- var total int64
- query := r.Db.WithContext(ctx).Model(&model.Gatewayip{})
- if req.Ip != "" {
- trimmedName := strings.TrimSpace(req.Ip)
- // 使用 LIKE 进行模糊匹配
- query = query.Where("ip LIKE CONCAT('%', ?, '%')", trimmedName)
- }
- if req.HostId != 0 {
- query = query.Where("host_id = ?", req.HostId)
- }
- if req.Name != "" {
- trimmedName := strings.TrimSpace(req.Name)
- // 使用 LIKE 进行模糊匹配
- query = query.Where("name LIKE CONCAT('%', ?, '%')", trimmedName)
- }
- if req.NodeArea != "" {
- trimmedName := strings.TrimSpace(req.NodeArea)
- // 使用 LIKE 进行模糊匹配
- query = query.Where("node_area LIKE CONCAT('%', ?, '%')", trimmedName)
- }
- if req.Comment != "" {
- trimmedName := strings.TrimSpace(req.Comment)
- // 使用 LIKE 进行模糊匹配
- query = query.Where("comment LIKE CONCAT('%', ?, '%')", trimmedName)
- }
- if req.Column != "" {
- if req.Column == "createTime" {
- query = query.Order("created_at" + " " + req.Order)
- }
- }
- if err := query.Count(&total).Error; err != nil {
- // 如果连计数都失败了,直接返回错误
- return nil, err
- }
- page := req.Current
- pageSize := req.PageSize
- if page <= 0 {
- page = 1
- }
- if pageSize <= 0 {
- pageSize = 10 // 默认每页 10 条
- } else if pageSize > 100 {
- pageSize = 100 // 每页最多 100 条
- }
- // 计算 offset (偏移量)
- // 例如,第 1 页,offset = (1-1)*10 = 0 (从第0条开始)
- // 第 2 页,offset = (2-1)*10 = 10 (从第10条开始)
- offset := (page - 1) * pageSize
- // 3. 执行最终的查询
- // 在所有条件都添加完毕后,再执行 .Find()
- result := query.Offset(offset).Limit(pageSize).Find(&res)
- if result.Error != nil {
- // 这里的错误可能是数据库连接问题等,而不是“未找到记录”
- return nil, result.Error
- }
- return &v1.PaginatedResponse[model.Gatewayip]{
- Records: res,
- Page: page,
- PageSize: pageSize,
- Total: total,
- TotalPages: int(math.Ceil(float64(total) / float64(pageSize))),
- }, nil
- }
- func (r *gatewayIpAdminRepository) AddGatewayIp(ctx context.Context,req model.Gatewayip) error {
- return r.DB(ctx).Create(&req).Error
- }
- func (r *gatewayIpAdminRepository) EditGatewayIp(ctx context.Context,req model.Gatewayip) error {
- return r.DB(ctx).Updates(&req).Error
- }
- func (r *gatewayIpAdminRepository) DeleteGatewayIp(ctx context.Context,id int64) error {
- return r.DB(ctx).Model(&model.Gatewayip{}).Where("id = ?", id).Delete(id).Error
- }
- func (r *gatewayIpAdminRepository) DeleteGatewayIps(ctx context.Context, ids []int64) error {
- return r.DB(ctx).Model(&model.Gatewayip{}).Where("id in ?", ids).Delete(ids).Error
- }
|