package com.tzld.piaoquan.recommend.server.dataloader;

import com.aliyun.odps.data.Record;
import com.aliyun.odps.tunnel.io.TunnelRecordReader;
import com.tzld.piaoquan.recommend.feature.domain.video.base.ItemFeature;
import com.tzld.piaoquan.recommend.server.common.ThreadPoolFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;


public class ItemFeatureToRedisLoader {

    private final String videoKeyFormat = "video:%s";
    private ExecutorService pool = ThreadPoolFactory.defaultPool();

    public static RedisTemplate<String, String> buildRedisTemplate() {
        RedisStandaloneConfiguration rsc = new RedisStandaloneConfiguration();
        rsc.setPort(6379);
        rsc.setPassword("Wqsd@2019");
        rsc.setHostName("r-bp1ps6my7lzg8rdhwx682.redis.rds.aliyuncs.com");
        RedisTemplate<String, String> template = new RedisTemplate<>();
        JedisConnectionFactory fac = new JedisConnectionFactory(rsc);
        fac.afterPropertiesSet();
        template.setDefaultSerializer(new StringRedisSerializer());
        template.setConnectionFactory(fac);
        template.afterPropertiesSet();
        return template;
    }


    public void loadFeatureToRedis(String table, String dt) {
        RedisTemplate<String, String> redisTemplate = buildRedisTemplate();
        String sql = String.format("select * from %s where dt ='%s';", table, dt);
        TunnelRecordReader reader = FeatureConstructor.loadDataFromOSSSession(sql);
        Record record;
        Map<String, String> itemFeaRedisFormat = new HashMap<String, String>();
        int count = 0;
        try {
            while ((record = reader.read()) != null) {
                ItemFeature itemFeature = FeatureConstructor.constructItemFeature(record);
                String key = String.format(videoKeyFormat, itemFeature.getKey());
                String value = itemFeature.getValue();
                itemFeaRedisFormat.put(key, value);
                if (count < 200) {
                    count++;
                } else if (count == 200) {
                    redisTemplate.opsForValue().multiSet(itemFeaRedisFormat);
                    System.out.println("------succes add 200-----");
                    itemFeaRedisFormat = new HashMap<String, String>();
                    count = 0;
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        if (args.length < 2) {
            System.out.println("--------args 缺失---------");
            return;
        }
        ItemFeatureToRedisLoader itemFeatureToRedisLoader = new ItemFeatureToRedisLoader();
        itemFeatureToRedisLoader.loadFeatureToRedis(args[0], args[1]);
    }

}
