util.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package model
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "go-common/app/service/openplatform/ticket-item/conf"
  6. "go-common/library/log"
  7. "io/ioutil"
  8. "net/http"
  9. "net/url"
  10. "reflect"
  11. "strconv"
  12. "strings"
  13. )
  14. // UT Data
  15. var (
  16. DataID = int64(75)
  17. DataIDs = []int64{75, 80}
  18. DataSIDs = []int64{90, 133, 136}
  19. DataTIDs = []int64{1179, 1180, 1368, 1360}
  20. NoDataID = int64(100000000)
  21. NoDataIDs = []int64{100000000, 100000001}
  22. NoDataSIDs = []int64{100000000, 100000001}
  23. NoDataTIDs = []int64{100000000, 100000001}
  24. )
  25. // JSONEncode 仿phpJSONEncode
  26. func JSONEncode(v interface{}) string {
  27. b, err := json.Marshal(v)
  28. if err != nil {
  29. log.Error("JSONEncode error(%v)", err)
  30. return ""
  31. }
  32. return string(b)
  33. }
  34. // String2Int64 convert string slice([]string) to int64 slice([]int64)
  35. func String2Int64(arr []string) (r []int64) {
  36. var (
  37. id int64
  38. err error
  39. )
  40. for _, v := range arr {
  41. if id, err = strconv.ParseInt(v, 10, 64); err != nil {
  42. continue
  43. }
  44. r = append(r, id)
  45. }
  46. return
  47. }
  48. // UniqueInt64 Ints returns a unique subset of the int slice provided.
  49. func UniqueInt64(input []int64) []int64 {
  50. u := make([]int64, 0, len(input))
  51. m := make(map[int64]bool)
  52. for _, val := range input {
  53. if _, ok := m[val]; !ok {
  54. m[val] = true
  55. u = append(u, val)
  56. }
  57. }
  58. return u
  59. }
  60. // Min 获取数组中最小值
  61. func Min(vars []int32) (minVar int32) {
  62. if vars != nil {
  63. minVar = vars[0]
  64. for _, v := range vars {
  65. if v < minVar {
  66. minVar = v
  67. }
  68. }
  69. }
  70. return
  71. }
  72. // Max 获取数组中最大值
  73. func Max(vars []int32) (maxVar int32) {
  74. for _, v := range vars {
  75. if v > maxVar {
  76. maxVar = v
  77. }
  78. }
  79. return
  80. }
  81. // GetTicketIDFromBase baseCenter获取票价id
  82. func GetTicketIDFromBase() (int64, error) {
  83. params := url.Values{}
  84. params.Add("count", "1")
  85. params.Add("biz_tag", "price")
  86. params.Add("app_id", conf.Conf.BASECenter.AppID)
  87. params.Add("app_token", conf.Conf.BASECenter.AppToken)
  88. reqParam := params.Encode()
  89. resp, err := http.Get(fmt.Sprintf(conf.Conf.BASECenter.URL+"orderid/get?%s", reqParam))
  90. if err != nil {
  91. log.Error("获取票价id HTTP REQUEST失败")
  92. return 0, err
  93. }
  94. defer resp.Body.Close()
  95. content, err := ioutil.ReadAll(resp.Body)
  96. if err != nil {
  97. log.Error("读取HTTP RESPONSE失败")
  98. return 0, err
  99. }
  100. var result Response
  101. if err := json.Unmarshal(content, &result); err != nil {
  102. log.Error("json解析失败")
  103. }
  104. return result.Data[0], nil
  105. }
  106. var alphabetTable []string
  107. // AlphabetTable 获取票价所需symbol的字母表
  108. func AlphabetTable() []string {
  109. if alphabetTable != nil {
  110. return alphabetTable
  111. }
  112. result := make([]string, 52)
  113. var i int
  114. ch := 97
  115. for i = 0; i < 26; i++ {
  116. result[i] = string(ch + i)
  117. }
  118. ch = 65
  119. j := i
  120. for i = 0; i < 26; i++ {
  121. result[j] = string(ch + i)
  122. j++
  123. }
  124. alphabetTable = result
  125. return alphabetTable
  126. }
  127. // ClassifyIDs 获取已经存在和需要被删除的id list
  128. func ClassifyIDs(oldIDs []int64, newIDs []int64) (needDel []int64, existed []int64) {
  129. newIDsMap := make(map[int64]int64)
  130. for _, newID := range newIDs {
  131. newIDsMap[newID] = newID
  132. }
  133. for _, oldID := range oldIDs {
  134. if oldID == 0 {
  135. continue
  136. }
  137. if _, ok := newIDsMap[oldID]; !ok {
  138. needDel = append(needDel, oldID)
  139. } else {
  140. existed = append(existed, oldID)
  141. }
  142. }
  143. return
  144. }
  145. // Implode 仅支持不同类型的数组
  146. func Implode(glue string, list interface{}) string {
  147. listValue := reflect.Indirect(reflect.ValueOf(list))
  148. if listValue.Kind() != reflect.Slice {
  149. // 数组以外类型返回空字符串
  150. return ""
  151. }
  152. count := listValue.Len()
  153. listStr := make([]string, 0, count)
  154. for i := 0; i < count; i++ {
  155. str := fmt.Sprint(listValue.Index(i).Interface())
  156. listStr = append(listStr, str)
  157. }
  158. return strings.Join(listStr, glue)
  159. }