package io.gisbi.dataset.server;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.gisbi.api.dataset.DatasetDataApi;
import io.gisbi.api.dataset.dto.BaseTreeNodeDTO;
import io.gisbi.api.dataset.dto.EnumValueRequest;
import io.gisbi.api.dataset.dto.MultFieldValuesRequest;
import io.gisbi.api.dataset.dto.PreviewSqlDTO;
import io.gisbi.api.dataset.union.DatasetGroupInfoDTO;
import io.gisbi.api.dataset.dto.EnumObj;
import io.gisbi.dataset.dao.auto.entity.CoreDatasetTable;
import io.gisbi.dataset.dao.auto.entity.CoreDatasetTableField;
import io.gisbi.dataset.manage.DatasetDataManage;
import io.gisbi.extensions.datasource.dto.DatasetTableDTO;
import io.gisbi.extensions.datasource.dto.DatasetTableFieldDTO;
import io.gisbi.utils.LogUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author bi-coder
 */
@RestController
@RequestMapping("datasetData")
public class DatasetDataServer implements DatasetDataApi {
    @Resource
    private DatasetDataManage datasetDataManage;

    @Override
    public Map<String, Object> previewData(DatasetGroupInfoDTO datasetGroupInfoDTO) throws Exception {
        return datasetDataManage.previewDataWithLimit(datasetGroupInfoDTO, 0, 100, false, true);
    }

    @Override
    public List<DatasetTableFieldDTO> tableField(DatasetTableDTO datasetTableDTO) throws Exception {
        return datasetDataManage.getTableFields(datasetTableDTO);
    }

    @Override
    public Map<String, Object> previewSql(PreviewSqlDTO dto) throws Exception {
        return datasetDataManage.previewSqlWithLog(dto);
    }

    @Override
    public Map<String, Object> previewSqlCheck(PreviewSqlDTO dto) throws Exception {
        return datasetDataManage.previewSql(dto);
    }

    @Override
    public List<String> getFieldEnumDs(EnumObj map) throws Exception {
        try {
            return datasetDataManage.getFieldEnumDs(map);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.error(e);
            return null;
        }
    }

    @Override
    public List<String> getFieldEnum(MultFieldValuesRequest multFieldValuesRequest) {
        try {
            return datasetDataManage.getFieldEnum(multFieldValuesRequest);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.error(e);
            return null;
        }
    }

    @Override
    public List<Map<String, Object>> getFieldEnumObj(EnumValueRequest request) throws Exception {
        try {
            return datasetDataManage.getFieldEnumObj(request);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.error(e);
            return null;
        }
    }

    @Override
    public Long getDatasetCount(DatasetGroupInfoDTO datasetGroupInfoDTO) throws Exception {
        return datasetDataManage.getDatasetTotal(datasetGroupInfoDTO.getId());
    }

    @Override
    public Long getDatasetTotal(DatasetGroupInfoDTO datasetGroupInfoDTO) throws Exception {
        return datasetDataManage.getDatasetCountWithWhere(datasetGroupInfoDTO.getId());
    }

    @Override
    public List<BaseTreeNodeDTO> getFieldValueTree(MultFieldValuesRequest multFieldValuesRequest) throws Exception {
        try {
            return datasetDataManage.getFieldValueTree(multFieldValuesRequest);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.error(e);
            return null;
        }
    }

    @GetMapping("getTablesByAppId")
    public List<Map<String,Object>> getTablesByAppId(String appid) throws Exception {
        List<Map<String,Object>> result = datasetDataManage.getTablesByAppId(appid);
        return result;
    }
    @GetMapping("getFieldsByTableId")
    public Map<String, Object> getFieldsByTableId(Long datasourceId,String tablename) throws Exception {
        Map<String, Object> result = datasetDataManage.getFieldsByTableId(datasourceId,tablename);
        return result;
    }
    @PostMapping("addTableData")
    public boolean addTableData(Long datasourceId,  @RequestParam("tableData") String tableData) throws Exception {
        boolean result = datasetDataManage.addTableData(
                datasourceId,
                tableData
        );
        return result;
    }

    @PostMapping("getTableDataByPk")
    public Map<String, Object> getTableDataByPk(Long datasourceId,@RequestParam("whereJson")  String whereJson) throws Exception {
        Map<String, Object> result=datasetDataManage.getTableDataByPk(
                datasourceId,
                whereJson
        );
        return result;
    }
    @PostMapping("updateTableData")
    public boolean updateTableData(Long datasourceId, @RequestParam("tableData") String tableData) throws Exception {
        boolean result = datasetDataManage.updateTableData(
                datasourceId,
                tableData
        );
        return result;
    }
    @PostMapping("deleteTableData")
    public boolean deleteTableData(Long datasourceId, @RequestParam("whereJson") String whereJson)  throws Exception {
        boolean result = datasetDataManage.deleteTableData(
                datasourceId,
                whereJson
        );
        return result;
    }
    @PostMapping("queryTableDataPaged")
    public Page<Map<String, Object>> queryTableDataPaged(Long datasourceId, @RequestParam("queryJson") String queryJson)  throws Exception {
        Page<Map<String, Object>> result = datasetDataManage.queryTableDataPaged(
                datasourceId,
                queryJson
        );
        return result;
    }
}
