package com.weaver.seconddev.njwebservice.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.json.JSONUtil;
import com.weaver.datasource.utils.rest.CommonRestService;
import com.weaver.ebuilder.datasource.api.entity.ExecuteSqlEntity;
import com.weaver.ebuilder.datasource.api.entity.SqlParamEntity;
import com.weaver.ebuilder.datasource.api.enums.SourceType;
import com.weaver.ebuilder.datasource.api.enums.SqlParamType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

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

/**
 * @Author liang.cheng
 * @Date 2025/1/7 2:02 PM
 * @Description: 数据库操作
 * @Version 1.0
 */
@Slf4j
@Component
public class DataSetUtil {

    @Autowired
    private CommonRestService commonRestService;

    public static String base64(String sql) {
        return Base64.encode(sql);
    }

    /**
     * 执行服务内部sql:/sapi/secondev/ds/executeSql
     * sourceType  :LOGIC
     * groupId  :   weaver-ebuilder-app-service 可以通过group接口获取
     * sql :      select * from ebda_app limit  10
     * @param sourceType LOGIC: 各模块提供业务数据(逻辑表)
     * @param groupId
     * @param sql
     * @return
     */
    public Map<String, Object> executeLogicSql(SourceType sourceType, String groupId, String sql) {
        //拼接参数
        Map<String, Object> valueMap = new HashMap<>();
        valueMap.put("sourceType", sourceType);
        valueMap.put("groupId", groupId);
        valueMap.put("sql", base64(sql));

        try {
            return commonRestService.getForObject("/sapi/secondev/ds/executeSql", valueMap, MediaType.APPLICATION_FORM_URLENCODED, Map.class);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
    }

    /**
     * 1201基线后支持的聚合执行接口
     * LOGIC:支持普通sql与预编译sql执行,支持带事务执行
     * EXTERNAL:支持普通sql与预编译sql执行,不支持带事务执行
     * @param sourceType 连接类型
     * @param groupId 连接id
     * @param sql sql语句
     * @param sqlParams 预编译sql参数
     * @param transId 事务id
     * @param startTrans 是否开启事务
     * @param commit 是否提交事务
     * @param rollback 是否回滚事务
     */
    public Map<String, Object> executeSqlWithTrans(SourceType sourceType, String groupId, String sql, List<SqlParamEntity> sqlParams, String transId, Boolean startTrans, Boolean commit, Boolean rollback) {
        //执行sql  参数sourceType   groupId  sql
        ExecuteSqlEntity executeSqlEntity = new ExecuteSqlEntity();
        executeSqlEntity.setSql(base64(sql));
        executeSqlEntity.setGroupId(groupId);
        executeSqlEntity.setSourceType(sourceType);

        //若通过占位符方式查询外部数据库,需增加此参数
        //占位符list 的顺序  要与 sql 的?占位符顺序一致
        if (sqlParams != null && !sqlParams.isEmpty()) {
            executeSqlEntity.setParams(sqlParams);
        }
        //设置事务id
        if (transId != null && !transId.isEmpty()) {
            executeSqlEntity.setTransactionId(transId);
        }
        //是否开启事务
        if (startTrans != null && startTrans) {
            executeSqlEntity.setStartTransaction(true);
        }
        //是否提交事务
        if (commit != null && commit) {
            executeSqlEntity.setCommitTransaction(true);
        }
        //是否回滚事务
        if (rollback != null && rollback) {
            executeSqlEntity.setRollbackTransaction(true);
        }

        //拼接参数
        Map<String, Object> valueMap = new HashMap<>();
        valueMap.put("entity", JSONUtil.toJsonStr(executeSqlEntity));

        try {
            return commonRestService.postForForm("/sapi/secondev/ds/executeSqlAll", Map.class, valueMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
    }

    public List<SqlParamEntity> getSqlParams() {
        List<SqlParamEntity> sqlParams = new ArrayList<>();

        SqlParamEntity sqlParam1 = new SqlParamEntity();
        sqlParam1.setParamType(SqlParamType.VARCHAR);
        sqlParam1.setValue("thsv5s4n2c");
        sqlParams.add(sqlParam1);

        SqlParamEntity sqlParam2 = new SqlParamEntity();
        sqlParam2.setParamType(SqlParamType.INTEGER);
        sqlParam2.setValue("0");
        sqlParams.add(sqlParam2);

        List<Long> ids = new ArrayList<>();
        ids.add(1054407582048280585L);
        ids.add(1054407345976074241L);
        ids.add(1054406667262189570L);
        for (Long id : ids) {
            SqlParamEntity sqlParam3 = new SqlParamEntity();
            sqlParam3.setParamType(SqlParamType.LONG);
            sqlParam3.setValue(String.valueOf(id));
            sqlParams.add(sqlParam3);
        }

        return sqlParams;
    }


}
