package com.engine.common.service.impl;

import com.engine.common.cmd.QueryCommonCmd;
import com.engine.common.service.CommonService;
import com.engine.common.util.CommonUtil;
import com.engine.common.util.DbTools;
import com.engine.common.util.Utils;
import com.engine.core.impl.Service;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class CommonServiceImpl extends Service implements CommonService {

    @Override
    public Map<String, Object> queryCommon(Map<String, Object> param) {
        return commandExecutor.execute(new QueryCommonCmd(param));
    }


    @Override
    public Map<String, Object> updateCommon(Map<String, Object> param) {
        Map<String, Object> result = new HashMap<>();
        log.info("updateCommon param:[{}]",param);
        result.put("result", DbTools.update(CommonUtil.makeUpdateSql(Util.null2String(param.get("tableName")),Utils.resolveMap(Util.null2String(param.get("data"))),Utils.resolveMap(Util.null2String(param.get("condition"))))));
        return result;
    }

    @Override
    public Map<String, Object> insertCommon(Map<String, Object> param) {
        Map<String, Object> result = new HashMap<>();
        log.info("insertCommon param:[{}]",param);
        Map<String,Object> params = Utils.resolveMap(Util.null2String(param.get("params")));
        String tableName = Util.null2String(params.get("tableName"));
        params.remove("tableName");
        result.put("result", CommonUtil.makeInsertSql(tableName,params));
        return result;

    }
}
