order.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package redis_data
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "github.com/gomodule/redigo/redis"
  6. "miads/adslib/ads_redis"
  7. "miads/adslib/graylog"
  8. "miads/adslib/utils"
  9. "strings"
  10. )
  11. type AdOrderInfo struct {
  12. BeginTime int64 `json:"begin_time"`
  13. Channel string `json:"channel"`
  14. City string `json:"city"`
  15. ClickKpi int64 `json:"click_kpi"`
  16. ClickURL string `json:"click_url"`
  17. EndTime int64 `json:"end_time"`
  18. ImeiReplaceFlag int64 `json:"imei_replace_flag"`
  19. JsOrderID int64 `json:"js_order_id"`
  20. Level int64 `json:"level"`
  21. LineType int64 `json:"line_type"`
  22. NewRegionFlag string `json:"new_region_flag"`
  23. NoCity string `json:"no_city"`
  24. NoProvince string `json:"no_province"`
  25. OrderID int64 `json:"order_id"`
  26. OrderType int64 `json:"order_type"`
  27. Province string `json:"province"`
  28. RegionFlag int64 `json:"region_flag"`
  29. RegionInfo string `json:"region_info"`
  30. ShowKpi int64 `json:"show_kpi"`
  31. ShowURL string `json:"show_url"`
  32. TargetURL string `json:"target_url"`
  33. Title string `json:"title"`
  34. }
  35. func getOrderIds() ([]string, error) {
  36. conn := ads_redis.RedisConn.Get()
  37. defer conn.Close()
  38. rsp, err := conn.Do("SMEMBERS", "order_key")
  39. if err != nil {
  40. return []string{}, err
  41. }
  42. if rsp == nil {
  43. return []string{}, err
  44. }
  45. orderIds, _ := redis.Strings(rsp, err)
  46. return orderIds, nil
  47. }
  48. func getOrderInfo(id string) (*AdOrderInfo, error) {
  49. conn := ads_redis.RedisConn.Get()
  50. defer conn.Close()
  51. rsp, err := conn.Do("GET", id)
  52. if err != nil {
  53. return nil, err
  54. }
  55. if rsp == nil {
  56. return nil, err
  57. }
  58. orderBase64Str, _ := redis.String(rsp, err)
  59. orderByte, err := base64.StdEncoding.DecodeString(orderBase64Str)
  60. if err != nil {
  61. return nil, err
  62. }
  63. a := AdOrderInfo{}
  64. err = json.Unmarshal(orderByte, &a)
  65. if err != nil {
  66. return nil, err
  67. }
  68. return &a, nil
  69. }
  70. func GetOrderInfos(dsp *utils.DspParam, fixFlag int) ([]AdOrderInfo, error) {
  71. orderIds, err := getOrderIds()
  72. if err != nil {
  73. return []AdOrderInfo{}, err
  74. }
  75. orders := make([]AdOrderInfo, 0, 50)
  76. for _, id := range orderIds {
  77. order, err := getOrderInfo(id)
  78. if err != nil {
  79. continue
  80. }
  81. if order == nil {
  82. continue
  83. }
  84. needInsert := true
  85. areaFlag := 0 // 0 通投 1 定投
  86. if dsp != nil {
  87. // 判断是否要做渠道剔除
  88. channelStr := order.Channel
  89. if channelStr != "" {
  90. channelArr := strings.Split(channelStr, ",")
  91. isValidChannel := false
  92. for _, channel := range channelArr {
  93. if dsp.ReqSource == channel {
  94. isValidChannel = true
  95. }
  96. }
  97. if !isValidChannel {
  98. continue
  99. }
  100. }
  101. newRegionFlagStr := order.NewRegionFlag
  102. newRegionFlagArr := strings.Split(newRegionFlagStr, ",")
  103. for _, regionFlag := range newRegionFlagArr {
  104. if regionFlag == "1" {
  105. // 对省定投
  106. if strings.Index(order.Province, dsp.Province) == -1 {
  107. needInsert = false
  108. break
  109. }
  110. } else if regionFlag == "2" {
  111. // 对市定投
  112. if strings.Index(order.City, dsp.City) == -1 {
  113. needInsert = false
  114. break
  115. }
  116. } else if regionFlag == "3" {
  117. // 对省禁投
  118. if order.NoProvince != "" && strings.Index(order.NoProvince, dsp.Province) != -1 {
  119. needInsert = false
  120. break
  121. }
  122. } else if regionFlag == "4" {
  123. // 对市禁投
  124. if order.NoCity != "" && strings.Index(order.NoCity, dsp.City) != -1 {
  125. needInsert = false
  126. break
  127. }
  128. } else {
  129. continue
  130. }
  131. areaFlag = 1
  132. }
  133. }
  134. if !needInsert {
  135. return []AdOrderInfo{}, nil
  136. }
  137. if dsp != nil {
  138. grayLogData := struct {
  139. Ip string
  140. ShortMessage string `json:"short_message"`
  141. ReqSource string `json:"req_source"`
  142. Province string
  143. City string
  144. OrderId int64 `json:"order_id"`
  145. orderProvince string `json:"order_province"`
  146. orderCity string `json:"order_city"`
  147. orderNoProvince string `json:"order_no_province"`
  148. orderNoCity string `json:"order_no_city"`
  149. }{
  150. Ip: dsp.Ip,
  151. ShortMessage: "area_result_v2",
  152. ReqSource: dsp.ReqSource,
  153. Province: dsp.Province,
  154. City: dsp.City,
  155. OrderId: order.OrderID,
  156. orderProvince: order.Province,
  157. orderCity: order.City,
  158. orderNoProvince: order.NoProvince,
  159. orderNoCity: order.NoCity,
  160. }
  161. graylog.Log(grayLogData)
  162. }
  163. if fixFlag == 0 && areaFlag == 0 {
  164. orders = append(orders, *order)
  165. } else if fixFlag == 1 && areaFlag == 1 {
  166. orders = append(orders, *order)
  167. } else if fixFlag == -1 {
  168. orders = append(orders, *order)
  169. }
  170. }
  171. return orders, nil
  172. }