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

import cn.hutool.json.JSONObject;
import com.jeelowcode.core.framework.entity.FormEntity;
import com.jeelowcode.core.framework.params.model.TableModel;
import com.jeelowcode.framework.utils.adapter.IJeeLowCodeAdapter;
import com.jeelowcode.framework.utils.model.ResultDataModel;
import com.jeelowcode.framework.utils.tool.CollectionUtil;
import com.jeelowcode.framework.utils.tool.spring.SpringUtils;
import com.jeelowcode.core.framework.config.btncommand.param.ButtonParamExport;
import com.jeelowcode.core.framework.params.model.ExcelModel;
import com.jeelowcode.core.framework.params.vo.role.DbFormRoleFieldVo;
import com.jeelowcode.core.framework.service.IDbFormRoleService;
import com.jeelowcode.core.framework.service.IExcelService;
import com.jeelowcode.core.framework.service.IFormService;
import com.jeelowcode.core.framework.service.IFrameService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 新增自定义执行者
 */
public class ButtonReceiverCustom extends ButtonReceiverBase implements IButtonCommandReceiver<TableModel> {


    private String pluginKey;

    private ButtonParamExport param;

    public ButtonReceiverCustom(ButtonParamExport param) {
        this.param = param;
    }

    /**
     * 执行命令
     *
     * @return
     */
    @Override
    public TableModel receiver() {
        IFormService formService = SpringUtils.getBean(IFormService.class);
        IFrameService frameService = SpringUtils.getBean(IFrameService.class);

        Long dbFormId = param.getDbFormId();
        Map<String, Object> params = param.getParams();

        FormEntity formEntity = formService.getFormEntityById(dbFormId);

        //人员信息自定义导出后刷新是否背调状态为是
        if (formEntity.getTableName().toLowerCase().equals("lc_outside_person")) {
            ResultDataModel model = frameService.getExportDataList(dbFormId, params);

            List<JSONObject> records = model.getRecords().stream()
                    .map(recordMap -> new JSONObject(recordMap))
                    .collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(records)) {
                for (JSONObject record : records) {
                    record.set("isValidate", "是");
                }
                frameService.editBatchData(dbFormId, records);
            }
        }
        return new TableModel(formEntity.getTableName(), formEntity.getTableDescribe());
    }


}
