package com.bocloud.ims.repository.resource;

import com.bocloud.common.utils.MapTools;
import com.bocloud.database.utils.PageHelper;
import com.bocloud.ims.entity.resource.Mdisk;
import com.bocloud.ims.entity.resource.MdiskVolume;
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;

@Repository("mdiskVolumeDao")
public class MdiskVolumeRepository extends EnhancedBaseRepository<MdiskVolume, Long> {

    public void remove(long id) {
        String sql = "delete from mdisk_volume where id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        this.update(sql, params);
    }

    public void removeByMdisk(long mdiskId) {
        String sql = "delete from mdisk_volume where mdisk_id = :mdiskId";
        Map<String, Object> params = MapTools.simpleMap("mdiskId", mdiskId);
        this.update(sql, params);
    }

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

    public List<MdiskVolume> listByMdisk(long mdiskId) {
        String sql = "select a.* from mdisk_volume a where a.mdisk_id = :mdiskId";
        Map<String, Object> params = MapTools.simpleMap("mdiskId", mdiskId);
        return this.list(MdiskVolume.class, sql, params);
    }

    public List<MdiskVolume> listByVolume(long volumeId) {
        String sql = "select a.* from mdisk_volume a where a.volume_id = :volumeId";
        Map<String, Object> params = MapTools.simpleMap("volumeId", volumeId);
        return this.list(MdiskVolume.class, sql, params);
    }

    public MdiskVolume getByVolumeAndMdisk(long mdiskId, long volumeId) {
        String sql = "select a.* from mdisk_volume a where a.volume_id = :volumeId and a.mdisk_id = :mdiskId";
        Map<String, Object> params = MapTools.simpleMap("volumeId", volumeId);
        params.put("mdiskId", mdiskId);
        List<MdiskVolume> list = this.list(MdiskVolume.class, sql, params);
        return list == null || list.size() == 0 ? null : list.get(0);
    }

    public List<Volume> listVolumeByMdisk(long mdiskId, int page, int rows, Map<String, Object> params, Map<String, String> sorter) {
        String sql = " select a.*, b.name as pool_name, c.name as storage_name from volume a " +
                " left join volume_pool b on a.pool_id = b.id " +
                " left join storage c on a.storage_id = c.id " +
                " where a.is_deleted = 0 and a.id in (select b.volume_id from mdisk_volume b where b.mdisk_id = :mdiskId) ";
        Map<String, Object> sqlParams = MapTools.simpleMap("mdiskId", mdiskId);
        if (params.containsKey("name")) {
            sql += " and a.name like :name";
            sqlParams.put("name","%" + params.get("name") + "%");
        }
        sql += PageHelper.buildPageSql(page, rows);
        List<Object> objects = this.queryForList(sql, sqlParams, Volume.class);
        List<Volume> volumes = new ArrayList<>();
        for (Object object : objects) {
            volumes.add((Volume) object);
        }
        return volumes;
    }

    public int countVolumeByMdisk(long mdiskId, Map<String, Object> params) {
        String sql = " select count(1) from volume a where a.is_deleted = 0 and a.id in (select b.volume_id from mdisk_volume b where b.mdisk_id = :mdiskId) ";
        Map<String, Object> sqlParams = MapTools.simpleMap("mdiskId", mdiskId);
        if (params.containsKey("name")) {
            sql += " and a.name like :name";
            sqlParams.put("name","%" + params.get("name") + "%");
        }
        return this.countQuery(sql, sqlParams).intValue();
    }

    public List<Mdisk> listMdiskByVolume(long volumeId, int page, int rows, Map<String, Object> params, Map<String, String> sorter) {
        String sql = " select a.*, b.name as storage_name, c.name as pool_name from mdisk a " +
                " left join storage b on a.storage_id = b.id " +
                " left join volume_pool c on a.pool_id = c.id " +
                " where a.is_deleted = 0 and a.id in (select b.mdisk_id from mdisk_volume b where b.volume_id = :volumeId) ";
        Map<String, Object> sqlParams = MapTools.simpleMap("volumeId", volumeId);
        if (params.containsKey("name")) {
            sql += " and a.name like :name";
            sqlParams.put("name","%" + params.get("name") + "%");
        }
        sql += PageHelper.buildPageSql(page, rows);
        List<Object> objects = this.queryForList(sql, sqlParams, Mdisk.class);
        List<Mdisk> mdisks = new ArrayList<>();
        for (Object object : objects) {
            mdisks.add((Mdisk) object);
        }
        return mdisks;
    }

    public int countMdiskByVolume(long volumeId, Map<String, Object> params) {
        String sql = " select count(1) from mdisk a where a.is_deleted = 0 and a.id in (select b.mdisk_id from mdisk_volume b where b.volume_id = :volumeId) ";
        Map<String, Object> sqlParams = MapTools.simpleMap("volumeId", volumeId);
        if (params.containsKey("name")) {
            sql += " and a.name like :name";
            sqlParams.put("name","%" + params.get("name") + "%");
        }
        return this.countQuery(sql, sqlParams).intValue();
    }
}
