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.SanFabricZone;
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("sanFabricZoneRepository")
public class SanFabricZoneRepository extends JdbcGenericDao<SanFabricZone, Long> {

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

    public List<SanFabricZone> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
        String sql = "select t.* from (select a.*,b.name as fabricName, b.vendor_id " +
                "from san_fabric_zone a left join san_fabric b on  a.fabric_id = b.id " +
                "where a.is_deleted = 0 and b.is_deleted = 0 " +
                ") t " +
                "where 1=1";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "t");
        return this.list(SanFabricZone.class, sql);
    }

    public int count(List<Param> params) {
        String sql = "select count(1) from " +
                "(select a.*,b.name as fabricName, b.vendor_id " +
                "from san_fabric_zone a left join san_fabric b on  a.fabric_id = b.id " +
                "where a.is_deleted = 0 and b.is_deleted = 0 " +
                ") t " +
                "where 1=1";
        sql = QueryBuilder.buildRaw(sql, params, null, "t");
        Map<String, Object> param = QueryBuilder.getParam(params);
        return this.countQuery(sql, param).intValue();
    }

    public void remove(long id) {
        String sql = "update san_fabric_zone 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_zone set is_deleted = 1 where fabric_id = :fabricId ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("fabricId", fabricId);
        execute(sql, params);
    }

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

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

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