package com.bocloud.ims.repository.resource;

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

import java.util.List;
import java.util.Map;

/**
 * @author luyanchao
 * @since 2019/1/25 11:11
 */
@Repository("volumeTemplateRepository")
public class VolumeTemplateRepository extends JdbcGenericDao<VolumeTemplate, Long> {

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

    public List<VolumeTemplate> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
        String sql = "select a.*, b.name as storage_name, c.name as pool_name, d.name as copy_pool_name "
                + "from volume_template a " + "left join storage b on a.storage_id = b.id "
                + "left join volume_pool c on a.pool_id = c.id " + "left join volume_pool d on a.copy_pool_id = d.id "
                + "where a.is_deleted = 0 ";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.list(VolumeTemplate.class, sql, paramMap);
    }

    public int count(List<Param> params) {
        String sql = "select count(a.id) from volume_template a where a.is_deleted = 0";
        sql = QueryBuilder.buildRaw(sql, params, null, "");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.countQuery(sql, paramMap).intValue();
    }

    public VolumeTemplate getByUuid(String uuid) {
        String sql = "select a.* from volume_template a where a.uuid = :uuid";
        Map<String, Object> params = MapTools.simpleMap("uuid", uuid);
        List<VolumeTemplate> templates = list(VolumeTemplate.class, sql, params);
        return templates == null || templates.size() == 0 ? null : templates.get(0);
    }

    public void remove(long id) {
        String sql = "update volume_template set is_deleted = 1 where id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        execute(sql, params);
    }

    public void removeByVendor(long vendorId) {
        String sql = "update volume_template set is_deleted = 1 where vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        execute(sql, params);
    }

    public void removeByStorage(long storageId) {
        String sql = "update volume_template set is_deleted = 1 where storage_id = :storageId";
        Map<String, Object> params = MapTools.simpleMap("storageId", storageId);
        execute(sql, params);
    }

    public List<VolumeTemplate> listByVendor(long vendorId) {
        String sql = "select a.*, b.name as storage_name, "
                + "c.name as pool_name, c.free_capacity as pool_free_capacity, "
                + "d.name as copy_pool_name, d.free_capacity as cpoy_pool_free_capacity " + "from volume_template a "
                + "left join storage b on a.storage_id = b.id "
                + "left join volume_pool c on (a.pool_id = c.id and c.is_deleted = 0) "
                + "left join volume_pool d on (a.copy_pool_id = d.id and d.is_deleted = 0)"
                + "where a.is_deleted = 0 and a.vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        return list(VolumeTemplate.class, sql, params);
    }

    public List<VolumeTemplate> listByStorage(long storageId) {
        String sql = "select a.*, b.name as storage_name, v.name as pool_name from " + "volume_template a "
                + "left join storage b on a.storage_id = b.id " + "left join volume_pool v on a.pool_id = v.id "
                + "where a.is_deleted = 0 and a.storage_id = :storageId";
        Map<String, Object> params = MapTools.simpleMap("storageId", storageId);
        return list(VolumeTemplate.class, sql, params);
    }

    public VolumeTemplate get(long id) {
        String sql = "select a.*, b.name as storage_name, c.name as pool_name, d.name as copy_pool_name "
                + "from volume_template a " + "left join storage b on a.storage_id = b.id "
                + "left join volume_pool c on a.pool_id = c.id " + "left join volume_pool d on a.copy_pool_id = d.id "
                + "where a.id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        List<VolumeTemplate> templates = list(VolumeTemplate.class, sql, params);
        return templates == null || templates.size() == 0 ? null : templates.get(0);
    }

    public VolumeTemplate getByOriginName(String originName, long vendorId) {
        String sql = "select a.* from volume_template a "
                + "where a.is_deleted = 0 and a.origin_name = :originName and a.vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap("originName", originName);
        params.put("vendorId", vendorId);
        List<VolumeTemplate> templates = list(VolumeTemplate.class, sql, params);
        return templates == null || templates.size() == 0 ? null : templates.get(0);
    }
}
