package com.ruoyi.spider.utils;

import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hutool.db.DbUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.Page;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.sql.Order;
import cn.hutool.log.level.Level;

public class DBUtil {
    public static Logger logger = LoggerFactory.getLogger(DBUtil.class);
    public static String target = "target";
    static {
        DbUtil.setShowSqlGlobal(true, true, true, Level.INFO);
    }

    public static List<Entity> find(Entity where) {
        return find(null, where);
    }

    public static List<Entity> find(String group, Entity where) {
        try {
            return DbUtil.use(DSFactory.get(group)).find(where);
        } catch (Exception e) {
            logger.error("查询[{}]数据出错:" + where.getTableName(), e);
        }
        return Collections.emptyList();
    }

    public static List<Entity> pageForEntityList(Entity where, int page, int numPerPage, Order... orders) {
        return pageForEntityList(null, where, page, numPerPage);
    }

    public static List<Entity> pageForEntityList(String group, Entity where, int page, int numPerPage, Order... orders) {
        try {
            Page _page = new Page(page, numPerPage);
            _page.addOrder(orders);
            return DbUtil.use(DSFactory.get(group)).pageForEntityList(where, _page);
        } catch (Exception e) {
            logger.error("查询[{}]数据出错:" + where.getTableName(), e);
        }
        return Collections.emptyList();
    }

    public static List<Entity> query(String sql, Object... params) {
        return query(null, sql, params);
    }

    public static List<Entity> query(String group, String sql, Object... params) {
        try {
            return DbUtil.use(DSFactory.get(group)).query(sql, params);
        } catch (Exception e) {
            logger.error("查询[{}]数据出错:" + sql, e);
        }
        return Collections.emptyList();
    }

    public static Long count(String group, Entity where) {
        try {
            return DbUtil.use(DSFactory.get(group)).count(where);
        } catch (Exception e) {
            logger.error("查询[{}]计数出错:" + where.getTableName(), e);
        }
        return 0l;
    }

    public static void insertOrUpdate(Entity record, String... keys) {
        insertOrUpdate(null, record, keys);
    }

    public static void insertOrUpdate(String group, Entity record, String... keys) {
        try {
            DbUtil.use(DSFactory.get(group)).insertOrUpdate(record, keys);
        } catch (Exception e) {
            logger.error("保存[{}]数据出错:" + record.getTableName(), e);
        }
    }

    public static void updateById(Entity record) {
        updateById(null, record);
    }

    public static void updateById(String group, Entity record) {
        try {
            DbUtil.use(DSFactory.get(group)).update(record, Entity.create().set("id", record.remove("id")));
        } catch (Exception e) {
            logger.error("更新[{}]数据出错:" + record.getTableName(), e);
        }
    }

    public static void update(String group, Entity record, Entity where) {
        try {
            DbUtil.use(DSFactory.get(group)).update(record, where);
        } catch (Exception e) {
            logger.error("更新[{}]数据出错:" + record.getTableName(), e);
        }

    }

    public static void del(Entity where) {
        del(null, where);
    }

    public static void del(String group, Entity where) {
        try {
            DbUtil.use(DSFactory.get(group)).del(where);
        } catch (Exception e) {
            logger.error("删除[{}]数据出错:" + where.getTableName(), e);
        }
    }

}
