商品rpc数据格式及调用方法
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
4.2 KiB

  1. package productrpc
  2. import (
  3. "encoding/json"
  4. "log"
  5. "sort"
  6. "strconv"
  7. // "strconv"
  8. // "time"
  9. )
  10. /**
  11. * 获取商品进行中活动
  12. * 2021/10/06
  13. * GZ
  14. */
  15. func GetActivity(site_id, dbname, id string, sku_id string, url ...string) ([]map[string]string, error) {
  16. conn, err := rpc_server_conn(url...)
  17. if err != nil {
  18. return nil, err
  19. }
  20. defer conn.Close()
  21. arg := GetActivityParam{site_id, dbname, id, sku_id}
  22. req, err := SetReqData(arg)
  23. if err != nil {
  24. return nil, err
  25. }
  26. res := &Response{}
  27. err = conn.GetActivity(req, res)
  28. if err != nil {
  29. return nil, err
  30. }
  31. res_data_de, err := GetResData(res)
  32. if err != nil {
  33. return nil, err
  34. }
  35. if res_data_de == "" {
  36. return nil, nil
  37. }
  38. var res_arr []map[string]string
  39. err = json.Unmarshal([]byte(res_data_de), &res_arr)
  40. if err != nil {
  41. return nil, err
  42. }
  43. var activitys Activitys
  44. if len(res_arr) > 0 {
  45. var start int
  46. for _, item := range res_arr {
  47. start, err = strconv.Atoi(item["starttime"])
  48. if err != nil {
  49. log.Println(err)
  50. }
  51. activitys = append(activitys, Activity{
  52. Id: item["id"],
  53. Start: start,
  54. })
  55. }
  56. }
  57. sort.Sort(activitys)
  58. var list []map[string]string = make([]map[string]string, 0)
  59. if len(activitys) > 0 {
  60. for _, v := range activitys {
  61. for _, old := range res_arr {
  62. if v.Id == old["id"] {
  63. list = append(list, old)
  64. break
  65. }
  66. }
  67. }
  68. }
  69. return list, nil
  70. }
  71. /**
  72. * 获取商品预热中活动
  73. * 2021/10/06
  74. * GZ
  75. */
  76. func GetWarmingActivity(site_id, dbname, id string, sku_id string, url ...string) ([]map[string]string, error) {
  77. conn, err := rpc_server_conn(url...)
  78. if err != nil {
  79. return nil, err
  80. }
  81. defer conn.Close()
  82. arg := GetActivityParam{site_id, dbname, id, sku_id}
  83. req, err := SetReqData(arg)
  84. if err != nil {
  85. return nil, err
  86. }
  87. res := &Response{}
  88. err = conn.GetWarmingActivity(req, res)
  89. if err != nil {
  90. return nil, err
  91. }
  92. res_data_de, err := GetResData(res)
  93. if err != nil {
  94. return nil, err
  95. }
  96. if res_data_de == "" {
  97. return nil, nil
  98. }
  99. var res_arr []map[string]string
  100. err = json.Unmarshal([]byte(res_data_de), &res_arr)
  101. if err != nil {
  102. return nil, err
  103. }
  104. var activitys Activitys
  105. if len(res_arr) > 0 {
  106. var start int
  107. for _, item := range res_arr {
  108. start, err = strconv.Atoi(item["starttime"])
  109. if err != nil {
  110. log.Println(err)
  111. }
  112. activitys = append(activitys, Activity{
  113. Id: item["id"],
  114. Start: start,
  115. })
  116. }
  117. }
  118. sort.Sort(activitys)
  119. var list []map[string]string = make([]map[string]string, 0)
  120. if len(activitys) > 0 {
  121. for _, v := range activitys {
  122. for _, old := range res_arr {
  123. if v.Id == old["id"] {
  124. list = append(list, old)
  125. break
  126. }
  127. }
  128. }
  129. }
  130. return list, nil
  131. }
  132. /**
  133. * 获取商品进行中活动
  134. * 2021/10/06
  135. * GZ
  136. */
  137. func GetAllActivity(site_id, dbname, id string, sku_id string, url ...string) ([]map[string]string, error) {
  138. conn, err := rpc_server_conn(url...)
  139. if err != nil {
  140. return nil, err
  141. }
  142. defer conn.Close()
  143. arg := GetActivityParam{site_id, dbname, id, sku_id}
  144. req, err := SetReqData(arg)
  145. if err != nil {
  146. return nil, err
  147. }
  148. res := &Response{}
  149. err = conn.GetAllActivity(req, res)
  150. if err != nil {
  151. return nil, err
  152. }
  153. res_data_de, err := GetResData(res)
  154. if err != nil {
  155. return nil, err
  156. }
  157. if res_data_de == "" {
  158. return nil, nil
  159. }
  160. var res_arr []map[string]string
  161. err = json.Unmarshal([]byte(res_data_de), &res_arr)
  162. if err != nil {
  163. return nil, err
  164. }
  165. var activitys Activitys
  166. if len(res_arr) > 0 {
  167. var start int
  168. for _, item := range res_arr {
  169. start, err = strconv.Atoi(item["starttime"])
  170. if err != nil {
  171. log.Println(err)
  172. }
  173. activitys = append(activitys, Activity{
  174. Id: item["id"],
  175. Start: start,
  176. })
  177. }
  178. }
  179. sort.Sort(activitys)
  180. var list []map[string]string = make([]map[string]string, 0)
  181. if len(activitys) > 0 {
  182. for _, v := range activitys {
  183. for _, old := range res_arr {
  184. if v.Id == old["id"] {
  185. list = append(list, old)
  186. break
  187. }
  188. }
  189. }
  190. }
  191. return list, nil
  192. }
  193. type Activity struct {
  194. Id string
  195. Start int
  196. }
  197. type Activitys []Activity
  198. func (s Activitys) Len() int {
  199. return len(s)
  200. }
  201. func (s Activitys) Swap(i, j int) {
  202. s[i], s[j] = s[j], s[i]
  203. }
  204. func (s Activitys) Less(i, j int) bool {
  205. return s[i].Start < s[j].Start
  206. }