package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.SimpleBean;
import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.ims.entity.resource.StorageRaid;
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;
import java.util.stream.Collectors;

@Repository("storageRaidDao")
public class StorageRaidRepository extends JdbcGenericDao<StorageRaid, Long> {

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

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

    public StorageRaid getByName(String name, long vendorId) {
        String sql = "select a.* from storage_raid a where is_deleted = 0 and name = :name and vendor_id = :vendorId ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("name", name);
        params.put("vendorId", vendorId);
        return this.list(StorageRaid.class, sql, params).stream().findFirst().orElse(null);
    }

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

    public List<StorageRaid> listByMdiskId(long mdiskId) {
        String sql = "select a.* from storage_raid a where is_deleted = 0 and mdisk_id = :mdiskId ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("mdiskId", mdiskId);
        return this.list(StorageRaid.class, sql, params);
    }

    public List<SimpleBean> listNameByMdiskId(long mdiskId) {
        String sql = "select a.name from storage_raid a where is_deleted = 0 and mdisk_id = :mdiskId group by name";
        Map<String, Object> params = new HashMap<>(8);
        params.put("mdiskId", mdiskId);
        List<StorageRaid> list = this.list(StorageRaid.class, sql, params);
        List<SimpleBean> beans = new ArrayList<>();
        for (StorageRaid storageRaid : list) {
            SimpleBean simpleBean = new SimpleBean(storageRaid.getId(), storageRaid.getName());
            beans.add(simpleBean);
        }
        return beans;
    }

    public List<SimpleBean> getByNameAndMdiskId(String name, long mdiskId) {
        String sql = "select a.tactics from storage_raid a where is_deleted = 0 and name = :name and mdisk_id = :mdiskId";
        Map<String, Object> params = new HashMap<>(8);
        params.put("name", name);
        params.put("mdiskId", mdiskId);
        List<StorageRaid> list = this.list(StorageRaid.class, sql, params);
        List<StorageRaid> newList = list.stream().filter(storageRaid -> storageRaid.getTactics() != null).collect(Collectors.toList());
        List<SimpleBean> beans = new ArrayList<>();
        for (StorageRaid storageRaid : newList) {
            SimpleBean simpleBean = new SimpleBean(storageRaid.getId(), storageRaid.getTactics());
            beans.add(simpleBean);
        }
        return beans;
    }

    public StorageRaid getByNameAndTactics(String name, String tactics, long mdiskId) {
        String sql = "select a.* from storage_raid a where is_deleted = 0 and name = :name and mdisk_id = :mdiskId ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("name", name);
        params.put("mdiskId", mdiskId);
        if (tactics != null && tactics.length() != 0) {
            sql = sql + "and tactics = :tactics ";
            params.put("tactics", tactics);
        }
        List<StorageRaid> list = this.list(StorageRaid.class, sql, params);
        return list == null || list.size() == 0 ? null : list.get(0);
    }
}
