package com.reyun.saas.mob.user.service.impl;

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.common.model.NoSqlTableName;
import com.reyun.saas.mob.user.domain.App;
import com.reyun.saas.mob.user.service.NoSqlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Set;

/**
 * @author: hanzepeng
 * @Date: 2022/02/09/15:30
 * @Description: 非关系型数据库业务层
 */
@Slf4j
@Service
public class NoSqlServiceImpl implements NoSqlService {

    @Autowired
    private AwsDynamoDBComponent dynamoDBComponent;

    public Map<String, Object> getItem2Map(String tableName, String primaryKeyName, String prmaryKeyValue){
        //校验参数
        if (StringUtils.isEmpty(tableName)
                ||StringUtils.isEmpty(primaryKeyName)
                ||StringUtils.isEmpty(prmaryKeyValue)) {
            throw new RuntimeException("参数错误");
        }

        Map<String, Object> result = null;
        Table table = dynamoDBComponent.getTable(tableName);
        if (table != null) {
            Item item = table.getItem(primaryKeyName, prmaryKeyValue);
            if (item != null) {
                result = item.asMap();
            }
        }

        return result;
    }


    public void putSet(String tableName, String primaryKeyName, String prmaryKeyValue,String fieldName, Set<String> set){
        if (StringUtils.isEmpty(tableName)
                ||StringUtils.isEmpty(primaryKeyName)
                ||StringUtils.isEmpty(prmaryKeyValue)) {
            throw new RuntimeException("参数错误");
        }

        Table table = dynamoDBComponent.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);
        }

        dynamoDBComponent.writeItem(tableName, item);
    }

    /**
     * 删除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 = dynamoDBComponent.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
        dynamoDBComponent.writeItem(NoSqlTableName.YY_APP_INFO,item);
    }



}
