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

import cn.hutool.json.JSONObject;
import com.jeelowcode.core.framework.config.btncommand.ButtonInvoker;
import com.jeelowcode.core.framework.config.btncommand.button.EditButtonCommand;
import com.jeelowcode.core.framework.config.btncommand.param.ButtonParamEdit;
import com.jeelowcode.core.framework.config.btncommand.receiver.ButtonReceiverEdit;
import com.jeelowcode.core.framework.config.btncommand.receiver.IButtonCommandReceiver;
import com.jeelowcode.core.framework.service.IDbformDataService;
import com.jeelowcode.core.framework.service.IFrameSqlService;
import com.jeelowcode.framework.plus.SqlHelper;
import com.jeelowcode.framework.plus.build.buildmodel.wrapper.SqlInfoQueryWrapper;
import com.jeelowcode.framework.plus.build.buildmodel.wrapper.SqlInfoUpdateWrapper;
import com.jeelowcode.framework.utils.model.ExecuteEnhanceModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class DbformDataService implements IDbformDataService {


    @Autowired
    private IFrameSqlService sqlService;

    @Override
    public void editJsonData(Long dbformId, JSONObject jsonObject) {
        Long id = jsonObject.getLong("id");

        //封装参数
        ButtonParamEdit buttonParam = new ButtonParamEdit();
        buttonParam.setDbFormId(dbformId);
        buttonParam.setDataId(id);
        buttonParam.setParams(jsonObject);

        //执行者绑定参数
        IButtonCommandReceiver receiver = new ButtonReceiverEdit(buttonParam);

        //执行者和命令绑定
        EditButtonCommand command = new EditButtonCommand(receiver);

        //创建调用者
        ButtonInvoker<ExecuteEnhanceModel> invoker = new ButtonInvoker();
        invoker.setButtonCommand(command);//调用者设置命令
        invoker.executeCommand();//调用者下发命令
    }


    public  void update(String tableName,String id,Map<String,Object> dataMap){
        SqlInfoUpdateWrapper.Wrapper wrapper = SqlHelper.getUpdateWrapper()
                .setTableName(tableName)
                .setMap(dataMap)
                .setWhere(where -> {
                    where.eq("id", id);
                }).build();
        sqlService.updateDataByPlus(wrapper);

    }





}
