DDBUtils.java 10.3 KB
Newer Older
shenggui.li committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
package com.reyun.saas.mob.util;

import cn.hutool.json.JSONUtil;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.reyun.saas.common.framework.component.AwsDynamoDBComponent;
import com.reyun.saas.mob.user.domain.App;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author: hanzepeng
 * @Date: 2021/10/11/16:29
 * @Description: ddb工具类
 */
@Slf4j
@Component
public class DDBUtils {

    @Resource
    private AwsDynamoDBComponent awsDynamoDBComponent;

    //ios设备状态表
    public static final String YY_TCTINFO_IOS =  "yy_rctinfo_ios";

    //安卓设备状态表
    public static final String YY_TCTINFO_ANDROID =  "yy_rctinfo_android";

    //ddb的rta白名单表
    public static final String RTA_TABLE_NAME = "yy_rta_whitelist";

    //app-设备表
    public static final  String YY_APP_DEVICEINFO = "yy_app_deviceinfo";

    //安卓sdk版本与策略关系表
    public static final  String SDK_POLICY_ANDROID = "yy_strategy_config_android";

    //IOS sdk版本与策略关系表
    public static final  String SDK_POLICY_IOS = "yy_strategy_config_ios";

    //标签映射表
    public static final String  LABEL_MAPPING = "yy_label_mapping";

    //appkey——oid.bid.os关联表
    public static final  String  YY_APP_INFO = "yy_app_info";


    /**
     * 获取ddb项目
     * @param tableName ddb表名
     * @param primaryKeyName 主键字段
     * @param prmaryKeyValue 主键值
     * @return item
     */
    public  Item getItem(String tableName,String primaryKeyName,String prmaryKeyValue){
        //校验参数
        if (StringUtils.isEmpty(tableName)
                ||StringUtils.isEmpty(primaryKeyName)
                ||StringUtils.isEmpty(prmaryKeyValue)) {
            throw new RuntimeException("参数错误");
        }

        Table table = awsDynamoDBComponent.getTable(tableName);

        return table.getItem(primaryKeyName, prmaryKeyValue);
    }


    /**
     * 获取ddb项目并转换成json
     * @param tableName ddb表名
     * @param primaryKeyName 主键字段
     * @param prmaryKeyValue 主键值
     * @return json
     */
    public  String getItemJSON(String tableName,String primaryKeyName,String prmaryKeyValue){
        //校验参数
        if (StringUtils.isEmpty(tableName)
                ||StringUtils.isEmpty(primaryKeyName)
                ||StringUtils.isEmpty(prmaryKeyValue)) {
            throw new RuntimeException("参数错误");
        }
        Table table = awsDynamoDBComponent.getTable(tableName);
        Item item = table.getItem(primaryKeyName, prmaryKeyValue);
        return item.toJSON();
    }

    /**
     * 获取ddb项目并且转换成MAP
     * @param tableName ddb表名
     * @param primaryKeyName 主键字段
     * @param prmaryKeyValue 主键值
     * @return map
     */
    public  Map getItem2Map(String tableName, String primaryKeyName, String prmaryKeyValue){
        //校验参数
        if (StringUtils.isEmpty(tableName)
                ||StringUtils.isEmpty(primaryKeyName)
                ||StringUtils.isEmpty(prmaryKeyValue)) {
            throw new RuntimeException("参数错误");
        }
        Table table = awsDynamoDBComponent.getTable(tableName);
        Item item = table.getItem(primaryKeyName, prmaryKeyValue);

        LinkedHashMap linkedHashMap = null;
        if(item != null){
            linkedHashMap = JSONUtil.toBean(item.toJSON(), LinkedHashMap.class);
        }
        return linkedHashMap;
    }


    /**
     * item添加String
     * @param tableName ddb表名
     * @param primaryKeyName 主键字段
     * @param prmaryKeyValue 主键值
     * @param fieldName  String的字段名
     * @param str  要添加的String
     * @return
     */
    public void putString (String tableName, String primaryKeyName, String prmaryKeyValue,String fieldName,String str){
        if (StringUtils.isEmpty(tableName)
                ||StringUtils.isEmpty(primaryKeyName)
                ||StringUtils.isEmpty(prmaryKeyValue)) {
            throw new RuntimeException("参数错误");
        }

        //如果item为空 创建一个item
        Table table = awsDynamoDBComponent.getTable(tableName);
        Item item = table.getItem(primaryKeyName, prmaryKeyValue);
        if (item == null) {
            item = new Item();
            item.withPrimaryKey(primaryKeyName,prmaryKeyValue);
        }

        if (StringUtils.isEmpty(str)) {
            item.removeAttribute(fieldName);
        } else {
            item.withString(fieldName,str);
        }
        awsDynamoDBComponent.writeItem(tableName, item);

    }

    /**
     * item添加List
     * @param tableName ddb表名
     * @param primaryKeyName 主键字段
     * @param prmaryKeyValue 主键值
     * @param fieldName  List集合
     * @param list  要添加的List
     * @return
     */
    public void putList (String tableName, String primaryKeyName, String prmaryKeyValue, String fieldName, List list){
        if (StringUtils.isEmpty(tableName)
                ||StringUtils.isEmpty(primaryKeyName)
                ||StringUtils.isEmpty(prmaryKeyValue)) {
            throw new RuntimeException("参数错误");
        }

        //如果item为空 创建一个item
        Table table = awsDynamoDBComponent.getTable(tableName);
        Item item = table.getItem(primaryKeyName, prmaryKeyValue);
        if (item == null) {
            item = new Item();
            item.withPrimaryKey(primaryKeyName,prmaryKeyValue);
        }

        if (CollectionUtils.isEmpty(list)) {
            item.removeAttribute(fieldName);
        } else {
            item.withList(fieldName,list);
        }
        awsDynamoDBComponent.writeItem(tableName, item);

    }


    /**
     * item添加map 如果map中没有元素 则删除该item的map
     * @param tableName ddb表名
     * @param primaryKeyName 主键字段
     * @param prmaryKeyValue 主键值
     * @param fieldName  set的字段名
     * @param map  要添加的map集合
     * @return
     */
    public void putMap (String tableName, String primaryKeyName, String prmaryKeyValue,String fieldName,Map map){
        if (StringUtils.isEmpty(tableName)
                ||StringUtils.isEmpty(primaryKeyName)
                ||StringUtils.isEmpty(prmaryKeyValue)) {
            throw new RuntimeException("参数错误");
        }

        //如果item为空 创建一个item
        Table table = awsDynamoDBComponent.getTable(tableName);
        Item item = table.getItem(primaryKeyName, prmaryKeyValue);
        if (item == null) {
            item = new Item();
            item.withPrimaryKey(primaryKeyName,prmaryKeyValue);
        }

        //校验map的size  如果为0 则删除item的该字段
        if (CollectionUtils.isEmpty(map)) {
            item.removeAttribute(fieldName);
        } else {
            item.withMap(fieldName,map);
        }
        awsDynamoDBComponent.writeItem(tableName, item);

    }

    /**
     * item添加set
     * @param tableName ddb表名
     * @param primaryKeyName 主键字段
     * @param prmaryKeyValue 主键值
     * @param fieldName set的字段名
     * @Param set 要添加的set集合
     * @return
     */
    public void putSet(String tableName, String primaryKeyName, String prmaryKeyValue,String fieldName, Set set){
        if (StringUtils.isEmpty(tableName)
                ||StringUtils.isEmpty(primaryKeyName)
                ||StringUtils.isEmpty(prmaryKeyValue)) {
            throw new RuntimeException("参数错误");
        }


        Table table = awsDynamoDBComponent.getTable(tableName);
        Item item = table.getItem(primaryKeyName, prmaryKeyValue);

        if (item == null) {
            item = new Item();
            item.withPrimaryKey(primaryKeyName,prmaryKeyValue);
        }
        if (CollectionUtils.isEmpty(set)) {
            item.removeAttribute(fieldName);
        } else {
            item.withStringSet(fieldName,set);
        }

        awsDynamoDBComponent.writeItem(tableName, item);
    }


    /**
     * 查询项目是否存在
     * @param tableName ddb表名
     * @param primaryKeyName 主键字段名称
     * @param prmaryKeyValue 主键value
     * @return 是否存在  true为存在  false为不存在
     */
    public boolean itemExists(String tableName, String primaryKeyName, String prmaryKeyValue){

        if (StringUtils.isEmpty(tableName)
                ||StringUtils.isEmpty(primaryKeyName)
                ||StringUtils.isEmpty(prmaryKeyValue)) {
            throw new RuntimeException("参数错误");
        }

        Table table = awsDynamoDBComponent.getTable(tableName);

        Item item = table.getItem(primaryKeyName, prmaryKeyValue);
        if(item == null){
            return false;
        }
        return true;
    }

    /**
     * 删除ddb表中的item
     * @param tableName 表名
     * @param primaryKeyName 主键字段
     * @param prmaryKeyValue 主键value
     */
    public void deleteIetm(String tableName, String primaryKeyName, String prmaryKeyValue){
        if (StringUtils.isEmpty(tableName)
                ||StringUtils.isEmpty(primaryKeyName)
                ||StringUtils.isEmpty(prmaryKeyValue)) {
            throw new RuntimeException("参数错误");
        }
        Table table = awsDynamoDBComponent.getTable(tableName);
        table.deleteItem(primaryKeyName, prmaryKeyValue);
    }

    /**
     * put appkey——oid,type.bundleid对应关系
     * @param app 目标app
     * @param oid //用户oid
     */
    public void putAppInfo(App app,String oid) {
        if(app == null || StringUtils.isEmpty(app.getAppkey())
            || StringUtils.isEmpty(oid)){
            throw new RuntimeException("参数错误");
        }

        String  type = "iOS".equals(app.getOs())?"1":"2";
        String bid = StringUtils.isEmpty(app.getBid())?"default":app.getBid();

        Item item = new Item();
        item.withPrimaryKey("appkey",app.getAppkey());//appkey
        item.withString("type",type);//系统
        item.withString("oid",oid);//oid
        item.withString("bundleid",bid);//bid
        awsDynamoDBComponent.writeItem(YY_APP_INFO,item);
    }
}