package com.trade.common.core.service;

import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.trade.common.core.domain.BaseI18nEntity;
import com.trade.common.core.domain.BizBaseI18nEntity;
import com.trade.common.exception.ServiceException;
import com.trade.common.utils.I18nUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;

@Service
public class BaseI18nService {

    private static final Logger log = LoggerFactory.getLogger(BaseI18nService.class);
    private DataSource dataSource;

    public BaseI18nService(DataSource dataSource){
        this.dataSource = dataSource;
    }

    /**
     * 更新对象列表的i18n属性，以及需要国际化的字段
     * 
     * @param entity 实体对象
     * @param clazz 类信息
     * @param <T> 继承BaseI18nDomain
     */
    public <T extends BaseI18nEntity> T setI18nMap(T entity, Class<T> clazz) {
        if (entity == null || clazz == null) {
            return entity;
        }
        setI18nMap(Collections.singletonList(entity), clazz);
        return entity;
    }

    /**
     * 更新对象列表的i18n属性，以及需要国际化的字段
     *
     * @param entity 实体对象
     * @param clazz 类信息
     * @param <T> 继承BaseI18nDomain
     */
    public <T extends BizBaseI18nEntity> T setI18nMap(T entity, Class<T> clazz) {
        if (entity == null || clazz == null) {
            return entity;
        }
        setBaseI18nMap(Collections.singletonList(entity), clazz);
        return entity;
    }

    /**
     * 更新对象列表的i18n属性
     * 
     * @param entityList 实体类集合
     * @param clazz 类信息
     * @param <T> BaseI18nEntity
     */
    public <T extends BaseI18nEntity> List<T> setI18nMap(List<T> entityList, Class<T> clazz) {
        exeI18nQuery(entityList, clazz,
                (tableInfo, connection) -> I18nUtil.setI18nMap(entityList, clazz, tableInfo, connection));
        return entityList;
    }

    /**
     * 更新对象列表的i18n属性
     *
     * @param entityList 实体类集合
     * @param clazz 类信息
     * @param <T> BaseI18nEntity
     */
    public <T extends BizBaseI18nEntity> List<T> setBaseI18nMap(List<T> entityList, Class<T> clazz) {
        exeBaseI18nQuery(entityList, clazz,
                (tableInfo, connection) -> I18nUtil.setBaseI18nMap(entityList, clazz, tableInfo, connection));
        return entityList;
    }

    private <T extends BaseI18nEntity> void exeI18nQuery(List<T> entityList, Class<T> clazz,
            BiConsumer<TableInfo, Connection> biConsumer) {
        if (CollectionUtils.isEmpty(entityList)) {
            return;
        }
        TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);
        if (tableInfo == null) {
            throw new ServiceException("无法获取表信息");
        }
        Connection connection = DataSourceUtils.getConnection(dataSource);
        try {
            biConsumer.accept(tableInfo, connection);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            DataSourceUtils.releaseConnection(connection, dataSource);
        }
    }

    private <T extends BizBaseI18nEntity> void exeBaseI18nQuery(List<T> entityList, Class<T> clazz,
            BiConsumer<TableInfo, Connection> biConsumer) {
        if (CollectionUtils.isEmpty(entityList)) {
            return;
        }
        TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);
        if (tableInfo == null) {
            throw new ServiceException("无法获取表信息");
        }
        Connection connection = DataSourceUtils.getConnection(dataSource);
        try {
            biConsumer.accept(tableInfo, connection);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            DataSourceUtils.releaseConnection(connection, dataSource);
        }
    }
}
