HonorPlacement.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.adx.tencent.honor;
  2. import java.util.List;
  3. import java.util.Map;
  4. public class HonorPlacement {
  5. private long baiduMediaId;
  6. private String baiduAppId;
  7. private String baiduTagId;
  8. private Map<String, HonorAppPlacement> platforms;
  9. private long bidFloor;
  10. private List<Integer> actionTypes;
  11. public long getBaiduMediaId() { return baiduMediaId; }
  12. public void setBaiduMediaId(long v) { this.baiduMediaId = v; }
  13. public String getBaiduAppId() { return baiduAppId; }
  14. public void setBaiduAppId(String v) { this.baiduAppId = v; }
  15. public String getBaiduTagId() { return baiduTagId; }
  16. public void setBaiduTagId(String v) { this.baiduTagId = v; }
  17. public Map<String, HonorAppPlacement> getPlatforms() { return platforms; }
  18. public void setPlatforms(Map<String, HonorAppPlacement> v) { this.platforms = v; }
  19. public long getBidFloor() { return bidFloor; }
  20. public void setBidFloor(long v) { this.bidFloor = v; }
  21. public List<Integer> getActionTypes() { return actionTypes; }
  22. public void setActionTypes(List<Integer> v) { this.actionTypes = v; }
  23. public Resolved resolve(Map<String, String> params) {
  24. String tagId = firstParam(params, "tagId", "tag_id", "baidu_tag_id");
  25. if (tagId != null && !tagId.isBlank()) {
  26. Resolved r = resolveByTagId(tagId);
  27. if (r != null) return r;
  28. throw new IllegalArgumentException("unknown honor baidu tag id: " + tagId);
  29. }
  30. String platform = normalizePlatform(firstParam(params, "platform", "os", "system", "device_os"));
  31. if (platform != null && !platform.isBlank()) {
  32. Resolved r = resolveByPlatform(platform);
  33. if (r != null) return r;
  34. throw new IllegalArgumentException("unsupported honor baidu platform: " + platform);
  35. }
  36. if (baiduAppId != null && !baiduAppId.isBlank()
  37. && baiduTagId != null && !baiduTagId.isBlank()) {
  38. return new Resolved(null, baiduAppId, baiduTagId);
  39. }
  40. if (platforms != null && platforms.size() == 1) {
  41. String p = platforms.keySet().iterator().next();
  42. Resolved r = resolveByPlatform(p);
  43. if (r != null) return r;
  44. }
  45. throw new IllegalArgumentException("missing honor tagId or platform");
  46. }
  47. private Resolved resolveByTagId(String tagId) {
  48. if (platforms != null) {
  49. for (Map.Entry<String, HonorAppPlacement> e : platforms.entrySet()) {
  50. HonorAppPlacement p = e.getValue();
  51. if (p.getAppId() != null && p.getTagIds() != null && p.getTagIds().contains(tagId)) {
  52. return new Resolved(e.getKey(), p.getAppId(), tagId);
  53. }
  54. }
  55. }
  56. if (baiduAppId != null && tagId.equals(baiduTagId)) {
  57. return new Resolved(null, baiduAppId, baiduTagId);
  58. }
  59. return null;
  60. }
  61. private Resolved resolveByPlatform(String platform) {
  62. if (platforms == null) return null;
  63. HonorAppPlacement p = platforms.get(platform);
  64. if (p == null || p.getAppId() == null || p.getTagIds() == null || p.getTagIds().isEmpty()) {
  65. return null;
  66. }
  67. if (p.getTagIds().size() > 1) {
  68. throw new IllegalArgumentException(
  69. "tagId is required for honor platform " + platform + " because multiple tag ids are configured");
  70. }
  71. return new Resolved(platform, p.getAppId(), p.getTagIds().get(0));
  72. }
  73. private static String firstParam(Map<String, String> p, String... keys) {
  74. if (p == null) return null;
  75. for (String k : keys) {
  76. String v = p.get(k);
  77. if (v != null && !v.isBlank()) return v.trim();
  78. }
  79. return null;
  80. }
  81. static String normalizePlatform(String value) {
  82. if (value == null) return null;
  83. return switch (value.toLowerCase().trim()) {
  84. case "ios", "iphone", "ipad", "1" -> "ios";
  85. case "android", "安卓", "0", "2" -> "android";
  86. default -> null;
  87. };
  88. }
  89. public static class HonorAppPlacement {
  90. private String appId;
  91. private List<String> tagIds;
  92. public String getAppId() { return appId; }
  93. public void setAppId(String v) { this.appId = v; }
  94. public List<String> getTagIds() { return tagIds; }
  95. public void setTagIds(List<String> v) { this.tagIds = v; }
  96. }
  97. public record Resolved(String platform, String appId, String tagId) {}
  98. }