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.UserFeature;
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 UserFeatureToRedisLoader {

    private final String userKeyFormat = "user:%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 userTable, String dt) {
        RedisTemplate<String, String> redisTemplate = buildRedisTemplate();
        String sql = String.format("select * from %s where dt ='%s';", userTable, dt);
        TunnelRecordReader reader = FeatureConstructor.loadDataFromOSSSession(sql);
        Record record;
        Map<String, String> userFeaRedisFormat = new HashMap<String, String>();
        int count = 0;
        try {
            while ((record = reader.read()) != null) {
                UserFeature userFeature = FeatureConstructor.constructUserFeature(record);
                String key = String.format(userKeyFormat, userFeature.getKey());
                String value = userFeature.getValue();
                userFeaRedisFormat.put(key, value);
                if(count < 200) {
                    count++;
                } else if (count == 200) {
                    redisTemplate.opsForValue().multiSet(userFeaRedisFormat);
                    System.out.println("------succes add 2000-----");
                    userFeaRedisFormat = 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;
        }
        UserFeatureToRedisLoader userFeatureToRedisLoader = new UserFeatureToRedisLoader();
        userFeatureToRedisLoader.loadFeatureToRedis(args[0], args[1]);
    }

}
