
package com.jeelowcode.core.framework.config.btncommand.receiver;

import com.jeelowcode.framework.excel.model.ExcelTitleModel;
import com.jeelowcode.framework.utils.adapter.IJeeLowCodeAdapter;
import com.jeelowcode.framework.utils.tool.spring.SpringUtils;
import com.jeelowcode.core.framework.config.btncommand.param.ButtonParamExportTemplate;
import com.jeelowcode.core.framework.params.model.ExcelTemplateModel;
import com.jeelowcode.core.framework.params.vo.role.DbFormRoleFieldVo;
import com.jeelowcode.core.framework.service.IDbFormRoleService;
import com.jeelowcode.core.framework.service.IExcelService;
import org.apache.commons.collections4.map.LinkedMap;

import java.util.List;

/**
 * 导出模板按钮执行者
 */
public class ButtonReceiverExportTemplate extends ButtonReceiverBase implements IButtonCommandReceiver<ExcelTemplateModel> {


    private String pluginKey;

    private ButtonParamExportTemplate param;

    public ButtonReceiverExportTemplate(ButtonParamExportTemplate param) {
        this.param = param;
    }

    /**
     * 执行命令
     * @return
     */
    @Override
    public ExcelTemplateModel receiver() {
        IExcelService excelService = SpringUtils.getBean(IExcelService.class);
        IDbFormRoleService dbFormRoleService = SpringUtils.getBean(IDbFormRoleService.class);
        IJeeLowCodeAdapter jeeLowCodeAdapter = SpringUtils.getBean(IJeeLowCodeAdapter.class);
        Long tenantId = jeeLowCodeAdapter.getTenantId();

        Long dbFormId = param.getDbFormId();
        ExcelTemplateModel excelTemplateModel = excelService.getImportExcelTemplate(dbFormId);
        LinkedMap<String, ExcelTitleModel> headTitleMap = excelTemplateModel.getHeadTitleMap();

        List<DbFormRoleFieldVo> roleFieldVoList = dbFormRoleService.listRoleField(tenantId, dbFormId,false);
        super.removeNotExport(headTitleMap,roleFieldVoList);
        return excelTemplateModel;
    }


}
