package com.mobvista.apptag.utils;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @package: com.mobvista.apptag.utils
 * @author: wangjf
 * @date: 2020/3/16
 * @time: 9:21 上午
 * @email: jinfeng.wang@mobvista.com
 * @phone: 152-1062-7698
 */
public class GetPackageFromOffer {
    private static final String CONNECTION_URL = "jdbc:mysql://adn-data-foronlinetest.c5yzcdreb1xr.us-east-1.rds.amazonaws.com:3306/mob_adn?useUnicode=true&characterEncoding=utf8&useSSL=false";
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String USER = "adnro";
    private static final String PASSWORD = "YcM123glh";
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) throws SQLException {
        String date = "";
        if (args.length == 1) {
            date = args[0];
        } else {
            System.exit(1);
        }
        List list = readOffer(date);
        insert(list);

        System.out.println("Insert " + list.size() + " package_names");
    }

    /**
     * 从新建单子中获取 package_name
     *
     * @param date
     * @return
     * @throws SQLException
     */
    static List readOffer(String date) throws SQLException {
        ResultSet resultSet = null;
        String start = date + " 00:00:00";
        String end = date + " 23:59:59";
        String sql = String.format("SELECT package_name FROM " +
                "(SELECT trace_app_id package_name FROM mob_adn.campaign_list " +
                "WHERE `advertiser_id` = 903 AND `timestamp` BETWEEN @startTime AND @endTime GROUP BY trace_app_id " +
                "UNION " +
                "SELECT package_name FROM mob_adn.publisher_channel " +
                "WHERE `timestamp` BETWEEN @startTime AND @endTime GROUP BY package_name) t " +
                "GROUP BY package_name");
        try {
            long startTime = sdf.parse(start).getTime();
            long endTime = sdf.parse(end).getTime();
            Class.forName(DRIVER);
            Connection connection = DriverManager.getConnection(CONNECTION_URL, USER, PASSWORD);
            sql = sql.replace("@startTime", String.valueOf(startTime / 1000))
                    .replace("@endTime", String.valueOf(endTime / 1000));
            System.out.println(sql);
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
        } catch (ParseException e) {
            System.out.println("Parse Date Error!!!");
        } catch (SQLException | ClassNotFoundException e) {
            System.out.println("SQL Execute Error!!!");
        }
        return covertList(resultSet);
    }

    /**
     * 批量写入
     *
     * @param list
     */
    static void insert(List<String> list) {
        if (list.size() % 1000 == 0) {
            for (int i = 0; i < list.size() / 1000; i++) {
                int fromIndex = i * 1000 + 1;
                int toIndex;
                if (i == list.size() / 1000) {
                    toIndex = list.size();
                } else {
                    toIndex = (i + 1) * 1000;
                }
                Set<String> set = new HashSet<>(list.subList(fromIndex, toIndex));
                MySqlUtil mySqlUtil = new MySqlUtil();
                mySqlUtil.insert(set);
            }
        } else {
            for (int i = 0; i < list.size() / 1000 + 1; i++) {
                int fromIndex = i * 1000 + 1;
                int toIndex;
                if (i == list.size() / 1000) {
                    toIndex = list.size();
                } else {
                    toIndex = (i + 1) * 1000;
                }
                Set<String> set = new HashSet<>(list.subList(fromIndex, toIndex));
                MySqlUtil mySqlUtil = new MySqlUtil();
                mySqlUtil.insert(set);
            }
        }
    }

    /**
     * @param rs
     * @return
     * @throws SQLException
     */
    static List covertList(ResultSet rs) throws SQLException {
        List<String> list = new ArrayList();
        while (rs.next()) {
            list.add(rs.getString("package_name"));
        }
        return list;
    }
}