package com.bocloud.ims.service.resource;

import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.GridBean;
import com.bocloud.ims.entity.resource.Mdisk;
import com.bocloud.ims.entity.resource.Volume;
import com.bocloud.ims.interfaces.resource.MdiskVolumeService;
import com.bocloud.ims.repository.resource.MdiskVolumeRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service("mdiskVolumeService")
public class MdiskVolumeServiceImpl implements MdiskVolumeService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MdiskVolumeRepository mdiskVolumeRepository;

    @Override
    public BocloudResult<GridBean<Volume>> listVolumeByMdisk(int page, int rows, long mdiskId, Map<String, Object> params, Map<String, String> sorter) {
        try {
            List<Volume> list = mdiskVolumeRepository.listVolumeByMdisk(mdiskId, page, rows, params, sorter);
            int count = mdiskVolumeRepository.countVolumeByMdisk(mdiskId, params);
            GridBean gridBean = new GridBean(page, rows, count, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            logger.error("查询失败：", e);
            return new BocloudResult<>(false, "查询失败");
        }
    }

    @Override
    public BocloudResult<GridBean<Mdisk>> listMdiskByVolume(int page, int rows, long volumeId, Map<String, Object> params, Map<String, String> sorter) {
        try {
            List<Mdisk> list = mdiskVolumeRepository.listMdiskByVolume(volumeId, page, rows, params, sorter);
            int count = mdiskVolumeRepository.countMdiskByVolume(volumeId, params);
            GridBean gridBean = new GridBean(page, rows, count, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            logger.error("查询失败：", e);
            return new BocloudResult<>(false, "查询失败");
        }
    }
}
