iot_server/internal/dtos/common.go

110 lines
3.2 KiB
Go
Raw Permalink Normal View History

2023-08-28 06:49:44 +00:00
/*******************************************************************************
* Copyright 2017 Dell Inc.
* Copyright (c) 2019 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*******************************************************************************/
package dtos
import "strings"
type PageRequest struct {
NameLike string `json:"nameLike" form:"nameLike"`
Page int `json:"page" form:"page"`
PageSize int `json:"pageSize" form:"pageSize"`
}
//func (p *PageRequest) ToRpc() *common.PageRequest {
// return &common.PageRequest{
// NameLike: p.NameLike,
// Page: int64(p.Page),
// PageSize: int64(p.PageSize),
// }
//}
type BaseSearchConditionQuery struct {
Page int `schema:"page,omitempty" form:"page"`
PageSize int `schema:"pageSize,omitempty" form:"pageSize" json:"pageSize"`
Id string `schema:"id,omitempty" form:"id"`
Ids string `schema:"ids,omitempty" form:"ids"`
LikeId string `schema:"likeId,omitempty" form:"likeId"`
Name string `schema:"name,omitempty" form:"name"`
NameLike string `schema:"nameLike,omitempty" form:"nameLike"`
IsAll bool `schema:"isAll,omitempty" form:"isAll"`
OrderBy string `schema:"orderBy,omitempty" form:"orderBy"`
}
func (req BaseSearchConditionQuery) GetPage() (int, int) {
var (
offset = (req.Page - 1) * req.PageSize
limit = req.PageSize
)
if req.Page == 0 && req.PageSize == 0 {
offset = 0
limit = -1
}
if req.IsAll {
offset = 0
limit = -1
}
return offset, limit
}
func ApiParamsStringToArray(str string) []string {
return strings.Split(str, ",")
}
/**
/order bykeymodels
models.Device.ProductName key ProductName product_name pn
*/
type ApiOrderBy struct {
Key string
IsDesc bool
}
func ApiParamsStringToOrderBy(str string) []ApiOrderBy {
orderBys := make([]ApiOrderBy, 0)
arr := strings.Split(str, ",")
if len(arr) <= 0 {
return nil
}
for _, v := range arr {
vArr := strings.Split(v, ":")
if len(vArr) <= 1 {
continue
}
switch vArr[1] {
case "desc":
orderBys = append(orderBys, ApiOrderBy{
Key: vArr[0],
IsDesc: true,
})
case "asc":
orderBys = append(orderBys, ApiOrderBy{
Key: vArr[0],
IsDesc: false,
})
default:
continue
}
}
return orderBys
}
func ApiParamsArrayToString(arr []string) string {
return strings.Join(arr, ",")
}