|
|
- package helper
-
- import (
- "errors"
- "net/url"
- "sort"
- "strings"
- )
-
- func MergeStringMap(x, y map[string]string) map[string]string {
-
- n := make(map[string]string)
- for i, v := range x {
- for j, w := range y {
- if i == j {
- n[i] = w
-
- } else {
- if _, ok := n[i]; !ok {
- n[i] = v
- }
- if _, ok := n[j]; !ok {
- n[j] = w
- }
- }
- }
- }
-
- return n
-
- }
-
- /**
- * 转换为map string数组
- * 2021/1/5
- * gz
- */
- func ToMapStringArray(x interface{}) ([]map[string]string, error) {
- list, ok := x.([]interface{})
- if !ok {
- return []map[string]string{}, errors.New("type error")
- }
-
- if len(list) < 1 {
- return []map[string]string{}, nil
- }
- var ret []map[string]string = make([]map[string]string, len(list))
- var tmp map[string]interface{}
- var map_tmp map[string]string
- var err error
-
- for key, item := range list {
- if tmp, ok = item.(map[string]interface{}); ok {
- map_tmp = make(map[string]string)
- for k, v := range tmp {
- map_tmp[k] = ToStr(v)
- }
- ret[key] = map_tmp
- } else {
- err = errors.New("data type error")
- break
- }
- }
-
- return ret, err
- }
-
- /**
- * 将map[string]string数组转成map[string]interface数组
- */
- func MapStringToInterfaceArr(src []map[string]string) []map[string]interface{} {
- var dest []map[string]interface{}
-
- for i := range src {
- m := src[i]
- n := make(map[string]interface{})
- for k, v := range m {
- n[k] = interface{}(v)
- }
- dest = append(dest, n)
- }
-
- return dest
- }
-
- /**
- * 将map[string]string转成map[string]interface
- */
- func MapStringToInterface(src map[string]string) map[string]interface{} {
- target := make(map[string]interface{})
- for k, v := range src {
- target[k] = interface{}(v)
- }
-
- return target
- }
-
- //将interface转成map[string]interface
- func InterfaceToMapInterface(data interface{}) (map[string]interface{}, error) {
- dataJson, err := json.Marshal(data)
-
- if err != nil {
- return nil, err
- }
-
- var returnData map[string]interface{}
- err = json.Unmarshal(dataJson, &returnData)
-
- return returnData, nil
- }
-
- //将interface转成map[string]string
- func InterfaceToMapString(data interface{}) (map[string]string, error) {
- dataJson, err := json.Marshal(data)
-
- if err != nil {
- return nil, err
- }
-
- var returnData map[string]string
- err = json.Unmarshal(dataJson, &returnData)
-
- return returnData, nil
- }
-
- //将interface转成[]map[string]interface
- func InterfaceToMapInterfaceArr(data interface{}) ([]map[string]interface{}, error) {
- dataJson, err := json.Marshal(data)
-
- if err != nil {
- return nil, err
- }
-
- var returnData []map[string]interface{}
- err = json.Unmarshal(dataJson, &returnData)
-
- return returnData, nil
- }
-
- //将interface转成[]map[string]string
- func InterfaceToMapStringArr(data interface{}) ([]map[string]string, error) {
- dataJson, err := json.Marshal(data)
-
- if err != nil {
- return nil, err
- }
-
- var returnData []map[string]string
- err = json.Unmarshal(dataJson, &returnData)
-
- return returnData, nil
- }
-
- /**
- * 对map进行字典序排序
- * @param data map[string]string
- * @2020/12/28
- * @linsen
- */
- func HttpBuildQuery(data map[string]string) string {
-
- keySlice := []string{}
-
- for k, _ := range data {
- keySlice = append(keySlice, k)
- }
- sort.Strings(keySlice) //排序字符串
-
- var query string
-
- for _, value := range keySlice {
- if value != "" && data[value] != "" {
- query += StringJoin(value, "=", data[value], "&")
- }
- }
-
- return strings.Trim(query, "&")
- }
-
- /**
- * 对map字典按照顺序进行拼接,并且把每个value进行类urlencode
- * @param data map[string]string
- * @2021/10/14
- * @wyy
- */
- func HttpBuildStringQuery(data map[string]string) string {
-
- keySlice := []string{}
-
- for k, _ := range data {
- keySlice = append(keySlice, k)
- }
- // sort.Strings(keySlice) //排序字符串
-
- var query string
-
- for _, value := range keySlice {
- if value != "" && data[value] != "" {
- query += StringJoin(value, "=", url.QueryEscape(data[value]), "&")
- }
- }
-
- return strings.Trim(query, "&")
- }
-
- /**
- * 对map切片 按值进行顺序
- * @param order desc/asc val_type float64/string MapDate []map[string]interface{}
- * @2022/10/27
- * @lo
- */
- func MapSort(order, val, val_type string, MapDate []map[string]interface{}) ([]map[string]interface{}, error) {
- type KVPair struct {
- Key int
- Val_float64 float64
- val_string string
- }
- res := []map[string]interface{}{}
- if order != "desc" && order != "asc" {
- return res, errors.New("order parameter err")
- }
- if val_type != "float64" && val_type != "string" {
- return res, errors.New("val_type parameter err")
- }
- if val == "" {
- return res, errors.New("val parameter err")
- }
- if len(MapDate) < 1 {
- return res, errors.New("MapDate parameter err")
- }
- tmpList := []KVPair{}
- if val_type == "string" {
- for k, item := range MapDate {
- tmpList = append(tmpList, KVPair{Key: k, val_string: ToStr(item[val])})
- }
- if order == "desc" {
- sort.Slice(tmpList, func(i, j int) bool {
- return tmpList[i].val_string > tmpList[j].val_string // 降序
- })
- } else {
- sort.Slice(tmpList, func(i, j int) bool {
- return tmpList[i].val_string < tmpList[j].val_string // 升序
- })
- }
- } else {
- for k, item := range MapDate {
- float64_val, _ := ToFloat64(item[val])
- tmpList = append(tmpList, KVPair{Key: k, Val_float64: float64_val})
- }
- if order == "desc" {
- sort.Slice(tmpList, func(i, j int) bool {
- return tmpList[i].Val_float64 > tmpList[j].Val_float64 // 降序
- })
- } else {
- sort.Slice(tmpList, func(i, j int) bool {
- return tmpList[i].Val_float64 < tmpList[j].Val_float64 // 升序
- })
- }
- }
- for _, tmp := range tmpList {
- res = append(res, MapDate[tmp.Key])
- }
- return res, nil
- }
- func MapPage(pageNum, pageSize int, MapDate []map[string]interface{}) []map[string]interface{} {
- stat := (pageNum - 1) * pageSize
- end := stat + pageSize
- count := len(MapDate)
- if stat >= count {
- return []map[string]interface{}{}
- }
- if end >= count {
- end = count
- }
- if stat < 0 || end < 0 {
- return []map[string]interface{}{}
- }
- return MapDate[stat:end]
- }
|