package com.boman.web.core.service.common;

import com.alibaba.fastjson.JSONObject;
import com.boman.common.redis.RedisKey;
import com.boman.common.redis.service.RedisService;
import com.boman.domain.GenTable;
import com.boman.web.core.service.TableServiceCmdService;
import com.boman.web.core.service.select.IBaseSelectService;
import com.boman.web.core.utils.IdUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

import static com.boman.common.core.utils.obj.ObjectUtils.requireNonNull;

/**
 * @author shiqian
 * @date 2021年04月15日 16:31
 **/
@Service
public class CommonServiceImpl implements ICommonService {

    @Resource
    private IBaseSelectService selectService;
    @Resource
    private RedisService redisService;
    @Resource
    private TableServiceCmdService cmdService;

    /**
     * 功能描述: 根据表名和id查找
     *
     * @param tableName tableName
     * @param id        id
     * @return JSONObject
     */
    @Override
    public JSONObject getById(String tableName, Long id) {
        requireNonNull(tableName, "tableName is empty");
        requireNonNull(id, "id is empty");

        GenTable genTable = cmdService.getTableFromRedisByTableName(RedisKey.TABLE_INFO, tableName);
        String pkName = IdUtils.getPkName(genTable.getColumns());
        return selectService.selectById(tableName, pkName, id);
    }

    /**
     * 功能描述: 根据condition进行查找
     *
     * @param tableName tableName
     * @param condition condition
     * @return java.util.List<com.alibaba.fastjson.JSONObject>
     */
    @Override
    public List<JSONObject> getByMap(String tableName, JSONObject condition) {
        requireNonNull(tableName, "tableName is empty");
        return selectService.getByMap(tableName, condition);
    }
}
