package com.bocloud.ims.repository.resource;

import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.ims.entity.resource.SanFabricPort;
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("sanFabricPortDao")
public class SanFabricPortRepository extends JdbcGenericDao<SanFabricPort, Long> {

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

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

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

    public List<SanFabricPort> listByFabric(long fabricId) {
        String sql = "select a.* from san_fabric_port a where is_deleted = 0 and fabric_id = :fabricId ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("fabricId", fabricId);
        return this.list(SanFabricPort.class, sql, params);
    }

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

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