package com.bocloud.ims.service.resource.fusionsphere;

import com.bocloud.cmp.params.fusionsphere.StorageDeviceParams;
import com.bocloud.common.model.*;
import com.bocloud.ims.entity.resource.fusionsphere.FusionSphereStorageDevice;
import com.bocloud.ims.interfaces.resource.fusionsphere.FusionSphereStorageDeviceService;
import com.bocloud.ims.repository.resource.fusionsphere.FusionSphereStorageDeviceRepository;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

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

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: FusionSphereStorageDeviceServiceImpl
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/10/20 17:55
 */
@Slf4j
@Service
public class FusionSphereStorageDeviceServiceImpl implements FusionSphereStorageDeviceService {

    @Autowired
    private FusionSphereStorageDeviceRepository fusionSphereStorageDeviceRepository;

    @Override
    public BocloudResult<GridBean<FusionSphereStorageDevice>> list(Pager pager, RequestContext context) {
        List<Param> params = Optional.ofNullable(pager.getParams()).orElse(new ArrayList<>());
        Map<String, String> sorter = Optional.ofNullable(pager.getSorter()).orElse(Maps.newHashMap());
        Integer page = Optional.ofNullable(pager.getPage()).orElse(1);
        Integer rows = Optional.ofNullable(pager.getRows()).orElse(10);
        try {
            int total = fusionSphereStorageDeviceRepository.count(params);
            List<FusionSphereStorageDevice> list = this.fusionSphereStorageDeviceRepository.list(page, rows, params, sorter);
            GridBean<FusionSphereStorageDevice> gridBean = new GridBean<>(page, rows, total, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            log.error("list FusionSphereStorageDevice error!", e);
            return new BocloudResult<>(false, "查询失败:" + e.getMessage());
        }
    }

    @Override
    public BocloudResult<FusionSphereStorageDevice> detail(Long id, RequestContext context) {
        try {
            FusionSphereStorageDevice entity = fusionSphereStorageDeviceRepository.query(id);
            Assert.notNull(entity, "资源不存在");
            return new BocloudResult<>(true, entity, "查询详情成功");
        } catch (Exception e) {
            log.error("detail FusionSphereStorageDevice fail:", e);
            return new BocloudResult<>(false, "查询详情失败:" + e.getMessage());
        }
    }

    @Override
    public BocloudResult listStorage(StorageDeviceParams params, RequestContext context) {
        try {
            List<FusionSphereStorageDevice> entity = fusionSphereStorageDeviceRepository.listStorage(params);
            return new BocloudResult<>(true, entity, "查询详情成功");
        } catch (Exception e) {
            log.error("listStorage FusionSphereStorageDevice fail:", e);
            return new BocloudResult<>(false, "查询详情失败:" + e.getMessage());
        }
    }
}