package com.bocloud.ims.repository.resource;

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

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

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

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

    public List<StorageFCPort> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
        String sql = "select * from (select d.*, c.name as storage_name, b.storage_id, b.name as node_name, e.id as vendor_id  from storage_fc_port d " +
                "left join storage_node b on d.node_id = b.id " +
                "left join storage c on b.storage_id = c.id " +
                "left join cloud_vendor e on c.vendor_id = e.id " +
                "where d.is_deleted = 0) a where 1=1 ";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(StorageFCPort.class, sql);
    }

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) {
        String sql = "select a.id, a.position, a.link_tech from (select d.*, c.name as storage_name, b.storage_id from storage_fc_port d " +
                "left join storage_node b on d.node_id = b.id " +
                "left join storage c on b.storage_id = c.id " +
                "where d.is_deleted = 0) a where 1=1 ";
        sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<StorageFCPort> fcPorts = this.list(StorageFCPort.class, sql, paramMap);
        List<SimpleBean> beans = new ArrayList<>();
        for (StorageFCPort fcPort : fcPorts) {
            SimpleBean simpleBean = new SimpleBean(fcPort.getId(), fcPort.getPosition(), fcPort.getLinkTech());
            beans.add(simpleBean);
        }
        return beans;
    }

    public int count(List<Param> params) {
        String sql = "select count(1) from (select d.*, c.name as storage_name, b.storage_id, e.id as vendor_id from storage_fc_port d " +
                "left join storage_node b on d.node_id = b.id " +
                "left join storage c on b.storage_id = c.id " +
                "left join cloud_vendor e on c.vendor_id = e.id " +
                "where d.is_deleted = 0) a where 1=1 ";
        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_fc_port set is_deleted = 1 where id = :id ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("id", id);
        execute(sql, params);
    }

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

    public List<StorageFCPort> listByNode(long nodeId) {
        String sql = "select a.* from storage_fc_port a where is_deleted = 0 and node_id = :nodeId ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("nodeId", nodeId);
        return this.list(StorageFCPort.class, sql, params);
    }

    public StorageFCPort get(long id) {
        String sql = "select a.*, c.name as storage_name from storage_fc_port a " +
                "left join storage_node b on a.node_id = b.id " +
                "left join storage c on b.storage_id = c.id " +
                "where a.id = :id ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("id", id);
        List<StorageFCPort> list = this.list(StorageFCPort.class, sql, params);
        return list == null || list.size() == 0 ? null : list.get(0);
    }

    public StorageFCPort getByUuidAndStorage(String portUuid, long storageId) {
        String sql = "select a.* from storage_fc_port a " +
                "left join storage_node b on a.node_id = b.id " +
                "where a.is_deleted = 0 and a.port_uuid = :portUuid and b.storage_id = :storageId ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("portUuid", portUuid);
        params.put("storageId", storageId);
        List<StorageFCPort> list = this.list(StorageFCPort.class, sql, params);
        return list == null || list.size() == 0 ? null : list.get(0);
    }
}
