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.SanFabric;
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("sanFabricRepository")
public class SanFabricRepository extends JdbcGenericDao<SanFabric, Long> {

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

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

    public int count(List<Param> params) {
        String sql = "select count(1) from san_fabric 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 san_fabric set is_deleted = 1 where id = :id ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("id", id);
        execute(sql, params);
    }

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

    public List<SanFabric> listByVendor(long vendorId) {
        String sql = "select a.* from san_fabric a where a.is_deleted = 0 and a.vendor_id = :vendorId ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("vendorId", vendorId);
        return this.list(SanFabric.class, sql, params);
    }

    public SanFabric getByUuid(String uuid, long vendorId) {
        String sql = "select a.* from san_fabric a where a.is_deleted = 0 and a.uuid = :uuid and a.vendor_id = :vendorId ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("uuid", uuid);
        params.put("vendorId", vendorId);
        List<SanFabric> list = this.list(SanFabric.class, sql, params);
        return list == null || list.size() == 0 ? null : list.get(0);
    }

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

    public SanFabric get(String ip, long vendorId) {
        String sql = "select a.* from san_fabric a where a.is_deleted = 0 and a.ip = :ip and a.vendor_id = :vendorId ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("ip", ip);
        params.put("vendorId", vendorId);
        List<SanFabric> list = this.list(SanFabric.class, sql, params);
        return list == null || list.size() == 0 ? null : list.get(0);
    }
}
