package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.StorageNode;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author luyanchao
 * @since 2019/2/22 14:57
 */
@Repository("storageNodeDao")
public class StorageNodeRepository extends JdbcGenericDao<StorageNode, Long> {

    public StorageNodeRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate) {
        super(jdbcTemplate, npJdbcTemplate);
    }

    public List<StorageNode> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
        String sql = "select a.* from storage_node a where a.is_deleted = 0";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(StorageNode.class, sql);
    }

    public int count(List<Param> params) {
        String sql = "select count(1) from storage_node a where a.is_deleted = 0";
        sql = QueryBuilder.buildRaw(sql, params, null, "a");
        Map<String, Object> param = QueryBuilder.getParam(params);
        return this.countQuery(sql, param).intValue();
    }

    public void remove(long id) {
        String sql = "update storage_node set is_deleted = 1 where id = :id ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("id", id);
        execute(sql, params);
    }

    public void removeByStorage(long storageId) {
        String sql = "update storage_node set is_deleted = 1 where storage_id = :storageId ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("storageId", storageId);
        execute(sql, params);
    }

    public List<StorageNode> listByStorage(long storageId) {
        String sql = "select a.* from storage_node a where is_deleted = 0 and storage_id = :storageId ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("storageId", storageId);
        return this.list(StorageNode.class, sql, params);
    }

    public StorageNode get(long id) {
        String sql = "select a.* from storage_node a where a.id = :id ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("id", id);
        List<StorageNode> list = this.list(StorageNode.class, sql, params);
        return list == null || list.size() == 0 ? null : list.get(0);
    }

    public StorageNode getByIdentifier(String identifier, long storageId) {
        String sql = "select a.* from storage_node a where is_deleted = 0 and identifier = :identifier and storage_id = :storageId ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("identifier", identifier);
        params.put("storageId", storageId);
        List<StorageNode> list = this.list(StorageNode.class, sql, params);
        return list == null || list.size() == 0 ? null : list.get(0);
    }
}
