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

import com.bocloud.common.model.*;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.ims.entity.middleware.RdsPublicCloudInstanceParameterGroup;
import com.bocloud.ims.interfaces.resource.database.RdsPublicCloudInstanceParamGroupService;
import com.bocloud.ims.repository.database.RdsPublicCloudInstanceParameterGroupRepository;
import com.bocloud.service.lock.LockFactory;
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;

/**
 * @author lyy
 * @version 1.0
 * @date 2021/5/26 16:39
 */
@Service("rdsPublicCloudInstanceParamGroupService")
public class RdsPublicCloudInstanceParamGroupServiceImpl implements RdsPublicCloudInstanceParamGroupService {
    private static Logger logger = LoggerFactory.getLogger(RdsPublicCloudInstanceParamGroupServiceImpl.class);
    @Autowired
    private LockFactory lockFactory;
    @Autowired
    private RdsPublicCloudInstanceParameterGroupRepository instanceParameterGroupRepository;

    @Override
    public BocloudResult list(Pager pager) {
        try {
            List<Param> params = pager.getParams();
            Integer page = pager.getPage();
            Integer rows = pager.getRows();
            Map<String, String> sorter = pager.getSorter();
            GridBean gridBean = null;
            if (null == params) {
                params = new ArrayList<>();
            }
            if (null == sorter) {
                sorter = Maps.newHashMap();
            }
            sorter.put("gmtCreate", Common.ONE);
            List<RdsPublicCloudInstanceParameterGroup> list = instanceParameterGroupRepository.list(page, rows, params, sorter);
            int total = instanceParameterGroupRepository.count(params);
            gridBean = GridHelper.getBean(page, rows, total, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            logger.error("list rds param group failure:", e);
            return new BocloudResult<>(false, "查询失败");
        }
    }

    @Override
    public BocloudResult detail(Long id) {
        try {
            RdsPublicCloudInstanceParameterGroup parameterGroup = instanceParameterGroupRepository.query(id);
            return new BocloudResult(true, parameterGroup, "查询成功");
        } catch (Exception e) {
            logger.error("query rds param group failure:", e);
            return new BocloudResult<>(false, "查询失败");
        }
    }

    @Override
    public BocloudResult create(RdsPublicCloudInstanceParameterGroup group, RequestContext context) {
        return null;
    }

    @Override
    public BocloudResult modify(RdsPublicCloudInstanceParameterGroup group, RequestContext context) {
        return null;
    }

    @Override
    public BocloudResult delete(Long id, RequestContext context) {
        return null;
    }
}
