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.domain.dto.FormDataDto;
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);
    }

    /**
     * 功能描述: 根据tableName和map(属性名和属性值)进行查查
     *
     * @param tableName tableName
     * @param param     属性名和属性值
     * @return com.alibaba.fastjson.JSONObject
     */
    @Override
    public JSONObject getOneByMap(String tableName, JSONObject param) {
        requireNonNull(tableName, "表名为空");
        return selectService.getOneByMap(tableName, param);
    }

    @Override
    public Long getMaxId(String tableName, String pkName) {
        return IdUtils.getMaxId(tableName, pkName);
    }

    /**
     * 功能描述: 根据条件count
     *
     * @param dto dto
     * @return java.lang.Long
     */
    @Override
    public int count(FormDataDto dto) {
        JSONObject condition = dto.getFixedData();
        String tableName = dto.getTable();
        requireNonNull(tableName, "tableName is empty");
        GenTable genTable = cmdService.getTableFromRedisByTableName(RedisKey.TABLE_INFO, tableName);
        JSONObject packCondition = selectService.packColCondition(genTable.getColumns(), condition);
        return selectService.countByCondition(tableName, condition, packCondition);
    }

    /**
     * 功能描述: 获取最新的一条数据 create desc
     *
     * @param tableName tableName
     * @return jcom.alibaba.fastjson.JSONObject
     */
    @Override
    public JSONObject getNewest(String tableName) {
        requireNonNull(tableName, "tableName is empty");
        return selectService.getNewest(tableName);
    }
}
