package com.bocloud.ims.repository.resource;

import com.bocloud.common.exception.BoCloudDBException;
import com.bocloud.common.utils.MapTools;
import com.bocloud.ims.entity.resource.ImageVolume;
import com.bocloud.ims.entity.resource.Volume;
import com.bocloud.ims.repository.resource.common.EnhancedBaseRepository;
import org.springframework.stereotype.Repository;

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

/**
 * 镜像上挂载的卷
 *
 * @author luyanchao
 * @since 2019/2/13 17:06
 */
@Repository("imageVolumeRepository")
public class ImageVolumeRepository extends EnhancedBaseRepository<ImageVolume, Long> {

    public List<ImageVolume> listByImage(long imageId) {
        String sql = "select a.*, b.name as image_name, c.name as volume_name " +
                "from image_volume a " +
                "join image b on a.image_id = b.id " +
                "join volume c on a.volume_id = c.id " +
                "where a.image_id = :imageId ";
        Map<String, Object> params = MapTools.simpleMap("imageId", imageId);
        return list(ImageVolume.class, sql, params);
    }

    public List<Volume> listVolumeByImage(long imageId) {
        String sql = "select a.*, c.name as template_name, d.name as storage_name, d.default_template_id as storage_default_template_id " +
                "from volume a join image_volume b on a.id = b.volume_id " +
                "left join volume_template c on a.template_id = c.id " +
                "left join storage d on a.storage_id = d.id " +
                "where b.image_id = :imageId ";
        Map<String, Object> params = MapTools.simpleMap("imageId", imageId);
        List<Object> list = queryForList(sql, params, Volume.class);
        List<Volume> volumes = new ArrayList<>();
        for (Object o : list) {
            volumes.add((Volume) o);
        }
        return volumes;
    }

    public List<ImageVolume> listByVolume(long volumeId) {
        String sql = "select a.*, b.name as image_name, c.name as volume_name " +
                "from image_volume a " +
                "join image b on a.image_id = b.id " +
                "join volume c on a.volume_id = c.id " +
                "where a.volume_id = :volumeId ";
        Map<String, Object> params = MapTools.simpleMap("volumeId", volumeId);
        return list(ImageVolume.class, sql, params);
    }

    public List<ImageVolume> listByStorage(long storageId) {
        String sql = "select a.*, b.name as image_name, c.name as volume_name " +
                "from image_volume a " +
                "join image b on a.image_id = b.id " +
                "join volume c on a.volume_id = c.id " +
                "where c.storage_id = :storageId ";
        Map<String, Object> params = MapTools.simpleMap("storageId", storageId);
        return list(ImageVolume.class, sql, params);
    }

    public void removeByImage(long imageId) throws BoCloudDBException {
        String sql = " delete from image_volume where image_id = :imageId ";
        Map<String, Object> params = MapTools.simpleMap("imageId", imageId);
        update(sql, params);
    }

    public void removeByVolume(long volumeId) throws BoCloudDBException {
        String sql = " delete from image_volume where volume_id = :volumeId ";
        Map<String, Object> params = MapTools.simpleMap("volumeId", volumeId);
        update(sql, params);
    }

    public void removeById(long id) throws BoCloudDBException {
        String sql = " delete from image_volume where id = :id ";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        update(sql, params);
    }

    public void remove(long imageId, long volumeId) throws BoCloudDBException {
        String sql = " delete from image_volume image_id = :imageId and volume_id = :volumeId ";
        Map<String, Object> params = MapTools.simpleMap("imageId", imageId);
        params.put("volumeId", volumeId);
        update(sql, params);
    }

    public void removeByVendorId(long vendorId, long userId) throws BoCloudDBException {
        String sql = " delete from image_volume where image_id in (select id from image where vendor_id = :vendorId) ";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        update(sql, params);
    }
}
