package com.lideeyunji.core.framework.config.aspect.enhance.strategy.sql;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lideeyunji.core.framework.config.aspect.enhance.model.EnhanceContext;
import com.lideeyunji.core.framework.entity.EnhanceSqlEntity;
import com.lideeyunji.core.framework.entity.FormEntity;
import com.lideeyunji.core.framework.mapper.LideeYunJiSqlMapper;
import com.lideeyunji.core.framework.service.IFormService;
import com.lideeyunji.core.framework.utils.Func;
import com.lideeyunji.core.framework.utils.FuncWeb;
import com.lideeyunji.tool.framework.common.constant.lideeYunJiBaseConstant;
import com.lideeyunji.tool.framework.yunji.adapter.IlideeYunjiAdapter;
import com.lideeyunji.tool.framework.yunji.tool.spring.SpringUtils;
import com.lideeyunji.tool.framework.yunji.utils.FuncBase;
import com.lideeyunji.tool.framework.yunji.utils.lideeYunJiUtils;

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

public class BaseEnhanceSqlStrategy {

    //列表
    public void executeEnhanceAfterList(EnhanceContext context, EnhanceSqlEntity sqlEntity) {
        IFormService dbFormService = SpringUtils.getBean(IFormService.class);
        Long dbFormId = context.getParam().getDbFormId();
        Map<String, Object> params = context.getParam().getParams();
        String executeSql = sqlEntity.getExecuteSql();

        FormEntity formEntity = dbFormService.getFormEntityById(dbFormId);
        String autoWhereSql = Func.getSqlAutoWhereSql(formEntity, params);
        List<Map<String, Object>> dataMapList = this.executeSelectListSql(executeSql, params, autoWhereSql);
        context.getResult().setRecords(dataMapList);
        context.getResult().setExitFlag(true);
    }

    public void executeEnhanceAfterPage(EnhanceContext context, EnhanceSqlEntity sqlEntity) {
        IFormService dbFormService = SpringUtils.getBean(IFormService.class);
        Long dbFormId = context.getParam().getDbFormId();
        Map<String, Object> params = context.getParam().getParams();
        Integer pageNo = lideeYunJiUtils.getMap2Integer(params, "pageNo");
        Integer pageSize = lideeYunJiUtils.getMap2Integer(params, "pageSize");
        Page page = FuncWeb.getPage(pageNo, pageSize);
        String executeSql = sqlEntity.getExecuteSql();

        FormEntity formEntity = dbFormService.getFormEntityById(dbFormId);
        String autoWhereSql = Func.getSqlAutoWhereSql(formEntity, params);
        IPage<Map<String, Object>> pages = this.executeSelectPageSql(executeSql, params, autoWhereSql, page);
        FuncWeb.setPageResult(context, pages);
    }


    /**
     * 执行查询
     *
     * @param executeSql
     * @param paramMap
     * @return
     * @throws Throwable
     */
    protected List<Map<String, Object>> executeSelectListSql(String executeSql, Map<String, Object> paramMap, String autoWhereSql) {
        String sql = this.getSql(executeSql, paramMap, autoWhereSql);
        Object obj = this.execute(sql, paramMap);
        return (List<Map<String, Object>>) obj;
    }

    //分页
    public IPage<Map<String, Object>> executeSelectPageSql(String executeSql, Map<String, Object> paramMap, String autoWhereSql, Page page) {
        String sql = this.getSql(executeSql, paramMap, autoWhereSql);
        Object obj = this.execute(sql, paramMap, page);
        return (IPage<Map<String, Object>>) obj;
    }
    
    protected void executeSql(String executeSql, Map<String, Object> paramMap) {
        String sql = getSql(executeSql, paramMap);
        execute(sql, paramMap);
    }

    protected String getSql(String executeSql, Map<String, Object> paramMap) {
        IlideeYunjiAdapter lideeYunJiAdapter = SpringUtils.getBean(IlideeYunjiAdapter.class);
        executeSql = Func.replaceParam(executeSql, paramMap, null, lideeYunJiAdapter);
        return executeSql.trim();
    }

    protected String getSql(String executeSql, Map<String, Object> paramMap, String autoWhereSql) {
        IlideeYunjiAdapter lideeYunJiAdapter = SpringUtils.getBean(IlideeYunjiAdapter.class);
        //替换参数
        executeSql = Func.replaceParam(executeSql, paramMap, autoWhereSql, lideeYunJiAdapter);
        return executeSql.trim();
    }

    /**
     * 执行sql
     *
     * @param sql
     * @return
     */
    private Object execute(String sql, Map<String, Object> dataMap) {
        return execute(sql, dataMap, null);
    }

    private Object execute(String sql, Map<String, Object> dataMap, Page page) {
        LideeYunJiSqlMapper sqlMapper = SpringUtils.getBean(LideeYunJiSqlMapper.class);
        sql = sql.trim();
        //执行sql
        if (sql.startsWith("insert") || sql.startsWith("INSERT")) {
            sqlMapper.insertData(lideeYunJiBaseConstant.DS_MASTER,sql, dataMap);
        } else if (sql.startsWith("update") || sql.startsWith("UPDATE")) {
            sqlMapper.updateData(lideeYunJiBaseConstant.DS_MASTER,sql, dataMap);
        } else if (sql.startsWith("delete") || sql.startsWith("DELETE")) {
            sqlMapper.deleteData(lideeYunJiBaseConstant.DS_MASTER,sql, dataMap);
        } else if (sql.startsWith("select") || sql.startsWith("SELECT")) {
            if (FuncBase.isNotEmpty(page)) {
                return sqlMapper.selectPageData(lideeYunJiBaseConstant.DS_MASTER,page, sql, dataMap);
            }
            return sqlMapper.selectData(lideeYunJiBaseConstant.DS_MASTER,sql, dataMap);
        }
        return null;
    }
}
