
package com.jeelowcode.core.framework.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeelowcode.core.framework.mapper.JeeLowCodeSqlMapper;
import com.jeelowcode.core.framework.service.IFrameSqlService;
import com.jeelowcode.framework.global.JeeLowCodeBaseConstant;
import com.jeelowcode.framework.plus.SqlHelper;
import com.jeelowcode.framework.plus.build.buildmodel.wrapper.SqlInfoDeleteWrapper;
import com.jeelowcode.framework.plus.build.buildmodel.wrapper.SqlInfoQueryWrapper;
import com.jeelowcode.framework.plus.build.buildmodel.wrapper.SqlInfoUpdateWrapper;
import com.jeelowcode.framework.plus.core.model.SqlFormatModel;
import com.jeelowcode.framework.utils.utils.FuncBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 低代码平台
 * 该接口下所有业务不用租户过滤
 */

@Service
public class FrameSqlServiceImpl implements IFrameSqlService {

    @Autowired
    private JeeLowCodeSqlMapper sqlMapper;

    //特殊处理
    String skipTablePre = "lowcode_zexample_";

    @Override
    public void executeDDL(String ddlSql) {
        if (ddlSql.contains(skipTablePre)) {
            sqlMapper.executeDDL(JeeLowCodeBaseConstant.DS_JEELOWCODE, ddlSql);
        } else {
            sqlMapper.executeDDL(JeeLowCodeBaseConstant.DS_MASTER, ddlSql);
        }

    }

    //获取数据，不做租户等其他处理
    @Override
    public List<Map<String, Object>> getSimpleData(String sql) {
        Map<String, Object> dataMap = new HashMap<>();
        if (sql.contains(skipTablePre)) {
            return sqlMapper.selectData(JeeLowCodeBaseConstant.DS_JEELOWCODE, sql, dataMap);
        } else {
            return sqlMapper.selectData(JeeLowCodeBaseConstant.DS_MASTER, sql, dataMap);
        }

    }


    //添加参数到sqlformatmodel
    private Map<String, Object> getSqlFormatModelParam(SqlFormatModel sqlFormatModel, Map<String, Object> params) {
        Map<String, Object> dataMap = sqlFormatModel.getDataMap();
        if (FuncBase.isEmpty(dataMap)) {
            dataMap = new HashMap<>();
        }

        if (FuncBase.isEmpty(params)) {
            return dataMap;
        }

        for (Map.Entry<String, Object> entry : params.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (dataMap.containsKey(key)) {
                continue;
            }
            // 如果map1中不存在该键，则添加
            dataMap.put(key, value);
        }
        return dataMap;
    }


    //查询数据相关
    //单个
    @Override
    public Map<String, Object> getDataOneByPlus(SqlInfoQueryWrapper.Wrapper wrapper) {
        return getDataOneByPlus(wrapper, null);
    }

    @Override
    public Map<String, Object> getDataOneByPlus(SqlInfoQueryWrapper.Wrapper wrapper, Map<String, Object> params) {
        SqlFormatModel sqlFormatModel = wrapper.buildSql();
        Map<String, Object> dataMap = getSqlFormatModelParam(sqlFormatModel, params);
        String sql = sqlFormatModel.getEwSql();
        List<Map<String, Object>> dataMapList = null;
        if (sql.contains(skipTablePre)) {
            dataMapList = sqlMapper.selectData(JeeLowCodeBaseConstant.DS_JEELOWCODE, sql, dataMap);
        } else {
            dataMapList = sqlMapper.selectData(JeeLowCodeBaseConstant.DS_MASTER, sql, dataMap);
        }
        return FuncBase.isEmpty(dataMapList) ? new HashMap<>() : dataMapList.get(0);
    }

    //多个
    @Override
    public List<Map<String, Object>> getDataListByPlus(SqlInfoQueryWrapper.Wrapper wrapper) {
        return getDataListByPlus(wrapper, null);
    }

    @Override
    public List<Map<String, Object>> getDataListByPlus(SqlInfoQueryWrapper.Wrapper wrapper, Map<String, Object> params) {
        SqlFormatModel sqlFormatModel = wrapper.buildSql();
        Map<String, Object> dataMap = getSqlFormatModelParam(sqlFormatModel, params);
        String sql = sqlFormatModel.getEwSql();
        List<Map<String, Object>> dataMapList = null;
        if (sql.contains(skipTablePre)) {
            dataMapList = sqlMapper.selectData(JeeLowCodeBaseConstant.DS_JEELOWCODE, sql, dataMap);
        } else {
            dataMapList = sqlMapper.selectData(JeeLowCodeBaseConstant.DS_MASTER, sql, dataMap);
        }
        return dataMapList;
    }

    @Override
    public List<Map<String, Object>> getDataListByPlus(String sql, Map<String, Object> params) {
        String publicSql = SqlHelper.getPublicSql(sql);
        SqlFormatModel sqlFormatModel = new SqlFormatModel();
        sqlFormatModel.setSql(publicSql);

        List<Map<String, Object>> dataMapList = null;
        if (sql.contains(skipTablePre)) {
            dataMapList = sqlMapper.selectData(JeeLowCodeBaseConstant.DS_JEELOWCODE, sql, params);
        } else {
            dataMapList = sqlMapper.selectData(JeeLowCodeBaseConstant.DS_MASTER, sql, params);
        }
        return dataMapList;
    }


    @Override
    public IPage<Map<String, Object>> getDataIPageByPlus(IPage page, SqlInfoQueryWrapper.Wrapper wrapper, Map<String, Object> params) {
        SqlFormatModel sqlFormatModel = wrapper.buildSql();
        Map<String, Object> dataMap = getSqlFormatModelParam(sqlFormatModel, params);
        IPage<Map<String, Object>> pages = null;
        String dataSourceType = wrapper.getDataSourceType();
        String sql = sqlFormatModel.getEwSql();
        if (FuncBase.isNotEmpty(dataSourceType)) {
            pages = sqlMapper.selectPageData(dataSourceType, page, sqlFormatModel.getEwSql(), dataMap);
        } else if (sql.contains(skipTablePre)) {
            pages = sqlMapper.selectPageData(JeeLowCodeBaseConstant.DS_JEELOWCODE, page, sqlFormatModel.getEwSql(), dataMap);
        } else {
            pages = sqlMapper.selectPageData(JeeLowCodeBaseConstant.DS_MASTER, page, sqlFormatModel.getEwSql(), dataMap);
        }
        return pages;
    }

    @Override
    public IPage<Map<String, Object>> getDataIPageByPlus(IPage page, SqlInfoQueryWrapper.Wrapper wrapper) {
        return getDataIPageByPlus(page, wrapper, null);
    }

    //修改数据相关
    @Override
    public void updateDataByPlus(SqlInfoUpdateWrapper.Wrapper wrapper) {
        SqlFormatModel sqlFormatModel = wrapper.buildSql();
        String sql = sqlFormatModel.getEwSql();

        if (sql.contains(skipTablePre)) {
            sqlMapper.updateData(JeeLowCodeBaseConstant.DS_JEELOWCODE,sql, sqlFormatModel.getDataMap());
        } else {
            sqlMapper.updateData(JeeLowCodeBaseConstant.DS_MASTER,sql, sqlFormatModel.getDataMap());
        }
    }

    //新增数据相关
    @Override
    public void insertDataByPlus(String tableName, Map<String, Object> addDataMap) {
        SqlFormatModel sqlFormatModel = SqlHelper.getInsertWrapper()
                .setTableName(tableName)
                .setMap(addDataMap)
                .buildSql();
        String sql = sqlFormatModel.getEwSql();
        if (sql.contains(skipTablePre)) {
            sqlMapper.insertData(JeeLowCodeBaseConstant.DS_JEELOWCODE,sql, sqlFormatModel.getDataMap());
        } else {
            sqlMapper.insertData(JeeLowCodeBaseConstant.DS_MASTER,sql, sqlFormatModel.getDataMap());
        }
    }


    //真实-删除数据
    @Override
    public void delRealDataByPlus(SqlInfoDeleteWrapper.Wrapper wrapper) {
        SqlFormatModel sqlFormatModel = wrapper.buildSql();
        String sql = sqlFormatModel.getEwSql();
        if (sql.contains(skipTablePre)) {
            sqlMapper.deleteData(JeeLowCodeBaseConstant.DS_JEELOWCODE,sqlFormatModel.getEwSql(), sqlFormatModel.getDataMap());
        } else {
            sqlMapper.deleteData(JeeLowCodeBaseConstant.DS_MASTER,sqlFormatModel.getEwSql(), sqlFormatModel.getDataMap());
        }


    }

    //逻辑-删除数据
    @Override
    public void delDataByPlus(SqlInfoUpdateWrapper.Wrapper wrapper) {
        wrapper.setColumn("is_deleted", -1);
        SqlFormatModel sqlFormatModel = wrapper.buildSql();

        String sql = sqlFormatModel.getEwSql();
        if (sql.contains(skipTablePre)) {
            sqlMapper.updateData(JeeLowCodeBaseConstant.DS_JEELOWCODE,sqlFormatModel.getEwSql(), sqlFormatModel.getDataMap());
        } else {
            sqlMapper.updateData(JeeLowCodeBaseConstant.DS_MASTER,sqlFormatModel.getEwSql(), sqlFormatModel.getDataMap());
        }
    }

    //----------------------------------------------------------
    //获取表数据
    @Override
    public List<Map<String, Object>> getTableDataList(String tableName) {
        SqlInfoQueryWrapper.Wrapper wrapper = SqlHelper.getQueryWrapper()
                .setTableName(tableName)
                .build();
        List<Map<String, Object>> resultList = this.getDataListByPlus(wrapper);
        return resultList;
    }

    @Override
    public List<Map<String, Object>> getTableDataList(String tableName, List<String> fieldList) {
        SqlInfoQueryWrapper.Wrapper wrapper = SqlHelper.getQueryWrapper()
                .setTableName(tableName)
                .select(fieldList, true)
                .build();
        return this.getDataListByPlus(wrapper);
    }

    //根据Id获取数据
    @Override
    public Map<String, Object> getTableDataById(String tableName, Long id) {
        SqlInfoQueryWrapper.Wrapper wrapper = SqlHelper.getQueryWrapper()
                .setTableName(tableName)
                .setWhere(where -> {
                    where.eq("id", id);
                })
                .build();
        return this.getDataOneByPlus(wrapper);
    }

    @Override
    public Map<String, Object> getTableDataById(String tableName, Long id, List<String> fieldList) {
        SqlInfoQueryWrapper.Wrapper wrapper = SqlHelper.getQueryWrapper()
                .setTableName(tableName)
                .select(fieldList, true)
                .setWhere(where -> {
                    where.eq("id", id);
                })
                .build();
        return this.getDataOneByPlus(wrapper);
    }

    //自定义根据某一个字段查询--单条
    @Override
    public Map<String, Object> getDataOneByField(String tableName, String whereFieldName, Object whereFieldValue) {
        SqlInfoQueryWrapper.Wrapper wrapper = SqlHelper.getQueryWrapper()
                .setTableName(tableName)
                .setWhere(where -> {
                    where.eq(whereFieldName, whereFieldValue);
                })
                .build();

        return this.getDataOneByPlus(wrapper);
    }

    @Override
    public Map<String, Object> getDataOneByField(String tableName, String whereFieldName, Object whereFieldValue, List<String> fieldList) {
        SqlInfoQueryWrapper.Wrapper wrapper = SqlHelper.getQueryWrapper()
                .setTableName(tableName)
                .select(fieldList, true)
                .setWhere(where -> {
                    where.eq(whereFieldName, whereFieldValue);
                })
                .build();
        return this.getDataOneByPlus(wrapper);
    }

    //自定义根据某一个字段查询--多条
    @Override
    public List<Map<String, Object>> getDataListByField(String tableName, String whereFieldName, Object whereFieldValue) {
        SqlInfoQueryWrapper.Wrapper wrapper = SqlHelper.getQueryWrapper()
                .setTableName(tableName)
                .setWhere(where -> {
                    where.eq(whereFieldName, whereFieldValue);
                })
                .build();
        return this.getDataListByPlus(wrapper);
    }

    @Override
    public List<Map<String, Object>> getDataListByField(String tableName, String whereFieldName, Object whereFieldValue, List<String> fieldList) {
        SqlInfoQueryWrapper.Wrapper wrapper = SqlHelper.getQueryWrapper()
                .setTableName(tableName)
                .select(fieldList, true)
                .setWhere(where -> {
                    where.eq(whereFieldName, whereFieldValue);
                })
                .build();

        return this.getDataListByPlus(wrapper);
    }

    //公共修改
    @Override
    public void baseUpdateDataById(String tableName, Map<String, Object> dataMap, Long id) {
        SqlInfoUpdateWrapper.Wrapper wrapper = SqlHelper.getUpdateWrapper()
                .setTableName(tableName)
                .setMap(dataMap)
                .setWhere(where -> {
                    where.eq("id", id);
                }).build();
        this.updateDataByPlus(wrapper);
    }

    public void baseUpdateDataByField(String tableName, Map<String, Object> dataMap, String whereFieldName, Object whereFieldValue) {
        SqlInfoUpdateWrapper.Wrapper wrapper = SqlHelper.getUpdateWrapper()
                .setTableName(tableName)
                .setMap(dataMap)
                .setWhere(where -> {
                    where.eq(whereFieldName, whereFieldValue);
                }).build();
        this.updateDataByPlus(wrapper);
    }

    //公共删除-逻辑
    @Override
    public void baseDelDataById(String tableName, Long id) {
        SqlInfoUpdateWrapper.Wrapper wrapper = SqlHelper.getUpdateWrapper()
                .setTableName(tableName)
                .setWhere(where -> {
                    where.eq("id", id);
                });
        this.delDataByPlus(wrapper);
    }

    @Override
    public void baseDelDataByField(String tableName, String whereFieldName, Object whereFieldValue) {
        SqlInfoUpdateWrapper.Wrapper wrapper = SqlHelper.getUpdateWrapper()
                .setTableName(tableName)
                .setWhere(where -> {
                    where.eq(whereFieldName, whereFieldValue);
                });
        this.delDataByPlus(wrapper);
    }

    //公共删除-真实
    @Override
    public void baseDelRealData(String tableName, Long id) {
        SqlInfoDeleteWrapper.Wrapper wrapper = SqlHelper.getDeleteWrapper()
                .setTableName(tableName)
                .setWhere(where -> {
                    where.eq("id", id);
                }).build();
        this.delRealDataByPlus(wrapper);
    }

    @Override
    public void baseDelRealDataByField(String tableName, String whereFieldName, Object whereFieldValue) {
        SqlInfoDeleteWrapper.Wrapper wrapper = SqlHelper.getDeleteWrapper()
                .setTableName(tableName)
                .setWhere(where -> {
                    where.eq(whereFieldName, whereFieldValue);
                }).build();
        this.delRealDataByPlus(wrapper);
    }

}
