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


import com.aliyun.odps.Instance;
import com.aliyun.odps.Odps;
import com.aliyun.odps.OdpsException;
import com.aliyun.odps.account.Account;
import com.aliyun.odps.account.AliyunAccount;
import com.aliyun.odps.data.Record;
import com.aliyun.odps.task.SQLTask;
import com.aliyun.odps.tunnel.InstanceTunnel;
import com.aliyun.odps.tunnel.io.TunnelRecordReader;
import com.tzld.piaoquan.recommend.feature.domain.video.base.ItemFeature;
import com.tzld.piaoquan.recommend.feature.domain.video.base.RequestContext;
import com.tzld.piaoquan.recommend.feature.domain.video.base.UserActionFeature;
import com.tzld.piaoquan.recommend.feature.domain.video.base.UserFeature;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FeatureConstructor {

    private static final String BUCKET_NAME = "ali-recommend";
    private static final Map<String, String> ODPS_CONFIG = new HashMap<String, String>();

    static {
        ODPS_CONFIG.put("ENDPOINT", "http://service.cn.maxcompute.aliyun.com/api");
        ODPS_CONFIG.put("ACCESSID", "LTAIWYUujJAm7CbH");
        ODPS_CONFIG.put("ACCESSKEY", "RfSjdiWwED1sGFlsjXv0DlfTnZTG1P");
    }

    ;

    private static final Account account = new AliyunAccount(ODPS_CONFIG.get("ACCESSID"), ODPS_CONFIG.get("ACCESSKEY"));


    public static List<Record> loadStreamDataFromOSS(String table, String dt) {
        Odps odps = new Odps(account);
        odps.setEndpoint(ODPS_CONFIG.get("ENDPOINT"));
        odps.setDefaultProject("loghubods");
        String sql = String.format("select * from %s where dt ='%s';", table, dt);
        Instance instance;
        List<Record> records = new ArrayList<Record>();
        try {
            instance = SQLTask.run(odps, sql);
            instance.waitForSuccess();
            records = SQLTask.getResult(instance);
        } catch (OdpsException e) {
            e.printStackTrace();
        }
        return records;
    }


    public static TunnelRecordReader loadDataFromOSSSession(String sql) {
        Odps odps = new Odps(account);
        odps.setEndpoint(ODPS_CONFIG.get("ENDPOINT"));
        odps.setDefaultProject("loghubods");
        TunnelRecordReader reader = null;
        try {
            Instance instance = SQLTask.run(odps, sql);
            instance.waitForSuccess();
            InstanceTunnel tunnel = new InstanceTunnel(odps);
            InstanceTunnel.DownloadSession session = tunnel.createDownloadSession(odps.getDefaultProject(), instance.getId());
            long count = session.getRecordCount();
            reader = session.openRecordReader(0, count);
        } catch (OdpsException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return reader;
    }


    public static RequestContext constructRequestContext(Record record) {
        RequestContext requestContext = new RequestContext();
        requestContext.setApptype(record.getString("apptype"));
        requestContext.setMachineinfo_brand(record.getString("machineinfo_brand"));
        requestContext.setMachineinfo_model(record.getString("machineinfo_model"));
        requestContext.setMachineinfo_platform(record.getString("machineinfo_platform"));
        requestContext.setMachineinfo_sdkversion(record.getString("machineinfo_sdkversion"));
        requestContext.setMachineinfo_system(record.getString("machineinfo_system"));
        requestContext.setMachineinfo_wechatversion(record.getString("machineinfo_wechatversion"));
        requestContext.setDay(record.getString("ctx_day"));
        requestContext.setWeek(record.getString("ctx_week"));
        requestContext.setHour(record.getString("ctx_hour"));
        requestContext.setRegion(record.getString("ctx_region"));
        requestContext.setCity(record.getString("ctx_city"));
        return requestContext;
    }


    public static UserFeature constructUserFeature(Record record) {
        UserFeature userFeature = new UserFeature();
        userFeature.setUid(record.get("uid").toString());
        userFeature.setUser_cycle_bucket_7days(record.getString("u_cycle_bucket_7days"));
        userFeature.setUser_cycle_bucket_30days(record.getString("u_cycle_bucket_30days"));
        userFeature.setUser_share_bucket_30days(record.getString("u_share_bucket_30days"));


        // 1day features
        UserActionFeature user1dayActionFeature = new UserActionFeature();
        user1dayActionFeature.setExp_cnt(record.getString("u_1day_exp_cnt"));
        user1dayActionFeature.setClick_cnt(record.getString("u_1day_click_cnt"));
        user1dayActionFeature.setShare_cnt(record.getString("u_1day_share_cnt"));
        user1dayActionFeature.setReturn_cnt(record.getString("u_1day_return_cnt"));
        user1dayActionFeature.setCtr(record.getString("u_ctr_1day"));
        user1dayActionFeature.setStr(record.getString("u_str_1day"));
        user1dayActionFeature.setRov(record.getString("u_rov_1day"));
        user1dayActionFeature.setRos(record.getString("u_ros_1day"));
        userFeature.setDay1_cnt_features(user1dayActionFeature);

        // 3day features
        UserActionFeature user3dayActionFeature = new UserActionFeature();
        user3dayActionFeature.setExp_cnt(record.getString("u_3day_exp_cnt"));
        user3dayActionFeature.setClick_cnt(record.getString("u_3day_click_cnt"));
        user3dayActionFeature.setShare_cnt(record.getString("u_3day_share_cnt"));
        user3dayActionFeature.setReturn_cnt(record.getString("u_3day_return_cnt"));
        user3dayActionFeature.setCtr(record.getString("u_ctr_3day"));
        user3dayActionFeature.setStr(record.getString("u_str_3day"));
        user3dayActionFeature.setRov(record.getString("u_rov_3day"));
        user3dayActionFeature.setRos(record.getString("u_ros_3day"));
        userFeature.setDay3_cnt_features(user3dayActionFeature);

        // 7day features
        UserActionFeature user7dayActionFeature = new UserActionFeature();
        user7dayActionFeature.setExp_cnt(record.getString("u_7day_exp_cnt"));
        user7dayActionFeature.setClick_cnt(record.getString("u_7day_click_cnt"));
        user7dayActionFeature.setShare_cnt(record.getString("u_7day_share_cnt"));
        user7dayActionFeature.setReturn_cnt(record.getString("u_7day_return_cnt"));
        user7dayActionFeature.setCtr(record.getString("u_ctr_7day"));
        user7dayActionFeature.setStr(record.getString("u_str_7day"));
        user7dayActionFeature.setRov(record.getString("u_rov_7day"));
        user7dayActionFeature.setRos(record.getString("u_ros_7day"));
        userFeature.setDay7_cnt_features(user7dayActionFeature);

        // 3month features
        UserActionFeature user3monthActionFeature = new UserActionFeature();
        user3monthActionFeature.setExp_cnt(record.getString("u_3month_exp_cnt"));
        user3monthActionFeature.setClick_cnt(record.getString("u_3month_click_cnt"));
        user3monthActionFeature.setShare_cnt(record.getString("u_3month_share_cnt"));
        user3monthActionFeature.setReturn_cnt(record.getString("u_3month_return_cnt"));
        user3monthActionFeature.setCtr(record.getString("u_ctr_3month"));
        user3monthActionFeature.setStr(record.getString("u_str_3month"));
        user3monthActionFeature.setRov(record.getString("u_rov_3month"));
        user3monthActionFeature.setRos(record.getString("u_ros_3month"));
        userFeature.setMonth3_cnt_features(user3monthActionFeature);

        return userFeature;
    }


    public static ItemFeature constructItemFeature(Record record) {
        ItemFeature itemFeature = new ItemFeature();
        itemFeature.setVideoId(record.get("videoid").toString());
        itemFeature.setUpId(record.get("uid").toString());
        //itemFeature.setTitleLength(record.get("play_count").toString());
        itemFeature.setPlayLength(record.get("total_time").toString());
        itemFeature.setTotalTime(record.get("total_time").toString());
        itemFeature.setDaysSinceUpload(record.get("existence_days").toString());

        UserActionFeature user1dayActionFeature = new UserActionFeature();
        user1dayActionFeature.setExp_cnt(record.getString("i_1day_exp_cnt"));
        user1dayActionFeature.setClick_cnt(record.getString("i_1day_click_cnt"));
        user1dayActionFeature.setShare_cnt(record.getString("i_1day_share_cnt"));
        user1dayActionFeature.setReturn_cnt(record.getString("i_1day_return_cnt"));
        user1dayActionFeature.setCtr(record.getString("i_ctr_1day"));
        user1dayActionFeature.setStr(record.getString("i_str_1day"));
        user1dayActionFeature.setRov(record.getString("i_rov_1day"));
        user1dayActionFeature.setRos(record.getString("i_ros_1day"));
        itemFeature.setDay1_cnt_features(user1dayActionFeature);

        UserActionFeature user3dayActionFeature = new UserActionFeature();
        user3dayActionFeature.setExp_cnt(record.getString("i_3day_exp_cnt"));
        user3dayActionFeature.setClick_cnt(record.getString("i_3day_click_cnt"));
        user3dayActionFeature.setShare_cnt(record.getString("i_3day_share_cnt"));
        user3dayActionFeature.setReturn_cnt(record.getString("i_3day_return_cnt"));
        user3dayActionFeature.setCtr(record.getString("i_ctr_3day"));
        user3dayActionFeature.setStr(record.getString("i_str_3day"));
        user3dayActionFeature.setRov(record.getString("i_rov_3day"));
        user3dayActionFeature.setRos(record.getString("i_ros_3day"));
        itemFeature.setDay3_cnt_features(user1dayActionFeature);

        UserActionFeature user7dayActionFeature = new UserActionFeature();
        user7dayActionFeature.setExp_cnt(record.getString("i_7day_exp_cnt"));
        user7dayActionFeature.setClick_cnt(record.getString("i_7day_click_cnt"));
        user7dayActionFeature.setShare_cnt(record.getString("i_7day_share_cnt"));
        user7dayActionFeature.setReturn_cnt(record.getString("i_7day_return_cnt"));
        user7dayActionFeature.setCtr(record.getString("i_ctr_7day"));
        user7dayActionFeature.setStr(record.getString("i_str_7day"));
        user7dayActionFeature.setRov(record.getString("i_rov_7day"));
        user7dayActionFeature.setRos(record.getString("i_ros_7day"));
        itemFeature.setDay7_cnt_features(user1dayActionFeature);

        UserActionFeature user3monthActionFeature = new UserActionFeature();
        user3monthActionFeature.setExp_cnt(record.getString("i_3month_exp_cnt"));
        user3monthActionFeature.setClick_cnt(record.getString("i_3month_click_cnt"));
        user3monthActionFeature.setShare_cnt(record.getString("i_3month_share_cnt"));
        user3monthActionFeature.setReturn_cnt(record.getString("i_3month_return_cnt"));
        user3monthActionFeature.setCtr(record.getString("i_ctr_3month"));
        user3monthActionFeature.setStr(record.getString("i_str_3month"));
        user3monthActionFeature.setRov(record.getString("i_rov_3month"));
        user3monthActionFeature.setRos(record.getString("i_ros_3month"));
        itemFeature.setMonth3_cnt_features(user3monthActionFeature);
        return itemFeature;
    }


}
