order.go 4.8 KB

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