package com.tzy.config;

import com.tzy.elasticsearch.dto.CardGroupInfoGoodsDTO;
import com.tzy.elasticsearch.service.CardGroupInfoGoodsService;
import com.tzy.framework.util.RedisTools;
import com.tzy.sportcard.api.dto.group.SimpleGroupDTO;
import com.tzy.sportcard.api.service.GroupApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.teasoft.bee.osql.SuidRich;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.List;

@Component
@Slf4j
public class ConnectionPoolWarmer implements ApplicationRunner {

    @Resource
    private DataSource dataSource;
    @Resource
    private CardGroupInfoGoodsService cardGroupInfoGoodsService;
    @Autowired
    @Qualifier("threadPoolTaskExecutor")
    private ThreadPoolTaskExecutor pools;
    @Resource
    private SuidRich suidRich;
    @Resource
    private GroupApiService groupApiService;


    List<String> tables = Arrays.asList(
            "card_group_order_info", "card_group_goods", "card_group_info", "order_list", "card_check_list", "tzy_sys_notice_record",
            "app_base_user", "tzy_app_info", "card_group_lives_config", "app_user_card_record", "tzy_card_base_info"
    );

    @Override
    public void run(ApplicationArguments args) {
        RedisTools.getRedisUtils().set("test_init", 1, 3);
        log.debug("redis init");
        initPool();
        log.debug("pool init");
        poolPrepare();
        log.debug("db pool init");
        indexPrepare();
        log.debug("db index init");
        initSuidRich();
        log.debug("suid init");
        initEs();
        log.debug("es init");
        initRecommend();
        log.debug("recommend init");
    }

    private void initRecommend() {
        try {
            groupApiService.getRecommendInfo(272, 1, 20);
        } catch (Exception ignored) {
        }
    }

    private void initSuidRich() {
        try {
            SimpleGroupDTO groupInfo = new SimpleGroupDTO();
            groupInfo.setStatus(201);
            suidRich.select(groupInfo);
        } catch (Exception ignored) {
        }
    }

    private void initPool() {
        pools.execute(() -> log.debug("db pool init"));
    }

    private void initEs() {
        try {
            CardGroupInfoGoodsDTO query = new CardGroupInfoGoodsDTO();
            query.setStatus("201");
            query.setRows(100);
            cardGroupInfoGoodsService.page(query);
        } catch (Exception ignored) {
        }
    }

    private void poolPrepare() {
        for (int i = 0; i < 30; i++) {
            try (Connection connection = dataSource.getConnection();
                 Statement statement = connection.createStatement()) {
                // 执行简单的查询，确保连接可用
                // 连接会在try块结束时自动关闭
                statement.execute("SELECT 1");

            } catch (SQLException ignored) {
            }
        }
    }

    private void indexPrepare() {
        try (Connection connection = dataSource.getConnection();
             Statement statement = connection.createStatement()) {
            for (String table : tables) {
                // 执行简单的查询，确保连接可用
                // 连接会在try块结束时自动关闭
                statement.execute("SELECT id FROM " + table + " ORDER BY id DESC LIMIT 1000");
                statement.execute("SELECT * FROM " + table + " LIMIT 1000");
            }

            statement.execute("SELECT id,username FROM  app_base_user  ORDER BY username LIMIT 1000");

            statement.execute("SELECT to_userid FROM  tzy_sys_notice_record  ORDER BY to_userid DESC LIMIT 1000");

            statement.execute("SELECT user_id FROM  app_user_card_record  where status='0' and user_id>0 ORDER BY user_id DESC LIMIT 1000");
            statement.execute("SELECT order_id FROM  app_user_card_record  where  order_id >0 ORDER BY order_id DESC LIMIT 1000");
            statement.execute("SELECT card_id FROM  app_user_card_record  where  card_id >0 ORDER BY card_id DESC LIMIT 1000");

            statement.execute("SELECT merchant_id FROM  tzy_card_base_info where  merchant_id >0  ORDER BY merchant_id DESC LIMIT 1000");

            statement.execute("SELECT user_id FROM  card_group_order_info  ORDER BY user_id DESC LIMIT 1000");
            statement.execute("SELECT group_info_id FROM  card_group_order_info  ORDER BY group_info_id DESC LIMIT 1000");
            statement.execute("SELECT merchant_id FROM  card_group_order_info  ORDER BY merchant_id DESC LIMIT 1000");

            statement.execute("SELECT min(user_id),max(user_id) FROM  card_group_order_info");
            statement.execute("SELECT min(group_info_id),max(group_info_id) FROM  card_group_order_info");
            statement.execute("SELECT min(merchant_id),max(merchant_id) FROM  card_group_order_info");

            statement.execute("SELECT order_id FROM  order_list  ORDER BY order_id DESC LIMIT 1000");
            statement.execute("SELECT sku_id FROM  order_list  where sku_id >0 ORDER BY sku_id DESC LIMIT 1000");
            statement.execute("SELECT spu_id FROM  order_list where spu_id> 0 ORDER BY spu_id DESC LIMIT 1000");

            statement.execute("SELECT min(order_id),max(order_id) FROM  order_list");
            statement.execute("SELECT min(sku_id),max(sku_id) FROM  order_list");
            statement.execute("SELECT min(spu_id),max(spu_id) FROM  order_list");

            statement.execute("SELECT user_id FROM  card_group_goods  ORDER BY user_id DESC LIMIT 1000");
            statement.execute("SELECT user_order_id FROM  card_group_goods  ORDER BY user_order_id DESC LIMIT 1000");
            statement.execute("SELECT group_info_id FROM  card_group_goods  ORDER BY group_info_id DESC LIMIT 1000");

            statement.execute("SELECT min(group_info_id),max(group_info_id) FROM  card_group_goods");
            statement.execute("SELECT min(user_order_id),max(user_order_id) FROM  card_group_goods");
            statement.execute("SELECT min(user_id),max(user_id) FROM  card_group_goods");

            statement.execute("SELECT group_info_id FROM  card_group_lives_config  ORDER BY group_info_id DESC LIMIT 1000");

            statement.execute("SELECT merchant_id FROM  card_group_info  ORDER BY merchant_id DESC LIMIT 1000");
            statement.execute("SELECT id,status,merchant_id FROM  card_group_info where  status = 201 LIMIT 1000");

        } catch (Exception e) {
            log.error("db index init error", e);
        }
    }
}