package com.bocloud.ims.service.resource.azure.db;

import com.bocloud.common.model.*;
import com.bocloud.ims.entity.resource.azure.db.PerformanceTier;
import com.bocloud.ims.interfaces.resource.azure.db.PerformanceTierService;
import com.bocloud.ims.repository.resource.azure.db.PerformanceTierRepository;
import com.bocloud.ims.repository.resource.azure.db.ServiceLevelObjectiveRepository;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: PerformanceTierServiceImpl
 * @Description:数据库规格
 * @author: zhenpingLi/李振平
 * @date: 2021/1/28 15:08
 */
@Service("performanceTierService")
public class PerformanceTierServiceImpl implements PerformanceTierService {

    private static Logger logger = LoggerFactory.getLogger(PerformanceTierServiceImpl.class);
    @Autowired
    private PerformanceTierRepository performanceTierRepository;
    @Autowired
    private ServiceLevelObjectiveRepository serviceLevelObjectiveRepository;


    @Override
    public BocloudResult<GridBean<PerformanceTier>> 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());
        Boolean simple = Optional.ofNullable(pager.getSimple()).orElse(false);
        Integer page = Optional.ofNullable(pager.getPage()).orElse(1);
        Integer rows = Optional.ofNullable(pager.getRows()).orElse(10);
        try {
            int total = performanceTierRepository.count(params);
            List<PerformanceTier> list = this.performanceTierRepository.list(page, rows, params, sorter);
            GridBean<PerformanceTier> gridBean = new GridBean<>(page, rows, total, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            logger.error("list performanceTierService error!", e);
            return new BocloudResult<>(false, "查询失败:" + e.getMessage());
        }
    }

    @Override
    public BocloudResult<PerformanceTier> detail(Long id, RequestContext context) {
        try {
            PerformanceTier entity = performanceTierRepository.query(id);
            entity.setServiceLevelObjectives(serviceLevelObjectiveRepository.listByPid(entity.getId()));
            return new BocloudResult<>(true, entity, "查询详情成功");
        } catch (Exception e) {
            logger.error("query performanceTierService error!", e);
            return new BocloudResult<>(false, "查询详情失败:" + e.getMessage());
        }
    }

}