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

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.service.IFormService;
import com.lideeyunji.core.framework.utils.Func;
import com.lideeyunji.tool.framework.yunji.tool.spring.SpringUtils;
import com.lideeyunji.tool.framework.yunji.utils.FuncBase;

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

/**
 * 导出策略
 */
public class ExportEnhanceSqlStrategy extends BaseEnhanceSqlStrategy implements EnhanceSqlStrategy {

    @Override
    public void execute(EnhanceContext context, EnhanceSqlEntity enhanceSqlEntity) {
        Long dbFormId = context.getParam().getDbFormId();
        Map<String, Object> params = context.getParam().getParams();
        String executeSql = enhanceSqlEntity.getExecuteSql();

        IFormService dbFormService = SpringUtils.getBean(IFormService.class);
        FormEntity formEntity = dbFormService.getFormEntityById(dbFormId);
        String autoWhereSql = Func.getSqlAutoWhereSql(formEntity, params);
        List<Map<String, Object>> dataMapList = this.executeSelectListSql(executeSql, params, autoWhereSql);
        if (FuncBase.isEmpty(dataMapList)) {
            dataMapList = new ArrayList<>();
        }
        context.getResult().setRecords(dataMapList);
        context.getResult().setExitFlag(true);
    }

}
